blob: fd51adbb056bc39468b9fca51ef7c2d30616d492 [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;
Gunnar Mills9a782242017-08-22 16:23:15 -050046 using VersionClass = phosphor::software::manager::Version;
Saqib Khan84a0e692017-06-28 17:27:01 -050047
Patrick Williamsbc1facd2020-06-03 05:58:27 -050048 sdbusplus::message::object_path objPath;
Saqib Khan84a0e692017-06-28 17:27:01 -050049 auto purpose = VersionPurpose::Unknown;
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +070050 std::string extendedVersion;
Saqib Khan705f1bf2017-06-09 23:58:38 -050051 std::string version;
Justin Ledford054bb0b2022-03-15 15:46:58 -070052 std::map<std::string,
53 std::map<std::string,
54 std::variant<std::string, std::vector<std::string>>>>
Adriana Kobylak2285fe02018-02-27 15:36:59 -060055 interfaces;
Patrick Williamse75d10f2017-05-30 16:56:32 -050056 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050057 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050058 std::string filePath;
Justin Ledford054bb0b2022-03-15 15:46:58 -070059 std::vector<std::string> compatibleNames;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050060
61 for (const auto& intf : interfaces)
62 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050063 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050064 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050065 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050066 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050067 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050068 {
Saqib Khan84a0e692017-06-28 17:27:01 -050069 auto value = SVersion::convertVersionPurposeFromString(
Patrick Williamse883fb82020-05-13 11:38:55 -050070 std::get<std::string>(property.second));
Saqib Khan84a0e692017-06-28 17:27:01 -050071 if (value == VersionPurpose::BMC ||
Vijay Khemkae9f6c842020-01-14 14:32:39 -080072#ifdef HOST_BIOS_UPGRADE
73 value == VersionPurpose::Host ||
74#endif
Saqib Khan84a0e692017-06-28 17:27:01 -050075 value == VersionPurpose::System)
76 {
77 purpose = value;
78 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050079 }
80 else if (property.first == "Version")
81 {
Patrick Williamse883fb82020-05-13 11:38:55 -050082 version = std::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050083 }
84 }
85 }
Saqib Khan19177d32017-06-20 08:11:49 -050086 else if (intf.first == FILEPATH_IFACE)
87 {
88 for (const auto& property : intf.second)
89 {
90 if (property.first == "Path")
91 {
Patrick Williamse883fb82020-05-13 11:38:55 -050092 filePath = std::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050093 }
94 }
95 }
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +070096 else if (intf.first == EXTENDED_VERSION_IFACE)
97 {
98 for (const auto& property : intf.second)
99 {
100 if (property.first == "ExtendedVersion")
101 {
102 extendedVersion = std::get<std::string>(property.second);
103 }
104 }
105 }
Justin Ledford054bb0b2022-03-15 15:46:58 -0700106 else if (intf.first == COMPATIBLE_IFACE)
107 {
108 for (const auto& property : intf.second)
109 {
110 if (property.first == "Names")
111 {
112 compatibleNames =
113 std::get<std::vector<std::string>>(property.second);
114 }
115 }
116 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500117 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600118 if (version.empty() || filePath.empty() ||
Saqib Khan84a0e692017-06-28 17:27:01 -0500119 purpose == VersionPurpose::Unknown)
Saqib Khan705f1bf2017-06-09 23:58:38 -0500120 {
121 return;
122 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500123
124 // Version id is the last item in the path
Pavithra Barithaya27d49382024-06-24 01:52:42 -0500125 auto pos = path.rfind('/');
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500126 if (pos == std::string::npos)
127 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500128 error("No version id found in object path: {PATH}", "PATH", path);
Patrick Williamse75d10f2017-05-30 16:56:32 -0500129 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500130 }
131
132 auto versionId = path.substr(pos + 1);
133
Patrick Williamse75d10f2017-05-30 16:56:32 -0500134 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500135 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500136 // Determine the Activation state by processing the given image dir.
137 auto activationState = server::Activation::Activations::Invalid;
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800138 ItemUpdater::ActivationStatus result;
139 if (purpose == VersionPurpose::BMC || purpose == VersionPurpose::System)
Pavithra Barithayace9a5c92024-06-25 02:24:07 -0500140 {
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800141 result = ItemUpdater::validateSquashFSImage(filePath);
Pavithra Barithayace9a5c92024-06-25 02:24:07 -0500142 }
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800143 else
Pavithra Barithayace9a5c92024-06-25 02:24:07 -0500144 {
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800145 result = ItemUpdater::ActivationStatus::ready;
Pavithra Barithayace9a5c92024-06-25 02:24:07 -0500146 }
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500147 AssociationList associations = {};
148
Saqib Khan35e83f32017-05-22 11:37:32 -0500149 if (result == ItemUpdater::ActivationStatus::ready)
150 {
151 activationState = server::Activation::Activations::Ready;
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500152 // Create an association to the BMC inventory item
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600153 associations.emplace_back(
154 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
155 ACTIVATION_REV_ASSOCIATION, bmcInventoryPath));
Saqib Khan35e83f32017-05-22 11:37:32 -0500156 }
Gunnar Millsb60add12017-08-24 16:41:42 -0500157
Saqib Khanee13e832017-10-23 12:53:11 -0500158 auto versionPtr = std::make_unique<VersionClass>(
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +0700159 bus, path, version, purpose, extendedVersion, filePath,
Justin Ledford054bb0b2022-03-15 15:46:58 -0700160 compatibleNames,
Adriana Kobylak59b640b2022-01-21 19:45:22 +0000161 std::bind(&ItemUpdater::erase, this, std::placeholders::_1),
162 versionId);
Saqib Khanee13e832017-10-23 12:53:11 -0500163 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600164 std::make_unique<phosphor::software::manager::Delete>(bus, path,
165 *versionPtr);
Saqib Khanee13e832017-10-23 12:53:11 -0500166 versions.insert(std::make_pair(versionId, std::move(versionPtr)));
Adriana Kobylak88ba1f92022-03-09 21:14:15 +0000167
168 activations.insert(std::make_pair(
169 versionId,
170 std::make_unique<Activation>(bus, path, *this, versionId,
171 activationState, associations)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500172 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500173 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500174}
175
Saqib Khanba239882017-05-26 08:41:54 -0500176void ItemUpdater::processBMCImage()
177{
Gunnar Mills88e8a322017-09-13 11:09:28 -0500178 using VersionClass = phosphor::software::manager::Version;
Lei YU269bff32018-08-21 15:21:40 +0800179
180 // Check MEDIA_DIR and create if it does not exist
181 try
182 {
183 if (!fs::is_directory(MEDIA_DIR))
184 {
185 fs::create_directory(MEDIA_DIR);
186 }
187 }
188 catch (const fs::filesystem_error& e)
189 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500190 error("Failed to prepare dir: {ERROR}", "ERROR", e);
Lei YU269bff32018-08-21 15:21:40 +0800191 return;
192 }
193
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000194 // Functional images are mounted as rofs-<location>-functional
195 constexpr auto functionalSuffix = "-functional";
Lei YUd474d9c2021-12-10 16:21:21 +0800196 bool functionalFound = false;
Gunnar Mills88e8a322017-09-13 11:09:28 -0500197
Saqib Khan1eef62d2017-08-10 15:29:34 -0500198 // Read os-release from folders under /media/ to get
199 // BMC Software Versions.
George Liu44b9fef2023-02-07 14:31:32 +0800200 std::error_code ec;
201 for (const auto& iter : fs::directory_iterator(MEDIA_DIR, ec))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500202 {
203 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500204 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500205
206 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600207 if (0 ==
208 iter.path().native().compare(0, BMC_RO_PREFIX_LEN, BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500209 {
Adriana Kobylak716cd782020-06-08 13:27:43 -0500210 // Get the version to calculate the id
Adriana Kobylak24a8d832020-06-10 08:29:36 -0500211 fs::path releaseFile(OS_RELEASE_FILE);
212 auto osRelease = iter.path() / releaseFile.relative_path();
George Liu44b9fef2023-02-07 14:31:32 +0800213 if (!fs::is_regular_file(osRelease, ec))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500214 {
Lei YUe56bf872022-02-22 18:40:37 +0800215#ifdef BMC_STATIC_DUAL_IMAGE
216 // For dual image, it is possible that the secondary image is
217 // empty or contains invalid data, ignore such case.
George Liu44b9fef2023-02-07 14:31:32 +0800218 info("Unable to find osRelease: {PATH}: {ERROR_MSG}", "PATH",
219 osRelease, "ERROR_MSG", ec.message());
Lei YUe56bf872022-02-22 18:40:37 +0800220#else
George Liu44b9fef2023-02-07 14:31:32 +0800221 error("Failed to read osRelease: {PATH}: {ERROR_MSG}", "PATH",
222 osRelease, "ERROR_MSG", ec.message());
Adriana Kobylak716cd782020-06-08 13:27:43 -0500223
224 // Try to get the version id from the mount directory name and
225 // call to delete it as this version may be corrupted. Dynamic
226 // volumes created by the UBI layout for example have the id in
227 // the mount directory name. The worst that can happen is that
228 // erase() is called with an non-existent id and returns.
229 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
Saqib Khan021c3652017-09-26 12:11:02 -0500230 ItemUpdater::erase(id);
Lei YUe56bf872022-02-22 18:40:37 +0800231#endif
Adriana Kobylak716cd782020-06-08 13:27:43 -0500232
Saqib Khan021c3652017-09-26 12:11:02 -0500233 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500234 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500235 auto version = VersionClass::getBMCVersion(osRelease);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500236 if (version.empty())
237 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500238 error("Failed to read version from osRelease: {PATH}", "PATH",
239 osRelease);
Adriana Kobylak716cd782020-06-08 13:27:43 -0500240
241 // Try to delete the version, same as above if the
242 // OS_RELEASE_FILE does not exist.
243 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
244 ItemUpdater::erase(id);
245
246 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500247 }
Saqib Khan021c3652017-09-26 12:11:02 -0500248
Adriana Kobylak59b640b2022-01-21 19:45:22 +0000249 // The flash location is part of the mount name: rofs-<location>
250 auto flashId = iter.path().native().substr(BMC_RO_PREFIX_LEN);
251
252 auto id = VersionClass::getId(version + flashId);
Adriana Kobylak716cd782020-06-08 13:27:43 -0500253
Adriana Kobylakf383d272020-06-16 15:17:22 -0500254 // Check if the id has already been added. This can happen if the
255 // BMC partitions / devices were manually flashed with the same
256 // image.
257 if (versions.find(id) != versions.end())
258 {
259 continue;
260 }
261
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000262 auto functional = false;
263 if (iter.path().native().find(functionalSuffix) !=
264 std::string::npos)
265 {
266 // Set functional to true and remove the functional suffix
267 functional = true;
268 flashId.erase(flashId.length() - strlen(functionalSuffix));
Lei YUd474d9c2021-12-10 16:21:21 +0800269 functionalFound = true;
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000270 }
Adriana Kobylak780220f2022-01-18 20:01:53 +0000271
Saqib Khan1eef62d2017-08-10 15:29:34 -0500272 auto purpose = server::Version::VersionPurpose::BMC;
Adriana Kobylak780220f2022-01-18 20:01:53 +0000273 restorePurpose(flashId, purpose);
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600274
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +0700275 // Read os-release from /etc/ to get the BMC extended version
276 std::string extendedVersion =
277 VersionClass::getBMCExtendedVersion(osRelease);
278
Saqib Khan1eef62d2017-08-10 15:29:34 -0500279 auto path = fs::path(SOFTWARE_OBJPATH) / id;
280
Lei YU269bff32018-08-21 15:21:40 +0800281 // Create functional association if this is the functional
282 // version
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000283 if (functional)
Gunnar Mills88e8a322017-09-13 11:09:28 -0500284 {
285 createFunctionalAssociation(path);
286 }
287
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500288 AssociationList associations = {};
289
290 if (activationState == server::Activation::Activations::Active)
291 {
292 // Create an association to the BMC inventory item
293 associations.emplace_back(std::make_tuple(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600294 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
295 bmcInventoryPath));
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500296
297 // Create an active association since this image is active
298 createActiveAssociation(path);
299 }
300
AppaRao Pulibbebec72020-01-28 23:57:41 +0530301 // All updateable firmware components must expose the updateable
302 // association.
303 createUpdateableAssociation(path);
304
Adriana Kobylakee590c72017-09-26 15:16:06 -0500305 // Create Version instance for this version.
306 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylaka84f06d2022-01-18 15:41:57 +0000307 bus, path, version, purpose, extendedVersion, flashId,
Justin Ledford054bb0b2022-03-15 15:46:58 -0700308 std::vector<std::string>(),
Adriana Kobylak59b640b2022-01-21 19:45:22 +0000309 std::bind(&ItemUpdater::erase, this, std::placeholders::_1),
310 id);
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000311 if (functional)
312 {
313 versionPtr->setFunctional(true);
314 }
315 else
Michael Tritz4254bec2017-10-03 17:18:22 -0500316 {
Saqib Khanee13e832017-10-23 12:53:11 -0500317 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600318 std::make_unique<phosphor::software::manager::Delete>(
319 bus, path, *versionPtr);
Michael Tritz4254bec2017-10-03 17:18:22 -0500320 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600321 versions.insert(std::make_pair(id, std::move(versionPtr)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500322
Saqib Khanee13e832017-10-23 12:53:11 -0500323 // Create Activation instance for this version.
324 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600325 id, std::make_unique<Activation>(
326 bus, path, *this, id, activationState, associations)));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500327
Lei YUbdf2d6c2021-12-15 14:05:01 +0800328#ifdef BMC_STATIC_DUAL_IMAGE
329 uint8_t priority;
330 if ((functional && (runningImageSlot == 0)) ||
331 (!functional && (runningImageSlot == 1)))
332 {
333 priority = 0;
334 }
335 else
336 {
337 priority = 1;
338 }
339 activations.find(id)->second->redundancyPriority =
340 std::make_unique<RedundancyPriority>(
341 bus, path, *(activations.find(id)->second), priority,
342 false);
343#else
Lei YU269bff32018-08-21 15:21:40 +0800344 // If Active, create RedundancyPriority instance for this
345 // version.
Saqib Khan1eef62d2017-08-10 15:29:34 -0500346 if (activationState == server::Activation::Activations::Active)
347 {
348 uint8_t priority = std::numeric_limits<uint8_t>::max();
Adriana Kobylak780220f2022-01-18 20:01:53 +0000349 if (!restorePriority(flashId, priority))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500350 {
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000351 if (functional)
Adriana Kobylakee590c72017-09-26 15:16:06 -0500352 {
353 priority = 0;
354 }
355 else
356 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500357 error(
358 "Unable to restore priority from file for {VERSIONID}",
359 "VERSIONID", id);
Adriana Kobylakee590c72017-09-26 15:16:06 -0500360 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500361 }
362 activations.find(id)->second->redundancyPriority =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600363 std::make_unique<RedundancyPriority>(
364 bus, path, *(activations.find(id)->second), priority,
365 false);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500366 }
Lei YUbdf2d6c2021-12-15 14:05:01 +0800367#endif
Saqib Khan1eef62d2017-08-10 15:29:34 -0500368 }
369 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500370
Lei YUd474d9c2021-12-10 16:21:21 +0800371 if (!functionalFound)
Saqib Khandcbfa042017-09-18 13:08:39 -0500372 {
Lei YUd474d9c2021-12-10 16:21:21 +0800373 // If there is no functional version found, read the /etc/os-release and
374 // create rofs-<versionId>-functional under MEDIA_DIR, then call again
375 // processBMCImage() to create the D-Bus interface for it.
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500376 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Adriana Kobylak59b640b2022-01-21 19:45:22 +0000377 auto id = phosphor::software::manager::Version::getId(version +
378 functionalSuffix);
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000379 auto versionFileDir = BMC_ROFS_PREFIX + id + functionalSuffix + "/etc/";
Saqib Khandcbfa042017-09-18 13:08:39 -0500380 try
381 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500382 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500383 {
384 fs::create_directories(versionFileDir);
385 }
Patrick Williamsd5e8e732023-05-10 07:50:18 -0500386 auto versionFilePath = BMC_ROFS_PREFIX + id + functionalSuffix +
387 OS_RELEASE_FILE;
Saqib Khandcbfa042017-09-18 13:08:39 -0500388 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
389 ItemUpdater::processBMCImage();
390 }
391 catch (const std::exception& e)
392 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500393 error("Exception during processing: {ERROR}", "ERROR", e);
Saqib Khandcbfa042017-09-18 13:08:39 -0500394 }
395 }
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600396
397 mirrorUbootToAlt();
Saqib Khanba239882017-05-26 08:41:54 -0500398 return;
399}
400
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500401void ItemUpdater::erase(std::string entryId)
402{
Eddie James6d873712017-09-01 11:29:07 -0500403 // Find entry in versions map
404 auto it = versions.find(entryId);
405 if (it != versions.end())
406 {
Lei YU0f88b5a2018-08-21 15:28:53 +0800407 if (it->second->isFunctional() && ACTIVE_BMC_MAX_ALLOWED > 1)
Eddie James6d873712017-09-01 11:29:07 -0500408 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500409 error(
410 "Version ({VERSIONID}) is currently running on the BMC; unable to remove.",
411 "VERSIONID", entryId);
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500412 return;
Eddie James6d873712017-09-01 11:29:07 -0500413 }
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500414 }
Eddie James6d873712017-09-01 11:29:07 -0500415
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500416 // First call resetUbootEnvVars() so that the BMC points to a valid image to
417 // boot from. If resetUbootEnvVars() is called after the image is actually
418 // deleted from the BMC flash, there'd be a time window where the BMC would
419 // be pointing to a non-existent image to boot from.
420 // Need to remove the entries from the activations map before that call so
421 // that resetUbootEnvVars() doesn't use the version to be deleted.
422 auto iteratorActivations = activations.find(entryId);
423 if (iteratorActivations == activations.end())
424 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500425 error(
426 "Failed to find version ({VERSIONID}) in item updater activations map; unable to remove.",
427 "VERSIONID", entryId);
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500428 }
429 else
430 {
431 removeAssociations(iteratorActivations->second->path);
Zami Seckae06d762021-08-13 20:11:15 -0500432 iteratorActivations->second->deleteImageManagerObject();
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500433 this->activations.erase(entryId);
434 }
435 ItemUpdater::resetUbootEnvVars();
436
437 if (it != versions.end())
438 {
Adriana Kobylak780220f2022-01-18 20:01:53 +0000439 auto flashId = it->second->path();
440
Adriana Kobylak25773a72022-01-21 15:24:48 +0000441 // Delete version data if it has been installed on flash (path is not
442 // the upload directory)
443 if (flashId.find(IMG_UPLOAD_DIR) == std::string::npos)
444 {
445 removeReadOnlyPartition(entryId);
446 removePersistDataDirectory(flashId);
447 helper.clearEntry(flashId);
448 }
Saqib Khanee13e832017-10-23 12:53:11 -0500449
450 // Removing entry in versions map
451 this->versions.erase(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500452 }
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500453
Saqib Khanee13e832017-10-23 12:53:11 -0500454 return;
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500455}
456
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500457void ItemUpdater::deleteAll()
458{
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600459 std::vector<std::string> deletableVersions;
460
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500461 for (const auto& versionIt : versions)
462 {
463 if (!versionIt.second->isFunctional())
464 {
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600465 deletableVersions.push_back(versionIt.first);
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500466 }
467 }
468
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600469 for (const auto& deletableIt : deletableVersions)
470 {
471 ItemUpdater::erase(deletableIt);
472 }
473
Lei YU56aaf452018-06-21 16:09:44 +0800474 helper.cleanup();
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500475}
476
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600477ItemUpdater::ActivationStatus
478 ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500479{
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800480 bool valid = true;
Saqib Khan35e83f32017-05-22 11:37:32 -0500481
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800482 // Record the images which are being updated
483 // First check for the fullimage, then check for images with partitions
484 imageUpdateList.push_back(bmcFullImages);
485 valid = checkImage(filePath, imageUpdateList);
486 if (!valid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500487 {
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800488 imageUpdateList.clear();
489 imageUpdateList.assign(bmcImages.begin(), bmcImages.end());
490 valid = checkImage(filePath, imageUpdateList);
491 if (!valid)
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500492 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500493 error("Failed to find the needed BMC images.");
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800494 return ItemUpdater::ActivationStatus::invalid;
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500495 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500496 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500497
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500498 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500499}
500
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500501void ItemUpdater::savePriority(const std::string& versionId, uint8_t value)
502{
Adriana Kobylak780220f2022-01-18 20:01:53 +0000503 auto flashId = versions.find(versionId)->second->path();
504 storePriority(flashId, value);
Adriana Kobylak25773a72022-01-21 15:24:48 +0000505 helper.setEntry(flashId, value);
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500506}
507
Saqib Khanb9da6632017-09-13 09:48:37 -0500508void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500509{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500510 std::map<std::string, uint8_t> priorityMap;
511
512 // Insert the requested version and priority, it may not exist yet.
513 priorityMap.insert(std::make_pair(versionId, value));
514
Saqib Khan4c1aec02017-07-06 11:46:13 -0500515 for (const auto& intf : activations)
516 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500517 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500518 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500519 priorityMap.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600520 intf.first, intf.second->redundancyPriority.get()->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500521 }
522 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500523
524 // Lambda function to compare 2 priority values, use <= to allow duplicates
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600525 typedef std::function<bool(std::pair<std::string, uint8_t>,
526 std::pair<std::string, uint8_t>)>
527 cmpPriority;
528 cmpPriority cmpPriorityFunc =
Pavithra Barithaya6d178522024-06-24 04:17:29 -0500529 [](const std::pair<std::string, uint8_t>& priority1,
530 const std::pair<std::string, uint8_t>& priority2) {
Patrick Williamsd5e8e732023-05-10 07:50:18 -0500531 return priority1.second <= priority2.second;
532 };
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500533
534 // Sort versions by ascending priority
535 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600536 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500537
538 auto freePriorityValue = value;
539 for (auto& element : prioritySet)
540 {
541 if (element.first == versionId)
542 {
543 continue;
544 }
545 if (element.second == freePriorityValue)
546 {
547 ++freePriorityValue;
548 auto it = activations.find(element.first);
549 it->second->redundancyPriority.get()->sdbusPriority(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600550 freePriorityValue);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500551 }
552 }
553
554 auto lowestVersion = prioritySet.begin()->first;
555 if (value == prioritySet.begin()->second)
556 {
557 lowestVersion = versionId;
558 }
559 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500560}
561
Michael Tritz37a59042017-07-12 13:44:53 -0500562void ItemUpdater::reset()
563{
Lei YU56aaf452018-06-21 16:09:44 +0800564 helper.factoryReset();
Michael Tritz37a59042017-07-12 13:44:53 -0500565
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500566 info("BMC factory reset will take effect upon reboot.");
Michael Tritz37a59042017-07-12 13:44:53 -0500567}
568
Pavithra Barithaya6d178522024-06-24 04:17:29 -0500569void ItemUpdater::removeReadOnlyPartition(const std::string& versionId)
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500570{
Adriana Kobylak25773a72022-01-21 15:24:48 +0000571 auto flashId = versions.find(versionId)->second->path();
572 helper.removeVersion(flashId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500573}
574
Michael Tritz0129d922017-08-10 19:33:46 -0500575bool ItemUpdater::fieldModeEnabled(bool value)
576{
577 // enabling field mode is intended to be one way: false -> true
578 if (value && !control::FieldMode::fieldModeEnabled())
579 {
580 control::FieldMode::fieldModeEnabled(value);
581
Adriana Kobylak22848ec2019-10-28 10:08:39 -0500582 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
583 SYSTEMD_INTERFACE, "StartUnit");
584 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
585 "replace");
586 bus.call_noreply(method);
587
588 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
589 SYSTEMD_INTERFACE, "StopUnit");
590 method.append("usr-local.mount", "replace");
591 bus.call_noreply(method);
592
593 std::vector<std::string> usrLocal = {"usr-local.mount"};
594
595 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
596 SYSTEMD_INTERFACE, "MaskUnitFiles");
597 method.append(usrLocal, false, true);
598 bus.call_noreply(method);
Michael Tritz0129d922017-08-10 19:33:46 -0500599 }
Adriana Kobylakd5b8f752019-05-01 11:52:35 -0500600 else if (!value && control::FieldMode::fieldModeEnabled())
601 {
Adriana Kobylakce82de52024-01-16 13:56:38 -0600602 elog<NotAllowed>(xyz::openbmc_project::common::NotAllowed::REASON(
Adriana Kobylakd5b8f752019-05-01 11:52:35 -0500603 "FieldMode is not allowed to be cleared"));
604 }
Michael Tritz0129d922017-08-10 19:33:46 -0500605
606 return control::FieldMode::fieldModeEnabled();
607}
608
609void ItemUpdater::restoreFieldModeStatus()
610{
Isaac Kurthdeb86b42022-03-08 19:47:53 -0600611 // The fieldmode u-boot environment variable may not exist since it is not
612 // part of the default environment, run fw_printenv with 2>&1 to ignore the
613 // error message in the journal "Error: "fieldmode" not defined"
Patrick Williamsd5e8e732023-05-10 07:50:18 -0500614 std::pair<int, std::string> ret = utils::execute("/sbin/fw_printenv", "-n",
615 "fieldmode", "2>&1");
Michael Tritz0129d922017-08-10 19:33:46 -0500616
Isaac Kurthdeb86b42022-03-08 19:47:53 -0600617 if (ret.first != 0)
618 {
619 return;
620 }
621
622 // truncate any extra characters off the end to compare against a "true" str
623 std::string result = ret.second.substr(0, 4);
624 if (result == "true")
Michael Tritz0129d922017-08-10 19:33:46 -0500625 {
626 ItemUpdater::fieldModeEnabled(true);
627 }
628}
629
Gunnar Millsb60add12017-08-24 16:41:42 -0500630void ItemUpdater::setBMCInventoryPath()
631{
Gunnar Millsb60add12017-08-24 16:41:42 -0500632 auto depth = 0;
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600633 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
634 MAPPER_INTERFACE, "GetSubTreePaths");
Gunnar Millsb60add12017-08-24 16:41:42 -0500635
Adriana Kobylak1254c622017-12-07 12:24:56 -0600636 mapperCall.append(INVENTORY_PATH);
Gunnar Millsb60add12017-08-24 16:41:42 -0500637 mapperCall.append(depth);
Adriana Kobylak1254c622017-12-07 12:24:56 -0600638 std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE};
Gunnar Millsb60add12017-08-24 16:41:42 -0500639 mapperCall.append(filter);
640
Ed Tanous87c78172018-08-10 12:51:53 -0700641 try
642 {
643 auto response = bus.call(mapperCall);
644
645 using ObjectPaths = std::vector<std::string>;
646 ObjectPaths result;
647 response.read(result);
648
649 if (!result.empty())
650 {
651 bmcInventoryPath = result.front();
652 }
653 }
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -0500654 catch (const sdbusplus::exception_t& e)
Gunnar Millsb60add12017-08-24 16:41:42 -0500655 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500656 error("Error in mapper GetSubTreePath: {ERROR}", "ERROR", e);
Gunnar Millsb60add12017-08-24 16:41:42 -0500657 return;
658 }
659
Adriana Kobylak1254c622017-12-07 12:24:56 -0600660 return;
Gunnar Millsb60add12017-08-24 16:41:42 -0500661}
662
Gunnar Millsf10b2322017-09-21 15:31:55 -0500663void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500664{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600665 assocs.emplace_back(
666 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
Gunnar Millsded875d2017-08-28 16:44:52 -0500667 associations(assocs);
668}
669
Gunnar Mills88e8a322017-09-13 11:09:28 -0500670void ItemUpdater::createFunctionalAssociation(const std::string& path)
671{
672 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600673 FUNCTIONAL_REV_ASSOCIATION, path));
Gunnar Mills88e8a322017-09-13 11:09:28 -0500674 associations(assocs);
675}
676
AppaRao Pulibbebec72020-01-28 23:57:41 +0530677void ItemUpdater::createUpdateableAssociation(const std::string& path)
678{
679 assocs.emplace_back(std::make_tuple(UPDATEABLE_FWD_ASSOCIATION,
680 UPDATEABLE_REV_ASSOCIATION, path));
681 associations(assocs);
682}
683
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600684void ItemUpdater::removeAssociations(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500685{
686 for (auto iter = assocs.begin(); iter != assocs.end();)
687 {
Pavithra Barithaya9e307b72024-06-26 01:27:38 -0500688 if (std::get<2>(*iter) == path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500689 {
690 iter = assocs.erase(iter);
691 associations(assocs);
692 }
693 else
694 {
695 ++iter;
696 }
697 }
698}
699
Saqib Khanb9da6632017-09-13 09:48:37 -0500700bool ItemUpdater::isLowestPriority(uint8_t value)
701{
702 for (const auto& intf : activations)
703 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500704 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500705 {
706 if (intf.second->redundancyPriority.get()->priority() < value)
707 {
708 return false;
709 }
710 }
711 }
712 return true;
713}
714
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500715void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
716{
Lei YU9c76a0a2022-03-14 11:37:15 +0800717 auto it = versions.find(versionId);
718 if (it == versions.end())
719 {
720 return;
721 }
722 auto flashId = it->second->path();
Adriana Kobylak25773a72022-01-21 15:24:48 +0000723 helper.updateUbootVersionId(flashId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500724}
725
Saqib Khan49446ae2017-10-02 10:54:20 -0500726void ItemUpdater::resetUbootEnvVars()
727{
728 decltype(activations.begin()->second->redundancyPriority.get()->priority())
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600729 lowestPriority = std::numeric_limits<uint8_t>::max();
Saqib Khan49446ae2017-10-02 10:54:20 -0500730 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
731 for (const auto& intf : activations)
732 {
733 if (!intf.second->redundancyPriority.get())
734 {
735 // Skip this version if the redundancyPriority is not initialized.
736 continue;
737 }
738
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600739 if (intf.second->redundancyPriority.get()->priority() <= lowestPriority)
Saqib Khan49446ae2017-10-02 10:54:20 -0500740 {
741 lowestPriority = intf.second->redundancyPriority.get()->priority();
742 lowestPriorityVersion = intf.second->versionId;
743 }
744 }
745
Saqib Khanf0382c32017-10-24 13:36:22 -0500746 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500747 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500748}
749
Lei YUd8c9eea2021-12-16 16:08:30 +0800750void ItemUpdater::freeSpace([[maybe_unused]] const Activation& caller)
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600751{
Lei YUd8c9eea2021-12-16 16:08:30 +0800752#ifdef BMC_STATIC_DUAL_IMAGE
753 // For the golden image case, always remove the version on the primary side
754 std::string versionIDtoErase;
755 for (const auto& iter : activations)
756 {
757 if (iter.second->redundancyPriority &&
758 iter.second->redundancyPriority.get()->priority() == 0)
759 {
760 versionIDtoErase = iter.second->versionId;
761 break;
762 }
763 }
764 if (!versionIDtoErase.empty())
765 {
766 erase(versionIDtoErase);
767 }
768 else
769 {
770 warning("Failed to find version to erase");
771 }
772#else
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600773 // Versions with the highest priority in front
774 std::priority_queue<std::pair<int, std::string>,
775 std::vector<std::pair<int, std::string>>,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600776 std::less<std::pair<int, std::string>>>
777 versionsPQ;
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600778
779 std::size_t count = 0;
780 for (const auto& iter : activations)
781 {
782 if ((iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600783 server::Activation::Activations::Active) ||
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600784 (iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600785 server::Activation::Activations::Failed))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600786 {
787 count++;
788 // Don't put the functional version on the queue since we can't
789 // remove the "running" BMC version.
Lei YU0f88b5a2018-08-21 15:28:53 +0800790 // If ACTIVE_BMC_MAX_ALLOWED <= 1, there is only one active BMC,
791 // so remove functional version as well.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500792 // Don't delete the the Activation object that called this function.
793 if ((versions.find(iter.second->versionId)
794 ->second->isFunctional() &&
795 ACTIVE_BMC_MAX_ALLOWED > 1) ||
796 (iter.second->versionId == caller.versionId))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600797 {
798 continue;
799 }
Adriana Kobylaka6963592018-09-07 14:13:29 -0500800
801 // Failed activations don't have priority, assign them a large value
802 // for sorting purposes.
803 auto priority = 999;
804 if (iter.second.get()->activation() ==
Lei YUcfb4b202021-05-06 17:47:28 +0800805 server::Activation::Activations::Active &&
806 iter.second->redundancyPriority)
Adriana Kobylaka6963592018-09-07 14:13:29 -0500807 {
808 priority = iter.second->redundancyPriority.get()->priority();
809 }
810
811 versionsPQ.push(std::make_pair(priority, iter.second->versionId));
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600812 }
813 }
814
815 // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1,
816 // remove the highest priority one(s).
817 while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty()))
818 {
819 erase(versionsPQ.top().second);
820 versionsPQ.pop();
821 count--;
822 }
Lei YUd8c9eea2021-12-16 16:08:30 +0800823#endif
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600824}
825
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600826void ItemUpdater::mirrorUbootToAlt()
827{
Lei YU56aaf452018-06-21 16:09:44 +0800828 helper.mirrorAlt();
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600829}
830
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800831bool ItemUpdater::checkImage(const std::string& filePath,
832 const std::vector<std::string>& imageList)
833{
834 bool valid = true;
835
836 for (auto& bmcImage : imageList)
837 {
838 fs::path file(filePath);
839 file /= bmcImage;
840 std::ifstream efile(file.c_str());
841 if (efile.good() != 1)
842 {
843 valid = false;
844 break;
845 }
846 }
847
848 return valid;
849}
850
Lei YU6e9fb1d2021-02-19 18:01:40 +0800851#ifdef HOST_BIOS_UPGRADE
852void ItemUpdater::createBIOSObject()
853{
854 std::string path = BIOS_OBJPATH;
855 // Get version id from last item in the path
Pavithra Barithaya27d49382024-06-24 01:52:42 -0500856 auto pos = path.rfind('/');
Lei YU6e9fb1d2021-02-19 18:01:40 +0800857 if (pos == std::string::npos)
858 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500859 error("No version id found in object path {PATH}", "PATH", path);
Lei YU6e9fb1d2021-02-19 18:01:40 +0800860 return;
861 }
862
863 createActiveAssociation(path);
864 createFunctionalAssociation(path);
Daniel Hsu27d734f2024-05-21 15:20:23 +0800865 createUpdateableAssociation(path);
Lei YU6e9fb1d2021-02-19 18:01:40 +0800866
867 auto versionId = path.substr(pos + 1);
868 auto version = "null";
869 AssociationList assocs = {};
870 biosActivation = std::make_unique<Activation>(
871 bus, path, *this, versionId, server::Activation::Activations::Active,
872 assocs);
Pavithra Barithaya6d178522024-06-24 04:17:29 -0500873 auto dummyErase = [](const std::string& /*entryId*/) {
Lei YU6e9fb1d2021-02-19 18:01:40 +0800874 // Do nothing;
875 };
876 biosVersion = std::make_unique<VersionClass>(
877 bus, path, version, VersionPurpose::Host, "", "",
Justin Ledford054bb0b2022-03-15 15:46:58 -0700878 std::vector<std::string>(),
Adriana Kobylak59b640b2022-01-21 19:45:22 +0000879 std::bind(dummyErase, std::placeholders::_1), "");
Lei YU6e9fb1d2021-02-19 18:01:40 +0800880 biosVersion->deleteObject =
881 std::make_unique<phosphor::software::manager::Delete>(bus, path,
882 *biosVersion);
883}
884#endif
885
Lei YU531fbc22021-12-10 20:03:18 +0800886void ItemUpdater::getRunningSlot()
887{
888 // Check /run/media/slot to get the slot number
889 constexpr auto slotFile = "/run/media/slot";
890 std::fstream f(slotFile, std::ios_base::in);
891 f >> runningImageSlot;
892}
893
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500894} // namespace updater
895} // namespace software
896} // namespace phosphor