blob: e8196af9e697a0dfac182246f5de29248ac801ee [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"
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +07008#include "xyz/openbmc_project/Software/ExtendedVersion/server.hpp"
Gunnar Millsb0ce9962018-09-07 13:39:10 -05009#include "xyz/openbmc_project/Software/Version/server.hpp"
10
Adriana Kobylakd5b8f752019-05-01 11:52:35 -050011#include <phosphor-logging/elog-errors.hpp>
Gunnar Millsb0ce9962018-09-07 13:39:10 -050012#include <phosphor-logging/elog.hpp>
13#include <phosphor-logging/log.hpp>
Adriana Kobylak58aa7502020-06-08 11:12:11 -050014#include <xyz/openbmc_project/Common/error.hpp>
15#include <xyz/openbmc_project/Software/Image/error.hpp>
16
17#include <filesystem>
18#include <fstream>
Adriana Kobylak204e1e72018-01-24 16:00:05 -060019#include <queue>
Adriana Kobylakb77551c2017-10-27 12:46:23 -050020#include <set>
Gunnar Millsec1b41c2017-05-02 12:20:36 -050021#include <string>
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;
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +070049 std::string extendedVersion;
Saqib Khan705f1bf2017-06-09 23:58:38 -050050 std::string version;
Patrick Williamsbc1facd2020-06-03 05:58:27 -050051 std::map<std::string, std::map<std::string, std::variant<std::string>>>
Adriana Kobylak2285fe02018-02-27 15:36:59 -060052 interfaces;
Patrick Williamse75d10f2017-05-30 16:56:32 -050053 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050054 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050055 std::string filePath;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050056
57 for (const auto& intf : interfaces)
58 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050059 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050060 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050061 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050062 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050063 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050064 {
Saqib Khan84a0e692017-06-28 17:27:01 -050065 auto value = SVersion::convertVersionPurposeFromString(
Patrick Williamse883fb82020-05-13 11:38:55 -050066 std::get<std::string>(property.second));
Saqib Khan84a0e692017-06-28 17:27:01 -050067 if (value == VersionPurpose::BMC ||
Vijay Khemkae9f6c842020-01-14 14:32:39 -080068#ifdef HOST_BIOS_UPGRADE
69 value == VersionPurpose::Host ||
70#endif
Saqib Khan84a0e692017-06-28 17:27:01 -050071 value == VersionPurpose::System)
72 {
73 purpose = value;
74 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050075 }
76 else if (property.first == "Version")
77 {
Patrick Williamse883fb82020-05-13 11:38:55 -050078 version = std::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050079 }
80 }
81 }
Saqib Khan19177d32017-06-20 08:11:49 -050082 else if (intf.first == FILEPATH_IFACE)
83 {
84 for (const auto& property : intf.second)
85 {
86 if (property.first == "Path")
87 {
Patrick Williamse883fb82020-05-13 11:38:55 -050088 filePath = std::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050089 }
90 }
91 }
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +070092 else if (intf.first == EXTENDED_VERSION_IFACE)
93 {
94 for (const auto& property : intf.second)
95 {
96 if (property.first == "ExtendedVersion")
97 {
98 extendedVersion = std::get<std::string>(property.second);
99 }
100 }
101 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500102 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600103 if (version.empty() || filePath.empty() ||
Saqib Khan84a0e692017-06-28 17:27:01 -0500104 purpose == VersionPurpose::Unknown)
Saqib Khan705f1bf2017-06-09 23:58:38 -0500105 {
106 return;
107 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500108
109 // Version id is the last item in the path
110 auto pos = path.rfind("/");
111 if (pos == std::string::npos)
112 {
113 log<level::ERR>("No version id found in object path",
Adriana Kobylak596466b2018-02-13 14:48:53 -0600114 entry("OBJPATH=%s", path.c_str()));
Patrick Williamse75d10f2017-05-30 16:56:32 -0500115 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500116 }
117
118 auto versionId = path.substr(pos + 1);
119
Patrick Williamse75d10f2017-05-30 16:56:32 -0500120 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500121 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500122 // Determine the Activation state by processing the given image dir.
123 auto activationState = server::Activation::Activations::Invalid;
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800124 ItemUpdater::ActivationStatus result;
125 if (purpose == VersionPurpose::BMC || purpose == VersionPurpose::System)
126 result = ItemUpdater::validateSquashFSImage(filePath);
127 else
128 result = ItemUpdater::ActivationStatus::ready;
129
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500130 AssociationList associations = {};
131
Saqib Khan35e83f32017-05-22 11:37:32 -0500132 if (result == ItemUpdater::ActivationStatus::ready)
133 {
134 activationState = server::Activation::Activations::Ready;
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500135 // Create an association to the BMC inventory item
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600136 associations.emplace_back(
137 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
138 ACTIVATION_REV_ASSOCIATION, bmcInventoryPath));
Saqib Khan35e83f32017-05-22 11:37:32 -0500139 }
Gunnar Millsb60add12017-08-24 16:41:42 -0500140
Saqib Khanee13e832017-10-23 12:53:11 -0500141 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600142 versionId,
143 std::make_unique<Activation>(bus, path, *this, versionId,
144 activationState, associations)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500145
Saqib Khanee13e832017-10-23 12:53:11 -0500146 auto versionPtr = std::make_unique<VersionClass>(
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +0700147 bus, path, version, purpose, extendedVersion, filePath,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600148 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Saqib Khanee13e832017-10-23 12:53:11 -0500149 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600150 std::make_unique<phosphor::software::manager::Delete>(bus, path,
151 *versionPtr);
Saqib Khanee13e832017-10-23 12:53:11 -0500152 versions.insert(std::make_pair(versionId, std::move(versionPtr)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500153 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500154 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500155}
156
Saqib Khanba239882017-05-26 08:41:54 -0500157void ItemUpdater::processBMCImage()
158{
Gunnar Mills88e8a322017-09-13 11:09:28 -0500159 using VersionClass = phosphor::software::manager::Version;
Lei YU269bff32018-08-21 15:21:40 +0800160
161 // Check MEDIA_DIR and create if it does not exist
162 try
163 {
164 if (!fs::is_directory(MEDIA_DIR))
165 {
166 fs::create_directory(MEDIA_DIR);
167 }
168 }
169 catch (const fs::filesystem_error& e)
170 {
171 log<level::ERR>("Failed to prepare dir", entry("ERR=%s", e.what()));
172 return;
173 }
174
Gunnar Mills88e8a322017-09-13 11:09:28 -0500175 // Read os-release from /etc/ to get the functional BMC version
176 auto functionalVersion = VersionClass::getBMCVersion(OS_RELEASE_FILE);
177
Saqib Khan1eef62d2017-08-10 15:29:34 -0500178 // Read os-release from folders under /media/ to get
179 // BMC Software Versions.
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500180 for (const auto& iter : fs::directory_iterator(MEDIA_DIR))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500181 {
182 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500183 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500184
185 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600186 if (0 ==
187 iter.path().native().compare(0, BMC_RO_PREFIX_LEN, BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500188 {
Adriana Kobylak716cd782020-06-08 13:27:43 -0500189 // Get the version to calculate the id
Adriana Kobylak24a8d832020-06-10 08:29:36 -0500190 fs::path releaseFile(OS_RELEASE_FILE);
191 auto osRelease = iter.path() / releaseFile.relative_path();
Saqib Khan1eef62d2017-08-10 15:29:34 -0500192 if (!fs::is_regular_file(osRelease))
193 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600194 log<level::ERR>(
195 "Failed to read osRelease",
196 entry("FILENAME=%s", osRelease.string().c_str()));
Adriana Kobylak716cd782020-06-08 13:27:43 -0500197
198 // Try to get the version id from the mount directory name and
199 // call to delete it as this version may be corrupted. Dynamic
200 // volumes created by the UBI layout for example have the id in
201 // the mount directory name. The worst that can happen is that
202 // erase() is called with an non-existent id and returns.
203 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
Saqib Khan021c3652017-09-26 12:11:02 -0500204 ItemUpdater::erase(id);
Adriana Kobylak716cd782020-06-08 13:27:43 -0500205
Saqib Khan021c3652017-09-26 12:11:02 -0500206 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500207 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500208 auto version = VersionClass::getBMCVersion(osRelease);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500209 if (version.empty())
210 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600211 log<level::ERR>(
212 "Failed to read version from osRelease",
213 entry("FILENAME=%s", osRelease.string().c_str()));
Adriana Kobylak716cd782020-06-08 13:27:43 -0500214
215 // Try to delete the version, same as above if the
216 // OS_RELEASE_FILE does not exist.
217 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
218 ItemUpdater::erase(id);
219
220 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500221 }
Saqib Khan021c3652017-09-26 12:11:02 -0500222
Adriana Kobylak716cd782020-06-08 13:27:43 -0500223 auto id = VersionClass::getId(version);
224
Adriana Kobylakf383d272020-06-16 15:17:22 -0500225 // Check if the id has already been added. This can happen if the
226 // BMC partitions / devices were manually flashed with the same
227 // image.
228 if (versions.find(id) != versions.end())
229 {
230 continue;
231 }
232
Saqib Khan1eef62d2017-08-10 15:29:34 -0500233 auto purpose = server::Version::VersionPurpose::BMC;
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600234 restorePurpose(id, purpose);
235
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +0700236 // Read os-release from /etc/ to get the BMC extended version
237 std::string extendedVersion =
238 VersionClass::getBMCExtendedVersion(osRelease);
239
Saqib Khan1eef62d2017-08-10 15:29:34 -0500240 auto path = fs::path(SOFTWARE_OBJPATH) / id;
241
Lei YU269bff32018-08-21 15:21:40 +0800242 // Create functional association if this is the functional
243 // version
Gunnar Mills88e8a322017-09-13 11:09:28 -0500244 if (version.compare(functionalVersion) == 0)
245 {
246 createFunctionalAssociation(path);
247 }
248
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500249 AssociationList associations = {};
250
251 if (activationState == server::Activation::Activations::Active)
252 {
253 // Create an association to the BMC inventory item
254 associations.emplace_back(std::make_tuple(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600255 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
256 bmcInventoryPath));
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500257
258 // Create an active association since this image is active
259 createActiveAssociation(path);
260 }
261
AppaRao Pulibbebec72020-01-28 23:57:41 +0530262 // All updateable firmware components must expose the updateable
263 // association.
264 createUpdateableAssociation(path);
265
Adriana Kobylakee590c72017-09-26 15:16:06 -0500266 // Create Version instance for this version.
267 auto versionPtr = std::make_unique<VersionClass>(
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +0700268 bus, path, version, purpose, extendedVersion, "",
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600269 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500270 auto isVersionFunctional = versionPtr->isFunctional();
Michael Tritz4254bec2017-10-03 17:18:22 -0500271 if (!isVersionFunctional)
272 {
Saqib Khanee13e832017-10-23 12:53:11 -0500273 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600274 std::make_unique<phosphor::software::manager::Delete>(
275 bus, path, *versionPtr);
Michael Tritz4254bec2017-10-03 17:18:22 -0500276 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600277 versions.insert(std::make_pair(id, std::move(versionPtr)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500278
Saqib Khanee13e832017-10-23 12:53:11 -0500279 // Create Activation instance for this version.
280 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600281 id, std::make_unique<Activation>(
282 bus, path, *this, id, activationState, associations)));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500283
Lei YU269bff32018-08-21 15:21:40 +0800284 // If Active, create RedundancyPriority instance for this
285 // version.
Saqib Khan1eef62d2017-08-10 15:29:34 -0500286 if (activationState == server::Activation::Activations::Active)
287 {
288 uint8_t priority = std::numeric_limits<uint8_t>::max();
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600289 if (!restorePriority(id, priority))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500290 {
Adriana Kobylakee590c72017-09-26 15:16:06 -0500291 if (isVersionFunctional)
292 {
293 priority = 0;
294 }
295 else
296 {
297 log<level::ERR>("Unable to restore priority from file.",
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600298 entry("VERSIONID=%s", id.c_str()));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500299 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500300 }
301 activations.find(id)->second->redundancyPriority =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600302 std::make_unique<RedundancyPriority>(
303 bus, path, *(activations.find(id)->second), priority,
304 false);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500305 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500306 }
307 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500308
Adriana Kobylak716cd782020-06-08 13:27:43 -0500309 // If there are no bmc versions mounted under MEDIA_DIR, then read the
310 // /etc/os-release and create rofs-<versionId> under MEDIA_DIR, then call
311 // again processBMCImage() to create the D-Bus interface for it.
Saqib Khandcbfa042017-09-18 13:08:39 -0500312 if (activations.size() == 0)
313 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500314 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Saqib Khandcbfa042017-09-18 13:08:39 -0500315 auto id = phosphor::software::manager::Version::getId(version);
316 auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/";
317 try
318 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500319 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500320 {
321 fs::create_directories(versionFileDir);
322 }
323 auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE;
324 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
325 ItemUpdater::processBMCImage();
326 }
327 catch (const std::exception& e)
328 {
329 log<level::ERR>(e.what());
330 }
331 }
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600332
333 mirrorUbootToAlt();
Saqib Khanba239882017-05-26 08:41:54 -0500334 return;
335}
336
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500337void ItemUpdater::erase(std::string entryId)
338{
Eddie James6d873712017-09-01 11:29:07 -0500339 // Find entry in versions map
340 auto it = versions.find(entryId);
341 if (it != versions.end())
342 {
Lei YU0f88b5a2018-08-21 15:28:53 +0800343 if (it->second->isFunctional() && ACTIVE_BMC_MAX_ALLOWED > 1)
Eddie James6d873712017-09-01 11:29:07 -0500344 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600345 log<level::ERR>("Error: Version is currently running on the BMC. "
346 "Unable to remove.",
347 entry("VERSIONID=%s", entryId.c_str()));
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500348 return;
Eddie James6d873712017-09-01 11:29:07 -0500349 }
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500350 }
Eddie James6d873712017-09-01 11:29:07 -0500351
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500352 // First call resetUbootEnvVars() so that the BMC points to a valid image to
353 // boot from. If resetUbootEnvVars() is called after the image is actually
354 // deleted from the BMC flash, there'd be a time window where the BMC would
355 // be pointing to a non-existent image to boot from.
356 // Need to remove the entries from the activations map before that call so
357 // that resetUbootEnvVars() doesn't use the version to be deleted.
358 auto iteratorActivations = activations.find(entryId);
359 if (iteratorActivations == activations.end())
360 {
361 log<level::ERR>("Error: Failed to find version in item updater "
362 "activations map. Unable to remove.",
363 entry("VERSIONID=%s", entryId.c_str()));
364 }
365 else
366 {
367 removeAssociations(iteratorActivations->second->path);
368 this->activations.erase(entryId);
369 }
370 ItemUpdater::resetUbootEnvVars();
371
372 if (it != versions.end())
373 {
Eddie James6d873712017-09-01 11:29:07 -0500374 // Delete ReadOnly partitions if it's not active
375 removeReadOnlyPartition(entryId);
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600376 removePersistDataDirectory(entryId);
Saqib Khanee13e832017-10-23 12:53:11 -0500377
378 // Removing entry in versions map
379 this->versions.erase(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500380 }
381 else
382 {
383 // Delete ReadOnly partitions even if we can't find the version
384 removeReadOnlyPartition(entryId);
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600385 removePersistDataDirectory(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500386
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600387 log<level::ERR>("Error: Failed to find version in item updater "
388 "versions map. Unable to remove.",
389 entry("VERSIONID=%s", entryId.c_str()));
Eddie James6d873712017-09-01 11:29:07 -0500390 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500391
Lei YU56aaf452018-06-21 16:09:44 +0800392 helper.clearEntry(entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500393
Saqib Khanee13e832017-10-23 12:53:11 -0500394 return;
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500395}
396
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500397void ItemUpdater::deleteAll()
398{
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600399 std::vector<std::string> deletableVersions;
400
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500401 for (const auto& versionIt : versions)
402 {
403 if (!versionIt.second->isFunctional())
404 {
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600405 deletableVersions.push_back(versionIt.first);
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500406 }
407 }
408
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600409 for (const auto& deletableIt : deletableVersions)
410 {
411 ItemUpdater::erase(deletableIt);
412 }
413
Lei YU56aaf452018-06-21 16:09:44 +0800414 helper.cleanup();
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500415}
416
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600417ItemUpdater::ActivationStatus
418 ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500419{
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800420 bool valid = true;
Saqib Khan35e83f32017-05-22 11:37:32 -0500421
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800422 // Record the images which are being updated
423 // First check for the fullimage, then check for images with partitions
424 imageUpdateList.push_back(bmcFullImages);
425 valid = checkImage(filePath, imageUpdateList);
426 if (!valid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500427 {
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800428 imageUpdateList.clear();
429 imageUpdateList.assign(bmcImages.begin(), bmcImages.end());
430 valid = checkImage(filePath, imageUpdateList);
431 if (!valid)
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500432 {
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800433 log<level::ERR>("Failed to find the needed BMC images.");
434 return ItemUpdater::ActivationStatus::invalid;
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500435 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500436 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500437
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500438 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500439}
440
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500441void ItemUpdater::savePriority(const std::string& versionId, uint8_t value)
442{
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600443 storePriority(versionId, value);
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500444 helper.setEntry(versionId, value);
445}
446
Saqib Khanb9da6632017-09-13 09:48:37 -0500447void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500448{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500449 std::map<std::string, uint8_t> priorityMap;
450
451 // Insert the requested version and priority, it may not exist yet.
452 priorityMap.insert(std::make_pair(versionId, value));
453
Saqib Khan4c1aec02017-07-06 11:46:13 -0500454 for (const auto& intf : activations)
455 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500456 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500457 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500458 priorityMap.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600459 intf.first, intf.second->redundancyPriority.get()->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500460 }
461 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500462
463 // Lambda function to compare 2 priority values, use <= to allow duplicates
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600464 typedef std::function<bool(std::pair<std::string, uint8_t>,
465 std::pair<std::string, uint8_t>)>
466 cmpPriority;
467 cmpPriority cmpPriorityFunc =
468 [](std::pair<std::string, uint8_t> priority1,
469 std::pair<std::string, uint8_t> priority2) {
470 return priority1.second <= priority2.second;
471 };
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500472
473 // Sort versions by ascending priority
474 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600475 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500476
477 auto freePriorityValue = value;
478 for (auto& element : prioritySet)
479 {
480 if (element.first == versionId)
481 {
482 continue;
483 }
484 if (element.second == freePriorityValue)
485 {
486 ++freePriorityValue;
487 auto it = activations.find(element.first);
488 it->second->redundancyPriority.get()->sdbusPriority(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600489 freePriorityValue);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500490 }
491 }
492
493 auto lowestVersion = prioritySet.begin()->first;
494 if (value == prioritySet.begin()->second)
495 {
496 lowestVersion = versionId;
497 }
498 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500499}
500
Michael Tritz37a59042017-07-12 13:44:53 -0500501void ItemUpdater::reset()
502{
Lei YU56aaf452018-06-21 16:09:44 +0800503 helper.factoryReset();
Michael Tritz37a59042017-07-12 13:44:53 -0500504
505 log<level::INFO>("BMC factory reset will take effect upon reboot.");
Michael Tritz37a59042017-07-12 13:44:53 -0500506}
507
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500508void ItemUpdater::removeReadOnlyPartition(std::string versionId)
509{
Lei YU56aaf452018-06-21 16:09:44 +0800510 helper.removeVersion(versionId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500511}
512
Michael Tritz0129d922017-08-10 19:33:46 -0500513bool ItemUpdater::fieldModeEnabled(bool value)
514{
515 // enabling field mode is intended to be one way: false -> true
516 if (value && !control::FieldMode::fieldModeEnabled())
517 {
518 control::FieldMode::fieldModeEnabled(value);
519
Adriana Kobylak22848ec2019-10-28 10:08:39 -0500520 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
521 SYSTEMD_INTERFACE, "StartUnit");
522 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
523 "replace");
524 bus.call_noreply(method);
525
526 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
527 SYSTEMD_INTERFACE, "StopUnit");
528 method.append("usr-local.mount", "replace");
529 bus.call_noreply(method);
530
531 std::vector<std::string> usrLocal = {"usr-local.mount"};
532
533 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
534 SYSTEMD_INTERFACE, "MaskUnitFiles");
535 method.append(usrLocal, false, true);
536 bus.call_noreply(method);
Michael Tritz0129d922017-08-10 19:33:46 -0500537 }
Adriana Kobylakd5b8f752019-05-01 11:52:35 -0500538 else if (!value && control::FieldMode::fieldModeEnabled())
539 {
540 elog<NotAllowed>(xyz::openbmc_project::Common::NotAllowed::REASON(
541 "FieldMode is not allowed to be cleared"));
542 }
Michael Tritz0129d922017-08-10 19:33:46 -0500543
544 return control::FieldMode::fieldModeEnabled();
545}
546
547void ItemUpdater::restoreFieldModeStatus()
548{
Michael Tritzff0b4212017-10-24 17:38:09 -0500549 std::ifstream input("/dev/mtd/u-boot-env");
Michael Tritz0129d922017-08-10 19:33:46 -0500550 std::string envVar;
551 std::getline(input, envVar);
552
Gunnar Mills9a782242017-08-22 16:23:15 -0500553 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500554 {
555 ItemUpdater::fieldModeEnabled(true);
556 }
557}
558
Gunnar Millsb60add12017-08-24 16:41:42 -0500559void ItemUpdater::setBMCInventoryPath()
560{
Gunnar Millsb60add12017-08-24 16:41:42 -0500561 auto depth = 0;
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600562 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
563 MAPPER_INTERFACE, "GetSubTreePaths");
Gunnar Millsb60add12017-08-24 16:41:42 -0500564
Adriana Kobylak1254c622017-12-07 12:24:56 -0600565 mapperCall.append(INVENTORY_PATH);
Gunnar Millsb60add12017-08-24 16:41:42 -0500566 mapperCall.append(depth);
Adriana Kobylak1254c622017-12-07 12:24:56 -0600567 std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE};
Gunnar Millsb60add12017-08-24 16:41:42 -0500568 mapperCall.append(filter);
569
Ed Tanous87c78172018-08-10 12:51:53 -0700570 try
571 {
572 auto response = bus.call(mapperCall);
573
574 using ObjectPaths = std::vector<std::string>;
575 ObjectPaths result;
576 response.read(result);
577
578 if (!result.empty())
579 {
580 bmcInventoryPath = result.front();
581 }
582 }
583 catch (const sdbusplus::exception::SdBusError& e)
Gunnar Millsb60add12017-08-24 16:41:42 -0500584 {
585 log<level::ERR>("Error in mapper GetSubTreePath");
586 return;
587 }
588
Adriana Kobylak1254c622017-12-07 12:24:56 -0600589 return;
Gunnar Millsb60add12017-08-24 16:41:42 -0500590}
591
Gunnar Millsf10b2322017-09-21 15:31:55 -0500592void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500593{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600594 assocs.emplace_back(
595 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
Gunnar Millsded875d2017-08-28 16:44:52 -0500596 associations(assocs);
597}
598
Gunnar Mills88e8a322017-09-13 11:09:28 -0500599void ItemUpdater::createFunctionalAssociation(const std::string& path)
600{
601 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600602 FUNCTIONAL_REV_ASSOCIATION, path));
Gunnar Mills88e8a322017-09-13 11:09:28 -0500603 associations(assocs);
604}
605
AppaRao Pulibbebec72020-01-28 23:57:41 +0530606void ItemUpdater::createUpdateableAssociation(const std::string& path)
607{
608 assocs.emplace_back(std::make_tuple(UPDATEABLE_FWD_ASSOCIATION,
609 UPDATEABLE_REV_ASSOCIATION, path));
610 associations(assocs);
611}
612
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600613void ItemUpdater::removeAssociations(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500614{
615 for (auto iter = assocs.begin(); iter != assocs.end();)
616 {
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600617 if ((std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500618 {
619 iter = assocs.erase(iter);
620 associations(assocs);
621 }
622 else
623 {
624 ++iter;
625 }
626 }
627}
628
Saqib Khanb9da6632017-09-13 09:48:37 -0500629bool ItemUpdater::isLowestPriority(uint8_t value)
630{
631 for (const auto& intf : activations)
632 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500633 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500634 {
635 if (intf.second->redundancyPriority.get()->priority() < value)
636 {
637 return false;
638 }
639 }
640 }
641 return true;
642}
643
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500644void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
645{
Lei YU56aaf452018-06-21 16:09:44 +0800646 helper.updateUbootVersionId(versionId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500647}
648
Saqib Khan49446ae2017-10-02 10:54:20 -0500649void ItemUpdater::resetUbootEnvVars()
650{
651 decltype(activations.begin()->second->redundancyPriority.get()->priority())
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600652 lowestPriority = std::numeric_limits<uint8_t>::max();
Saqib Khan49446ae2017-10-02 10:54:20 -0500653 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
654 for (const auto& intf : activations)
655 {
656 if (!intf.second->redundancyPriority.get())
657 {
658 // Skip this version if the redundancyPriority is not initialized.
659 continue;
660 }
661
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600662 if (intf.second->redundancyPriority.get()->priority() <= lowestPriority)
Saqib Khan49446ae2017-10-02 10:54:20 -0500663 {
664 lowestPriority = intf.second->redundancyPriority.get()->priority();
665 lowestPriorityVersion = intf.second->versionId;
666 }
667 }
668
Saqib Khanf0382c32017-10-24 13:36:22 -0500669 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500670 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500671}
672
Adriana Kobylaka6963592018-09-07 14:13:29 -0500673void ItemUpdater::freeSpace(Activation& caller)
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600674{
675 // Versions with the highest priority in front
676 std::priority_queue<std::pair<int, std::string>,
677 std::vector<std::pair<int, std::string>>,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600678 std::less<std::pair<int, std::string>>>
679 versionsPQ;
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600680
681 std::size_t count = 0;
682 for (const auto& iter : activations)
683 {
684 if ((iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600685 server::Activation::Activations::Active) ||
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600686 (iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600687 server::Activation::Activations::Failed))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600688 {
689 count++;
690 // Don't put the functional version on the queue since we can't
691 // remove the "running" BMC version.
Lei YU0f88b5a2018-08-21 15:28:53 +0800692 // If ACTIVE_BMC_MAX_ALLOWED <= 1, there is only one active BMC,
693 // so remove functional version as well.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500694 // Don't delete the the Activation object that called this function.
695 if ((versions.find(iter.second->versionId)
696 ->second->isFunctional() &&
697 ACTIVE_BMC_MAX_ALLOWED > 1) ||
698 (iter.second->versionId == caller.versionId))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600699 {
700 continue;
701 }
Adriana Kobylaka6963592018-09-07 14:13:29 -0500702
703 // Failed activations don't have priority, assign them a large value
704 // for sorting purposes.
705 auto priority = 999;
706 if (iter.second.get()->activation() ==
707 server::Activation::Activations::Active)
708 {
709 priority = iter.second->redundancyPriority.get()->priority();
710 }
711
712 versionsPQ.push(std::make_pair(priority, iter.second->versionId));
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600713 }
714 }
715
716 // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1,
717 // remove the highest priority one(s).
718 while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty()))
719 {
720 erase(versionsPQ.top().second);
721 versionsPQ.pop();
722 count--;
723 }
724}
725
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600726void ItemUpdater::mirrorUbootToAlt()
727{
Lei YU56aaf452018-06-21 16:09:44 +0800728 helper.mirrorAlt();
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600729}
730
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800731bool ItemUpdater::checkImage(const std::string& filePath,
732 const std::vector<std::string>& imageList)
733{
734 bool valid = true;
735
736 for (auto& bmcImage : imageList)
737 {
738 fs::path file(filePath);
739 file /= bmcImage;
740 std::ifstream efile(file.c_str());
741 if (efile.good() != 1)
742 {
743 valid = false;
744 break;
745 }
746 }
747
748 return valid;
749}
750
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500751} // namespace updater
752} // namespace software
753} // namespace phosphor