blob: ea7cb2d7c27d827f1f7793666a7fb83b556ac837 [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>
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
Patrick Williamsc9bb6422021-08-27 06:18:35 -050034PHOSPHOR_LOG2_USING;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050035using namespace phosphor::logging;
Adriana Kobylak43699ca2018-10-17 14:56:29 -050036using namespace sdbusplus::xyz::openbmc_project::Software::Image::Error;
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060037using namespace phosphor::software::image;
Adriana Kobylakc98d9122020-05-05 10:36:01 -050038namespace fs = std::filesystem;
Adriana Kobylakd5b8f752019-05-01 11:52:35 -050039using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
Saqib Khan35e83f32017-05-22 11:37:32 -050040
Patrick Williamse75d10f2017-05-30 16:56:32 -050041void ItemUpdater::createActivation(sdbusplus::message::message& msg)
Gunnar Millsec1b41c2017-05-02 12:20:36 -050042{
Saqib Khan84a0e692017-06-28 17:27:01 -050043
44 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;
Patrick Williamsbc1facd2020-06-03 05:58:27 -050052 std::map<std::string, std::map<std::string, std::variant<std::string>>>
Adriana Kobylak2285fe02018-02-27 15:36:59 -060053 interfaces;
Patrick Williamse75d10f2017-05-30 16:56:32 -050054 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050055 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050056 std::string filePath;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050057
58 for (const auto& intf : interfaces)
59 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050060 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050061 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050062 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050063 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050064 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050065 {
Saqib Khan84a0e692017-06-28 17:27:01 -050066 auto value = SVersion::convertVersionPurposeFromString(
Patrick Williamse883fb82020-05-13 11:38:55 -050067 std::get<std::string>(property.second));
Saqib Khan84a0e692017-06-28 17:27:01 -050068 if (value == VersionPurpose::BMC ||
Vijay Khemkae9f6c842020-01-14 14:32:39 -080069#ifdef HOST_BIOS_UPGRADE
70 value == VersionPurpose::Host ||
71#endif
Saqib Khan84a0e692017-06-28 17:27:01 -050072 value == VersionPurpose::System)
73 {
74 purpose = value;
75 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050076 }
77 else if (property.first == "Version")
78 {
Patrick Williamse883fb82020-05-13 11:38:55 -050079 version = std::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050080 }
81 }
82 }
Saqib Khan19177d32017-06-20 08:11:49 -050083 else if (intf.first == FILEPATH_IFACE)
84 {
85 for (const auto& property : intf.second)
86 {
87 if (property.first == "Path")
88 {
Patrick Williamse883fb82020-05-13 11:38:55 -050089 filePath = std::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050090 }
91 }
92 }
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +070093 else if (intf.first == EXTENDED_VERSION_IFACE)
94 {
95 for (const auto& property : intf.second)
96 {
97 if (property.first == "ExtendedVersion")
98 {
99 extendedVersion = std::get<std::string>(property.second);
100 }
101 }
102 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500103 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600104 if (version.empty() || filePath.empty() ||
Saqib Khan84a0e692017-06-28 17:27:01 -0500105 purpose == VersionPurpose::Unknown)
Saqib Khan705f1bf2017-06-09 23:58:38 -0500106 {
107 return;
108 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500109
110 // Version id is the last item in the path
111 auto pos = path.rfind("/");
112 if (pos == std::string::npos)
113 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500114 error("No version id found in object path: {PATH}", "PATH", path);
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 auto versionPtr = std::make_unique<VersionClass>(
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +0700142 bus, path, version, purpose, extendedVersion, filePath,
Adriana Kobylak59b640b2022-01-21 19:45:22 +0000143 std::bind(&ItemUpdater::erase, this, std::placeholders::_1),
144 versionId);
Saqib Khanee13e832017-10-23 12:53:11 -0500145 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600146 std::make_unique<phosphor::software::manager::Delete>(bus, path,
147 *versionPtr);
Saqib Khanee13e832017-10-23 12:53:11 -0500148 versions.insert(std::make_pair(versionId, std::move(versionPtr)));
Adriana Kobylak88ba1f92022-03-09 21:14:15 +0000149
150 activations.insert(std::make_pair(
151 versionId,
152 std::make_unique<Activation>(bus, path, *this, versionId,
153 activationState, associations)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500154 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500155 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500156}
157
Saqib Khanba239882017-05-26 08:41:54 -0500158void ItemUpdater::processBMCImage()
159{
Gunnar Mills88e8a322017-09-13 11:09:28 -0500160 using VersionClass = phosphor::software::manager::Version;
Lei YU269bff32018-08-21 15:21:40 +0800161
162 // Check MEDIA_DIR and create if it does not exist
163 try
164 {
165 if (!fs::is_directory(MEDIA_DIR))
166 {
167 fs::create_directory(MEDIA_DIR);
168 }
169 }
170 catch (const fs::filesystem_error& e)
171 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500172 error("Failed to prepare dir: {ERROR}", "ERROR", e);
Lei YU269bff32018-08-21 15:21:40 +0800173 return;
174 }
175
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000176 // Functional images are mounted as rofs-<location>-functional
177 constexpr auto functionalSuffix = "-functional";
Lei YUd474d9c2021-12-10 16:21:21 +0800178 bool functionalFound = false;
Gunnar Mills88e8a322017-09-13 11:09:28 -0500179
Saqib Khan1eef62d2017-08-10 15:29:34 -0500180 // Read os-release from folders under /media/ to get
181 // BMC Software Versions.
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500182 for (const auto& iter : fs::directory_iterator(MEDIA_DIR))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500183 {
184 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500185 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500186
187 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600188 if (0 ==
189 iter.path().native().compare(0, BMC_RO_PREFIX_LEN, BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500190 {
Adriana Kobylak716cd782020-06-08 13:27:43 -0500191 // Get the version to calculate the id
Adriana Kobylak24a8d832020-06-10 08:29:36 -0500192 fs::path releaseFile(OS_RELEASE_FILE);
193 auto osRelease = iter.path() / releaseFile.relative_path();
Saqib Khan1eef62d2017-08-10 15:29:34 -0500194 if (!fs::is_regular_file(osRelease))
195 {
Lei YUe56bf872022-02-22 18:40:37 +0800196#ifdef BMC_STATIC_DUAL_IMAGE
197 // For dual image, it is possible that the secondary image is
198 // empty or contains invalid data, ignore such case.
199 info("Unable to find osRelease: {PATH}", "PATH", osRelease);
200#else
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500201 error("Failed to read osRelease: {PATH}", "PATH", osRelease);
Adriana Kobylak716cd782020-06-08 13:27:43 -0500202
203 // Try to get the version id from the mount directory name and
204 // call to delete it as this version may be corrupted. Dynamic
205 // volumes created by the UBI layout for example have the id in
206 // the mount directory name. The worst that can happen is that
207 // erase() is called with an non-existent id and returns.
208 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
Saqib Khan021c3652017-09-26 12:11:02 -0500209 ItemUpdater::erase(id);
Lei YUe56bf872022-02-22 18:40:37 +0800210#endif
Adriana Kobylak716cd782020-06-08 13:27:43 -0500211
Saqib Khan021c3652017-09-26 12:11:02 -0500212 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500213 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500214 auto version = VersionClass::getBMCVersion(osRelease);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500215 if (version.empty())
216 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500217 error("Failed to read version from osRelease: {PATH}", "PATH",
218 osRelease);
Adriana Kobylak716cd782020-06-08 13:27:43 -0500219
220 // Try to delete the version, same as above if the
221 // OS_RELEASE_FILE does not exist.
222 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
223 ItemUpdater::erase(id);
224
225 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500226 }
Saqib Khan021c3652017-09-26 12:11:02 -0500227
Adriana Kobylak59b640b2022-01-21 19:45:22 +0000228 // The flash location is part of the mount name: rofs-<location>
229 auto flashId = iter.path().native().substr(BMC_RO_PREFIX_LEN);
230
231 auto id = VersionClass::getId(version + flashId);
Adriana Kobylak716cd782020-06-08 13:27:43 -0500232
Adriana Kobylakf383d272020-06-16 15:17:22 -0500233 // Check if the id has already been added. This can happen if the
234 // BMC partitions / devices were manually flashed with the same
235 // image.
236 if (versions.find(id) != versions.end())
237 {
238 continue;
239 }
240
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000241 auto functional = false;
242 if (iter.path().native().find(functionalSuffix) !=
243 std::string::npos)
244 {
245 // Set functional to true and remove the functional suffix
246 functional = true;
247 flashId.erase(flashId.length() - strlen(functionalSuffix));
Lei YUd474d9c2021-12-10 16:21:21 +0800248 functionalFound = true;
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000249 }
Adriana Kobylak780220f2022-01-18 20:01:53 +0000250
Saqib Khan1eef62d2017-08-10 15:29:34 -0500251 auto purpose = server::Version::VersionPurpose::BMC;
Adriana Kobylak780220f2022-01-18 20:01:53 +0000252 restorePurpose(flashId, purpose);
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600253
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +0700254 // Read os-release from /etc/ to get the BMC extended version
255 std::string extendedVersion =
256 VersionClass::getBMCExtendedVersion(osRelease);
257
Saqib Khan1eef62d2017-08-10 15:29:34 -0500258 auto path = fs::path(SOFTWARE_OBJPATH) / id;
259
Lei YU269bff32018-08-21 15:21:40 +0800260 // Create functional association if this is the functional
261 // version
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000262 if (functional)
Gunnar Mills88e8a322017-09-13 11:09:28 -0500263 {
264 createFunctionalAssociation(path);
265 }
266
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500267 AssociationList associations = {};
268
269 if (activationState == server::Activation::Activations::Active)
270 {
271 // Create an association to the BMC inventory item
272 associations.emplace_back(std::make_tuple(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600273 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
274 bmcInventoryPath));
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500275
276 // Create an active association since this image is active
277 createActiveAssociation(path);
278 }
279
AppaRao Pulibbebec72020-01-28 23:57:41 +0530280 // All updateable firmware components must expose the updateable
281 // association.
282 createUpdateableAssociation(path);
283
Adriana Kobylakee590c72017-09-26 15:16:06 -0500284 // Create Version instance for this version.
285 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylaka84f06d2022-01-18 15:41:57 +0000286 bus, path, version, purpose, extendedVersion, flashId,
Adriana Kobylak59b640b2022-01-21 19:45:22 +0000287 std::bind(&ItemUpdater::erase, this, std::placeholders::_1),
288 id);
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000289 if (functional)
290 {
291 versionPtr->setFunctional(true);
292 }
293 else
Michael Tritz4254bec2017-10-03 17:18:22 -0500294 {
Saqib Khanee13e832017-10-23 12:53:11 -0500295 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600296 std::make_unique<phosphor::software::manager::Delete>(
297 bus, path, *versionPtr);
Michael Tritz4254bec2017-10-03 17:18:22 -0500298 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600299 versions.insert(std::make_pair(id, std::move(versionPtr)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500300
Saqib Khanee13e832017-10-23 12:53:11 -0500301 // Create Activation instance for this version.
302 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600303 id, std::make_unique<Activation>(
304 bus, path, *this, id, activationState, associations)));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500305
Lei YU269bff32018-08-21 15:21:40 +0800306 // If Active, create RedundancyPriority instance for this
307 // version.
Saqib Khan1eef62d2017-08-10 15:29:34 -0500308 if (activationState == server::Activation::Activations::Active)
309 {
310 uint8_t priority = std::numeric_limits<uint8_t>::max();
Adriana Kobylak780220f2022-01-18 20:01:53 +0000311 if (!restorePriority(flashId, priority))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500312 {
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000313 if (functional)
Adriana Kobylakee590c72017-09-26 15:16:06 -0500314 {
315 priority = 0;
316 }
317 else
318 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500319 error(
320 "Unable to restore priority from file for {VERSIONID}",
321 "VERSIONID", id);
Adriana Kobylakee590c72017-09-26 15:16:06 -0500322 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500323 }
324 activations.find(id)->second->redundancyPriority =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600325 std::make_unique<RedundancyPriority>(
326 bus, path, *(activations.find(id)->second), priority,
327 false);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500328 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500329 }
330 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500331
Lei YUd474d9c2021-12-10 16:21:21 +0800332 if (!functionalFound)
Saqib Khandcbfa042017-09-18 13:08:39 -0500333 {
Lei YUd474d9c2021-12-10 16:21:21 +0800334 // If there is no functional version found, read the /etc/os-release and
335 // create rofs-<versionId>-functional under MEDIA_DIR, then call again
336 // processBMCImage() to create the D-Bus interface for it.
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500337 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Adriana Kobylak59b640b2022-01-21 19:45:22 +0000338 auto id = phosphor::software::manager::Version::getId(version +
339 functionalSuffix);
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000340 auto versionFileDir = BMC_ROFS_PREFIX + id + functionalSuffix + "/etc/";
Saqib Khandcbfa042017-09-18 13:08:39 -0500341 try
342 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500343 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500344 {
345 fs::create_directories(versionFileDir);
346 }
Adriana Kobylak1e81f232022-01-18 22:28:47 +0000347 auto versionFilePath =
348 BMC_ROFS_PREFIX + id + functionalSuffix + OS_RELEASE_FILE;
Saqib Khandcbfa042017-09-18 13:08:39 -0500349 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
350 ItemUpdater::processBMCImage();
351 }
352 catch (const std::exception& e)
353 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500354 error("Exception during processing: {ERROR}", "ERROR", e);
Saqib Khandcbfa042017-09-18 13:08:39 -0500355 }
356 }
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600357
358 mirrorUbootToAlt();
Saqib Khanba239882017-05-26 08:41:54 -0500359 return;
360}
361
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500362void ItemUpdater::erase(std::string entryId)
363{
Eddie James6d873712017-09-01 11:29:07 -0500364 // Find entry in versions map
365 auto it = versions.find(entryId);
366 if (it != versions.end())
367 {
Lei YU0f88b5a2018-08-21 15:28:53 +0800368 if (it->second->isFunctional() && ACTIVE_BMC_MAX_ALLOWED > 1)
Eddie James6d873712017-09-01 11:29:07 -0500369 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500370 error(
371 "Version ({VERSIONID}) is currently running on the BMC; unable to remove.",
372 "VERSIONID", entryId);
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500373 return;
Eddie James6d873712017-09-01 11:29:07 -0500374 }
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500375 }
Eddie James6d873712017-09-01 11:29:07 -0500376
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500377 // First call resetUbootEnvVars() so that the BMC points to a valid image to
378 // boot from. If resetUbootEnvVars() is called after the image is actually
379 // deleted from the BMC flash, there'd be a time window where the BMC would
380 // be pointing to a non-existent image to boot from.
381 // Need to remove the entries from the activations map before that call so
382 // that resetUbootEnvVars() doesn't use the version to be deleted.
383 auto iteratorActivations = activations.find(entryId);
384 if (iteratorActivations == activations.end())
385 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500386 error(
387 "Failed to find version ({VERSIONID}) in item updater activations map; unable to remove.",
388 "VERSIONID", entryId);
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500389 }
390 else
391 {
392 removeAssociations(iteratorActivations->second->path);
Zami Seckae06d762021-08-13 20:11:15 -0500393 iteratorActivations->second->deleteImageManagerObject();
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500394 this->activations.erase(entryId);
395 }
396 ItemUpdater::resetUbootEnvVars();
397
398 if (it != versions.end())
399 {
Adriana Kobylak780220f2022-01-18 20:01:53 +0000400 auto flashId = it->second->path();
401
Adriana Kobylak25773a72022-01-21 15:24:48 +0000402 // Delete version data if it has been installed on flash (path is not
403 // the upload directory)
404 if (flashId.find(IMG_UPLOAD_DIR) == std::string::npos)
405 {
406 removeReadOnlyPartition(entryId);
407 removePersistDataDirectory(flashId);
408 helper.clearEntry(flashId);
409 }
Saqib Khanee13e832017-10-23 12:53:11 -0500410
411 // Removing entry in versions map
412 this->versions.erase(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500413 }
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500414
Saqib Khanee13e832017-10-23 12:53:11 -0500415 return;
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500416}
417
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500418void ItemUpdater::deleteAll()
419{
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600420 std::vector<std::string> deletableVersions;
421
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500422 for (const auto& versionIt : versions)
423 {
424 if (!versionIt.second->isFunctional())
425 {
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600426 deletableVersions.push_back(versionIt.first);
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500427 }
428 }
429
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600430 for (const auto& deletableIt : deletableVersions)
431 {
432 ItemUpdater::erase(deletableIt);
433 }
434
Lei YU56aaf452018-06-21 16:09:44 +0800435 helper.cleanup();
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500436}
437
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600438ItemUpdater::ActivationStatus
439 ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500440{
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800441 bool valid = true;
Saqib Khan35e83f32017-05-22 11:37:32 -0500442
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800443 // Record the images which are being updated
444 // First check for the fullimage, then check for images with partitions
445 imageUpdateList.push_back(bmcFullImages);
446 valid = checkImage(filePath, imageUpdateList);
447 if (!valid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500448 {
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800449 imageUpdateList.clear();
450 imageUpdateList.assign(bmcImages.begin(), bmcImages.end());
451 valid = checkImage(filePath, imageUpdateList);
452 if (!valid)
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500453 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500454 error("Failed to find the needed BMC images.");
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800455 return ItemUpdater::ActivationStatus::invalid;
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500456 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500457 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500458
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500459 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500460}
461
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500462void ItemUpdater::savePriority(const std::string& versionId, uint8_t value)
463{
Adriana Kobylak780220f2022-01-18 20:01:53 +0000464 auto flashId = versions.find(versionId)->second->path();
465 storePriority(flashId, value);
Adriana Kobylak25773a72022-01-21 15:24:48 +0000466 helper.setEntry(flashId, value);
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500467}
468
Saqib Khanb9da6632017-09-13 09:48:37 -0500469void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500470{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500471 std::map<std::string, uint8_t> priorityMap;
472
473 // Insert the requested version and priority, it may not exist yet.
474 priorityMap.insert(std::make_pair(versionId, value));
475
Saqib Khan4c1aec02017-07-06 11:46:13 -0500476 for (const auto& intf : activations)
477 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500478 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500479 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500480 priorityMap.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600481 intf.first, intf.second->redundancyPriority.get()->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500482 }
483 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500484
485 // Lambda function to compare 2 priority values, use <= to allow duplicates
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600486 typedef std::function<bool(std::pair<std::string, uint8_t>,
487 std::pair<std::string, uint8_t>)>
488 cmpPriority;
489 cmpPriority cmpPriorityFunc =
490 [](std::pair<std::string, uint8_t> priority1,
491 std::pair<std::string, uint8_t> priority2) {
492 return priority1.second <= priority2.second;
493 };
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500494
495 // Sort versions by ascending priority
496 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600497 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500498
499 auto freePriorityValue = value;
500 for (auto& element : prioritySet)
501 {
502 if (element.first == versionId)
503 {
504 continue;
505 }
506 if (element.second == freePriorityValue)
507 {
508 ++freePriorityValue;
509 auto it = activations.find(element.first);
510 it->second->redundancyPriority.get()->sdbusPriority(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600511 freePriorityValue);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500512 }
513 }
514
515 auto lowestVersion = prioritySet.begin()->first;
516 if (value == prioritySet.begin()->second)
517 {
518 lowestVersion = versionId;
519 }
520 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500521}
522
Michael Tritz37a59042017-07-12 13:44:53 -0500523void ItemUpdater::reset()
524{
Lei YU56aaf452018-06-21 16:09:44 +0800525 helper.factoryReset();
Michael Tritz37a59042017-07-12 13:44:53 -0500526
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500527 info("BMC factory reset will take effect upon reboot.");
Michael Tritz37a59042017-07-12 13:44:53 -0500528}
529
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500530void ItemUpdater::removeReadOnlyPartition(std::string versionId)
531{
Adriana Kobylak25773a72022-01-21 15:24:48 +0000532 auto flashId = versions.find(versionId)->second->path();
533 helper.removeVersion(flashId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500534}
535
Michael Tritz0129d922017-08-10 19:33:46 -0500536bool ItemUpdater::fieldModeEnabled(bool value)
537{
538 // enabling field mode is intended to be one way: false -> true
539 if (value && !control::FieldMode::fieldModeEnabled())
540 {
541 control::FieldMode::fieldModeEnabled(value);
542
Adriana Kobylak22848ec2019-10-28 10:08:39 -0500543 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
544 SYSTEMD_INTERFACE, "StartUnit");
545 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
546 "replace");
547 bus.call_noreply(method);
548
549 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
550 SYSTEMD_INTERFACE, "StopUnit");
551 method.append("usr-local.mount", "replace");
552 bus.call_noreply(method);
553
554 std::vector<std::string> usrLocal = {"usr-local.mount"};
555
556 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
557 SYSTEMD_INTERFACE, "MaskUnitFiles");
558 method.append(usrLocal, false, true);
559 bus.call_noreply(method);
Michael Tritz0129d922017-08-10 19:33:46 -0500560 }
Adriana Kobylakd5b8f752019-05-01 11:52:35 -0500561 else if (!value && control::FieldMode::fieldModeEnabled())
562 {
563 elog<NotAllowed>(xyz::openbmc_project::Common::NotAllowed::REASON(
564 "FieldMode is not allowed to be cleared"));
565 }
Michael Tritz0129d922017-08-10 19:33:46 -0500566
567 return control::FieldMode::fieldModeEnabled();
568}
569
570void ItemUpdater::restoreFieldModeStatus()
571{
Michael Tritzff0b4212017-10-24 17:38:09 -0500572 std::ifstream input("/dev/mtd/u-boot-env");
Michael Tritz0129d922017-08-10 19:33:46 -0500573 std::string envVar;
574 std::getline(input, envVar);
575
Gunnar Mills9a782242017-08-22 16:23:15 -0500576 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500577 {
578 ItemUpdater::fieldModeEnabled(true);
579 }
580}
581
Gunnar Millsb60add12017-08-24 16:41:42 -0500582void ItemUpdater::setBMCInventoryPath()
583{
Gunnar Millsb60add12017-08-24 16:41:42 -0500584 auto depth = 0;
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600585 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
586 MAPPER_INTERFACE, "GetSubTreePaths");
Gunnar Millsb60add12017-08-24 16:41:42 -0500587
Adriana Kobylak1254c622017-12-07 12:24:56 -0600588 mapperCall.append(INVENTORY_PATH);
Gunnar Millsb60add12017-08-24 16:41:42 -0500589 mapperCall.append(depth);
Adriana Kobylak1254c622017-12-07 12:24:56 -0600590 std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE};
Gunnar Millsb60add12017-08-24 16:41:42 -0500591 mapperCall.append(filter);
592
Ed Tanous87c78172018-08-10 12:51:53 -0700593 try
594 {
595 auto response = bus.call(mapperCall);
596
597 using ObjectPaths = std::vector<std::string>;
598 ObjectPaths result;
599 response.read(result);
600
601 if (!result.empty())
602 {
603 bmcInventoryPath = result.front();
604 }
605 }
Patrick Williams4ce901c2021-09-02 09:34:45 -0500606 catch (const sdbusplus::exception::exception& e)
Gunnar Millsb60add12017-08-24 16:41:42 -0500607 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500608 error("Error in mapper GetSubTreePath: {ERROR}", "ERROR", e);
Gunnar Millsb60add12017-08-24 16:41:42 -0500609 return;
610 }
611
Adriana Kobylak1254c622017-12-07 12:24:56 -0600612 return;
Gunnar Millsb60add12017-08-24 16:41:42 -0500613}
614
Gunnar Millsf10b2322017-09-21 15:31:55 -0500615void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500616{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600617 assocs.emplace_back(
618 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
Gunnar Millsded875d2017-08-28 16:44:52 -0500619 associations(assocs);
620}
621
Gunnar Mills88e8a322017-09-13 11:09:28 -0500622void ItemUpdater::createFunctionalAssociation(const std::string& path)
623{
624 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600625 FUNCTIONAL_REV_ASSOCIATION, path));
Gunnar Mills88e8a322017-09-13 11:09:28 -0500626 associations(assocs);
627}
628
AppaRao Pulibbebec72020-01-28 23:57:41 +0530629void ItemUpdater::createUpdateableAssociation(const std::string& path)
630{
631 assocs.emplace_back(std::make_tuple(UPDATEABLE_FWD_ASSOCIATION,
632 UPDATEABLE_REV_ASSOCIATION, path));
633 associations(assocs);
634}
635
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600636void ItemUpdater::removeAssociations(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500637{
638 for (auto iter = assocs.begin(); iter != assocs.end();)
639 {
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600640 if ((std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500641 {
642 iter = assocs.erase(iter);
643 associations(assocs);
644 }
645 else
646 {
647 ++iter;
648 }
649 }
650}
651
Saqib Khanb9da6632017-09-13 09:48:37 -0500652bool ItemUpdater::isLowestPriority(uint8_t value)
653{
654 for (const auto& intf : activations)
655 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500656 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500657 {
658 if (intf.second->redundancyPriority.get()->priority() < value)
659 {
660 return false;
661 }
662 }
663 }
664 return true;
665}
666
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500667void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
668{
Lei YU9c76a0a2022-03-14 11:37:15 +0800669 auto it = versions.find(versionId);
670 if (it == versions.end())
671 {
672 return;
673 }
674 auto flashId = it->second->path();
Adriana Kobylak25773a72022-01-21 15:24:48 +0000675 helper.updateUbootVersionId(flashId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500676}
677
Saqib Khan49446ae2017-10-02 10:54:20 -0500678void ItemUpdater::resetUbootEnvVars()
679{
680 decltype(activations.begin()->second->redundancyPriority.get()->priority())
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600681 lowestPriority = std::numeric_limits<uint8_t>::max();
Saqib Khan49446ae2017-10-02 10:54:20 -0500682 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
683 for (const auto& intf : activations)
684 {
685 if (!intf.second->redundancyPriority.get())
686 {
687 // Skip this version if the redundancyPriority is not initialized.
688 continue;
689 }
690
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600691 if (intf.second->redundancyPriority.get()->priority() <= lowestPriority)
Saqib Khan49446ae2017-10-02 10:54:20 -0500692 {
693 lowestPriority = intf.second->redundancyPriority.get()->priority();
694 lowestPriorityVersion = intf.second->versionId;
695 }
696 }
697
Saqib Khanf0382c32017-10-24 13:36:22 -0500698 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500699 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500700}
701
Lei YU0cd6d842021-12-27 11:56:02 +0800702void ItemUpdater::freeSpace(const Activation& caller)
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600703{
704 // Versions with the highest priority in front
705 std::priority_queue<std::pair<int, std::string>,
706 std::vector<std::pair<int, std::string>>,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600707 std::less<std::pair<int, std::string>>>
708 versionsPQ;
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600709
710 std::size_t count = 0;
711 for (const auto& iter : activations)
712 {
713 if ((iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600714 server::Activation::Activations::Active) ||
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600715 (iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600716 server::Activation::Activations::Failed))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600717 {
718 count++;
719 // Don't put the functional version on the queue since we can't
720 // remove the "running" BMC version.
Lei YU0f88b5a2018-08-21 15:28:53 +0800721 // If ACTIVE_BMC_MAX_ALLOWED <= 1, there is only one active BMC,
722 // so remove functional version as well.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500723 // Don't delete the the Activation object that called this function.
724 if ((versions.find(iter.second->versionId)
725 ->second->isFunctional() &&
726 ACTIVE_BMC_MAX_ALLOWED > 1) ||
727 (iter.second->versionId == caller.versionId))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600728 {
729 continue;
730 }
Adriana Kobylaka6963592018-09-07 14:13:29 -0500731
732 // Failed activations don't have priority, assign them a large value
733 // for sorting purposes.
734 auto priority = 999;
735 if (iter.second.get()->activation() ==
Lei YUcfb4b202021-05-06 17:47:28 +0800736 server::Activation::Activations::Active &&
737 iter.second->redundancyPriority)
Adriana Kobylaka6963592018-09-07 14:13:29 -0500738 {
739 priority = iter.second->redundancyPriority.get()->priority();
740 }
741
742 versionsPQ.push(std::make_pair(priority, iter.second->versionId));
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600743 }
744 }
745
746 // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1,
747 // remove the highest priority one(s).
748 while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty()))
749 {
750 erase(versionsPQ.top().second);
751 versionsPQ.pop();
752 count--;
753 }
754}
755
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600756void ItemUpdater::mirrorUbootToAlt()
757{
Lei YU56aaf452018-06-21 16:09:44 +0800758 helper.mirrorAlt();
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600759}
760
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800761bool ItemUpdater::checkImage(const std::string& filePath,
762 const std::vector<std::string>& imageList)
763{
764 bool valid = true;
765
766 for (auto& bmcImage : imageList)
767 {
768 fs::path file(filePath);
769 file /= bmcImage;
770 std::ifstream efile(file.c_str());
771 if (efile.good() != 1)
772 {
773 valid = false;
774 break;
775 }
776 }
777
778 return valid;
779}
780
Lei YU6e9fb1d2021-02-19 18:01:40 +0800781#ifdef HOST_BIOS_UPGRADE
782void ItemUpdater::createBIOSObject()
783{
784 std::string path = BIOS_OBJPATH;
785 // Get version id from last item in the path
786 auto pos = path.rfind("/");
787 if (pos == std::string::npos)
788 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500789 error("No version id found in object path {PATH}", "PATH", path);
Lei YU6e9fb1d2021-02-19 18:01:40 +0800790 return;
791 }
792
793 createActiveAssociation(path);
794 createFunctionalAssociation(path);
795
796 auto versionId = path.substr(pos + 1);
797 auto version = "null";
798 AssociationList assocs = {};
799 biosActivation = std::make_unique<Activation>(
800 bus, path, *this, versionId, server::Activation::Activations::Active,
801 assocs);
802 auto dummyErase = [](std::string /*entryId*/) {
803 // Do nothing;
804 };
805 biosVersion = std::make_unique<VersionClass>(
806 bus, path, version, VersionPurpose::Host, "", "",
Adriana Kobylak59b640b2022-01-21 19:45:22 +0000807 std::bind(dummyErase, std::placeholders::_1), "");
Lei YU6e9fb1d2021-02-19 18:01:40 +0800808 biosVersion->deleteObject =
809 std::make_unique<phosphor::software::manager::Delete>(bus, path,
810 *biosVersion);
811}
812#endif
813
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500814} // namespace updater
815} // namespace software
816} // namespace phosphor