blob: 52b60b13990d34c779854b450ee277946dcfc6f5 [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 Kobylakc98d9122020-05-05 10:36:01 -050010#include <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>
Gunnar Mills60f5ccf2020-05-28 12:26:39 -050018#include <thread>
Adriana Kobylakd5b8f752019-05-01 11:52:35 -050019#include <xyz/openbmc_project/Common/error.hpp>
Adriana Kobylak43699ca2018-10-17 14:56:29 -050020#include <xyz/openbmc_project/Software/Image/error.hpp>
Gunnar Millsec1b41c2017-05-02 12:20:36 -050021
22namespace phosphor
23{
24namespace software
25{
26namespace updater
27{
28
Gunnar Mills2ce7da22017-05-04 15:37:56 -050029// When you see server:: you know we're referencing our base class
30namespace server = sdbusplus::xyz::openbmc_project::Software::server;
Michael Tritz0129d922017-08-10 19:33:46 -050031namespace control = sdbusplus::xyz::openbmc_project::Control::server;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050032
33using namespace phosphor::logging;
Adriana Kobylak43699ca2018-10-17 14:56:29 -050034using namespace sdbusplus::xyz::openbmc_project::Software::Image::Error;
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060035using namespace phosphor::software::image;
Adriana Kobylakc98d9122020-05-05 10:36:01 -050036namespace fs = std::filesystem;
Adriana Kobylakd5b8f752019-05-01 11:52:35 -050037using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
Saqib Khan35e83f32017-05-22 11:37:32 -050038
Patrick Williamse75d10f2017-05-30 16:56:32 -050039void ItemUpdater::createActivation(sdbusplus::message::message& msg)
Gunnar Millsec1b41c2017-05-02 12:20:36 -050040{
Saqib Khan84a0e692017-06-28 17:27:01 -050041
42 using SVersion = server::Version;
43 using VersionPurpose = SVersion::VersionPurpose;
Gunnar Mills9a782242017-08-22 16:23:15 -050044 using VersionClass = phosphor::software::manager::Version;
Saqib Khan84a0e692017-06-28 17:27:01 -050045
Patrick Williamsbc1facd2020-06-03 05:58:27 -050046 sdbusplus::message::object_path objPath;
Saqib Khan84a0e692017-06-28 17:27:01 -050047 auto purpose = VersionPurpose::Unknown;
Saqib Khan705f1bf2017-06-09 23:58:38 -050048 std::string version;
Patrick Williamsbc1facd2020-06-03 05:58:27 -050049 std::map<std::string, std::map<std::string, std::variant<std::string>>>
Adriana Kobylak2285fe02018-02-27 15:36:59 -060050 interfaces;
Patrick Williamse75d10f2017-05-30 16:56:32 -050051 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050052 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050053 std::string filePath;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050054
55 for (const auto& intf : interfaces)
56 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050057 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050058 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050059 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050060 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050061 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050062 {
Saqib Khan84a0e692017-06-28 17:27:01 -050063 auto value = SVersion::convertVersionPurposeFromString(
Patrick Williamse883fb82020-05-13 11:38:55 -050064 std::get<std::string>(property.second));
Saqib Khan84a0e692017-06-28 17:27:01 -050065 if (value == VersionPurpose::BMC ||
Vijay Khemkae9f6c842020-01-14 14:32:39 -080066#ifdef HOST_BIOS_UPGRADE
67 value == VersionPurpose::Host ||
68#endif
Saqib Khan84a0e692017-06-28 17:27:01 -050069 value == VersionPurpose::System)
70 {
71 purpose = value;
72 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050073 }
74 else if (property.first == "Version")
75 {
Patrick Williamse883fb82020-05-13 11:38:55 -050076 version = std::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050077 }
78 }
79 }
Saqib Khan19177d32017-06-20 08:11:49 -050080 else if (intf.first == FILEPATH_IFACE)
81 {
82 for (const auto& property : intf.second)
83 {
84 if (property.first == "Path")
85 {
Patrick Williamse883fb82020-05-13 11:38:55 -050086 filePath = std::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050087 }
88 }
89 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050090 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -060091 if (version.empty() || filePath.empty() ||
Saqib Khan84a0e692017-06-28 17:27:01 -050092 purpose == VersionPurpose::Unknown)
Saqib Khan705f1bf2017-06-09 23:58:38 -050093 {
94 return;
95 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050096
97 // Version id is the last item in the path
98 auto pos = path.rfind("/");
99 if (pos == std::string::npos)
100 {
101 log<level::ERR>("No version id found in object path",
Adriana Kobylak596466b2018-02-13 14:48:53 -0600102 entry("OBJPATH=%s", path.c_str()));
Patrick Williamse75d10f2017-05-30 16:56:32 -0500103 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500104 }
105
106 auto versionId = path.substr(pos + 1);
107
Patrick Williamse75d10f2017-05-30 16:56:32 -0500108 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500109 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500110 // Determine the Activation state by processing the given image dir.
111 auto activationState = server::Activation::Activations::Invalid;
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800112 ItemUpdater::ActivationStatus result;
113 if (purpose == VersionPurpose::BMC || purpose == VersionPurpose::System)
114 result = ItemUpdater::validateSquashFSImage(filePath);
115 else
116 result = ItemUpdater::ActivationStatus::ready;
117
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500118 AssociationList associations = {};
119
Saqib Khan35e83f32017-05-22 11:37:32 -0500120 if (result == ItemUpdater::ActivationStatus::ready)
121 {
122 activationState = server::Activation::Activations::Ready;
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500123 // Create an association to the BMC inventory item
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600124 associations.emplace_back(
125 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
126 ACTIVATION_REV_ASSOCIATION, bmcInventoryPath));
Saqib Khan35e83f32017-05-22 11:37:32 -0500127 }
Gunnar Millsb60add12017-08-24 16:41:42 -0500128
Saqib Khanee13e832017-10-23 12:53:11 -0500129 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600130 versionId,
131 std::make_unique<Activation>(bus, path, *this, versionId,
132 activationState, associations)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500133
Saqib Khanee13e832017-10-23 12:53:11 -0500134 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600135 bus, path, version, purpose, filePath,
136 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Saqib Khanee13e832017-10-23 12:53:11 -0500137 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600138 std::make_unique<phosphor::software::manager::Delete>(bus, path,
139 *versionPtr);
Saqib Khanee13e832017-10-23 12:53:11 -0500140 versions.insert(std::make_pair(versionId, std::move(versionPtr)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500141 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500142 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500143}
144
Saqib Khanba239882017-05-26 08:41:54 -0500145void ItemUpdater::processBMCImage()
146{
Gunnar Mills88e8a322017-09-13 11:09:28 -0500147 using VersionClass = phosphor::software::manager::Version;
Lei YU269bff32018-08-21 15:21:40 +0800148
149 // Check MEDIA_DIR and create if it does not exist
150 try
151 {
152 if (!fs::is_directory(MEDIA_DIR))
153 {
154 fs::create_directory(MEDIA_DIR);
155 }
156 }
157 catch (const fs::filesystem_error& e)
158 {
159 log<level::ERR>("Failed to prepare dir", entry("ERR=%s", e.what()));
160 return;
161 }
162
Gunnar Mills88e8a322017-09-13 11:09:28 -0500163 // Read os-release from /etc/ to get the functional BMC version
164 auto functionalVersion = VersionClass::getBMCVersion(OS_RELEASE_FILE);
165
Saqib Khan1eef62d2017-08-10 15:29:34 -0500166 // Read os-release from folders under /media/ to get
167 // BMC Software Versions.
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500168 for (const auto& iter : fs::directory_iterator(MEDIA_DIR))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500169 {
170 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500171 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500172
173 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600174 if (0 ==
175 iter.path().native().compare(0, BMC_RO_PREFIX_LEN, BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500176 {
Saqib Khan021c3652017-09-26 12:11:02 -0500177 // The versionId is extracted from the path
178 // for example /media/ro-2a1022fe.
179 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500180 auto osRelease = iter.path() / OS_RELEASE_FILE;
181 if (!fs::is_regular_file(osRelease))
182 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600183 log<level::ERR>(
184 "Failed to read osRelease",
185 entry("FILENAME=%s", osRelease.string().c_str()));
Saqib Khan021c3652017-09-26 12:11:02 -0500186 ItemUpdater::erase(id);
187 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500188 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500189 auto version = VersionClass::getBMCVersion(osRelease);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500190 if (version.empty())
191 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600192 log<level::ERR>(
193 "Failed to read version from osRelease",
194 entry("FILENAME=%s", osRelease.string().c_str()));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500195 activationState = server::Activation::Activations::Invalid;
196 }
Saqib Khan021c3652017-09-26 12:11:02 -0500197
Saqib Khan1eef62d2017-08-10 15:29:34 -0500198 auto purpose = server::Version::VersionPurpose::BMC;
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600199 restorePurpose(id, purpose);
200
Saqib Khan1eef62d2017-08-10 15:29:34 -0500201 auto path = fs::path(SOFTWARE_OBJPATH) / id;
202
Lei YU269bff32018-08-21 15:21:40 +0800203 // Create functional association if this is the functional
204 // version
Gunnar Mills88e8a322017-09-13 11:09:28 -0500205 if (version.compare(functionalVersion) == 0)
206 {
207 createFunctionalAssociation(path);
208 }
209
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500210 AssociationList associations = {};
211
212 if (activationState == server::Activation::Activations::Active)
213 {
214 // Create an association to the BMC inventory item
215 associations.emplace_back(std::make_tuple(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600216 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
217 bmcInventoryPath));
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500218
219 // Create an active association since this image is active
220 createActiveAssociation(path);
221 }
222
AppaRao Pulibbebec72020-01-28 23:57:41 +0530223 // All updateable firmware components must expose the updateable
224 // association.
225 createUpdateableAssociation(path);
226
Adriana Kobylakee590c72017-09-26 15:16:06 -0500227 // Create Version instance for this version.
228 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600229 bus, path, version, purpose, "",
230 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500231 auto isVersionFunctional = versionPtr->isFunctional();
Michael Tritz4254bec2017-10-03 17:18:22 -0500232 if (!isVersionFunctional)
233 {
Saqib Khanee13e832017-10-23 12:53:11 -0500234 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600235 std::make_unique<phosphor::software::manager::Delete>(
236 bus, path, *versionPtr);
Michael Tritz4254bec2017-10-03 17:18:22 -0500237 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600238 versions.insert(std::make_pair(id, std::move(versionPtr)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500239
Saqib Khanee13e832017-10-23 12:53:11 -0500240 // Create Activation instance for this version.
241 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600242 id, std::make_unique<Activation>(
243 bus, path, *this, id, activationState, associations)));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500244
Lei YU269bff32018-08-21 15:21:40 +0800245 // If Active, create RedundancyPriority instance for this
246 // version.
Saqib Khan1eef62d2017-08-10 15:29:34 -0500247 if (activationState == server::Activation::Activations::Active)
248 {
249 uint8_t priority = std::numeric_limits<uint8_t>::max();
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600250 if (!restorePriority(id, priority))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500251 {
Adriana Kobylakee590c72017-09-26 15:16:06 -0500252 if (isVersionFunctional)
253 {
254 priority = 0;
255 }
256 else
257 {
258 log<level::ERR>("Unable to restore priority from file.",
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600259 entry("VERSIONID=%s", id.c_str()));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500260 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500261 }
262 activations.find(id)->second->redundancyPriority =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600263 std::make_unique<RedundancyPriority>(
264 bus, path, *(activations.find(id)->second), priority,
265 false);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500266 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500267 }
268 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500269
270 // If there is no ubi volume for bmc version then read the /etc/os-release
271 // and create rofs-<versionId> under /media
272 if (activations.size() == 0)
273 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500274 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Saqib Khandcbfa042017-09-18 13:08:39 -0500275 auto id = phosphor::software::manager::Version::getId(version);
276 auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/";
277 try
278 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500279 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500280 {
281 fs::create_directories(versionFileDir);
282 }
283 auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE;
284 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
285 ItemUpdater::processBMCImage();
286 }
287 catch (const std::exception& e)
288 {
289 log<level::ERR>(e.what());
290 }
291 }
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600292
293 mirrorUbootToAlt();
Saqib Khanba239882017-05-26 08:41:54 -0500294 return;
295}
296
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500297void ItemUpdater::erase(std::string entryId)
298{
Eddie James6d873712017-09-01 11:29:07 -0500299 // Find entry in versions map
300 auto it = versions.find(entryId);
301 if (it != versions.end())
302 {
Lei YU0f88b5a2018-08-21 15:28:53 +0800303 if (it->second->isFunctional() && ACTIVE_BMC_MAX_ALLOWED > 1)
Eddie James6d873712017-09-01 11:29:07 -0500304 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600305 log<level::ERR>("Error: Version is currently running on the BMC. "
306 "Unable to remove.",
307 entry("VERSIONID=%s", entryId.c_str()));
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500308 return;
Eddie James6d873712017-09-01 11:29:07 -0500309 }
310
311 // Delete ReadOnly partitions if it's not active
312 removeReadOnlyPartition(entryId);
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600313 removePersistDataDirectory(entryId);
Saqib Khanee13e832017-10-23 12:53:11 -0500314
315 // Removing entry in versions map
316 this->versions.erase(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500317 }
318 else
319 {
320 // Delete ReadOnly partitions even if we can't find the version
321 removeReadOnlyPartition(entryId);
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600322 removePersistDataDirectory(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500323
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600324 log<level::ERR>("Error: Failed to find version in item updater "
325 "versions map. Unable to remove.",
326 entry("VERSIONID=%s", entryId.c_str()));
Eddie James6d873712017-09-01 11:29:07 -0500327 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500328
Lei YU56aaf452018-06-21 16:09:44 +0800329 helper.clearEntry(entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500330
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500331 // Removing entry in activations map
332 auto ita = activations.find(entryId);
333 if (ita == activations.end())
334 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600335 log<level::ERR>("Error: Failed to find version in item updater "
336 "activations map. Unable to remove.",
337 entry("VERSIONID=%s", entryId.c_str()));
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500338 }
Saqib Khanee13e832017-10-23 12:53:11 -0500339 else
340 {
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600341 removeAssociations(ita->second->path);
Saqib Khanee13e832017-10-23 12:53:11 -0500342 this->activations.erase(entryId);
343 }
Saqib Khan49446ae2017-10-02 10:54:20 -0500344 ItemUpdater::resetUbootEnvVars();
Saqib Khanee13e832017-10-23 12:53:11 -0500345 return;
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500346}
347
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500348void ItemUpdater::deleteAll()
349{
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600350 std::vector<std::string> deletableVersions;
351
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500352 for (const auto& versionIt : versions)
353 {
354 if (!versionIt.second->isFunctional())
355 {
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600356 deletableVersions.push_back(versionIt.first);
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500357 }
358 }
359
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600360 for (const auto& deletableIt : deletableVersions)
361 {
362 ItemUpdater::erase(deletableIt);
363 }
364
Lei YU56aaf452018-06-21 16:09:44 +0800365 helper.cleanup();
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500366}
367
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600368ItemUpdater::ActivationStatus
369 ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500370{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500371 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500372
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500373 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500374 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500375 fs::path file(filePath);
376 file /= bmcImage;
377 std::ifstream efile(file.c_str());
378 if (efile.good() != 1)
379 {
380 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500381 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500382 invalid = true;
383 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500384 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500385
386 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500387 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500388 return ItemUpdater::ActivationStatus::invalid;
389 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500390
391 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500392}
393
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500394void ItemUpdater::savePriority(const std::string& versionId, uint8_t value)
395{
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600396 storePriority(versionId, value);
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500397 helper.setEntry(versionId, value);
398}
399
Saqib Khanb9da6632017-09-13 09:48:37 -0500400void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500401{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500402 std::map<std::string, uint8_t> priorityMap;
403
404 // Insert the requested version and priority, it may not exist yet.
405 priorityMap.insert(std::make_pair(versionId, value));
406
Saqib Khan4c1aec02017-07-06 11:46:13 -0500407 for (const auto& intf : activations)
408 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500409 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500410 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500411 priorityMap.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600412 intf.first, intf.second->redundancyPriority.get()->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500413 }
414 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500415
416 // Lambda function to compare 2 priority values, use <= to allow duplicates
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600417 typedef std::function<bool(std::pair<std::string, uint8_t>,
418 std::pair<std::string, uint8_t>)>
419 cmpPriority;
420 cmpPriority cmpPriorityFunc =
421 [](std::pair<std::string, uint8_t> priority1,
422 std::pair<std::string, uint8_t> priority2) {
423 return priority1.second <= priority2.second;
424 };
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500425
426 // Sort versions by ascending priority
427 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600428 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500429
430 auto freePriorityValue = value;
431 for (auto& element : prioritySet)
432 {
433 if (element.first == versionId)
434 {
435 continue;
436 }
437 if (element.second == freePriorityValue)
438 {
439 ++freePriorityValue;
440 auto it = activations.find(element.first);
441 it->second->redundancyPriority.get()->sdbusPriority(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600442 freePriorityValue);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500443 }
444 }
445
446 auto lowestVersion = prioritySet.begin()->first;
447 if (value == prioritySet.begin()->second)
448 {
449 lowestVersion = versionId;
450 }
451 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500452}
453
Michael Tritz37a59042017-07-12 13:44:53 -0500454void ItemUpdater::reset()
455{
Gunnar Mills60f5ccf2020-05-28 12:26:39 -0500456 constexpr auto setFactoryResetWait = std::chrono::seconds(3);
Lei YU56aaf452018-06-21 16:09:44 +0800457 helper.factoryReset();
Michael Tritz37a59042017-07-12 13:44:53 -0500458
Gunnar Mills60f5ccf2020-05-28 12:26:39 -0500459 // Need to wait for env variables to complete, otherwise an immediate reboot
460 // will not factory reset.
461 std::this_thread::sleep_for(setFactoryResetWait);
462
Michael Tritz37a59042017-07-12 13:44:53 -0500463 log<level::INFO>("BMC factory reset will take effect upon reboot.");
Michael Tritz37a59042017-07-12 13:44:53 -0500464}
465
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500466void ItemUpdater::removeReadOnlyPartition(std::string versionId)
467{
Lei YU56aaf452018-06-21 16:09:44 +0800468 helper.removeVersion(versionId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500469}
470
Michael Tritz0129d922017-08-10 19:33:46 -0500471bool ItemUpdater::fieldModeEnabled(bool value)
472{
473 // enabling field mode is intended to be one way: false -> true
474 if (value && !control::FieldMode::fieldModeEnabled())
475 {
476 control::FieldMode::fieldModeEnabled(value);
477
Adriana Kobylak22848ec2019-10-28 10:08:39 -0500478 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
479 SYSTEMD_INTERFACE, "StartUnit");
480 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
481 "replace");
482 bus.call_noreply(method);
483
484 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
485 SYSTEMD_INTERFACE, "StopUnit");
486 method.append("usr-local.mount", "replace");
487 bus.call_noreply(method);
488
489 std::vector<std::string> usrLocal = {"usr-local.mount"};
490
491 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
492 SYSTEMD_INTERFACE, "MaskUnitFiles");
493 method.append(usrLocal, false, true);
494 bus.call_noreply(method);
Michael Tritz0129d922017-08-10 19:33:46 -0500495 }
Adriana Kobylakd5b8f752019-05-01 11:52:35 -0500496 else if (!value && control::FieldMode::fieldModeEnabled())
497 {
498 elog<NotAllowed>(xyz::openbmc_project::Common::NotAllowed::REASON(
499 "FieldMode is not allowed to be cleared"));
500 }
Michael Tritz0129d922017-08-10 19:33:46 -0500501
502 return control::FieldMode::fieldModeEnabled();
503}
504
505void ItemUpdater::restoreFieldModeStatus()
506{
Michael Tritzff0b4212017-10-24 17:38:09 -0500507 std::ifstream input("/dev/mtd/u-boot-env");
Michael Tritz0129d922017-08-10 19:33:46 -0500508 std::string envVar;
509 std::getline(input, envVar);
510
Gunnar Mills9a782242017-08-22 16:23:15 -0500511 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500512 {
513 ItemUpdater::fieldModeEnabled(true);
514 }
515}
516
Gunnar Millsb60add12017-08-24 16:41:42 -0500517void ItemUpdater::setBMCInventoryPath()
518{
Gunnar Millsb60add12017-08-24 16:41:42 -0500519 auto depth = 0;
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600520 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
521 MAPPER_INTERFACE, "GetSubTreePaths");
Gunnar Millsb60add12017-08-24 16:41:42 -0500522
Adriana Kobylak1254c622017-12-07 12:24:56 -0600523 mapperCall.append(INVENTORY_PATH);
Gunnar Millsb60add12017-08-24 16:41:42 -0500524 mapperCall.append(depth);
Adriana Kobylak1254c622017-12-07 12:24:56 -0600525 std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE};
Gunnar Millsb60add12017-08-24 16:41:42 -0500526 mapperCall.append(filter);
527
Ed Tanous87c78172018-08-10 12:51:53 -0700528 try
529 {
530 auto response = bus.call(mapperCall);
531
532 using ObjectPaths = std::vector<std::string>;
533 ObjectPaths result;
534 response.read(result);
535
536 if (!result.empty())
537 {
538 bmcInventoryPath = result.front();
539 }
540 }
541 catch (const sdbusplus::exception::SdBusError& e)
Gunnar Millsb60add12017-08-24 16:41:42 -0500542 {
543 log<level::ERR>("Error in mapper GetSubTreePath");
544 return;
545 }
546
Adriana Kobylak1254c622017-12-07 12:24:56 -0600547 return;
Gunnar Millsb60add12017-08-24 16:41:42 -0500548}
549
Gunnar Millsf10b2322017-09-21 15:31:55 -0500550void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500551{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600552 assocs.emplace_back(
553 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
Gunnar Millsded875d2017-08-28 16:44:52 -0500554 associations(assocs);
555}
556
Gunnar Mills88e8a322017-09-13 11:09:28 -0500557void ItemUpdater::createFunctionalAssociation(const std::string& path)
558{
559 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600560 FUNCTIONAL_REV_ASSOCIATION, path));
Gunnar Mills88e8a322017-09-13 11:09:28 -0500561 associations(assocs);
562}
563
AppaRao Pulibbebec72020-01-28 23:57:41 +0530564void ItemUpdater::createUpdateableAssociation(const std::string& path)
565{
566 assocs.emplace_back(std::make_tuple(UPDATEABLE_FWD_ASSOCIATION,
567 UPDATEABLE_REV_ASSOCIATION, path));
568 associations(assocs);
569}
570
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600571void ItemUpdater::removeAssociations(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500572{
573 for (auto iter = assocs.begin(); iter != assocs.end();)
574 {
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600575 if ((std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500576 {
577 iter = assocs.erase(iter);
578 associations(assocs);
579 }
580 else
581 {
582 ++iter;
583 }
584 }
585}
586
Saqib Khanb9da6632017-09-13 09:48:37 -0500587bool ItemUpdater::isLowestPriority(uint8_t value)
588{
589 for (const auto& intf : activations)
590 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500591 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500592 {
593 if (intf.second->redundancyPriority.get()->priority() < value)
594 {
595 return false;
596 }
597 }
598 }
599 return true;
600}
601
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500602void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
603{
Lei YU56aaf452018-06-21 16:09:44 +0800604 helper.updateUbootVersionId(versionId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500605}
606
Saqib Khan49446ae2017-10-02 10:54:20 -0500607void ItemUpdater::resetUbootEnvVars()
608{
609 decltype(activations.begin()->second->redundancyPriority.get()->priority())
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600610 lowestPriority = std::numeric_limits<uint8_t>::max();
Saqib Khan49446ae2017-10-02 10:54:20 -0500611 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
612 for (const auto& intf : activations)
613 {
614 if (!intf.second->redundancyPriority.get())
615 {
616 // Skip this version if the redundancyPriority is not initialized.
617 continue;
618 }
619
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600620 if (intf.second->redundancyPriority.get()->priority() <= lowestPriority)
Saqib Khan49446ae2017-10-02 10:54:20 -0500621 {
622 lowestPriority = intf.second->redundancyPriority.get()->priority();
623 lowestPriorityVersion = intf.second->versionId;
624 }
625 }
626
Saqib Khanf0382c32017-10-24 13:36:22 -0500627 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500628 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500629}
630
Adriana Kobylaka6963592018-09-07 14:13:29 -0500631void ItemUpdater::freeSpace(Activation& caller)
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600632{
633 // Versions with the highest priority in front
634 std::priority_queue<std::pair<int, std::string>,
635 std::vector<std::pair<int, std::string>>,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600636 std::less<std::pair<int, std::string>>>
637 versionsPQ;
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600638
639 std::size_t count = 0;
640 for (const auto& iter : activations)
641 {
642 if ((iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600643 server::Activation::Activations::Active) ||
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600644 (iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600645 server::Activation::Activations::Failed))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600646 {
647 count++;
648 // Don't put the functional version on the queue since we can't
649 // remove the "running" BMC version.
Lei YU0f88b5a2018-08-21 15:28:53 +0800650 // If ACTIVE_BMC_MAX_ALLOWED <= 1, there is only one active BMC,
651 // so remove functional version as well.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500652 // Don't delete the the Activation object that called this function.
653 if ((versions.find(iter.second->versionId)
654 ->second->isFunctional() &&
655 ACTIVE_BMC_MAX_ALLOWED > 1) ||
656 (iter.second->versionId == caller.versionId))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600657 {
658 continue;
659 }
Adriana Kobylaka6963592018-09-07 14:13:29 -0500660
661 // Failed activations don't have priority, assign them a large value
662 // for sorting purposes.
663 auto priority = 999;
664 if (iter.second.get()->activation() ==
665 server::Activation::Activations::Active)
666 {
667 priority = iter.second->redundancyPriority.get()->priority();
668 }
669
670 versionsPQ.push(std::make_pair(priority, iter.second->versionId));
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600671 }
672 }
673
674 // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1,
675 // remove the highest priority one(s).
676 while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty()))
677 {
678 erase(versionsPQ.top().second);
679 versionsPQ.pop();
680 count--;
681 }
682}
683
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600684void ItemUpdater::mirrorUbootToAlt()
685{
Lei YU56aaf452018-06-21 16:09:44 +0800686 helper.mirrorAlt();
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600687}
688
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500689} // namespace updater
690} // namespace software
691} // namespace phosphor