blob: 2cd9c390c9675de35bddb31f605892a903ec21fa [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;
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600193 restorePurpose(id, purpose);
194
Saqib Khan1eef62d2017-08-10 15:29:34 -0500195 auto path = fs::path(SOFTWARE_OBJPATH) / id;
196
Lei YU269bff32018-08-21 15:21:40 +0800197 // Create functional association if this is the functional
198 // version
Gunnar Mills88e8a322017-09-13 11:09:28 -0500199 if (version.compare(functionalVersion) == 0)
200 {
201 createFunctionalAssociation(path);
202 }
203
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500204 AssociationList associations = {};
205
206 if (activationState == server::Activation::Activations::Active)
207 {
208 // Create an association to the BMC inventory item
209 associations.emplace_back(std::make_tuple(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600210 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
211 bmcInventoryPath));
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500212
213 // Create an active association since this image is active
214 createActiveAssociation(path);
215 }
216
Adriana Kobylakee590c72017-09-26 15:16:06 -0500217 // Create Version instance for this version.
218 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600219 bus, path, version, purpose, "",
220 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500221 auto isVersionFunctional = versionPtr->isFunctional();
Michael Tritz4254bec2017-10-03 17:18:22 -0500222 if (!isVersionFunctional)
223 {
Saqib Khanee13e832017-10-23 12:53:11 -0500224 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600225 std::make_unique<phosphor::software::manager::Delete>(
226 bus, path, *versionPtr);
Michael Tritz4254bec2017-10-03 17:18:22 -0500227 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600228 versions.insert(std::make_pair(id, std::move(versionPtr)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500229
Saqib Khanee13e832017-10-23 12:53:11 -0500230 // Create Activation instance for this version.
231 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600232 id, std::make_unique<Activation>(
233 bus, path, *this, id, activationState, associations)));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500234
Lei YU269bff32018-08-21 15:21:40 +0800235 // If Active, create RedundancyPriority instance for this
236 // version.
Saqib Khan1eef62d2017-08-10 15:29:34 -0500237 if (activationState == server::Activation::Activations::Active)
238 {
239 uint8_t priority = std::numeric_limits<uint8_t>::max();
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600240 if (!restorePriority(id, priority))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500241 {
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.",
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600249 entry("VERSIONID=%s", id.c_str()));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500250 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500251 }
252 activations.find(id)->second->redundancyPriority =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600253 std::make_unique<RedundancyPriority>(
254 bus, path, *(activations.find(id)->second), priority,
255 false);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500256 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500257 }
258 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500259
260 // If there is no ubi volume for bmc version then read the /etc/os-release
261 // and create rofs-<versionId> under /media
262 if (activations.size() == 0)
263 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500264 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Saqib Khandcbfa042017-09-18 13:08:39 -0500265 auto id = phosphor::software::manager::Version::getId(version);
266 auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/";
267 try
268 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500269 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500270 {
271 fs::create_directories(versionFileDir);
272 }
273 auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE;
274 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
275 ItemUpdater::processBMCImage();
276 }
277 catch (const std::exception& e)
278 {
279 log<level::ERR>(e.what());
280 }
281 }
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600282
283 mirrorUbootToAlt();
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 {
Lei YU0f88b5a2018-08-21 15:28:53 +0800293 if (it->second->isFunctional() && ACTIVE_BMC_MAX_ALLOWED > 1)
Eddie James6d873712017-09-01 11:29:07 -0500294 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600295 log<level::ERR>("Error: Version is currently running on the BMC. "
296 "Unable to remove.",
297 entry("VERSIONID=%s", entryId.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);
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600303 removePersistDataDirectory(entryId);
Saqib Khanee13e832017-10-23 12:53:11 -0500304
305 // Removing entry in versions map
306 this->versions.erase(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500307 }
308 else
309 {
310 // Delete ReadOnly partitions even if we can't find the version
311 removeReadOnlyPartition(entryId);
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600312 removePersistDataDirectory(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500313
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600314 log<level::ERR>("Error: Failed to find version in item updater "
315 "versions map. Unable to remove.",
316 entry("VERSIONID=%s", entryId.c_str()));
Eddie James6d873712017-09-01 11:29:07 -0500317 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500318
Lei YU56aaf452018-06-21 16:09:44 +0800319 helper.clearEntry(entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500320
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500321 // Removing entry in activations map
322 auto ita = activations.find(entryId);
323 if (ita == activations.end())
324 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600325 log<level::ERR>("Error: Failed to find version in item updater "
326 "activations map. Unable to remove.",
327 entry("VERSIONID=%s", entryId.c_str()));
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500328 }
Saqib Khanee13e832017-10-23 12:53:11 -0500329 else
330 {
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600331 removeAssociations(ita->second->path);
Saqib Khanee13e832017-10-23 12:53:11 -0500332 this->activations.erase(entryId);
333 }
Saqib Khan49446ae2017-10-02 10:54:20 -0500334 ItemUpdater::resetUbootEnvVars();
Saqib Khanee13e832017-10-23 12:53:11 -0500335 return;
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500336}
337
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500338void ItemUpdater::deleteAll()
339{
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600340 std::vector<std::string> deletableVersions;
341
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500342 for (const auto& versionIt : versions)
343 {
344 if (!versionIt.second->isFunctional())
345 {
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600346 deletableVersions.push_back(versionIt.first);
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500347 }
348 }
349
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600350 for (const auto& deletableIt : deletableVersions)
351 {
352 ItemUpdater::erase(deletableIt);
353 }
354
Lei YU56aaf452018-06-21 16:09:44 +0800355 helper.cleanup();
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500356}
357
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600358ItemUpdater::ActivationStatus
359 ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500360{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500361 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500362
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500363 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500364 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500365 fs::path file(filePath);
366 file /= bmcImage;
367 std::ifstream efile(file.c_str());
368 if (efile.good() != 1)
369 {
370 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500371 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500372 invalid = true;
373 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500374 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500375
376 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500377 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500378 return ItemUpdater::ActivationStatus::invalid;
379 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500380
381 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500382}
383
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500384void ItemUpdater::savePriority(const std::string& versionId, uint8_t value)
385{
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600386 storePriority(versionId, value);
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500387 helper.setEntry(versionId, value);
388}
389
Saqib Khanb9da6632017-09-13 09:48:37 -0500390void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500391{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500392 std::map<std::string, uint8_t> priorityMap;
393
394 // Insert the requested version and priority, it may not exist yet.
395 priorityMap.insert(std::make_pair(versionId, value));
396
Saqib Khan4c1aec02017-07-06 11:46:13 -0500397 for (const auto& intf : activations)
398 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500399 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500400 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500401 priorityMap.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600402 intf.first, intf.second->redundancyPriority.get()->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500403 }
404 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500405
406 // Lambda function to compare 2 priority values, use <= to allow duplicates
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600407 typedef std::function<bool(std::pair<std::string, uint8_t>,
408 std::pair<std::string, uint8_t>)>
409 cmpPriority;
410 cmpPriority cmpPriorityFunc =
411 [](std::pair<std::string, uint8_t> priority1,
412 std::pair<std::string, uint8_t> priority2) {
413 return priority1.second <= priority2.second;
414 };
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500415
416 // Sort versions by ascending priority
417 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600418 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500419
420 auto freePriorityValue = value;
421 for (auto& element : prioritySet)
422 {
423 if (element.first == versionId)
424 {
425 continue;
426 }
427 if (element.second == freePriorityValue)
428 {
429 ++freePriorityValue;
430 auto it = activations.find(element.first);
431 it->second->redundancyPriority.get()->sdbusPriority(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600432 freePriorityValue);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500433 }
434 }
435
436 auto lowestVersion = prioritySet.begin()->first;
437 if (value == prioritySet.begin()->second)
438 {
439 lowestVersion = versionId;
440 }
441 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500442}
443
Michael Tritz37a59042017-07-12 13:44:53 -0500444void ItemUpdater::reset()
445{
Lei YU56aaf452018-06-21 16:09:44 +0800446 helper.factoryReset();
Michael Tritz37a59042017-07-12 13:44:53 -0500447
448 log<level::INFO>("BMC factory reset will take effect upon reboot.");
Michael Tritz37a59042017-07-12 13:44:53 -0500449}
450
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500451void ItemUpdater::removeReadOnlyPartition(std::string versionId)
452{
Lei YU56aaf452018-06-21 16:09:44 +0800453 helper.removeVersion(versionId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500454}
455
Michael Tritz0129d922017-08-10 19:33:46 -0500456bool ItemUpdater::fieldModeEnabled(bool value)
457{
458 // enabling field mode is intended to be one way: false -> true
459 if (value && !control::FieldMode::fieldModeEnabled())
460 {
461 control::FieldMode::fieldModeEnabled(value);
462
Adriana Kobylak22848ec2019-10-28 10:08:39 -0500463 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
464 SYSTEMD_INTERFACE, "StartUnit");
465 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
466 "replace");
467 bus.call_noreply(method);
468
469 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
470 SYSTEMD_INTERFACE, "StopUnit");
471 method.append("usr-local.mount", "replace");
472 bus.call_noreply(method);
473
474 std::vector<std::string> usrLocal = {"usr-local.mount"};
475
476 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
477 SYSTEMD_INTERFACE, "MaskUnitFiles");
478 method.append(usrLocal, false, true);
479 bus.call_noreply(method);
Michael Tritz0129d922017-08-10 19:33:46 -0500480 }
Adriana Kobylakd5b8f752019-05-01 11:52:35 -0500481 else if (!value && control::FieldMode::fieldModeEnabled())
482 {
483 elog<NotAllowed>(xyz::openbmc_project::Common::NotAllowed::REASON(
484 "FieldMode is not allowed to be cleared"));
485 }
Michael Tritz0129d922017-08-10 19:33:46 -0500486
487 return control::FieldMode::fieldModeEnabled();
488}
489
490void ItemUpdater::restoreFieldModeStatus()
491{
Michael Tritzff0b4212017-10-24 17:38:09 -0500492 std::ifstream input("/dev/mtd/u-boot-env");
Michael Tritz0129d922017-08-10 19:33:46 -0500493 std::string envVar;
494 std::getline(input, envVar);
495
Gunnar Mills9a782242017-08-22 16:23:15 -0500496 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500497 {
498 ItemUpdater::fieldModeEnabled(true);
499 }
500}
501
Gunnar Millsb60add12017-08-24 16:41:42 -0500502void ItemUpdater::setBMCInventoryPath()
503{
Gunnar Millsb60add12017-08-24 16:41:42 -0500504 auto depth = 0;
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600505 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
506 MAPPER_INTERFACE, "GetSubTreePaths");
Gunnar Millsb60add12017-08-24 16:41:42 -0500507
Adriana Kobylak1254c622017-12-07 12:24:56 -0600508 mapperCall.append(INVENTORY_PATH);
Gunnar Millsb60add12017-08-24 16:41:42 -0500509 mapperCall.append(depth);
Adriana Kobylak1254c622017-12-07 12:24:56 -0600510 std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE};
Gunnar Millsb60add12017-08-24 16:41:42 -0500511 mapperCall.append(filter);
512
Ed Tanous87c78172018-08-10 12:51:53 -0700513 try
514 {
515 auto response = bus.call(mapperCall);
516
517 using ObjectPaths = std::vector<std::string>;
518 ObjectPaths result;
519 response.read(result);
520
521 if (!result.empty())
522 {
523 bmcInventoryPath = result.front();
524 }
525 }
526 catch (const sdbusplus::exception::SdBusError& e)
Gunnar Millsb60add12017-08-24 16:41:42 -0500527 {
528 log<level::ERR>("Error in mapper GetSubTreePath");
529 return;
530 }
531
Adriana Kobylak1254c622017-12-07 12:24:56 -0600532 return;
Gunnar Millsb60add12017-08-24 16:41:42 -0500533}
534
Gunnar Millsf10b2322017-09-21 15:31:55 -0500535void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500536{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600537 assocs.emplace_back(
538 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
Gunnar Millsded875d2017-08-28 16:44:52 -0500539 associations(assocs);
540}
541
Gunnar Mills88e8a322017-09-13 11:09:28 -0500542void ItemUpdater::createFunctionalAssociation(const std::string& path)
543{
544 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600545 FUNCTIONAL_REV_ASSOCIATION, path));
Gunnar Mills88e8a322017-09-13 11:09:28 -0500546 associations(assocs);
547}
548
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600549void ItemUpdater::removeAssociations(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500550{
551 for (auto iter = assocs.begin(); iter != assocs.end();)
552 {
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600553 if ((std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500554 {
555 iter = assocs.erase(iter);
556 associations(assocs);
557 }
558 else
559 {
560 ++iter;
561 }
562 }
563}
564
Saqib Khanb9da6632017-09-13 09:48:37 -0500565bool ItemUpdater::isLowestPriority(uint8_t value)
566{
567 for (const auto& intf : activations)
568 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500569 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500570 {
571 if (intf.second->redundancyPriority.get()->priority() < value)
572 {
573 return false;
574 }
575 }
576 }
577 return true;
578}
579
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500580void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
581{
Lei YU56aaf452018-06-21 16:09:44 +0800582 helper.updateUbootVersionId(versionId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500583}
584
Saqib Khan49446ae2017-10-02 10:54:20 -0500585void ItemUpdater::resetUbootEnvVars()
586{
587 decltype(activations.begin()->second->redundancyPriority.get()->priority())
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600588 lowestPriority = std::numeric_limits<uint8_t>::max();
Saqib Khan49446ae2017-10-02 10:54:20 -0500589 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
590 for (const auto& intf : activations)
591 {
592 if (!intf.second->redundancyPriority.get())
593 {
594 // Skip this version if the redundancyPriority is not initialized.
595 continue;
596 }
597
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600598 if (intf.second->redundancyPriority.get()->priority() <= lowestPriority)
Saqib Khan49446ae2017-10-02 10:54:20 -0500599 {
600 lowestPriority = intf.second->redundancyPriority.get()->priority();
601 lowestPriorityVersion = intf.second->versionId;
602 }
603 }
604
Saqib Khanf0382c32017-10-24 13:36:22 -0500605 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500606 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500607}
608
Adriana Kobylaka6963592018-09-07 14:13:29 -0500609void ItemUpdater::freeSpace(Activation& caller)
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600610{
611 // Versions with the highest priority in front
612 std::priority_queue<std::pair<int, std::string>,
613 std::vector<std::pair<int, std::string>>,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600614 std::less<std::pair<int, std::string>>>
615 versionsPQ;
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600616
617 std::size_t count = 0;
618 for (const auto& iter : activations)
619 {
620 if ((iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600621 server::Activation::Activations::Active) ||
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600622 (iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600623 server::Activation::Activations::Failed))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600624 {
625 count++;
626 // Don't put the functional version on the queue since we can't
627 // remove the "running" BMC version.
Lei YU0f88b5a2018-08-21 15:28:53 +0800628 // If ACTIVE_BMC_MAX_ALLOWED <= 1, there is only one active BMC,
629 // so remove functional version as well.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500630 // Don't delete the the Activation object that called this function.
631 if ((versions.find(iter.second->versionId)
632 ->second->isFunctional() &&
633 ACTIVE_BMC_MAX_ALLOWED > 1) ||
634 (iter.second->versionId == caller.versionId))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600635 {
636 continue;
637 }
Adriana Kobylaka6963592018-09-07 14:13:29 -0500638
639 // Failed activations don't have priority, assign them a large value
640 // for sorting purposes.
641 auto priority = 999;
642 if (iter.second.get()->activation() ==
643 server::Activation::Activations::Active)
644 {
645 priority = iter.second->redundancyPriority.get()->priority();
646 }
647
648 versionsPQ.push(std::make_pair(priority, iter.second->versionId));
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600649 }
650 }
651
652 // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1,
653 // remove the highest priority one(s).
654 while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty()))
655 {
656 erase(versionsPQ.top().second);
657 versionsPQ.pop();
658 count--;
659 }
660}
661
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600662void ItemUpdater::mirrorUbootToAlt()
663{
Lei YU56aaf452018-06-21 16:09:44 +0800664 helper.mirrorAlt();
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600665}
666
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500667} // namespace updater
668} // namespace software
669} // namespace phosphor