blob: 21fb6e04e8d5499940ceb3cfef23f38db3cfe396 [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
Gunnar Millsb0ce9962018-09-07 13:39:10 -050010#include <experimental/filesystem>
Saqib Khan35e83f32017-05-22 11:37:32 -050011#include <fstream>
Adriana Kobylakd5b8f752019-05-01 11:52:35 -050012#include <phosphor-logging/elog-errors.hpp>
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 Kobylakd5b8f752019-05-01 11:52:35 -050018#include <xyz/openbmc_project/Common/error.hpp>
Adriana Kobylak43699ca2018-10-17 14:56:29 -050019#include <xyz/openbmc_project/Software/Image/error.hpp>
Gunnar Millsec1b41c2017-05-02 12:20:36 -050020
21namespace phosphor
22{
23namespace software
24{
25namespace updater
26{
27
Gunnar Mills2ce7da22017-05-04 15:37:56 -050028// When you see server:: you know we're referencing our base class
29namespace server = sdbusplus::xyz::openbmc_project::Software::server;
Michael Tritz0129d922017-08-10 19:33:46 -050030namespace control = sdbusplus::xyz::openbmc_project::Control::server;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050031
32using namespace phosphor::logging;
Adriana Kobylak43699ca2018-10-17 14:56:29 -050033using namespace sdbusplus::xyz::openbmc_project::Software::Image::Error;
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060034using namespace phosphor::software::image;
Saqib Khan35e83f32017-05-22 11:37:32 -050035namespace fs = std::experimental::filesystem;
Adriana Kobylakd5b8f752019-05-01 11:52:35 -050036using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
Saqib Khan35e83f32017-05-22 11:37:32 -050037
Patrick Williamse75d10f2017-05-30 16:56:32 -050038void ItemUpdater::createActivation(sdbusplus::message::message& msg)
Gunnar Millsec1b41c2017-05-02 12:20:36 -050039{
Saqib Khan84a0e692017-06-28 17:27:01 -050040
41 using SVersion = server::Version;
42 using VersionPurpose = SVersion::VersionPurpose;
Gunnar Mills9a782242017-08-22 16:23:15 -050043 using VersionClass = phosphor::software::manager::Version;
Saqib Khan84a0e692017-06-28 17:27:01 -050044 namespace mesg = sdbusplus::message;
45 namespace variant_ns = mesg::variant_ns;
46
47 mesg::object_path objPath;
48 auto purpose = VersionPurpose::Unknown;
Saqib Khan705f1bf2017-06-09 23:58:38 -050049 std::string version;
Adriana Kobylak2285fe02018-02-27 15:36:59 -060050 std::map<std::string, std::map<std::string, mesg::variant<std::string>>>
51 interfaces;
Patrick Williamse75d10f2017-05-30 16:56:32 -050052 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050053 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050054 std::string filePath;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050055
56 for (const auto& intf : interfaces)
57 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050058 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050059 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050060 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050061 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050062 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050063 {
Saqib Khan84a0e692017-06-28 17:27:01 -050064 auto value = SVersion::convertVersionPurposeFromString(
Adriana Kobylak2285fe02018-02-27 15:36:59 -060065 variant_ns::get<std::string>(property.second));
Saqib Khan84a0e692017-06-28 17:27:01 -050066 if (value == VersionPurpose::BMC ||
67 value == VersionPurpose::System)
68 {
69 purpose = value;
70 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050071 }
72 else if (property.first == "Version")
73 {
Saqib Khan84a0e692017-06-28 17:27:01 -050074 version = variant_ns::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050075 }
76 }
77 }
Saqib Khan19177d32017-06-20 08:11:49 -050078 else if (intf.first == FILEPATH_IFACE)
79 {
80 for (const auto& property : intf.second)
81 {
82 if (property.first == "Path")
83 {
Saqib Khan84a0e692017-06-28 17:27:01 -050084 filePath = variant_ns::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050085 }
86 }
87 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050088 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -060089 if (version.empty() || filePath.empty() ||
Saqib Khan84a0e692017-06-28 17:27:01 -050090 purpose == VersionPurpose::Unknown)
Saqib Khan705f1bf2017-06-09 23:58:38 -050091 {
92 return;
93 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050094
95 // Version id is the last item in the path
96 auto pos = path.rfind("/");
97 if (pos == std::string::npos)
98 {
99 log<level::ERR>("No version id found in object path",
Adriana Kobylak596466b2018-02-13 14:48:53 -0600100 entry("OBJPATH=%s", path.c_str()));
Patrick Williamse75d10f2017-05-30 16:56:32 -0500101 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500102 }
103
104 auto versionId = path.substr(pos + 1);
105
Patrick Williamse75d10f2017-05-30 16:56:32 -0500106 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500107 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500108 // Determine the Activation state by processing the given image dir.
109 auto activationState = server::Activation::Activations::Invalid;
Gunnar Mills9a782242017-08-22 16:23:15 -0500110 ItemUpdater::ActivationStatus result =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600111 ItemUpdater::validateSquashFSImage(filePath);
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500112 AssociationList associations = {};
113
Saqib Khan35e83f32017-05-22 11:37:32 -0500114 if (result == ItemUpdater::ActivationStatus::ready)
115 {
116 activationState = server::Activation::Activations::Ready;
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500117 // Create an association to the BMC inventory item
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600118 associations.emplace_back(
119 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
120 ACTIVATION_REV_ASSOCIATION, bmcInventoryPath));
Saqib Khan35e83f32017-05-22 11:37:32 -0500121 }
Gunnar Millsb60add12017-08-24 16:41:42 -0500122
Saqib Khanee13e832017-10-23 12:53:11 -0500123 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600124 versionId,
125 std::make_unique<Activation>(bus, path, *this, versionId,
126 activationState, associations)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500127
Saqib Khanee13e832017-10-23 12:53:11 -0500128 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600129 bus, path, version, purpose, filePath,
130 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Saqib Khanee13e832017-10-23 12:53:11 -0500131 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600132 std::make_unique<phosphor::software::manager::Delete>(bus, path,
133 *versionPtr);
Saqib Khanee13e832017-10-23 12:53:11 -0500134 versions.insert(std::make_pair(versionId, std::move(versionPtr)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500135 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500136 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500137}
138
Saqib Khanba239882017-05-26 08:41:54 -0500139void ItemUpdater::processBMCImage()
140{
Gunnar Mills88e8a322017-09-13 11:09:28 -0500141 using VersionClass = phosphor::software::manager::Version;
Lei YU269bff32018-08-21 15:21:40 +0800142
143 // Check MEDIA_DIR and create if it does not exist
144 try
145 {
146 if (!fs::is_directory(MEDIA_DIR))
147 {
148 fs::create_directory(MEDIA_DIR);
149 }
150 }
151 catch (const fs::filesystem_error& e)
152 {
153 log<level::ERR>("Failed to prepare dir", entry("ERR=%s", e.what()));
154 return;
155 }
156
Gunnar Mills88e8a322017-09-13 11:09:28 -0500157 // Read os-release from /etc/ to get the functional BMC version
158 auto functionalVersion = VersionClass::getBMCVersion(OS_RELEASE_FILE);
159
Saqib Khan1eef62d2017-08-10 15:29:34 -0500160 // Read os-release from folders under /media/ to get
161 // BMC Software Versions.
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500162 for (const auto& iter : fs::directory_iterator(MEDIA_DIR))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500163 {
164 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500165 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500166
167 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600168 if (0 ==
169 iter.path().native().compare(0, BMC_RO_PREFIX_LEN, BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500170 {
Saqib Khan021c3652017-09-26 12:11:02 -0500171 // The versionId is extracted from the path
172 // for example /media/ro-2a1022fe.
173 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500174 auto osRelease = iter.path() / OS_RELEASE_FILE;
175 if (!fs::is_regular_file(osRelease))
176 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600177 log<level::ERR>(
178 "Failed to read osRelease",
179 entry("FILENAME=%s", osRelease.string().c_str()));
Saqib Khan021c3652017-09-26 12:11:02 -0500180 ItemUpdater::erase(id);
181 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500182 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500183 auto version = VersionClass::getBMCVersion(osRelease);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500184 if (version.empty())
185 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600186 log<level::ERR>(
187 "Failed to read version from osRelease",
188 entry("FILENAME=%s", osRelease.string().c_str()));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500189 activationState = server::Activation::Activations::Invalid;
190 }
Saqib Khan021c3652017-09-26 12:11:02 -0500191
Saqib Khan1eef62d2017-08-10 15:29:34 -0500192 auto purpose = server::Version::VersionPurpose::BMC;
193 auto path = fs::path(SOFTWARE_OBJPATH) / id;
194
Lei YU269bff32018-08-21 15:21:40 +0800195 // Create functional association if this is the functional
196 // version
Gunnar Mills88e8a322017-09-13 11:09:28 -0500197 if (version.compare(functionalVersion) == 0)
198 {
199 createFunctionalAssociation(path);
200 }
201
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500202 AssociationList associations = {};
203
204 if (activationState == server::Activation::Activations::Active)
205 {
206 // Create an association to the BMC inventory item
207 associations.emplace_back(std::make_tuple(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600208 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
209 bmcInventoryPath));
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500210
211 // Create an active association since this image is active
212 createActiveAssociation(path);
213 }
214
Adriana Kobylakee590c72017-09-26 15:16:06 -0500215 // Create Version instance for this version.
216 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600217 bus, path, version, purpose, "",
218 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500219 auto isVersionFunctional = versionPtr->isFunctional();
Michael Tritz4254bec2017-10-03 17:18:22 -0500220 if (!isVersionFunctional)
221 {
Saqib Khanee13e832017-10-23 12:53:11 -0500222 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600223 std::make_unique<phosphor::software::manager::Delete>(
224 bus, path, *versionPtr);
Michael Tritz4254bec2017-10-03 17:18:22 -0500225 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600226 versions.insert(std::make_pair(id, std::move(versionPtr)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500227
Saqib Khanee13e832017-10-23 12:53:11 -0500228 // Create Activation instance for this version.
229 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600230 id, std::make_unique<Activation>(
231 bus, path, *this, id, activationState, associations)));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500232
Lei YU269bff32018-08-21 15:21:40 +0800233 // If Active, create RedundancyPriority instance for this
234 // version.
Saqib Khan1eef62d2017-08-10 15:29:34 -0500235 if (activationState == server::Activation::Activations::Active)
236 {
237 uint8_t priority = std::numeric_limits<uint8_t>::max();
238 if (!restoreFromFile(id, priority))
239 {
Adriana Kobylakee590c72017-09-26 15:16:06 -0500240 if (isVersionFunctional)
241 {
242 priority = 0;
243 }
244 else
245 {
246 log<level::ERR>("Unable to restore priority from file.",
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600247 entry("VERSIONID=%s", id.c_str()));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500248 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500249 }
250 activations.find(id)->second->redundancyPriority =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600251 std::make_unique<RedundancyPriority>(
252 bus, path, *(activations.find(id)->second), priority,
253 false);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500254 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500255 }
256 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500257
258 // If there is no ubi volume for bmc version then read the /etc/os-release
259 // and create rofs-<versionId> under /media
260 if (activations.size() == 0)
261 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500262 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Saqib Khandcbfa042017-09-18 13:08:39 -0500263 auto id = phosphor::software::manager::Version::getId(version);
264 auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/";
265 try
266 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500267 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500268 {
269 fs::create_directories(versionFileDir);
270 }
271 auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE;
272 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
273 ItemUpdater::processBMCImage();
274 }
275 catch (const std::exception& e)
276 {
277 log<level::ERR>(e.what());
278 }
279 }
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600280
281 mirrorUbootToAlt();
Saqib Khanba239882017-05-26 08:41:54 -0500282 return;
283}
284
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500285void ItemUpdater::erase(std::string entryId)
286{
Eddie James6d873712017-09-01 11:29:07 -0500287 // Find entry in versions map
288 auto it = versions.find(entryId);
289 if (it != versions.end())
290 {
Lei YU0f88b5a2018-08-21 15:28:53 +0800291 if (it->second->isFunctional() && ACTIVE_BMC_MAX_ALLOWED > 1)
Eddie James6d873712017-09-01 11:29:07 -0500292 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600293 log<level::ERR>("Error: Version is currently running on the BMC. "
294 "Unable to remove.",
295 entry("VERSIONID=%s", entryId.c_str()));
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500296 return;
Eddie James6d873712017-09-01 11:29:07 -0500297 }
298
299 // Delete ReadOnly partitions if it's not active
300 removeReadOnlyPartition(entryId);
301 removeFile(entryId);
Saqib Khanee13e832017-10-23 12:53:11 -0500302
303 // Removing entry in versions map
304 this->versions.erase(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500305 }
306 else
307 {
308 // Delete ReadOnly partitions even if we can't find the version
309 removeReadOnlyPartition(entryId);
310 removeFile(entryId);
311
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600312 log<level::ERR>("Error: Failed to find version in item updater "
313 "versions map. Unable to remove.",
314 entry("VERSIONID=%s", entryId.c_str()));
Eddie James6d873712017-09-01 11:29:07 -0500315 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500316
Lei YU56aaf452018-06-21 16:09:44 +0800317 helper.clearEntry(entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500318
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500319 // Removing entry in activations map
320 auto ita = activations.find(entryId);
321 if (ita == activations.end())
322 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600323 log<level::ERR>("Error: Failed to find version in item updater "
324 "activations map. Unable to remove.",
325 entry("VERSIONID=%s", entryId.c_str()));
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500326 }
Saqib Khanee13e832017-10-23 12:53:11 -0500327 else
328 {
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600329 removeAssociations(ita->second->path);
Saqib Khanee13e832017-10-23 12:53:11 -0500330 this->activations.erase(entryId);
331 }
Saqib Khan49446ae2017-10-02 10:54:20 -0500332 ItemUpdater::resetUbootEnvVars();
Saqib Khanee13e832017-10-23 12:53:11 -0500333 return;
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500334}
335
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500336void ItemUpdater::deleteAll()
337{
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600338 std::vector<std::string> deletableVersions;
339
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500340 for (const auto& versionIt : versions)
341 {
342 if (!versionIt.second->isFunctional())
343 {
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600344 deletableVersions.push_back(versionIt.first);
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500345 }
346 }
347
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600348 for (const auto& deletableIt : deletableVersions)
349 {
350 ItemUpdater::erase(deletableIt);
351 }
352
Lei YU56aaf452018-06-21 16:09:44 +0800353 helper.cleanup();
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500354}
355
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600356ItemUpdater::ActivationStatus
357 ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500358{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500359 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500360
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500361 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500362 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500363 fs::path file(filePath);
364 file /= bmcImage;
365 std::ifstream efile(file.c_str());
366 if (efile.good() != 1)
367 {
368 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500369 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500370 invalid = true;
371 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500372 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500373
374 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500375 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500376 return ItemUpdater::ActivationStatus::invalid;
377 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500378
379 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500380}
381
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500382void ItemUpdater::savePriority(const std::string& versionId, uint8_t value)
383{
384 storeToFile(versionId, value);
385 helper.setEntry(versionId, value);
386}
387
Saqib Khanb9da6632017-09-13 09:48:37 -0500388void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500389{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500390 std::map<std::string, uint8_t> priorityMap;
391
392 // Insert the requested version and priority, it may not exist yet.
393 priorityMap.insert(std::make_pair(versionId, value));
394
Saqib Khan4c1aec02017-07-06 11:46:13 -0500395 for (const auto& intf : activations)
396 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500397 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500398 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500399 priorityMap.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600400 intf.first, intf.second->redundancyPriority.get()->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500401 }
402 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500403
404 // Lambda function to compare 2 priority values, use <= to allow duplicates
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600405 typedef std::function<bool(std::pair<std::string, uint8_t>,
406 std::pair<std::string, uint8_t>)>
407 cmpPriority;
408 cmpPriority cmpPriorityFunc =
409 [](std::pair<std::string, uint8_t> priority1,
410 std::pair<std::string, uint8_t> priority2) {
411 return priority1.second <= priority2.second;
412 };
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500413
414 // Sort versions by ascending priority
415 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600416 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500417
418 auto freePriorityValue = value;
419 for (auto& element : prioritySet)
420 {
421 if (element.first == versionId)
422 {
423 continue;
424 }
425 if (element.second == freePriorityValue)
426 {
427 ++freePriorityValue;
428 auto it = activations.find(element.first);
429 it->second->redundancyPriority.get()->sdbusPriority(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600430 freePriorityValue);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500431 }
432 }
433
434 auto lowestVersion = prioritySet.begin()->first;
435 if (value == prioritySet.begin()->second)
436 {
437 lowestVersion = versionId;
438 }
439 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500440}
441
Michael Tritz37a59042017-07-12 13:44:53 -0500442void ItemUpdater::reset()
443{
Lei YU56aaf452018-06-21 16:09:44 +0800444 helper.factoryReset();
Michael Tritz37a59042017-07-12 13:44:53 -0500445
446 log<level::INFO>("BMC factory reset will take effect upon reboot.");
Michael Tritz37a59042017-07-12 13:44:53 -0500447}
448
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500449void ItemUpdater::removeReadOnlyPartition(std::string versionId)
450{
Lei YU56aaf452018-06-21 16:09:44 +0800451 helper.removeVersion(versionId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500452}
453
Michael Tritz0129d922017-08-10 19:33:46 -0500454bool ItemUpdater::fieldModeEnabled(bool value)
455{
456 // enabling field mode is intended to be one way: false -> true
457 if (value && !control::FieldMode::fieldModeEnabled())
458 {
459 control::FieldMode::fieldModeEnabled(value);
460
Lei YU56aaf452018-06-21 16:09:44 +0800461 helper.enableFieldMode();
Michael Tritz0129d922017-08-10 19:33:46 -0500462 }
Adriana Kobylakd5b8f752019-05-01 11:52:35 -0500463 else if (!value && control::FieldMode::fieldModeEnabled())
464 {
465 elog<NotAllowed>(xyz::openbmc_project::Common::NotAllowed::REASON(
466 "FieldMode is not allowed to be cleared"));
467 }
Michael Tritz0129d922017-08-10 19:33:46 -0500468
469 return control::FieldMode::fieldModeEnabled();
470}
471
472void ItemUpdater::restoreFieldModeStatus()
473{
Michael Tritzff0b4212017-10-24 17:38:09 -0500474 std::ifstream input("/dev/mtd/u-boot-env");
Michael Tritz0129d922017-08-10 19:33:46 -0500475 std::string envVar;
476 std::getline(input, envVar);
477
Gunnar Mills9a782242017-08-22 16:23:15 -0500478 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500479 {
480 ItemUpdater::fieldModeEnabled(true);
481 }
482}
483
Gunnar Millsb60add12017-08-24 16:41:42 -0500484void ItemUpdater::setBMCInventoryPath()
485{
Gunnar Millsb60add12017-08-24 16:41:42 -0500486 auto depth = 0;
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600487 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
488 MAPPER_INTERFACE, "GetSubTreePaths");
Gunnar Millsb60add12017-08-24 16:41:42 -0500489
Adriana Kobylak1254c622017-12-07 12:24:56 -0600490 mapperCall.append(INVENTORY_PATH);
Gunnar Millsb60add12017-08-24 16:41:42 -0500491 mapperCall.append(depth);
Adriana Kobylak1254c622017-12-07 12:24:56 -0600492 std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE};
Gunnar Millsb60add12017-08-24 16:41:42 -0500493 mapperCall.append(filter);
494
Ed Tanous87c78172018-08-10 12:51:53 -0700495 try
496 {
497 auto response = bus.call(mapperCall);
498
499 using ObjectPaths = std::vector<std::string>;
500 ObjectPaths result;
501 response.read(result);
502
503 if (!result.empty())
504 {
505 bmcInventoryPath = result.front();
506 }
507 }
508 catch (const sdbusplus::exception::SdBusError& e)
Gunnar Millsb60add12017-08-24 16:41:42 -0500509 {
510 log<level::ERR>("Error in mapper GetSubTreePath");
511 return;
512 }
513
Adriana Kobylak1254c622017-12-07 12:24:56 -0600514 return;
Gunnar Millsb60add12017-08-24 16:41:42 -0500515}
516
Gunnar Millsf10b2322017-09-21 15:31:55 -0500517void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500518{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600519 assocs.emplace_back(
520 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
Gunnar Millsded875d2017-08-28 16:44:52 -0500521 associations(assocs);
522}
523
Gunnar Mills88e8a322017-09-13 11:09:28 -0500524void ItemUpdater::createFunctionalAssociation(const std::string& path)
525{
526 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600527 FUNCTIONAL_REV_ASSOCIATION, path));
Gunnar Mills88e8a322017-09-13 11:09:28 -0500528 associations(assocs);
529}
530
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600531void ItemUpdater::removeAssociations(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500532{
533 for (auto iter = assocs.begin(); iter != assocs.end();)
534 {
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600535 if ((std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500536 {
537 iter = assocs.erase(iter);
538 associations(assocs);
539 }
540 else
541 {
542 ++iter;
543 }
544 }
545}
546
Saqib Khanb9da6632017-09-13 09:48:37 -0500547bool ItemUpdater::isLowestPriority(uint8_t value)
548{
549 for (const auto& intf : activations)
550 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500551 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500552 {
553 if (intf.second->redundancyPriority.get()->priority() < value)
554 {
555 return false;
556 }
557 }
558 }
559 return true;
560}
561
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500562void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
563{
Lei YU56aaf452018-06-21 16:09:44 +0800564 helper.updateUbootVersionId(versionId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500565}
566
Saqib Khan49446ae2017-10-02 10:54:20 -0500567void ItemUpdater::resetUbootEnvVars()
568{
569 decltype(activations.begin()->second->redundancyPriority.get()->priority())
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600570 lowestPriority = std::numeric_limits<uint8_t>::max();
Saqib Khan49446ae2017-10-02 10:54:20 -0500571 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
572 for (const auto& intf : activations)
573 {
574 if (!intf.second->redundancyPriority.get())
575 {
576 // Skip this version if the redundancyPriority is not initialized.
577 continue;
578 }
579
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600580 if (intf.second->redundancyPriority.get()->priority() <= lowestPriority)
Saqib Khan49446ae2017-10-02 10:54:20 -0500581 {
582 lowestPriority = intf.second->redundancyPriority.get()->priority();
583 lowestPriorityVersion = intf.second->versionId;
584 }
585 }
586
Saqib Khanf0382c32017-10-24 13:36:22 -0500587 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500588 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500589}
590
Adriana Kobylaka6963592018-09-07 14:13:29 -0500591void ItemUpdater::freeSpace(Activation& caller)
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600592{
593 // Versions with the highest priority in front
594 std::priority_queue<std::pair<int, std::string>,
595 std::vector<std::pair<int, std::string>>,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600596 std::less<std::pair<int, std::string>>>
597 versionsPQ;
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600598
599 std::size_t count = 0;
600 for (const auto& iter : activations)
601 {
602 if ((iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600603 server::Activation::Activations::Active) ||
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600604 (iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600605 server::Activation::Activations::Failed))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600606 {
607 count++;
608 // Don't put the functional version on the queue since we can't
609 // remove the "running" BMC version.
Lei YU0f88b5a2018-08-21 15:28:53 +0800610 // If ACTIVE_BMC_MAX_ALLOWED <= 1, there is only one active BMC,
611 // so remove functional version as well.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500612 // Don't delete the the Activation object that called this function.
613 if ((versions.find(iter.second->versionId)
614 ->second->isFunctional() &&
615 ACTIVE_BMC_MAX_ALLOWED > 1) ||
616 (iter.second->versionId == caller.versionId))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600617 {
618 continue;
619 }
Adriana Kobylaka6963592018-09-07 14:13:29 -0500620
621 // Failed activations don't have priority, assign them a large value
622 // for sorting purposes.
623 auto priority = 999;
624 if (iter.second.get()->activation() ==
625 server::Activation::Activations::Active)
626 {
627 priority = iter.second->redundancyPriority.get()->priority();
628 }
629
630 versionsPQ.push(std::make_pair(priority, iter.second->versionId));
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600631 }
632 }
633
634 // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1,
635 // remove the highest priority one(s).
636 while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty()))
637 {
638 erase(versionsPQ.top().second);
639 versionsPQ.pop();
640 count--;
641 }
642}
643
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600644void ItemUpdater::mirrorUbootToAlt()
645{
Lei YU56aaf452018-06-21 16:09:44 +0800646 helper.mirrorAlt();
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600647}
648
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500649} // namespace updater
650} // namespace software
651} // namespace phosphor