blob: 173ee2977947270bba08e080fca98a00420c928f [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>
Adriana Kobylak43699ca2018-10-17 14:56:29 -050018#include <xyz/openbmc_project/Software/Image/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;
Adriana Kobylak43699ca2018-10-17 14:56:29 -050032using namespace sdbusplus::xyz::openbmc_project::Software::Image::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 {
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600327 removeAssociations(ita->second->path);
Saqib Khanee13e832017-10-23 12:53:11 -0500328 this->activations.erase(entryId);
329 }
Saqib Khan49446ae2017-10-02 10:54:20 -0500330 ItemUpdater::resetUbootEnvVars();
Saqib Khanee13e832017-10-23 12:53:11 -0500331 return;
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500332}
333
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500334void ItemUpdater::deleteAll()
335{
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600336 std::vector<std::string> deletableVersions;
337
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500338 for (const auto& versionIt : versions)
339 {
340 if (!versionIt.second->isFunctional())
341 {
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600342 deletableVersions.push_back(versionIt.first);
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500343 }
344 }
345
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600346 for (const auto& deletableIt : deletableVersions)
347 {
348 ItemUpdater::erase(deletableIt);
349 }
350
Lei YU56aaf452018-06-21 16:09:44 +0800351 helper.cleanup();
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500352}
353
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600354ItemUpdater::ActivationStatus
355 ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500356{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500357 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500358
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500359 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500360 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500361 fs::path file(filePath);
362 file /= bmcImage;
363 std::ifstream efile(file.c_str());
364 if (efile.good() != 1)
365 {
366 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500367 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500368 invalid = true;
369 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500370 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500371
372 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500373 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500374 return ItemUpdater::ActivationStatus::invalid;
375 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500376
377 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500378}
379
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500380void ItemUpdater::savePriority(const std::string& versionId, uint8_t value)
381{
382 storeToFile(versionId, value);
383 helper.setEntry(versionId, value);
384}
385
Saqib Khanb9da6632017-09-13 09:48:37 -0500386void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500387{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500388 std::map<std::string, uint8_t> priorityMap;
389
390 // Insert the requested version and priority, it may not exist yet.
391 priorityMap.insert(std::make_pair(versionId, value));
392
Saqib Khan4c1aec02017-07-06 11:46:13 -0500393 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 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500397 priorityMap.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600398 intf.first, intf.second->redundancyPriority.get()->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500399 }
400 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500401
402 // Lambda function to compare 2 priority values, use <= to allow duplicates
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600403 typedef std::function<bool(std::pair<std::string, uint8_t>,
404 std::pair<std::string, uint8_t>)>
405 cmpPriority;
406 cmpPriority cmpPriorityFunc =
407 [](std::pair<std::string, uint8_t> priority1,
408 std::pair<std::string, uint8_t> priority2) {
409 return priority1.second <= priority2.second;
410 };
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500411
412 // Sort versions by ascending priority
413 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600414 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500415
416 auto freePriorityValue = value;
417 for (auto& element : prioritySet)
418 {
419 if (element.first == versionId)
420 {
421 continue;
422 }
423 if (element.second == freePriorityValue)
424 {
425 ++freePriorityValue;
426 auto it = activations.find(element.first);
427 it->second->redundancyPriority.get()->sdbusPriority(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600428 freePriorityValue);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500429 }
430 }
431
432 auto lowestVersion = prioritySet.begin()->first;
433 if (value == prioritySet.begin()->second)
434 {
435 lowestVersion = versionId;
436 }
437 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500438}
439
Michael Tritz37a59042017-07-12 13:44:53 -0500440void ItemUpdater::reset()
441{
Lei YU56aaf452018-06-21 16:09:44 +0800442 helper.factoryReset();
Michael Tritz37a59042017-07-12 13:44:53 -0500443
444 log<level::INFO>("BMC factory reset will take effect upon reboot.");
Michael Tritz37a59042017-07-12 13:44:53 -0500445}
446
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500447void ItemUpdater::removeReadOnlyPartition(std::string versionId)
448{
Lei YU56aaf452018-06-21 16:09:44 +0800449 helper.removeVersion(versionId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500450}
451
Michael Tritz0129d922017-08-10 19:33:46 -0500452bool ItemUpdater::fieldModeEnabled(bool value)
453{
454 // enabling field mode is intended to be one way: false -> true
455 if (value && !control::FieldMode::fieldModeEnabled())
456 {
457 control::FieldMode::fieldModeEnabled(value);
458
Lei YU56aaf452018-06-21 16:09:44 +0800459 helper.enableFieldMode();
Michael Tritz0129d922017-08-10 19:33:46 -0500460 }
461
462 return control::FieldMode::fieldModeEnabled();
463}
464
465void ItemUpdater::restoreFieldModeStatus()
466{
Michael Tritzff0b4212017-10-24 17:38:09 -0500467 std::ifstream input("/dev/mtd/u-boot-env");
Michael Tritz0129d922017-08-10 19:33:46 -0500468 std::string envVar;
469 std::getline(input, envVar);
470
Gunnar Mills9a782242017-08-22 16:23:15 -0500471 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500472 {
473 ItemUpdater::fieldModeEnabled(true);
474 }
475}
476
Gunnar Millsb60add12017-08-24 16:41:42 -0500477void ItemUpdater::setBMCInventoryPath()
478{
Gunnar Millsb60add12017-08-24 16:41:42 -0500479 auto depth = 0;
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600480 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
481 MAPPER_INTERFACE, "GetSubTreePaths");
Gunnar Millsb60add12017-08-24 16:41:42 -0500482
Adriana Kobylak1254c622017-12-07 12:24:56 -0600483 mapperCall.append(INVENTORY_PATH);
Gunnar Millsb60add12017-08-24 16:41:42 -0500484 mapperCall.append(depth);
Adriana Kobylak1254c622017-12-07 12:24:56 -0600485 std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE};
Gunnar Millsb60add12017-08-24 16:41:42 -0500486 mapperCall.append(filter);
487
Ed Tanous87c78172018-08-10 12:51:53 -0700488 try
489 {
490 auto response = bus.call(mapperCall);
491
492 using ObjectPaths = std::vector<std::string>;
493 ObjectPaths result;
494 response.read(result);
495
496 if (!result.empty())
497 {
498 bmcInventoryPath = result.front();
499 }
500 }
501 catch (const sdbusplus::exception::SdBusError& e)
Gunnar Millsb60add12017-08-24 16:41:42 -0500502 {
503 log<level::ERR>("Error in mapper GetSubTreePath");
504 return;
505 }
506
Adriana Kobylak1254c622017-12-07 12:24:56 -0600507 return;
Gunnar Millsb60add12017-08-24 16:41:42 -0500508}
509
Gunnar Millsf10b2322017-09-21 15:31:55 -0500510void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500511{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600512 assocs.emplace_back(
513 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
Gunnar Millsded875d2017-08-28 16:44:52 -0500514 associations(assocs);
515}
516
Gunnar Mills88e8a322017-09-13 11:09:28 -0500517void ItemUpdater::createFunctionalAssociation(const std::string& path)
518{
519 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600520 FUNCTIONAL_REV_ASSOCIATION, path));
Gunnar Mills88e8a322017-09-13 11:09:28 -0500521 associations(assocs);
522}
523
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600524void ItemUpdater::removeAssociations(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500525{
526 for (auto iter = assocs.begin(); iter != assocs.end();)
527 {
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600528 if ((std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500529 {
530 iter = assocs.erase(iter);
531 associations(assocs);
532 }
533 else
534 {
535 ++iter;
536 }
537 }
538}
539
Saqib Khanb9da6632017-09-13 09:48:37 -0500540bool ItemUpdater::isLowestPriority(uint8_t value)
541{
542 for (const auto& intf : activations)
543 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500544 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500545 {
546 if (intf.second->redundancyPriority.get()->priority() < value)
547 {
548 return false;
549 }
550 }
551 }
552 return true;
553}
554
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500555void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
556{
Lei YU56aaf452018-06-21 16:09:44 +0800557 helper.updateUbootVersionId(versionId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500558}
559
Saqib Khan49446ae2017-10-02 10:54:20 -0500560void ItemUpdater::resetUbootEnvVars()
561{
562 decltype(activations.begin()->second->redundancyPriority.get()->priority())
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600563 lowestPriority = std::numeric_limits<uint8_t>::max();
Saqib Khan49446ae2017-10-02 10:54:20 -0500564 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
565 for (const auto& intf : activations)
566 {
567 if (!intf.second->redundancyPriority.get())
568 {
569 // Skip this version if the redundancyPriority is not initialized.
570 continue;
571 }
572
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600573 if (intf.second->redundancyPriority.get()->priority() <= lowestPriority)
Saqib Khan49446ae2017-10-02 10:54:20 -0500574 {
575 lowestPriority = intf.second->redundancyPriority.get()->priority();
576 lowestPriorityVersion = intf.second->versionId;
577 }
578 }
579
Saqib Khanf0382c32017-10-24 13:36:22 -0500580 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500581 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500582}
583
Adriana Kobylaka6963592018-09-07 14:13:29 -0500584void ItemUpdater::freeSpace(Activation& caller)
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600585{
586 // Versions with the highest priority in front
587 std::priority_queue<std::pair<int, std::string>,
588 std::vector<std::pair<int, std::string>>,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600589 std::less<std::pair<int, std::string>>>
590 versionsPQ;
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600591
592 std::size_t count = 0;
593 for (const auto& iter : activations)
594 {
595 if ((iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600596 server::Activation::Activations::Active) ||
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600597 (iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600598 server::Activation::Activations::Failed))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600599 {
600 count++;
601 // Don't put the functional version on the queue since we can't
602 // remove the "running" BMC version.
Lei YU0f88b5a2018-08-21 15:28:53 +0800603 // If ACTIVE_BMC_MAX_ALLOWED <= 1, there is only one active BMC,
604 // so remove functional version as well.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500605 // Don't delete the the Activation object that called this function.
606 if ((versions.find(iter.second->versionId)
607 ->second->isFunctional() &&
608 ACTIVE_BMC_MAX_ALLOWED > 1) ||
609 (iter.second->versionId == caller.versionId))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600610 {
611 continue;
612 }
Adriana Kobylaka6963592018-09-07 14:13:29 -0500613
614 // Failed activations don't have priority, assign them a large value
615 // for sorting purposes.
616 auto priority = 999;
617 if (iter.second.get()->activation() ==
618 server::Activation::Activations::Active)
619 {
620 priority = iter.second->redundancyPriority.get()->priority();
621 }
622
623 versionsPQ.push(std::make_pair(priority, iter.second->versionId));
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600624 }
625 }
626
627 // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1,
628 // remove the highest priority one(s).
629 while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty()))
630 {
631 erase(versionsPQ.top().second);
632 versionsPQ.pop();
633 count--;
634 }
635}
636
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600637void ItemUpdater::mirrorUbootToAlt()
638{
Lei YU56aaf452018-06-21 16:09:44 +0800639 helper.mirrorAlt();
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600640}
641
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500642} // namespace updater
643} // namespace software
644} // namespace phosphor