blob: 71ed3fd29734a8aba607e6c6b8ed72ae60b014d1 [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
Adriana Kobylakd5b8f752019-05-01 11:52:35 -050010#include <phosphor-logging/elog-errors.hpp>
Gunnar Millsb0ce9962018-09-07 13:39:10 -050011#include <phosphor-logging/elog.hpp>
12#include <phosphor-logging/log.hpp>
Adriana Kobylak58aa7502020-06-08 11:12:11 -050013#include <xyz/openbmc_project/Common/error.hpp>
14#include <xyz/openbmc_project/Software/Image/error.hpp>
15
16#include <filesystem>
17#include <fstream>
Adriana Kobylak204e1e72018-01-24 16:00:05 -060018#include <queue>
Adriana Kobylakb77551c2017-10-27 12:46:23 -050019#include <set>
Gunnar Millsec1b41c2017-05-02 12:20:36 -050020#include <string>
Gunnar Mills60f5ccf2020-05-28 12:26:39 -050021#include <thread>
Gunnar Millsec1b41c2017-05-02 12:20:36 -050022
23namespace phosphor
24{
25namespace software
26{
27namespace updater
28{
29
Gunnar Mills2ce7da22017-05-04 15:37:56 -050030// When you see server:: you know we're referencing our base class
31namespace server = sdbusplus::xyz::openbmc_project::Software::server;
Michael Tritz0129d922017-08-10 19:33:46 -050032namespace control = sdbusplus::xyz::openbmc_project::Control::server;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050033
34using namespace phosphor::logging;
Adriana Kobylak43699ca2018-10-17 14:56:29 -050035using namespace sdbusplus::xyz::openbmc_project::Software::Image::Error;
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060036using namespace phosphor::software::image;
Adriana Kobylakc98d9122020-05-05 10:36:01 -050037namespace fs = std::filesystem;
Adriana Kobylakd5b8f752019-05-01 11:52:35 -050038using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
Saqib Khan35e83f32017-05-22 11:37:32 -050039
Patrick Williamse75d10f2017-05-30 16:56:32 -050040void ItemUpdater::createActivation(sdbusplus::message::message& msg)
Gunnar Millsec1b41c2017-05-02 12:20:36 -050041{
Saqib Khan84a0e692017-06-28 17:27:01 -050042
43 using SVersion = server::Version;
44 using VersionPurpose = SVersion::VersionPurpose;
Gunnar Mills9a782242017-08-22 16:23:15 -050045 using VersionClass = phosphor::software::manager::Version;
Saqib Khan84a0e692017-06-28 17:27:01 -050046
Patrick Williamsbc1facd2020-06-03 05:58:27 -050047 sdbusplus::message::object_path objPath;
Saqib Khan84a0e692017-06-28 17:27:01 -050048 auto purpose = VersionPurpose::Unknown;
Saqib Khan705f1bf2017-06-09 23:58:38 -050049 std::string version;
Patrick Williamsbc1facd2020-06-03 05:58:27 -050050 std::map<std::string, std::map<std::string, std::variant<std::string>>>
Adriana Kobylak2285fe02018-02-27 15:36:59 -060051 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(
Patrick Williamse883fb82020-05-13 11:38:55 -050065 std::get<std::string>(property.second));
Saqib Khan84a0e692017-06-28 17:27:01 -050066 if (value == VersionPurpose::BMC ||
Vijay Khemkae9f6c842020-01-14 14:32:39 -080067#ifdef HOST_BIOS_UPGRADE
68 value == VersionPurpose::Host ||
69#endif
Saqib Khan84a0e692017-06-28 17:27:01 -050070 value == VersionPurpose::System)
71 {
72 purpose = value;
73 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050074 }
75 else if (property.first == "Version")
76 {
Patrick Williamse883fb82020-05-13 11:38:55 -050077 version = std::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050078 }
79 }
80 }
Saqib Khan19177d32017-06-20 08:11:49 -050081 else if (intf.first == FILEPATH_IFACE)
82 {
83 for (const auto& property : intf.second)
84 {
85 if (property.first == "Path")
86 {
Patrick Williamse883fb82020-05-13 11:38:55 -050087 filePath = std::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050088 }
89 }
90 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050091 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -060092 if (version.empty() || filePath.empty() ||
Saqib Khan84a0e692017-06-28 17:27:01 -050093 purpose == VersionPurpose::Unknown)
Saqib Khan705f1bf2017-06-09 23:58:38 -050094 {
95 return;
96 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050097
98 // Version id is the last item in the path
99 auto pos = path.rfind("/");
100 if (pos == std::string::npos)
101 {
102 log<level::ERR>("No version id found in object path",
Adriana Kobylak596466b2018-02-13 14:48:53 -0600103 entry("OBJPATH=%s", path.c_str()));
Patrick Williamse75d10f2017-05-30 16:56:32 -0500104 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500105 }
106
107 auto versionId = path.substr(pos + 1);
108
Patrick Williamse75d10f2017-05-30 16:56:32 -0500109 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500110 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500111 // Determine the Activation state by processing the given image dir.
112 auto activationState = server::Activation::Activations::Invalid;
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800113 ItemUpdater::ActivationStatus result;
114 if (purpose == VersionPurpose::BMC || purpose == VersionPurpose::System)
115 result = ItemUpdater::validateSquashFSImage(filePath);
116 else
117 result = ItemUpdater::ActivationStatus::ready;
118
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500119 AssociationList associations = {};
120
Saqib Khan35e83f32017-05-22 11:37:32 -0500121 if (result == ItemUpdater::ActivationStatus::ready)
122 {
123 activationState = server::Activation::Activations::Ready;
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500124 // Create an association to the BMC inventory item
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600125 associations.emplace_back(
126 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
127 ACTIVATION_REV_ASSOCIATION, bmcInventoryPath));
Saqib Khan35e83f32017-05-22 11:37:32 -0500128 }
Gunnar Millsb60add12017-08-24 16:41:42 -0500129
Saqib Khanee13e832017-10-23 12:53:11 -0500130 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600131 versionId,
132 std::make_unique<Activation>(bus, path, *this, versionId,
133 activationState, associations)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500134
Saqib Khanee13e832017-10-23 12:53:11 -0500135 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600136 bus, path, version, purpose, filePath,
137 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Saqib Khanee13e832017-10-23 12:53:11 -0500138 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600139 std::make_unique<phosphor::software::manager::Delete>(bus, path,
140 *versionPtr);
Saqib Khanee13e832017-10-23 12:53:11 -0500141 versions.insert(std::make_pair(versionId, std::move(versionPtr)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500142 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500143 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500144}
145
Saqib Khanba239882017-05-26 08:41:54 -0500146void ItemUpdater::processBMCImage()
147{
Gunnar Mills88e8a322017-09-13 11:09:28 -0500148 using VersionClass = phosphor::software::manager::Version;
Lei YU269bff32018-08-21 15:21:40 +0800149
150 // Check MEDIA_DIR and create if it does not exist
151 try
152 {
153 if (!fs::is_directory(MEDIA_DIR))
154 {
155 fs::create_directory(MEDIA_DIR);
156 }
157 }
158 catch (const fs::filesystem_error& e)
159 {
160 log<level::ERR>("Failed to prepare dir", entry("ERR=%s", e.what()));
161 return;
162 }
163
Gunnar Mills88e8a322017-09-13 11:09:28 -0500164 // Read os-release from /etc/ to get the functional BMC version
165 auto functionalVersion = VersionClass::getBMCVersion(OS_RELEASE_FILE);
166
Saqib Khan1eef62d2017-08-10 15:29:34 -0500167 // Read os-release from folders under /media/ to get
168 // BMC Software Versions.
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500169 for (const auto& iter : fs::directory_iterator(MEDIA_DIR))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500170 {
171 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500172 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500173
174 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600175 if (0 ==
176 iter.path().native().compare(0, BMC_RO_PREFIX_LEN, BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500177 {
Saqib Khan021c3652017-09-26 12:11:02 -0500178 // The versionId is extracted from the path
179 // for example /media/ro-2a1022fe.
180 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
Adriana Kobylak24a8d832020-06-10 08:29:36 -0500181 fs::path releaseFile(OS_RELEASE_FILE);
182 auto osRelease = iter.path() / releaseFile.relative_path();
Saqib Khan1eef62d2017-08-10 15:29:34 -0500183 if (!fs::is_regular_file(osRelease))
184 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600185 log<level::ERR>(
186 "Failed to read osRelease",
187 entry("FILENAME=%s", osRelease.string().c_str()));
Saqib Khan021c3652017-09-26 12:11:02 -0500188 ItemUpdater::erase(id);
189 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500190 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500191 auto version = VersionClass::getBMCVersion(osRelease);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500192 if (version.empty())
193 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600194 log<level::ERR>(
195 "Failed to read version from osRelease",
196 entry("FILENAME=%s", osRelease.string().c_str()));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500197 activationState = server::Activation::Activations::Invalid;
198 }
Saqib Khan021c3652017-09-26 12:11:02 -0500199
Saqib Khan1eef62d2017-08-10 15:29:34 -0500200 auto purpose = server::Version::VersionPurpose::BMC;
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600201 restorePurpose(id, purpose);
202
Saqib Khan1eef62d2017-08-10 15:29:34 -0500203 auto path = fs::path(SOFTWARE_OBJPATH) / id;
204
Lei YU269bff32018-08-21 15:21:40 +0800205 // Create functional association if this is the functional
206 // version
Gunnar Mills88e8a322017-09-13 11:09:28 -0500207 if (version.compare(functionalVersion) == 0)
208 {
209 createFunctionalAssociation(path);
210 }
211
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500212 AssociationList associations = {};
213
214 if (activationState == server::Activation::Activations::Active)
215 {
216 // Create an association to the BMC inventory item
217 associations.emplace_back(std::make_tuple(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600218 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
219 bmcInventoryPath));
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500220
221 // Create an active association since this image is active
222 createActiveAssociation(path);
223 }
224
AppaRao Pulibbebec72020-01-28 23:57:41 +0530225 // All updateable firmware components must expose the updateable
226 // association.
227 createUpdateableAssociation(path);
228
Adriana Kobylakee590c72017-09-26 15:16:06 -0500229 // Create Version instance for this version.
230 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600231 bus, path, version, purpose, "",
232 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500233 auto isVersionFunctional = versionPtr->isFunctional();
Michael Tritz4254bec2017-10-03 17:18:22 -0500234 if (!isVersionFunctional)
235 {
Saqib Khanee13e832017-10-23 12:53:11 -0500236 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600237 std::make_unique<phosphor::software::manager::Delete>(
238 bus, path, *versionPtr);
Michael Tritz4254bec2017-10-03 17:18:22 -0500239 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600240 versions.insert(std::make_pair(id, std::move(versionPtr)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500241
Saqib Khanee13e832017-10-23 12:53:11 -0500242 // Create Activation instance for this version.
243 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600244 id, std::make_unique<Activation>(
245 bus, path, *this, id, activationState, associations)));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500246
Lei YU269bff32018-08-21 15:21:40 +0800247 // If Active, create RedundancyPriority instance for this
248 // version.
Saqib Khan1eef62d2017-08-10 15:29:34 -0500249 if (activationState == server::Activation::Activations::Active)
250 {
251 uint8_t priority = std::numeric_limits<uint8_t>::max();
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600252 if (!restorePriority(id, priority))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500253 {
Adriana Kobylakee590c72017-09-26 15:16:06 -0500254 if (isVersionFunctional)
255 {
256 priority = 0;
257 }
258 else
259 {
260 log<level::ERR>("Unable to restore priority from file.",
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600261 entry("VERSIONID=%s", id.c_str()));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500262 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500263 }
264 activations.find(id)->second->redundancyPriority =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600265 std::make_unique<RedundancyPriority>(
266 bus, path, *(activations.find(id)->second), priority,
267 false);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500268 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500269 }
270 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500271
272 // If there is no ubi volume for bmc version then read the /etc/os-release
273 // and create rofs-<versionId> under /media
274 if (activations.size() == 0)
275 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500276 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Saqib Khandcbfa042017-09-18 13:08:39 -0500277 auto id = phosphor::software::manager::Version::getId(version);
278 auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/";
279 try
280 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500281 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500282 {
283 fs::create_directories(versionFileDir);
284 }
285 auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE;
286 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
287 ItemUpdater::processBMCImage();
288 }
289 catch (const std::exception& e)
290 {
291 log<level::ERR>(e.what());
292 }
293 }
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600294
295 mirrorUbootToAlt();
Saqib Khanba239882017-05-26 08:41:54 -0500296 return;
297}
298
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500299void ItemUpdater::erase(std::string entryId)
300{
Eddie James6d873712017-09-01 11:29:07 -0500301 // Find entry in versions map
302 auto it = versions.find(entryId);
303 if (it != versions.end())
304 {
Lei YU0f88b5a2018-08-21 15:28:53 +0800305 if (it->second->isFunctional() && ACTIVE_BMC_MAX_ALLOWED > 1)
Eddie James6d873712017-09-01 11:29:07 -0500306 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600307 log<level::ERR>("Error: Version is currently running on the BMC. "
308 "Unable to remove.",
309 entry("VERSIONID=%s", entryId.c_str()));
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500310 return;
Eddie James6d873712017-09-01 11:29:07 -0500311 }
312
313 // Delete ReadOnly partitions if it's not active
314 removeReadOnlyPartition(entryId);
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600315 removePersistDataDirectory(entryId);
Saqib Khanee13e832017-10-23 12:53:11 -0500316
317 // Removing entry in versions map
318 this->versions.erase(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500319 }
320 else
321 {
322 // Delete ReadOnly partitions even if we can't find the version
323 removeReadOnlyPartition(entryId);
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600324 removePersistDataDirectory(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500325
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600326 log<level::ERR>("Error: Failed to find version in item updater "
327 "versions map. Unable to remove.",
328 entry("VERSIONID=%s", entryId.c_str()));
Eddie James6d873712017-09-01 11:29:07 -0500329 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500330
Lei YU56aaf452018-06-21 16:09:44 +0800331 helper.clearEntry(entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500332
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500333 // Removing entry in activations map
334 auto ita = activations.find(entryId);
335 if (ita == activations.end())
336 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600337 log<level::ERR>("Error: Failed to find version in item updater "
338 "activations map. Unable to remove.",
339 entry("VERSIONID=%s", entryId.c_str()));
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500340 }
Saqib Khanee13e832017-10-23 12:53:11 -0500341 else
342 {
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600343 removeAssociations(ita->second->path);
Saqib Khanee13e832017-10-23 12:53:11 -0500344 this->activations.erase(entryId);
345 }
Saqib Khan49446ae2017-10-02 10:54:20 -0500346 ItemUpdater::resetUbootEnvVars();
Saqib Khanee13e832017-10-23 12:53:11 -0500347 return;
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500348}
349
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500350void ItemUpdater::deleteAll()
351{
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600352 std::vector<std::string> deletableVersions;
353
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500354 for (const auto& versionIt : versions)
355 {
356 if (!versionIt.second->isFunctional())
357 {
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600358 deletableVersions.push_back(versionIt.first);
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500359 }
360 }
361
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600362 for (const auto& deletableIt : deletableVersions)
363 {
364 ItemUpdater::erase(deletableIt);
365 }
366
Lei YU56aaf452018-06-21 16:09:44 +0800367 helper.cleanup();
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500368}
369
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600370ItemUpdater::ActivationStatus
371 ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500372{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500373 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500374
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500375 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500376 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500377 fs::path file(filePath);
378 file /= bmcImage;
379 std::ifstream efile(file.c_str());
380 if (efile.good() != 1)
381 {
382 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500383 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500384 invalid = true;
385 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500386 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500387
388 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500389 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500390 return ItemUpdater::ActivationStatus::invalid;
391 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500392
393 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500394}
395
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500396void ItemUpdater::savePriority(const std::string& versionId, uint8_t value)
397{
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600398 storePriority(versionId, value);
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500399 helper.setEntry(versionId, value);
400}
401
Saqib Khanb9da6632017-09-13 09:48:37 -0500402void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500403{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500404 std::map<std::string, uint8_t> priorityMap;
405
406 // Insert the requested version and priority, it may not exist yet.
407 priorityMap.insert(std::make_pair(versionId, value));
408
Saqib Khan4c1aec02017-07-06 11:46:13 -0500409 for (const auto& intf : activations)
410 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500411 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500412 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500413 priorityMap.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600414 intf.first, intf.second->redundancyPriority.get()->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500415 }
416 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500417
418 // Lambda function to compare 2 priority values, use <= to allow duplicates
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600419 typedef std::function<bool(std::pair<std::string, uint8_t>,
420 std::pair<std::string, uint8_t>)>
421 cmpPriority;
422 cmpPriority cmpPriorityFunc =
423 [](std::pair<std::string, uint8_t> priority1,
424 std::pair<std::string, uint8_t> priority2) {
425 return priority1.second <= priority2.second;
426 };
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500427
428 // Sort versions by ascending priority
429 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600430 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500431
432 auto freePriorityValue = value;
433 for (auto& element : prioritySet)
434 {
435 if (element.first == versionId)
436 {
437 continue;
438 }
439 if (element.second == freePriorityValue)
440 {
441 ++freePriorityValue;
442 auto it = activations.find(element.first);
443 it->second->redundancyPriority.get()->sdbusPriority(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600444 freePriorityValue);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500445 }
446 }
447
448 auto lowestVersion = prioritySet.begin()->first;
449 if (value == prioritySet.begin()->second)
450 {
451 lowestVersion = versionId;
452 }
453 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500454}
455
Michael Tritz37a59042017-07-12 13:44:53 -0500456void ItemUpdater::reset()
457{
Gunnar Mills60f5ccf2020-05-28 12:26:39 -0500458 constexpr auto setFactoryResetWait = std::chrono::seconds(3);
Lei YU56aaf452018-06-21 16:09:44 +0800459 helper.factoryReset();
Michael Tritz37a59042017-07-12 13:44:53 -0500460
Gunnar Mills60f5ccf2020-05-28 12:26:39 -0500461 // Need to wait for env variables to complete, otherwise an immediate reboot
462 // will not factory reset.
463 std::this_thread::sleep_for(setFactoryResetWait);
464
Michael Tritz37a59042017-07-12 13:44:53 -0500465 log<level::INFO>("BMC factory reset will take effect upon reboot.");
Michael Tritz37a59042017-07-12 13:44:53 -0500466}
467
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500468void ItemUpdater::removeReadOnlyPartition(std::string versionId)
469{
Lei YU56aaf452018-06-21 16:09:44 +0800470 helper.removeVersion(versionId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500471}
472
Michael Tritz0129d922017-08-10 19:33:46 -0500473bool ItemUpdater::fieldModeEnabled(bool value)
474{
475 // enabling field mode is intended to be one way: false -> true
476 if (value && !control::FieldMode::fieldModeEnabled())
477 {
478 control::FieldMode::fieldModeEnabled(value);
479
Adriana Kobylak22848ec2019-10-28 10:08:39 -0500480 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
481 SYSTEMD_INTERFACE, "StartUnit");
482 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
483 "replace");
484 bus.call_noreply(method);
485
486 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
487 SYSTEMD_INTERFACE, "StopUnit");
488 method.append("usr-local.mount", "replace");
489 bus.call_noreply(method);
490
491 std::vector<std::string> usrLocal = {"usr-local.mount"};
492
493 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
494 SYSTEMD_INTERFACE, "MaskUnitFiles");
495 method.append(usrLocal, false, true);
496 bus.call_noreply(method);
Michael Tritz0129d922017-08-10 19:33:46 -0500497 }
Adriana Kobylakd5b8f752019-05-01 11:52:35 -0500498 else if (!value && control::FieldMode::fieldModeEnabled())
499 {
500 elog<NotAllowed>(xyz::openbmc_project::Common::NotAllowed::REASON(
501 "FieldMode is not allowed to be cleared"));
502 }
Michael Tritz0129d922017-08-10 19:33:46 -0500503
504 return control::FieldMode::fieldModeEnabled();
505}
506
507void ItemUpdater::restoreFieldModeStatus()
508{
Michael Tritzff0b4212017-10-24 17:38:09 -0500509 std::ifstream input("/dev/mtd/u-boot-env");
Michael Tritz0129d922017-08-10 19:33:46 -0500510 std::string envVar;
511 std::getline(input, envVar);
512
Gunnar Mills9a782242017-08-22 16:23:15 -0500513 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500514 {
515 ItemUpdater::fieldModeEnabled(true);
516 }
517}
518
Gunnar Millsb60add12017-08-24 16:41:42 -0500519void ItemUpdater::setBMCInventoryPath()
520{
Gunnar Millsb60add12017-08-24 16:41:42 -0500521 auto depth = 0;
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600522 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
523 MAPPER_INTERFACE, "GetSubTreePaths");
Gunnar Millsb60add12017-08-24 16:41:42 -0500524
Adriana Kobylak1254c622017-12-07 12:24:56 -0600525 mapperCall.append(INVENTORY_PATH);
Gunnar Millsb60add12017-08-24 16:41:42 -0500526 mapperCall.append(depth);
Adriana Kobylak1254c622017-12-07 12:24:56 -0600527 std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE};
Gunnar Millsb60add12017-08-24 16:41:42 -0500528 mapperCall.append(filter);
529
Ed Tanous87c78172018-08-10 12:51:53 -0700530 try
531 {
532 auto response = bus.call(mapperCall);
533
534 using ObjectPaths = std::vector<std::string>;
535 ObjectPaths result;
536 response.read(result);
537
538 if (!result.empty())
539 {
540 bmcInventoryPath = result.front();
541 }
542 }
543 catch (const sdbusplus::exception::SdBusError& e)
Gunnar Millsb60add12017-08-24 16:41:42 -0500544 {
545 log<level::ERR>("Error in mapper GetSubTreePath");
546 return;
547 }
548
Adriana Kobylak1254c622017-12-07 12:24:56 -0600549 return;
Gunnar Millsb60add12017-08-24 16:41:42 -0500550}
551
Gunnar Millsf10b2322017-09-21 15:31:55 -0500552void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500553{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600554 assocs.emplace_back(
555 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
Gunnar Millsded875d2017-08-28 16:44:52 -0500556 associations(assocs);
557}
558
Gunnar Mills88e8a322017-09-13 11:09:28 -0500559void ItemUpdater::createFunctionalAssociation(const std::string& path)
560{
561 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600562 FUNCTIONAL_REV_ASSOCIATION, path));
Gunnar Mills88e8a322017-09-13 11:09:28 -0500563 associations(assocs);
564}
565
AppaRao Pulibbebec72020-01-28 23:57:41 +0530566void ItemUpdater::createUpdateableAssociation(const std::string& path)
567{
568 assocs.emplace_back(std::make_tuple(UPDATEABLE_FWD_ASSOCIATION,
569 UPDATEABLE_REV_ASSOCIATION, path));
570 associations(assocs);
571}
572
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600573void ItemUpdater::removeAssociations(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500574{
575 for (auto iter = assocs.begin(); iter != assocs.end();)
576 {
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600577 if ((std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500578 {
579 iter = assocs.erase(iter);
580 associations(assocs);
581 }
582 else
583 {
584 ++iter;
585 }
586 }
587}
588
Saqib Khanb9da6632017-09-13 09:48:37 -0500589bool ItemUpdater::isLowestPriority(uint8_t value)
590{
591 for (const auto& intf : activations)
592 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500593 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500594 {
595 if (intf.second->redundancyPriority.get()->priority() < value)
596 {
597 return false;
598 }
599 }
600 }
601 return true;
602}
603
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500604void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
605{
Lei YU56aaf452018-06-21 16:09:44 +0800606 helper.updateUbootVersionId(versionId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500607}
608
Saqib Khan49446ae2017-10-02 10:54:20 -0500609void ItemUpdater::resetUbootEnvVars()
610{
611 decltype(activations.begin()->second->redundancyPriority.get()->priority())
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600612 lowestPriority = std::numeric_limits<uint8_t>::max();
Saqib Khan49446ae2017-10-02 10:54:20 -0500613 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
614 for (const auto& intf : activations)
615 {
616 if (!intf.second->redundancyPriority.get())
617 {
618 // Skip this version if the redundancyPriority is not initialized.
619 continue;
620 }
621
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600622 if (intf.second->redundancyPriority.get()->priority() <= lowestPriority)
Saqib Khan49446ae2017-10-02 10:54:20 -0500623 {
624 lowestPriority = intf.second->redundancyPriority.get()->priority();
625 lowestPriorityVersion = intf.second->versionId;
626 }
627 }
628
Saqib Khanf0382c32017-10-24 13:36:22 -0500629 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500630 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500631}
632
Adriana Kobylaka6963592018-09-07 14:13:29 -0500633void ItemUpdater::freeSpace(Activation& caller)
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600634{
635 // Versions with the highest priority in front
636 std::priority_queue<std::pair<int, std::string>,
637 std::vector<std::pair<int, std::string>>,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600638 std::less<std::pair<int, std::string>>>
639 versionsPQ;
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600640
641 std::size_t count = 0;
642 for (const auto& iter : activations)
643 {
644 if ((iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600645 server::Activation::Activations::Active) ||
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600646 (iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600647 server::Activation::Activations::Failed))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600648 {
649 count++;
650 // Don't put the functional version on the queue since we can't
651 // remove the "running" BMC version.
Lei YU0f88b5a2018-08-21 15:28:53 +0800652 // If ACTIVE_BMC_MAX_ALLOWED <= 1, there is only one active BMC,
653 // so remove functional version as well.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500654 // Don't delete the the Activation object that called this function.
655 if ((versions.find(iter.second->versionId)
656 ->second->isFunctional() &&
657 ACTIVE_BMC_MAX_ALLOWED > 1) ||
658 (iter.second->versionId == caller.versionId))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600659 {
660 continue;
661 }
Adriana Kobylaka6963592018-09-07 14:13:29 -0500662
663 // Failed activations don't have priority, assign them a large value
664 // for sorting purposes.
665 auto priority = 999;
666 if (iter.second.get()->activation() ==
667 server::Activation::Activations::Active)
668 {
669 priority = iter.second->redundancyPriority.get()->priority();
670 }
671
672 versionsPQ.push(std::make_pair(priority, iter.second->versionId));
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600673 }
674 }
675
676 // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1,
677 // remove the highest priority one(s).
678 while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty()))
679 {
680 erase(versionsPQ.top().second);
681 versionsPQ.pop();
682 count--;
683 }
684}
685
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600686void ItemUpdater::mirrorUbootToAlt()
687{
Lei YU56aaf452018-06-21 16:09:44 +0800688 helper.mirrorAlt();
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600689}
690
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500691} // namespace updater
692} // namespace software
693} // namespace phosphor