blob: d83121768c23b2855e29f8c2d7e32d6c9175ef1f [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 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 {
Adriana Kobylak716cd782020-06-08 13:27:43 -0500177 // Get the version to calculate the id
Adriana Kobylak24a8d832020-06-10 08:29:36 -0500178 fs::path releaseFile(OS_RELEASE_FILE);
179 auto osRelease = iter.path() / releaseFile.relative_path();
Saqib Khan1eef62d2017-08-10 15:29:34 -0500180 if (!fs::is_regular_file(osRelease))
181 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600182 log<level::ERR>(
183 "Failed to read osRelease",
184 entry("FILENAME=%s", osRelease.string().c_str()));
Adriana Kobylak716cd782020-06-08 13:27:43 -0500185
186 // Try to get the version id from the mount directory name and
187 // call to delete it as this version may be corrupted. Dynamic
188 // volumes created by the UBI layout for example have the id in
189 // the mount directory name. The worst that can happen is that
190 // erase() is called with an non-existent id and returns.
191 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
Saqib Khan021c3652017-09-26 12:11:02 -0500192 ItemUpdater::erase(id);
Adriana Kobylak716cd782020-06-08 13:27:43 -0500193
Saqib Khan021c3652017-09-26 12:11:02 -0500194 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500195 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500196 auto version = VersionClass::getBMCVersion(osRelease);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500197 if (version.empty())
198 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600199 log<level::ERR>(
200 "Failed to read version from osRelease",
201 entry("FILENAME=%s", osRelease.string().c_str()));
Adriana Kobylak716cd782020-06-08 13:27:43 -0500202
203 // Try to delete the version, same as above if the
204 // OS_RELEASE_FILE does not exist.
205 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
206 ItemUpdater::erase(id);
207
208 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500209 }
Saqib Khan021c3652017-09-26 12:11:02 -0500210
Adriana Kobylak716cd782020-06-08 13:27:43 -0500211 auto id = VersionClass::getId(version);
212
Adriana Kobylakf383d272020-06-16 15:17:22 -0500213 // Check if the id has already been added. This can happen if the
214 // BMC partitions / devices were manually flashed with the same
215 // image.
216 if (versions.find(id) != versions.end())
217 {
218 continue;
219 }
220
Saqib Khan1eef62d2017-08-10 15:29:34 -0500221 auto purpose = server::Version::VersionPurpose::BMC;
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600222 restorePurpose(id, purpose);
223
Saqib Khan1eef62d2017-08-10 15:29:34 -0500224 auto path = fs::path(SOFTWARE_OBJPATH) / id;
225
Lei YU269bff32018-08-21 15:21:40 +0800226 // Create functional association if this is the functional
227 // version
Gunnar Mills88e8a322017-09-13 11:09:28 -0500228 if (version.compare(functionalVersion) == 0)
229 {
230 createFunctionalAssociation(path);
231 }
232
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500233 AssociationList associations = {};
234
235 if (activationState == server::Activation::Activations::Active)
236 {
237 // Create an association to the BMC inventory item
238 associations.emplace_back(std::make_tuple(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600239 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
240 bmcInventoryPath));
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500241
242 // Create an active association since this image is active
243 createActiveAssociation(path);
244 }
245
AppaRao Pulibbebec72020-01-28 23:57:41 +0530246 // All updateable firmware components must expose the updateable
247 // association.
248 createUpdateableAssociation(path);
249
Adriana Kobylakee590c72017-09-26 15:16:06 -0500250 // Create Version instance for this version.
251 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600252 bus, path, version, purpose, "",
253 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500254 auto isVersionFunctional = versionPtr->isFunctional();
Michael Tritz4254bec2017-10-03 17:18:22 -0500255 if (!isVersionFunctional)
256 {
Saqib Khanee13e832017-10-23 12:53:11 -0500257 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600258 std::make_unique<phosphor::software::manager::Delete>(
259 bus, path, *versionPtr);
Michael Tritz4254bec2017-10-03 17:18:22 -0500260 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600261 versions.insert(std::make_pair(id, std::move(versionPtr)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500262
Saqib Khanee13e832017-10-23 12:53:11 -0500263 // Create Activation instance for this version.
264 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600265 id, std::make_unique<Activation>(
266 bus, path, *this, id, activationState, associations)));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500267
Lei YU269bff32018-08-21 15:21:40 +0800268 // If Active, create RedundancyPriority instance for this
269 // version.
Saqib Khan1eef62d2017-08-10 15:29:34 -0500270 if (activationState == server::Activation::Activations::Active)
271 {
272 uint8_t priority = std::numeric_limits<uint8_t>::max();
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600273 if (!restorePriority(id, priority))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500274 {
Adriana Kobylakee590c72017-09-26 15:16:06 -0500275 if (isVersionFunctional)
276 {
277 priority = 0;
278 }
279 else
280 {
281 log<level::ERR>("Unable to restore priority from file.",
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600282 entry("VERSIONID=%s", id.c_str()));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500283 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500284 }
285 activations.find(id)->second->redundancyPriority =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600286 std::make_unique<RedundancyPriority>(
287 bus, path, *(activations.find(id)->second), priority,
288 false);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500289 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500290 }
291 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500292
Adriana Kobylak716cd782020-06-08 13:27:43 -0500293 // If there are no bmc versions mounted under MEDIA_DIR, then read the
294 // /etc/os-release and create rofs-<versionId> under MEDIA_DIR, then call
295 // again processBMCImage() to create the D-Bus interface for it.
Saqib Khandcbfa042017-09-18 13:08:39 -0500296 if (activations.size() == 0)
297 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500298 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Saqib Khandcbfa042017-09-18 13:08:39 -0500299 auto id = phosphor::software::manager::Version::getId(version);
300 auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/";
301 try
302 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500303 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500304 {
305 fs::create_directories(versionFileDir);
306 }
307 auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE;
308 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
309 ItemUpdater::processBMCImage();
310 }
311 catch (const std::exception& e)
312 {
313 log<level::ERR>(e.what());
314 }
315 }
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600316
317 mirrorUbootToAlt();
Saqib Khanba239882017-05-26 08:41:54 -0500318 return;
319}
320
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500321void ItemUpdater::erase(std::string entryId)
322{
Eddie James6d873712017-09-01 11:29:07 -0500323 // Find entry in versions map
324 auto it = versions.find(entryId);
325 if (it != versions.end())
326 {
Lei YU0f88b5a2018-08-21 15:28:53 +0800327 if (it->second->isFunctional() && ACTIVE_BMC_MAX_ALLOWED > 1)
Eddie James6d873712017-09-01 11:29:07 -0500328 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600329 log<level::ERR>("Error: Version is currently running on the BMC. "
330 "Unable to remove.",
331 entry("VERSIONID=%s", entryId.c_str()));
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500332 return;
Eddie James6d873712017-09-01 11:29:07 -0500333 }
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500334 }
Eddie James6d873712017-09-01 11:29:07 -0500335
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500336 // First call resetUbootEnvVars() so that the BMC points to a valid image to
337 // boot from. If resetUbootEnvVars() is called after the image is actually
338 // deleted from the BMC flash, there'd be a time window where the BMC would
339 // be pointing to a non-existent image to boot from.
340 // Need to remove the entries from the activations map before that call so
341 // that resetUbootEnvVars() doesn't use the version to be deleted.
342 auto iteratorActivations = activations.find(entryId);
343 if (iteratorActivations == activations.end())
344 {
345 log<level::ERR>("Error: Failed to find version in item updater "
346 "activations map. Unable to remove.",
347 entry("VERSIONID=%s", entryId.c_str()));
348 }
349 else
350 {
351 removeAssociations(iteratorActivations->second->path);
352 this->activations.erase(entryId);
353 }
354 ItemUpdater::resetUbootEnvVars();
355
356 if (it != versions.end())
357 {
Eddie James6d873712017-09-01 11:29:07 -0500358 // Delete ReadOnly partitions if it's not active
359 removeReadOnlyPartition(entryId);
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600360 removePersistDataDirectory(entryId);
Saqib Khanee13e832017-10-23 12:53:11 -0500361
362 // Removing entry in versions map
363 this->versions.erase(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500364 }
365 else
366 {
367 // Delete ReadOnly partitions even if we can't find the version
368 removeReadOnlyPartition(entryId);
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600369 removePersistDataDirectory(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500370
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600371 log<level::ERR>("Error: Failed to find version in item updater "
372 "versions map. Unable to remove.",
373 entry("VERSIONID=%s", entryId.c_str()));
Eddie James6d873712017-09-01 11:29:07 -0500374 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500375
Lei YU56aaf452018-06-21 16:09:44 +0800376 helper.clearEntry(entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500377
Saqib Khanee13e832017-10-23 12:53:11 -0500378 return;
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500379}
380
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500381void ItemUpdater::deleteAll()
382{
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600383 std::vector<std::string> deletableVersions;
384
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500385 for (const auto& versionIt : versions)
386 {
387 if (!versionIt.second->isFunctional())
388 {
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600389 deletableVersions.push_back(versionIt.first);
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500390 }
391 }
392
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600393 for (const auto& deletableIt : deletableVersions)
394 {
395 ItemUpdater::erase(deletableIt);
396 }
397
Lei YU56aaf452018-06-21 16:09:44 +0800398 helper.cleanup();
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500399}
400
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600401ItemUpdater::ActivationStatus
402 ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500403{
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800404 bool valid = true;
Saqib Khan35e83f32017-05-22 11:37:32 -0500405
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800406 // Record the images which are being updated
407 // First check for the fullimage, then check for images with partitions
408 imageUpdateList.push_back(bmcFullImages);
409 valid = checkImage(filePath, imageUpdateList);
410 if (!valid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500411 {
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800412 imageUpdateList.clear();
413 imageUpdateList.assign(bmcImages.begin(), bmcImages.end());
414 valid = checkImage(filePath, imageUpdateList);
415 if (!valid)
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500416 {
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800417 log<level::ERR>("Failed to find the needed BMC images.");
418 return ItemUpdater::ActivationStatus::invalid;
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500419 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500420 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500421
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500422 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500423}
424
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500425void ItemUpdater::savePriority(const std::string& versionId, uint8_t value)
426{
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600427 storePriority(versionId, value);
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500428 helper.setEntry(versionId, value);
429}
430
Saqib Khanb9da6632017-09-13 09:48:37 -0500431void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500432{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500433 std::map<std::string, uint8_t> priorityMap;
434
435 // Insert the requested version and priority, it may not exist yet.
436 priorityMap.insert(std::make_pair(versionId, value));
437
Saqib Khan4c1aec02017-07-06 11:46:13 -0500438 for (const auto& intf : activations)
439 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500440 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500441 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500442 priorityMap.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600443 intf.first, intf.second->redundancyPriority.get()->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500444 }
445 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500446
447 // Lambda function to compare 2 priority values, use <= to allow duplicates
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600448 typedef std::function<bool(std::pair<std::string, uint8_t>,
449 std::pair<std::string, uint8_t>)>
450 cmpPriority;
451 cmpPriority cmpPriorityFunc =
452 [](std::pair<std::string, uint8_t> priority1,
453 std::pair<std::string, uint8_t> priority2) {
454 return priority1.second <= priority2.second;
455 };
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500456
457 // Sort versions by ascending priority
458 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600459 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500460
461 auto freePriorityValue = value;
462 for (auto& element : prioritySet)
463 {
464 if (element.first == versionId)
465 {
466 continue;
467 }
468 if (element.second == freePriorityValue)
469 {
470 ++freePriorityValue;
471 auto it = activations.find(element.first);
472 it->second->redundancyPriority.get()->sdbusPriority(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600473 freePriorityValue);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500474 }
475 }
476
477 auto lowestVersion = prioritySet.begin()->first;
478 if (value == prioritySet.begin()->second)
479 {
480 lowestVersion = versionId;
481 }
482 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500483}
484
Michael Tritz37a59042017-07-12 13:44:53 -0500485void ItemUpdater::reset()
486{
Lei YU56aaf452018-06-21 16:09:44 +0800487 helper.factoryReset();
Michael Tritz37a59042017-07-12 13:44:53 -0500488
489 log<level::INFO>("BMC factory reset will take effect upon reboot.");
Michael Tritz37a59042017-07-12 13:44:53 -0500490}
491
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500492void ItemUpdater::removeReadOnlyPartition(std::string versionId)
493{
Lei YU56aaf452018-06-21 16:09:44 +0800494 helper.removeVersion(versionId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500495}
496
Michael Tritz0129d922017-08-10 19:33:46 -0500497bool ItemUpdater::fieldModeEnabled(bool value)
498{
499 // enabling field mode is intended to be one way: false -> true
500 if (value && !control::FieldMode::fieldModeEnabled())
501 {
502 control::FieldMode::fieldModeEnabled(value);
503
Adriana Kobylak22848ec2019-10-28 10:08:39 -0500504 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
505 SYSTEMD_INTERFACE, "StartUnit");
506 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
507 "replace");
508 bus.call_noreply(method);
509
510 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
511 SYSTEMD_INTERFACE, "StopUnit");
512 method.append("usr-local.mount", "replace");
513 bus.call_noreply(method);
514
515 std::vector<std::string> usrLocal = {"usr-local.mount"};
516
517 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
518 SYSTEMD_INTERFACE, "MaskUnitFiles");
519 method.append(usrLocal, false, true);
520 bus.call_noreply(method);
Michael Tritz0129d922017-08-10 19:33:46 -0500521 }
Adriana Kobylakd5b8f752019-05-01 11:52:35 -0500522 else if (!value && control::FieldMode::fieldModeEnabled())
523 {
524 elog<NotAllowed>(xyz::openbmc_project::Common::NotAllowed::REASON(
525 "FieldMode is not allowed to be cleared"));
526 }
Michael Tritz0129d922017-08-10 19:33:46 -0500527
528 return control::FieldMode::fieldModeEnabled();
529}
530
531void ItemUpdater::restoreFieldModeStatus()
532{
Michael Tritzff0b4212017-10-24 17:38:09 -0500533 std::ifstream input("/dev/mtd/u-boot-env");
Michael Tritz0129d922017-08-10 19:33:46 -0500534 std::string envVar;
535 std::getline(input, envVar);
536
Gunnar Mills9a782242017-08-22 16:23:15 -0500537 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500538 {
539 ItemUpdater::fieldModeEnabled(true);
540 }
541}
542
Gunnar Millsb60add12017-08-24 16:41:42 -0500543void ItemUpdater::setBMCInventoryPath()
544{
Gunnar Millsb60add12017-08-24 16:41:42 -0500545 auto depth = 0;
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600546 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
547 MAPPER_INTERFACE, "GetSubTreePaths");
Gunnar Millsb60add12017-08-24 16:41:42 -0500548
Adriana Kobylak1254c622017-12-07 12:24:56 -0600549 mapperCall.append(INVENTORY_PATH);
Gunnar Millsb60add12017-08-24 16:41:42 -0500550 mapperCall.append(depth);
Adriana Kobylak1254c622017-12-07 12:24:56 -0600551 std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE};
Gunnar Millsb60add12017-08-24 16:41:42 -0500552 mapperCall.append(filter);
553
Ed Tanous87c78172018-08-10 12:51:53 -0700554 try
555 {
556 auto response = bus.call(mapperCall);
557
558 using ObjectPaths = std::vector<std::string>;
559 ObjectPaths result;
560 response.read(result);
561
562 if (!result.empty())
563 {
564 bmcInventoryPath = result.front();
565 }
566 }
567 catch (const sdbusplus::exception::SdBusError& e)
Gunnar Millsb60add12017-08-24 16:41:42 -0500568 {
569 log<level::ERR>("Error in mapper GetSubTreePath");
570 return;
571 }
572
Adriana Kobylak1254c622017-12-07 12:24:56 -0600573 return;
Gunnar Millsb60add12017-08-24 16:41:42 -0500574}
575
Gunnar Millsf10b2322017-09-21 15:31:55 -0500576void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500577{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600578 assocs.emplace_back(
579 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
Gunnar Millsded875d2017-08-28 16:44:52 -0500580 associations(assocs);
581}
582
Gunnar Mills88e8a322017-09-13 11:09:28 -0500583void ItemUpdater::createFunctionalAssociation(const std::string& path)
584{
585 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600586 FUNCTIONAL_REV_ASSOCIATION, path));
Gunnar Mills88e8a322017-09-13 11:09:28 -0500587 associations(assocs);
588}
589
AppaRao Pulibbebec72020-01-28 23:57:41 +0530590void ItemUpdater::createUpdateableAssociation(const std::string& path)
591{
592 assocs.emplace_back(std::make_tuple(UPDATEABLE_FWD_ASSOCIATION,
593 UPDATEABLE_REV_ASSOCIATION, path));
594 associations(assocs);
595}
596
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600597void ItemUpdater::removeAssociations(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500598{
599 for (auto iter = assocs.begin(); iter != assocs.end();)
600 {
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600601 if ((std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500602 {
603 iter = assocs.erase(iter);
604 associations(assocs);
605 }
606 else
607 {
608 ++iter;
609 }
610 }
611}
612
Saqib Khanb9da6632017-09-13 09:48:37 -0500613bool ItemUpdater::isLowestPriority(uint8_t value)
614{
615 for (const auto& intf : activations)
616 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500617 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500618 {
619 if (intf.second->redundancyPriority.get()->priority() < value)
620 {
621 return false;
622 }
623 }
624 }
625 return true;
626}
627
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500628void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
629{
Lei YU56aaf452018-06-21 16:09:44 +0800630 helper.updateUbootVersionId(versionId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500631}
632
Saqib Khan49446ae2017-10-02 10:54:20 -0500633void ItemUpdater::resetUbootEnvVars()
634{
635 decltype(activations.begin()->second->redundancyPriority.get()->priority())
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600636 lowestPriority = std::numeric_limits<uint8_t>::max();
Saqib Khan49446ae2017-10-02 10:54:20 -0500637 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
638 for (const auto& intf : activations)
639 {
640 if (!intf.second->redundancyPriority.get())
641 {
642 // Skip this version if the redundancyPriority is not initialized.
643 continue;
644 }
645
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600646 if (intf.second->redundancyPriority.get()->priority() <= lowestPriority)
Saqib Khan49446ae2017-10-02 10:54:20 -0500647 {
648 lowestPriority = intf.second->redundancyPriority.get()->priority();
649 lowestPriorityVersion = intf.second->versionId;
650 }
651 }
652
Saqib Khanf0382c32017-10-24 13:36:22 -0500653 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500654 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500655}
656
Adriana Kobylaka6963592018-09-07 14:13:29 -0500657void ItemUpdater::freeSpace(Activation& caller)
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600658{
659 // Versions with the highest priority in front
660 std::priority_queue<std::pair<int, std::string>,
661 std::vector<std::pair<int, std::string>>,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600662 std::less<std::pair<int, std::string>>>
663 versionsPQ;
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600664
665 std::size_t count = 0;
666 for (const auto& iter : activations)
667 {
668 if ((iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600669 server::Activation::Activations::Active) ||
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600670 (iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600671 server::Activation::Activations::Failed))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600672 {
673 count++;
674 // Don't put the functional version on the queue since we can't
675 // remove the "running" BMC version.
Lei YU0f88b5a2018-08-21 15:28:53 +0800676 // If ACTIVE_BMC_MAX_ALLOWED <= 1, there is only one active BMC,
677 // so remove functional version as well.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500678 // Don't delete the the Activation object that called this function.
679 if ((versions.find(iter.second->versionId)
680 ->second->isFunctional() &&
681 ACTIVE_BMC_MAX_ALLOWED > 1) ||
682 (iter.second->versionId == caller.versionId))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600683 {
684 continue;
685 }
Adriana Kobylaka6963592018-09-07 14:13:29 -0500686
687 // Failed activations don't have priority, assign them a large value
688 // for sorting purposes.
689 auto priority = 999;
690 if (iter.second.get()->activation() ==
691 server::Activation::Activations::Active)
692 {
693 priority = iter.second->redundancyPriority.get()->priority();
694 }
695
696 versionsPQ.push(std::make_pair(priority, iter.second->versionId));
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600697 }
698 }
699
700 // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1,
701 // remove the highest priority one(s).
702 while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty()))
703 {
704 erase(versionsPQ.top().second);
705 versionsPQ.pop();
706 count--;
707 }
708}
709
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600710void ItemUpdater::mirrorUbootToAlt()
711{
Lei YU56aaf452018-06-21 16:09:44 +0800712 helper.mirrorAlt();
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600713}
714
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800715bool ItemUpdater::checkImage(const std::string& filePath,
716 const std::vector<std::string>& imageList)
717{
718 bool valid = true;
719
720 for (auto& bmcImage : imageList)
721 {
722 fs::path file(filePath);
723 file /= bmcImage;
724 std::ifstream efile(file.c_str());
725 if (efile.good() != 1)
726 {
727 valid = false;
728 break;
729 }
730 }
731
732 return valid;
733}
734
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500735} // namespace updater
736} // namespace software
737} // namespace phosphor