blob: 41ae150c364b906539db6e34b1ba3754144a6157 [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>
Patrick Williamsc9bb6422021-08-27 06:18:35 -050013#include <phosphor-logging/lg2.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>
George Liu44b9fef2023-02-07 14:31:32 +080022#include <system_error>
Gunnar Millsec1b41c2017-05-02 12:20:36 -050023
24namespace phosphor
25{
26namespace software
27{
28namespace updater
29{
30
Gunnar Mills2ce7da22017-05-04 15:37:56 -050031// When you see server:: you know we're referencing our base class
Patrick Williams1e9a5f12023-08-23 16:53:06 -050032namespace server = sdbusplus::server::xyz::openbmc_project::software;
33namespace control = sdbusplus::server::xyz::openbmc_project::control;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050034
Patrick Williamsc9bb6422021-08-27 06:18:35 -050035PHOSPHOR_LOG2_USING;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050036using namespace phosphor::logging;
Adriana Kobylakce82de52024-01-16 13:56:38 -060037using namespace sdbusplus::error::xyz::openbmc_project::software::image;
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060038using namespace phosphor::software::image;
Adriana Kobylakc98d9122020-05-05 10:36:01 -050039namespace fs = std::filesystem;
Adriana Kobylakce82de52024-01-16 13:56:38 -060040using NotAllowed = sdbusplus::error::xyz::openbmc_project::common::NotAllowed;
Saqib Khan35e83f32017-05-22 11:37:32 -050041
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -050042void ItemUpdater::createActivation(sdbusplus::message_t& msg)
Gunnar Millsec1b41c2017-05-02 12:20:36 -050043{
Saqib Khan84a0e692017-06-28 17:27:01 -050044 using SVersion = server::Version;
45 using VersionPurpose = SVersion::VersionPurpose;
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;
Justin Ledford054bb0b2022-03-15 15:46:58 -070051 std::map<std::string,
52 std::map<std::string,
53 std::variant<std::string, std::vector<std::string>>>>
Adriana Kobylak2285fe02018-02-27 15:36:59 -060054 interfaces;
Patrick Williamse75d10f2017-05-30 16:56:32 -050055 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050056 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050057 std::string filePath;
Justin Ledford054bb0b2022-03-15 15:46:58 -070058 std::vector<std::string> compatibleNames;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050059
60 for (const auto& intf : interfaces)
61 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050062 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050063 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050064 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050065 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050066 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050067 {
Saqib Khan84a0e692017-06-28 17:27:01 -050068 auto value = SVersion::convertVersionPurposeFromString(
Patrick Williamse883fb82020-05-13 11:38:55 -050069 std::get<std::string>(property.second));
Saqib Khan84a0e692017-06-28 17:27:01 -050070 if (value == VersionPurpose::BMC ||
Vijay Khemkae9f6c842020-01-14 14:32:39 -080071#ifdef HOST_BIOS_UPGRADE
72 value == VersionPurpose::Host ||
73#endif
Saqib Khan84a0e692017-06-28 17:27:01 -050074 value == VersionPurpose::System)
75 {
76 purpose = value;
77 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050078 }
79 else if (property.first == "Version")
80 {
Patrick Williamse883fb82020-05-13 11:38:55 -050081 version = std::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050082 }
83 }
84 }
Saqib Khan19177d32017-06-20 08:11:49 -050085 else if (intf.first == FILEPATH_IFACE)
86 {
87 for (const auto& property : intf.second)
88 {
89 if (property.first == "Path")
90 {
Patrick Williamse883fb82020-05-13 11:38:55 -050091 filePath = std::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050092 }
93 }
94 }
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +070095 else if (intf.first == EXTENDED_VERSION_IFACE)
96 {
97 for (const auto& property : intf.second)
98 {
99 if (property.first == "ExtendedVersion")
100 {
101 extendedVersion = std::get<std::string>(property.second);
102 }
103 }
104 }
Justin Ledford054bb0b2022-03-15 15:46:58 -0700105 else if (intf.first == COMPATIBLE_IFACE)
106 {
107 for (const auto& property : intf.second)
108 {
109 if (property.first == "Names")
110 {
111 compatibleNames =
112 std::get<std::vector<std::string>>(property.second);
113 }
114 }
115 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500116 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600117 if (version.empty() || filePath.empty() ||
Saqib Khan84a0e692017-06-28 17:27:01 -0500118 purpose == VersionPurpose::Unknown)
Saqib Khan705f1bf2017-06-09 23:58:38 -0500119 {
120 return;
121 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500122
123 // Version id is the last item in the path
Pavithra Barithaya27d49382024-06-24 01:52:42 -0500124 auto pos = path.rfind('/');
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500125 if (pos == std::string::npos)
126 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500127 error("No version id found in object path: {PATH}", "PATH", path);
Patrick Williamse75d10f2017-05-30 16:56:32 -0500128 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500129 }
130
131 auto versionId = path.substr(pos + 1);
132
Patrick Williamse75d10f2017-05-30 16:56:32 -0500133 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500134 {
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -0700135 verifyAndCreateObjects(versionId, path, version, purpose,
136 extendedVersion, filePath, compatibleNames);
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500137 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500138 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500139}
140
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -0700141void ItemUpdater::createActivationWithApplyTime(
142 std::string& id, std::string& path,
143 ApplyTimeIntf::RequestedApplyTimes applyTime)
144{
145 info("Creating Activation object for id: {ID}", "ID", id);
146 AssociationList associations = {};
147 // Create an association to the BMC inventory item
148 associations.emplace_back(
149 std::make_tuple(ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
150 bmcInventoryPath));
151 activations.insert(std::make_pair(
152 id, std::make_unique<Activation>(
Jagpal Singh Gill6397c092025-07-16 21:18:00 -0700153 ctx, path, *this, id, server::Activation::Activations::NotReady,
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -0700154 associations)));
155 activations[id]->applyTime = applyTime;
156}
157
158ActivationIntf::Activations ItemUpdater::verifyAndCreateObjects(
159 std::string& id, std::string& path, std::string& version,
160 VersionClass::VersionPurpose purpose, std::string& extendedVersion,
161 std ::string& filePath, std::vector<std::string>& compatibleNames)
162{
163 // Determine the Activation state by processing the given image dir.
164 auto activationState = server::Activation::Activations::Invalid;
165 ItemUpdater::ActivationStatus result;
166 if (purpose == VersionPurpose::BMC || purpose == VersionPurpose::System)
167 {
168 result = ItemUpdater::validateSquashFSImage(filePath);
169 }
170 else
171 {
172 result = ItemUpdater::ActivationStatus::ready;
173 }
174
175 AssociationList associations = {};
176
177 if (result == ItemUpdater::ActivationStatus::ready)
178 {
179 activationState = server::Activation::Activations::Ready;
180 // Create an association to the BMC inventory item
181 associations.emplace_back(
182 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
183 ACTIVATION_REV_ASSOCIATION, bmcInventoryPath));
184 }
185
186 auto versionPtr = std::make_unique<VersionClass>(
187 bus, path, version, purpose, extendedVersion, filePath, compatibleNames,
188 std::bind(&ItemUpdater::erase, this, std::placeholders::_1), id);
189 versionPtr->deleteObject =
190 std::make_unique<phosphor::software::manager::Delete>(
191 bus, path, *versionPtr);
192 versions.insert(std::make_pair(id, std::move(versionPtr)));
193
194 auto activation = activations.find(id);
195 if (activation == activations.end())
196 {
197 activations.insert(std::make_pair(
Jagpal Singh Gill6397c092025-07-16 21:18:00 -0700198 id, std::make_unique<Activation>(ctx, path, *this, id,
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -0700199 activationState, associations)));
200 }
201 else
202 {
203 activation->second->activation(activationState);
204 }
205 return activationState;
206}
207
208bool ItemUpdater::requestActivation(std::string& id)
209{
210 auto activation = activations.find(id);
211 if (activation == activations.end())
212 {
213 error("Activation object not found for id: {ID}", "ID", id);
214 return false;
215 }
216 activation->second->requestedActivation(
217 server::Activation::RequestedActivations::Active);
218 return true;
219}
220
221bool ItemUpdater::updateActivationStatus(std::string& id,
222 ActivationIntf::Activations status)
223{
224 auto activation = activations.find(id);
225 if (activation == activations.end())
226 {
227 error("Activation object not found for id: {ID}", "ID", id);
228 return false;
229 }
230 activation->second->activation(status);
231 return true;
232}
233
Jagpal Singh Gill6d131aa2024-04-07 23:56:48 -0700234void ItemUpdater::createUpdateObject(const std::string& id,
235 const std::string& path)
236{
237 if (updateManagers.find(id) != updateManagers.end())
238 {
239 error("UpdateManager object already exists for id: {ID}", "ID", id);
240 return;
241 }
242 updateManagers.insert(
243 std::make_pair(id, std::make_unique<UpdateManager>(ctx, path, *this)));
244}
245
Saqib Khanba239882017-05-26 08:41:54 -0500246void ItemUpdater::processBMCImage()
247{
Gunnar Mills88e8a322017-09-13 11:09:28 -0500248 using VersionClass = phosphor::software::manager::Version;
Lei YU269bff32018-08-21 15:21:40 +0800249
250 // Check MEDIA_DIR and create if it does not exist
251 try
252 {
253 if (!fs::is_directory(MEDIA_DIR))
254 {
255 fs::create_directory(MEDIA_DIR);
256 }
257 }
258 catch (const fs::filesystem_error& e)
259 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500260 error("Failed to prepare dir: {ERROR}", "ERROR", e);
Lei YU269bff32018-08-21 15:21:40 +0800261 return;
262 }
263
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000264 // Functional images are mounted as rofs-<location>-functional
265 constexpr auto functionalSuffix = "-functional";
Lei YUd474d9c2021-12-10 16:21:21 +0800266 bool functionalFound = false;
Gunnar Mills88e8a322017-09-13 11:09:28 -0500267
Saqib Khan1eef62d2017-08-10 15:29:34 -0500268 // Read os-release from folders under /media/ to get
269 // BMC Software Versions.
George Liu44b9fef2023-02-07 14:31:32 +0800270 std::error_code ec;
271 for (const auto& iter : fs::directory_iterator(MEDIA_DIR, ec))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500272 {
273 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500274 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500275
276 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600277 if (0 ==
278 iter.path().native().compare(0, BMC_RO_PREFIX_LEN, BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500279 {
Adriana Kobylak716cd782020-06-08 13:27:43 -0500280 // Get the version to calculate the id
Adriana Kobylak24a8d832020-06-10 08:29:36 -0500281 fs::path releaseFile(OS_RELEASE_FILE);
282 auto osRelease = iter.path() / releaseFile.relative_path();
George Liu44b9fef2023-02-07 14:31:32 +0800283 if (!fs::is_regular_file(osRelease, ec))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500284 {
Lei YUe56bf872022-02-22 18:40:37 +0800285#ifdef BMC_STATIC_DUAL_IMAGE
286 // For dual image, it is possible that the secondary image is
287 // empty or contains invalid data, ignore such case.
George Liu44b9fef2023-02-07 14:31:32 +0800288 info("Unable to find osRelease: {PATH}: {ERROR_MSG}", "PATH",
289 osRelease, "ERROR_MSG", ec.message());
Lei YUe56bf872022-02-22 18:40:37 +0800290#else
George Liu44b9fef2023-02-07 14:31:32 +0800291 error("Failed to read osRelease: {PATH}: {ERROR_MSG}", "PATH",
292 osRelease, "ERROR_MSG", ec.message());
Adriana Kobylak716cd782020-06-08 13:27:43 -0500293
294 // Try to get the version id from the mount directory name and
295 // call to delete it as this version may be corrupted. Dynamic
296 // volumes created by the UBI layout for example have the id in
297 // the mount directory name. The worst that can happen is that
298 // erase() is called with an non-existent id and returns.
299 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
Saqib Khan021c3652017-09-26 12:11:02 -0500300 ItemUpdater::erase(id);
Lei YUe56bf872022-02-22 18:40:37 +0800301#endif
Adriana Kobylak716cd782020-06-08 13:27:43 -0500302
Saqib Khan021c3652017-09-26 12:11:02 -0500303 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500304 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500305 auto version = VersionClass::getBMCVersion(osRelease);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500306 if (version.empty())
307 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500308 error("Failed to read version from osRelease: {PATH}", "PATH",
309 osRelease);
Adriana Kobylak716cd782020-06-08 13:27:43 -0500310
311 // Try to delete the version, same as above if the
312 // OS_RELEASE_FILE does not exist.
313 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
314 ItemUpdater::erase(id);
315
316 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500317 }
Saqib Khan021c3652017-09-26 12:11:02 -0500318
Adriana Kobylak59b640b2022-01-21 19:45:22 +0000319 // The flash location is part of the mount name: rofs-<location>
320 auto flashId = iter.path().native().substr(BMC_RO_PREFIX_LEN);
321
322 auto id = VersionClass::getId(version + flashId);
Adriana Kobylak716cd782020-06-08 13:27:43 -0500323
Adriana Kobylakf383d272020-06-16 15:17:22 -0500324 // Check if the id has already been added. This can happen if the
325 // BMC partitions / devices were manually flashed with the same
326 // image.
327 if (versions.find(id) != versions.end())
328 {
329 continue;
330 }
331
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000332 auto functional = false;
333 if (iter.path().native().find(functionalSuffix) !=
334 std::string::npos)
335 {
336 // Set functional to true and remove the functional suffix
337 functional = true;
338 flashId.erase(flashId.length() - strlen(functionalSuffix));
Lei YUd474d9c2021-12-10 16:21:21 +0800339 functionalFound = true;
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000340 }
Adriana Kobylak780220f2022-01-18 20:01:53 +0000341
Saqib Khan1eef62d2017-08-10 15:29:34 -0500342 auto purpose = server::Version::VersionPurpose::BMC;
Adriana Kobylak780220f2022-01-18 20:01:53 +0000343 restorePurpose(flashId, purpose);
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600344
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +0700345 // Read os-release from /etc/ to get the BMC extended version
346 std::string extendedVersion =
347 VersionClass::getBMCExtendedVersion(osRelease);
348
Saqib Khan1eef62d2017-08-10 15:29:34 -0500349 auto path = fs::path(SOFTWARE_OBJPATH) / id;
350
Adriana Kobylak7b11bba2024-06-24 10:14:49 -0500351 // Create functional association and minimum ship level instance if
352 // this is the functional version
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000353 if (functional)
Gunnar Mills88e8a322017-09-13 11:09:28 -0500354 {
355 createFunctionalAssociation(path);
Adriana Kobylak7b11bba2024-06-24 10:14:49 -0500356
357 if (minimum_ship_level::enabled())
358 {
359 minimumVersionObject =
360 std::make_unique<MinimumVersion>(bus, path);
361 minimumVersionObject->minimumVersion(
362 minimum_ship_level::getMinimumVersion());
363 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500364 }
365
Patrick Williamsfc33ba82024-08-16 15:19:54 -0400366 AssociationList associations;
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500367
368 if (activationState == server::Activation::Activations::Active)
369 {
370 // Create an association to the BMC inventory item
371 associations.emplace_back(std::make_tuple(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600372 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
373 bmcInventoryPath));
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500374
375 // Create an active association since this image is active
376 createActiveAssociation(path);
377 }
378
Adriana Kobylakee590c72017-09-26 15:16:06 -0500379 // Create Version instance for this version.
380 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylaka84f06d2022-01-18 15:41:57 +0000381 bus, path, version, purpose, extendedVersion, flashId,
Justin Ledford054bb0b2022-03-15 15:46:58 -0700382 std::vector<std::string>(),
Adriana Kobylak59b640b2022-01-21 19:45:22 +0000383 std::bind(&ItemUpdater::erase, this, std::placeholders::_1),
384 id);
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000385 if (functional)
386 {
387 versionPtr->setFunctional(true);
388 }
389 else
Michael Tritz4254bec2017-10-03 17:18:22 -0500390 {
Saqib Khanee13e832017-10-23 12:53:11 -0500391 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600392 std::make_unique<phosphor::software::manager::Delete>(
393 bus, path, *versionPtr);
Michael Tritz4254bec2017-10-03 17:18:22 -0500394 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600395 versions.insert(std::make_pair(id, std::move(versionPtr)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500396
Saqib Khanee13e832017-10-23 12:53:11 -0500397 // Create Activation instance for this version.
398 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600399 id, std::make_unique<Activation>(
Jagpal Singh Gill6397c092025-07-16 21:18:00 -0700400 ctx, path, *this, id, activationState, associations)));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500401
Lei YUbdf2d6c2021-12-15 14:05:01 +0800402#ifdef BMC_STATIC_DUAL_IMAGE
403 uint8_t priority;
404 if ((functional && (runningImageSlot == 0)) ||
405 (!functional && (runningImageSlot == 1)))
406 {
407 priority = 0;
408 }
409 else
410 {
411 priority = 1;
412 }
413 activations.find(id)->second->redundancyPriority =
414 std::make_unique<RedundancyPriority>(
415 bus, path, *(activations.find(id)->second), priority,
416 false);
417#else
Lei YU269bff32018-08-21 15:21:40 +0800418 // If Active, create RedundancyPriority instance for this
419 // version.
Saqib Khan1eef62d2017-08-10 15:29:34 -0500420 if (activationState == server::Activation::Activations::Active)
421 {
422 uint8_t priority = std::numeric_limits<uint8_t>::max();
Adriana Kobylak780220f2022-01-18 20:01:53 +0000423 if (!restorePriority(flashId, priority))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500424 {
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000425 if (functional)
Adriana Kobylakee590c72017-09-26 15:16:06 -0500426 {
427 priority = 0;
428 }
429 else
430 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500431 error(
432 "Unable to restore priority from file for {VERSIONID}",
433 "VERSIONID", id);
Adriana Kobylakee590c72017-09-26 15:16:06 -0500434 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500435 }
436 activations.find(id)->second->redundancyPriority =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600437 std::make_unique<RedundancyPriority>(
438 bus, path, *(activations.find(id)->second), priority,
439 false);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500440 }
Lei YUbdf2d6c2021-12-15 14:05:01 +0800441#endif
Saqib Khan1eef62d2017-08-10 15:29:34 -0500442 }
443 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500444
Jagpal Singh Gillc111b932024-08-12 16:07:33 -0700445 for (const auto& version : versions)
446 {
447 if ((versions.size() == 1) || (!version.second->isFunctional()))
448 {
449 // This is the only BMC version or the non-functional BMC version
450 // (in a system with more than one flash), hence create Update
451 // object and Updateable association for this version
452 if (useUpdateDBusInterface)
453 {
454 createUpdateObject(version.first, version.second->objPath);
455 }
456 createUpdateableAssociation(version.second->objPath);
457 }
458 }
459
Lei YUd474d9c2021-12-10 16:21:21 +0800460 if (!functionalFound)
Saqib Khandcbfa042017-09-18 13:08:39 -0500461 {
Lei YUd474d9c2021-12-10 16:21:21 +0800462 // If there is no functional version found, read the /etc/os-release and
463 // create rofs-<versionId>-functional under MEDIA_DIR, then call again
464 // processBMCImage() to create the D-Bus interface for it.
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500465 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Patrick Williamsfc33ba82024-08-16 15:19:54 -0400466 auto id = phosphor::software::manager::Version::getId(
467 version + functionalSuffix);
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000468 auto versionFileDir = BMC_ROFS_PREFIX + id + functionalSuffix + "/etc/";
Saqib Khandcbfa042017-09-18 13:08:39 -0500469 try
470 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500471 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500472 {
473 fs::create_directories(versionFileDir);
474 }
Patrick Williamsfc33ba82024-08-16 15:19:54 -0400475 auto versionFilePath =
476 BMC_ROFS_PREFIX + id + functionalSuffix + OS_RELEASE_FILE;
Saqib Khandcbfa042017-09-18 13:08:39 -0500477 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
478 ItemUpdater::processBMCImage();
479 }
480 catch (const std::exception& e)
481 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500482 error("Exception during processing: {ERROR}", "ERROR", e);
Saqib Khandcbfa042017-09-18 13:08:39 -0500483 }
484 }
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600485
486 mirrorUbootToAlt();
Saqib Khanba239882017-05-26 08:41:54 -0500487 return;
488}
489
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500490void ItemUpdater::erase(std::string entryId)
491{
Eddie James6d873712017-09-01 11:29:07 -0500492 // Find entry in versions map
493 auto it = versions.find(entryId);
494 if (it != versions.end())
495 {
Alexander Hansen01ba9562025-05-12 16:19:36 +0200496 // NOLINTNEXTLINE(misc-redundant-expression)
Lei YU0f88b5a2018-08-21 15:28:53 +0800497 if (it->second->isFunctional() && ACTIVE_BMC_MAX_ALLOWED > 1)
Eddie James6d873712017-09-01 11:29:07 -0500498 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500499 error(
500 "Version ({VERSIONID}) is currently running on the BMC; unable to remove.",
501 "VERSIONID", entryId);
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500502 return;
Eddie James6d873712017-09-01 11:29:07 -0500503 }
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500504 }
Eddie James6d873712017-09-01 11:29:07 -0500505
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500506 // First call resetUbootEnvVars() so that the BMC points to a valid image to
507 // boot from. If resetUbootEnvVars() is called after the image is actually
508 // deleted from the BMC flash, there'd be a time window where the BMC would
509 // be pointing to a non-existent image to boot from.
510 // Need to remove the entries from the activations map before that call so
511 // that resetUbootEnvVars() doesn't use the version to be deleted.
512 auto iteratorActivations = activations.find(entryId);
513 if (iteratorActivations == activations.end())
514 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500515 error(
516 "Failed to find version ({VERSIONID}) in item updater activations map; unable to remove.",
517 "VERSIONID", entryId);
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500518 }
519 else
520 {
521 removeAssociations(iteratorActivations->second->path);
Zami Seckae06d762021-08-13 20:11:15 -0500522 iteratorActivations->second->deleteImageManagerObject();
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500523 this->activations.erase(entryId);
524 }
525 ItemUpdater::resetUbootEnvVars();
526
527 if (it != versions.end())
528 {
Adriana Kobylak780220f2022-01-18 20:01:53 +0000529 auto flashId = it->second->path();
530
Adriana Kobylak25773a72022-01-21 15:24:48 +0000531 // Delete version data if it has been installed on flash (path is not
532 // the upload directory)
533 if (flashId.find(IMG_UPLOAD_DIR) == std::string::npos)
534 {
535 removeReadOnlyPartition(entryId);
536 removePersistDataDirectory(flashId);
537 helper.clearEntry(flashId);
538 }
Chau Ly5f1346a2025-09-05 02:51:09 +0000539 else if (useUpdateDBusInterface)
540 {
541 fs::path imageDirPath = flashId;
542 if (fs::exists(imageDirPath))
543 {
544 info("Erasing image temporary folder {ID}", "ID", flashId);
545 fs::remove_all(imageDirPath);
546 }
547 }
Saqib Khanee13e832017-10-23 12:53:11 -0500548
549 // Removing entry in versions map
550 this->versions.erase(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500551 }
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500552
Jagpal Singh Gill6d131aa2024-04-07 23:56:48 -0700553 // Removing entry in updateManagers map
554 auto updateManagerIt = updateManagers.find(entryId);
555 if (updateManagerIt != updateManagers.end())
556 {
557 updateManagers.erase(entryId);
558 }
559
Saqib Khanee13e832017-10-23 12:53:11 -0500560 return;
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500561}
562
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500563void ItemUpdater::deleteAll()
564{
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600565 std::vector<std::string> deletableVersions;
566
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500567 for (const auto& versionIt : versions)
568 {
569 if (!versionIt.second->isFunctional())
570 {
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600571 deletableVersions.push_back(versionIt.first);
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500572 }
573 }
574
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600575 for (const auto& deletableIt : deletableVersions)
576 {
577 ItemUpdater::erase(deletableIt);
578 }
579
Lei YU56aaf452018-06-21 16:09:44 +0800580 helper.cleanup();
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500581}
582
Patrick Williamseae5ec92025-02-01 08:21:45 -0500583ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
584 const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500585{
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800586 bool valid = true;
Saqib Khan35e83f32017-05-22 11:37:32 -0500587
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800588 // Record the images which are being updated
589 // First check for the fullimage, then check for images with partitions
590 imageUpdateList.push_back(bmcFullImages);
591 valid = checkImage(filePath, imageUpdateList);
592 if (!valid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500593 {
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800594 imageUpdateList.clear();
595 imageUpdateList.assign(bmcImages.begin(), bmcImages.end());
596 valid = checkImage(filePath, imageUpdateList);
597 if (!valid)
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500598 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500599 error("Failed to find the needed BMC images.");
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800600 return ItemUpdater::ActivationStatus::invalid;
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500601 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500602 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500603
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500604 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500605}
606
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500607void ItemUpdater::savePriority(const std::string& versionId, uint8_t value)
608{
Adriana Kobylak780220f2022-01-18 20:01:53 +0000609 auto flashId = versions.find(versionId)->second->path();
610 storePriority(flashId, value);
Adriana Kobylak25773a72022-01-21 15:24:48 +0000611 helper.setEntry(flashId, value);
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500612}
613
Saqib Khanb9da6632017-09-13 09:48:37 -0500614void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500615{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500616 std::map<std::string, uint8_t> priorityMap;
617
618 // Insert the requested version and priority, it may not exist yet.
619 priorityMap.insert(std::make_pair(versionId, value));
620
Saqib Khan4c1aec02017-07-06 11:46:13 -0500621 for (const auto& intf : activations)
622 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500623 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500624 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500625 priorityMap.insert(std::make_pair(
Pavithra Barithaya9d7b3312024-06-26 01:58:51 -0500626 intf.first, intf.second->redundancyPriority->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500627 }
628 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500629
630 // Lambda function to compare 2 priority values, use <= to allow duplicates
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600631 typedef std::function<bool(std::pair<std::string, uint8_t>,
632 std::pair<std::string, uint8_t>)>
633 cmpPriority;
634 cmpPriority cmpPriorityFunc =
Pavithra Barithaya6d178522024-06-24 04:17:29 -0500635 [](const std::pair<std::string, uint8_t>& priority1,
636 const std::pair<std::string, uint8_t>& priority2) {
Patrick Williamsfc33ba82024-08-16 15:19:54 -0400637 return priority1.second <= priority2.second;
638 };
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500639
640 // Sort versions by ascending priority
641 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600642 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500643
644 auto freePriorityValue = value;
645 for (auto& element : prioritySet)
646 {
647 if (element.first == versionId)
648 {
649 continue;
650 }
651 if (element.second == freePriorityValue)
652 {
653 ++freePriorityValue;
654 auto it = activations.find(element.first);
Pavithra Barithaya9d7b3312024-06-26 01:58:51 -0500655 it->second->redundancyPriority->sdbusPriority(freePriorityValue);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500656 }
657 }
658
659 auto lowestVersion = prioritySet.begin()->first;
660 if (value == prioritySet.begin()->second)
661 {
662 lowestVersion = versionId;
663 }
664 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500665}
666
Michael Tritz37a59042017-07-12 13:44:53 -0500667void ItemUpdater::reset()
668{
Pavithra Barithayac5f6e7e2024-06-24 09:50:21 -0500669 phosphor::software::updater::Helper::factoryReset();
Michael Tritz37a59042017-07-12 13:44:53 -0500670
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500671 info("BMC factory reset will take effect upon reboot.");
Michael Tritz37a59042017-07-12 13:44:53 -0500672}
673
Pavithra Barithaya6d178522024-06-24 04:17:29 -0500674void ItemUpdater::removeReadOnlyPartition(const std::string& versionId)
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500675{
Adriana Kobylak25773a72022-01-21 15:24:48 +0000676 auto flashId = versions.find(versionId)->second->path();
677 helper.removeVersion(flashId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500678}
679
Michael Tritz0129d922017-08-10 19:33:46 -0500680bool ItemUpdater::fieldModeEnabled(bool value)
681{
682 // enabling field mode is intended to be one way: false -> true
683 if (value && !control::FieldMode::fieldModeEnabled())
684 {
685 control::FieldMode::fieldModeEnabled(value);
686
Adriana Kobylak22848ec2019-10-28 10:08:39 -0500687 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
688 SYSTEMD_INTERFACE, "StartUnit");
689 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
690 "replace");
691 bus.call_noreply(method);
692
693 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
694 SYSTEMD_INTERFACE, "StopUnit");
695 method.append("usr-local.mount", "replace");
696 bus.call_noreply(method);
697
698 std::vector<std::string> usrLocal = {"usr-local.mount"};
699
700 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
701 SYSTEMD_INTERFACE, "MaskUnitFiles");
702 method.append(usrLocal, false, true);
703 bus.call_noreply(method);
Michael Tritz0129d922017-08-10 19:33:46 -0500704 }
Adriana Kobylakd5b8f752019-05-01 11:52:35 -0500705 else if (!value && control::FieldMode::fieldModeEnabled())
706 {
Adriana Kobylakce82de52024-01-16 13:56:38 -0600707 elog<NotAllowed>(xyz::openbmc_project::common::NotAllowed::REASON(
Adriana Kobylakd5b8f752019-05-01 11:52:35 -0500708 "FieldMode is not allowed to be cleared"));
709 }
Michael Tritz0129d922017-08-10 19:33:46 -0500710
711 return control::FieldMode::fieldModeEnabled();
712}
713
714void ItemUpdater::restoreFieldModeStatus()
715{
Isaac Kurthdeb86b42022-03-08 19:47:53 -0600716 // The fieldmode u-boot environment variable may not exist since it is not
717 // part of the default environment, run fw_printenv with 2>&1 to ignore the
718 // error message in the journal "Error: "fieldmode" not defined"
Patrick Williamsfc33ba82024-08-16 15:19:54 -0400719 std::pair<int, std::string> ret =
720 utils::execute("/sbin/fw_printenv", "-n", "fieldmode", "2>&1");
Michael Tritz0129d922017-08-10 19:33:46 -0500721
Isaac Kurthdeb86b42022-03-08 19:47:53 -0600722 if (ret.first != 0)
723 {
724 return;
725 }
726
727 // truncate any extra characters off the end to compare against a "true" str
728 std::string result = ret.second.substr(0, 4);
729 if (result == "true")
Michael Tritz0129d922017-08-10 19:33:46 -0500730 {
731 ItemUpdater::fieldModeEnabled(true);
732 }
733}
734
Gunnar Millsb60add12017-08-24 16:41:42 -0500735void ItemUpdater::setBMCInventoryPath()
736{
Gunnar Millsb60add12017-08-24 16:41:42 -0500737 auto depth = 0;
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600738 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
739 MAPPER_INTERFACE, "GetSubTreePaths");
Gunnar Millsb60add12017-08-24 16:41:42 -0500740
Adriana Kobylak1254c622017-12-07 12:24:56 -0600741 mapperCall.append(INVENTORY_PATH);
Gunnar Millsb60add12017-08-24 16:41:42 -0500742 mapperCall.append(depth);
Adriana Kobylak1254c622017-12-07 12:24:56 -0600743 std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE};
Gunnar Millsb60add12017-08-24 16:41:42 -0500744 mapperCall.append(filter);
745
Ed Tanous87c78172018-08-10 12:51:53 -0700746 try
747 {
748 auto response = bus.call(mapperCall);
749
750 using ObjectPaths = std::vector<std::string>;
751 ObjectPaths result;
752 response.read(result);
753
754 if (!result.empty())
755 {
756 bmcInventoryPath = result.front();
757 }
758 }
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -0500759 catch (const sdbusplus::exception_t& e)
Gunnar Millsb60add12017-08-24 16:41:42 -0500760 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500761 error("Error in mapper GetSubTreePath: {ERROR}", "ERROR", e);
Gunnar Millsb60add12017-08-24 16:41:42 -0500762 return;
763 }
764
Adriana Kobylak1254c622017-12-07 12:24:56 -0600765 return;
Gunnar Millsb60add12017-08-24 16:41:42 -0500766}
767
Gunnar Millsf10b2322017-09-21 15:31:55 -0500768void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500769{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600770 assocs.emplace_back(
771 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
Gunnar Millsded875d2017-08-28 16:44:52 -0500772 associations(assocs);
773}
774
Gunnar Mills88e8a322017-09-13 11:09:28 -0500775void ItemUpdater::createFunctionalAssociation(const std::string& path)
776{
777 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600778 FUNCTIONAL_REV_ASSOCIATION, path));
Gunnar Mills88e8a322017-09-13 11:09:28 -0500779 associations(assocs);
780}
781
AppaRao Pulibbebec72020-01-28 23:57:41 +0530782void ItemUpdater::createUpdateableAssociation(const std::string& path)
783{
784 assocs.emplace_back(std::make_tuple(UPDATEABLE_FWD_ASSOCIATION,
785 UPDATEABLE_REV_ASSOCIATION, path));
786 associations(assocs);
787}
788
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600789void ItemUpdater::removeAssociations(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500790{
791 for (auto iter = assocs.begin(); iter != assocs.end();)
792 {
Pavithra Barithaya9e307b72024-06-26 01:27:38 -0500793 if (std::get<2>(*iter) == path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500794 {
795 iter = assocs.erase(iter);
796 associations(assocs);
797 }
798 else
799 {
800 ++iter;
801 }
802 }
803}
804
Saqib Khanb9da6632017-09-13 09:48:37 -0500805bool ItemUpdater::isLowestPriority(uint8_t value)
806{
807 for (const auto& intf : activations)
808 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500809 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500810 {
Pavithra Barithaya9d7b3312024-06-26 01:58:51 -0500811 if (intf.second->redundancyPriority->priority() < value)
Saqib Khanb9da6632017-09-13 09:48:37 -0500812 {
813 return false;
814 }
815 }
816 }
817 return true;
818}
819
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500820void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
821{
Lei YU9c76a0a2022-03-14 11:37:15 +0800822 auto it = versions.find(versionId);
823 if (it == versions.end())
824 {
825 return;
826 }
827 auto flashId = it->second->path();
Adriana Kobylak25773a72022-01-21 15:24:48 +0000828 helper.updateUbootVersionId(flashId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500829}
830
Saqib Khan49446ae2017-10-02 10:54:20 -0500831void ItemUpdater::resetUbootEnvVars()
832{
Pavithra Barithaya9d7b3312024-06-26 01:58:51 -0500833 decltype(activations.begin()->second->redundancyPriority->priority())
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600834 lowestPriority = std::numeric_limits<uint8_t>::max();
Saqib Khan49446ae2017-10-02 10:54:20 -0500835 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
836 for (const auto& intf : activations)
837 {
Pavithra Barithaya9d7b3312024-06-26 01:58:51 -0500838 if (!intf.second->redundancyPriority)
Saqib Khan49446ae2017-10-02 10:54:20 -0500839 {
840 // Skip this version if the redundancyPriority is not initialized.
841 continue;
842 }
843
Pavithra Barithaya9d7b3312024-06-26 01:58:51 -0500844 if (intf.second->redundancyPriority->priority() <= lowestPriority)
Saqib Khan49446ae2017-10-02 10:54:20 -0500845 {
Pavithra Barithaya9d7b3312024-06-26 01:58:51 -0500846 lowestPriority = intf.second->redundancyPriority->priority();
Saqib Khan49446ae2017-10-02 10:54:20 -0500847 lowestPriorityVersion = intf.second->versionId;
848 }
849 }
850
Saqib Khanf0382c32017-10-24 13:36:22 -0500851 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500852 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500853}
854
Lei YUd8c9eea2021-12-16 16:08:30 +0800855void ItemUpdater::freeSpace([[maybe_unused]] const Activation& caller)
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600856{
Lei YUd8c9eea2021-12-16 16:08:30 +0800857#ifdef BMC_STATIC_DUAL_IMAGE
858 // For the golden image case, always remove the version on the primary side
859 std::string versionIDtoErase;
860 for (const auto& iter : activations)
861 {
862 if (iter.second->redundancyPriority &&
Pavithra Barithaya9d7b3312024-06-26 01:58:51 -0500863 iter.second->redundancyPriority->priority() == 0)
Lei YUd8c9eea2021-12-16 16:08:30 +0800864 {
865 versionIDtoErase = iter.second->versionId;
866 break;
867 }
868 }
869 if (!versionIDtoErase.empty())
870 {
871 erase(versionIDtoErase);
872 }
873 else
874 {
875 warning("Failed to find version to erase");
876 }
877#else
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600878 // Versions with the highest priority in front
879 std::priority_queue<std::pair<int, std::string>,
880 std::vector<std::pair<int, std::string>>,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600881 std::less<std::pair<int, std::string>>>
882 versionsPQ;
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600883
884 std::size_t count = 0;
885 for (const auto& iter : activations)
886 {
887 if ((iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600888 server::Activation::Activations::Active) ||
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600889 (iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600890 server::Activation::Activations::Failed))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600891 {
892 count++;
893 // Don't put the functional version on the queue since we can't
894 // remove the "running" BMC version.
Lei YU0f88b5a2018-08-21 15:28:53 +0800895 // If ACTIVE_BMC_MAX_ALLOWED <= 1, there is only one active BMC,
896 // so remove functional version as well.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500897 // Don't delete the the Activation object that called this function.
898 if ((versions.find(iter.second->versionId)
899 ->second->isFunctional() &&
900 ACTIVE_BMC_MAX_ALLOWED > 1) ||
901 (iter.second->versionId == caller.versionId))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600902 {
903 continue;
904 }
Adriana Kobylaka6963592018-09-07 14:13:29 -0500905
906 // Failed activations don't have priority, assign them a large value
907 // for sorting purposes.
908 auto priority = 999;
909 if (iter.second.get()->activation() ==
Lei YUcfb4b202021-05-06 17:47:28 +0800910 server::Activation::Activations::Active &&
911 iter.second->redundancyPriority)
Adriana Kobylaka6963592018-09-07 14:13:29 -0500912 {
913 priority = iter.second->redundancyPriority.get()->priority();
914 }
915
916 versionsPQ.push(std::make_pair(priority, iter.second->versionId));
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600917 }
918 }
919
920 // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1,
921 // remove the highest priority one(s).
922 while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty()))
923 {
924 erase(versionsPQ.top().second);
925 versionsPQ.pop();
926 count--;
927 }
Lei YUd8c9eea2021-12-16 16:08:30 +0800928#endif
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600929}
930
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600931void ItemUpdater::mirrorUbootToAlt()
932{
Lei YU56aaf452018-06-21 16:09:44 +0800933 helper.mirrorAlt();
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600934}
935
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800936bool ItemUpdater::checkImage(const std::string& filePath,
937 const std::vector<std::string>& imageList)
938{
939 bool valid = true;
940
941 for (auto& bmcImage : imageList)
942 {
943 fs::path file(filePath);
944 file /= bmcImage;
945 std::ifstream efile(file.c_str());
946 if (efile.good() != 1)
947 {
948 valid = false;
949 break;
950 }
951 }
952
953 return valid;
954}
955
Lei YU6e9fb1d2021-02-19 18:01:40 +0800956#ifdef HOST_BIOS_UPGRADE
957void ItemUpdater::createBIOSObject()
958{
959 std::string path = BIOS_OBJPATH;
960 // Get version id from last item in the path
Pavithra Barithaya27d49382024-06-24 01:52:42 -0500961 auto pos = path.rfind('/');
Lei YU6e9fb1d2021-02-19 18:01:40 +0800962 if (pos == std::string::npos)
963 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500964 error("No version id found in object path {PATH}", "PATH", path);
Lei YU6e9fb1d2021-02-19 18:01:40 +0800965 return;
966 }
967
968 createActiveAssociation(path);
969 createFunctionalAssociation(path);
Daniel Hsu27d734f2024-05-21 15:20:23 +0800970 createUpdateableAssociation(path);
Lei YU6e9fb1d2021-02-19 18:01:40 +0800971
972 auto versionId = path.substr(pos + 1);
973 auto version = "null";
Patrick Williamsfc33ba82024-08-16 15:19:54 -0400974 AssociationList assocs;
Lei YU6e9fb1d2021-02-19 18:01:40 +0800975 biosActivation = std::make_unique<Activation>(
Jagpal Singh Gill6397c092025-07-16 21:18:00 -0700976 ctx, path, *this, versionId, server::Activation::Activations::Active,
Lei YU6e9fb1d2021-02-19 18:01:40 +0800977 assocs);
Pavithra Barithaya6d178522024-06-24 04:17:29 -0500978 auto dummyErase = [](const std::string& /*entryId*/) {
Lei YU6e9fb1d2021-02-19 18:01:40 +0800979 // Do nothing;
980 };
981 biosVersion = std::make_unique<VersionClass>(
982 bus, path, version, VersionPurpose::Host, "", "",
Justin Ledford054bb0b2022-03-15 15:46:58 -0700983 std::vector<std::string>(),
Adriana Kobylak59b640b2022-01-21 19:45:22 +0000984 std::bind(dummyErase, std::placeholders::_1), "");
Lei YU6e9fb1d2021-02-19 18:01:40 +0800985 biosVersion->deleteObject =
Patrick Williamsfc33ba82024-08-16 15:19:54 -0400986 std::make_unique<phosphor::software::manager::Delete>(
987 bus, path, *biosVersion);
Jagpal Singh Gill6d131aa2024-04-07 23:56:48 -0700988
989 if (useUpdateDBusInterface)
990 {
991 createUpdateObject(versionId, path);
992 }
Lei YU6e9fb1d2021-02-19 18:01:40 +0800993}
994#endif
995
Lei YU531fbc22021-12-10 20:03:18 +0800996void ItemUpdater::getRunningSlot()
997{
998 // Check /run/media/slot to get the slot number
999 constexpr auto slotFile = "/run/media/slot";
1000 std::fstream f(slotFile, std::ios_base::in);
1001 f >> runningImageSlot;
1002}
1003
Gunnar Millsec1b41c2017-05-02 12:20:36 -05001004} // namespace updater
1005} // namespace software
1006} // namespace phosphor