blob: 550f84565d972ad409696dfac393911c8233b053 [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 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600142 versionId,
143 std::make_unique<Activation>(bus, path, *this, versionId,
144 activationState, associations)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500145
Saqib Khanee13e832017-10-23 12:53:11 -0500146 auto versionPtr = std::make_unique<VersionClass>(
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +0700147 bus, path, version, purpose, extendedVersion, filePath,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600148 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Saqib Khanee13e832017-10-23 12:53:11 -0500149 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600150 std::make_unique<phosphor::software::manager::Delete>(bus, path,
151 *versionPtr);
Saqib Khanee13e832017-10-23 12:53:11 -0500152 versions.insert(std::make_pair(versionId, std::move(versionPtr)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500153 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500154 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500155}
156
Saqib Khanba239882017-05-26 08:41:54 -0500157void ItemUpdater::processBMCImage()
158{
Gunnar Mills88e8a322017-09-13 11:09:28 -0500159 using VersionClass = phosphor::software::manager::Version;
Lei YU269bff32018-08-21 15:21:40 +0800160
161 // Check MEDIA_DIR and create if it does not exist
162 try
163 {
164 if (!fs::is_directory(MEDIA_DIR))
165 {
166 fs::create_directory(MEDIA_DIR);
167 }
168 }
169 catch (const fs::filesystem_error& e)
170 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500171 error("Failed to prepare dir: {ERROR}", "ERROR", e);
Lei YU269bff32018-08-21 15:21:40 +0800172 return;
173 }
174
Gunnar Mills88e8a322017-09-13 11:09:28 -0500175 // Read os-release from /etc/ to get the functional BMC version
176 auto functionalVersion = VersionClass::getBMCVersion(OS_RELEASE_FILE);
177
Saqib Khan1eef62d2017-08-10 15:29:34 -0500178 // Read os-release from folders under /media/ to get
179 // BMC Software Versions.
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500180 for (const auto& iter : fs::directory_iterator(MEDIA_DIR))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500181 {
182 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500183 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500184
185 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600186 if (0 ==
187 iter.path().native().compare(0, BMC_RO_PREFIX_LEN, BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500188 {
Adriana Kobylak716cd782020-06-08 13:27:43 -0500189 // Get the version to calculate the id
Adriana Kobylak24a8d832020-06-10 08:29:36 -0500190 fs::path releaseFile(OS_RELEASE_FILE);
191 auto osRelease = iter.path() / releaseFile.relative_path();
Saqib Khan1eef62d2017-08-10 15:29:34 -0500192 if (!fs::is_regular_file(osRelease))
193 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500194 error("Failed to read osRelease: {PATH}", "PATH", osRelease);
Adriana Kobylak716cd782020-06-08 13:27:43 -0500195
196 // Try to get the version id from the mount directory name and
197 // call to delete it as this version may be corrupted. Dynamic
198 // volumes created by the UBI layout for example have the id in
199 // the mount directory name. The worst that can happen is that
200 // erase() is called with an non-existent id and returns.
201 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
Saqib Khan021c3652017-09-26 12:11:02 -0500202 ItemUpdater::erase(id);
Adriana Kobylak716cd782020-06-08 13:27:43 -0500203
Saqib Khan021c3652017-09-26 12:11:02 -0500204 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500205 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500206 auto version = VersionClass::getBMCVersion(osRelease);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500207 if (version.empty())
208 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500209 error("Failed to read version from osRelease: {PATH}", "PATH",
210 osRelease);
Adriana Kobylak716cd782020-06-08 13:27:43 -0500211
212 // Try to delete the version, same as above if the
213 // OS_RELEASE_FILE does not exist.
214 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
215 ItemUpdater::erase(id);
216
217 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500218 }
Saqib Khan021c3652017-09-26 12:11:02 -0500219
Adriana Kobylak716cd782020-06-08 13:27:43 -0500220 auto id = VersionClass::getId(version);
221
Adriana Kobylakf383d272020-06-16 15:17:22 -0500222 // Check if the id has already been added. This can happen if the
223 // BMC partitions / devices were manually flashed with the same
224 // image.
225 if (versions.find(id) != versions.end())
226 {
227 continue;
228 }
229
Saqib Khan1eef62d2017-08-10 15:29:34 -0500230 auto purpose = server::Version::VersionPurpose::BMC;
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600231 restorePurpose(id, purpose);
232
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +0700233 // Read os-release from /etc/ to get the BMC extended version
234 std::string extendedVersion =
235 VersionClass::getBMCExtendedVersion(osRelease);
236
Saqib Khan1eef62d2017-08-10 15:29:34 -0500237 auto path = fs::path(SOFTWARE_OBJPATH) / id;
238
Lei YU269bff32018-08-21 15:21:40 +0800239 // Create functional association if this is the functional
240 // version
Gunnar Mills88e8a322017-09-13 11:09:28 -0500241 if (version.compare(functionalVersion) == 0)
242 {
243 createFunctionalAssociation(path);
244 }
245
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500246 AssociationList associations = {};
247
248 if (activationState == server::Activation::Activations::Active)
249 {
250 // Create an association to the BMC inventory item
251 associations.emplace_back(std::make_tuple(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600252 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
253 bmcInventoryPath));
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500254
255 // Create an active association since this image is active
256 createActiveAssociation(path);
257 }
258
AppaRao Pulibbebec72020-01-28 23:57:41 +0530259 // All updateable firmware components must expose the updateable
260 // association.
261 createUpdateableAssociation(path);
262
Adriana Kobylaka84f06d2022-01-18 15:41:57 +0000263 // The flash location is part of the mount name: rofs-<location>
264 auto flashId = iter.path().native().substr(BMC_RO_PREFIX_LEN);
265
Adriana Kobylakee590c72017-09-26 15:16:06 -0500266 // Create Version instance for this version.
267 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylaka84f06d2022-01-18 15:41:57 +0000268 bus, path, version, purpose, extendedVersion, flashId,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600269 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500270 auto isVersionFunctional = versionPtr->isFunctional();
Michael Tritz4254bec2017-10-03 17:18:22 -0500271 if (!isVersionFunctional)
272 {
Saqib Khanee13e832017-10-23 12:53:11 -0500273 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600274 std::make_unique<phosphor::software::manager::Delete>(
275 bus, path, *versionPtr);
Michael Tritz4254bec2017-10-03 17:18:22 -0500276 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600277 versions.insert(std::make_pair(id, std::move(versionPtr)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500278
Saqib Khanee13e832017-10-23 12:53:11 -0500279 // Create Activation instance for this version.
280 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600281 id, std::make_unique<Activation>(
282 bus, path, *this, id, activationState, associations)));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500283
Lei YU269bff32018-08-21 15:21:40 +0800284 // If Active, create RedundancyPriority instance for this
285 // version.
Saqib Khan1eef62d2017-08-10 15:29:34 -0500286 if (activationState == server::Activation::Activations::Active)
287 {
288 uint8_t priority = std::numeric_limits<uint8_t>::max();
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600289 if (!restorePriority(id, priority))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500290 {
Adriana Kobylakee590c72017-09-26 15:16:06 -0500291 if (isVersionFunctional)
292 {
293 priority = 0;
294 }
295 else
296 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500297 error(
298 "Unable to restore priority from file for {VERSIONID}",
299 "VERSIONID", id);
Adriana Kobylakee590c72017-09-26 15:16:06 -0500300 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500301 }
302 activations.find(id)->second->redundancyPriority =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600303 std::make_unique<RedundancyPriority>(
304 bus, path, *(activations.find(id)->second), priority,
305 false);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500306 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500307 }
308 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500309
Adriana Kobylak716cd782020-06-08 13:27:43 -0500310 // If there are no bmc versions mounted under MEDIA_DIR, then read the
311 // /etc/os-release and create rofs-<versionId> under MEDIA_DIR, then call
312 // again processBMCImage() to create the D-Bus interface for it.
Saqib Khandcbfa042017-09-18 13:08:39 -0500313 if (activations.size() == 0)
314 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500315 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Saqib Khandcbfa042017-09-18 13:08:39 -0500316 auto id = phosphor::software::manager::Version::getId(version);
317 auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/";
318 try
319 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500320 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500321 {
322 fs::create_directories(versionFileDir);
323 }
324 auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE;
325 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
326 ItemUpdater::processBMCImage();
327 }
328 catch (const std::exception& e)
329 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500330 error("Exception during processing: {ERROR}", "ERROR", e);
Saqib Khandcbfa042017-09-18 13:08:39 -0500331 }
332 }
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600333
334 mirrorUbootToAlt();
Saqib Khanba239882017-05-26 08:41:54 -0500335 return;
336}
337
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500338void ItemUpdater::erase(std::string entryId)
339{
Eddie James6d873712017-09-01 11:29:07 -0500340 // Find entry in versions map
341 auto it = versions.find(entryId);
342 if (it != versions.end())
343 {
Lei YU0f88b5a2018-08-21 15:28:53 +0800344 if (it->second->isFunctional() && ACTIVE_BMC_MAX_ALLOWED > 1)
Eddie James6d873712017-09-01 11:29:07 -0500345 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500346 error(
347 "Version ({VERSIONID}) is currently running on the BMC; unable to remove.",
348 "VERSIONID", entryId);
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500349 return;
Eddie James6d873712017-09-01 11:29:07 -0500350 }
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500351 }
Eddie James6d873712017-09-01 11:29:07 -0500352
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500353 // First call resetUbootEnvVars() so that the BMC points to a valid image to
354 // boot from. If resetUbootEnvVars() is called after the image is actually
355 // deleted from the BMC flash, there'd be a time window where the BMC would
356 // be pointing to a non-existent image to boot from.
357 // Need to remove the entries from the activations map before that call so
358 // that resetUbootEnvVars() doesn't use the version to be deleted.
359 auto iteratorActivations = activations.find(entryId);
360 if (iteratorActivations == activations.end())
361 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500362 error(
363 "Failed to find version ({VERSIONID}) in item updater activations map; unable to remove.",
364 "VERSIONID", entryId);
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500365 }
366 else
367 {
368 removeAssociations(iteratorActivations->second->path);
Zami Seckae06d762021-08-13 20:11:15 -0500369 iteratorActivations->second->deleteImageManagerObject();
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500370 this->activations.erase(entryId);
371 }
372 ItemUpdater::resetUbootEnvVars();
373
374 if (it != versions.end())
375 {
Eddie James6d873712017-09-01 11:29:07 -0500376 // Delete ReadOnly partitions if it's not active
377 removeReadOnlyPartition(entryId);
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600378 removePersistDataDirectory(entryId);
Saqib Khanee13e832017-10-23 12:53:11 -0500379
380 // Removing entry in versions map
381 this->versions.erase(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500382 }
383 else
384 {
385 // Delete ReadOnly partitions even if we can't find the version
386 removeReadOnlyPartition(entryId);
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600387 removePersistDataDirectory(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500388
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500389 error(
390 "Failed to find version ({VERSIONID}) in item updater versions map; unable to remove.",
391 "VERSIONID", entryId);
Eddie James6d873712017-09-01 11:29:07 -0500392 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500393
Lei YU56aaf452018-06-21 16:09:44 +0800394 helper.clearEntry(entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500395
Saqib Khanee13e832017-10-23 12:53:11 -0500396 return;
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500397}
398
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500399void ItemUpdater::deleteAll()
400{
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600401 std::vector<std::string> deletableVersions;
402
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500403 for (const auto& versionIt : versions)
404 {
405 if (!versionIt.second->isFunctional())
406 {
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600407 deletableVersions.push_back(versionIt.first);
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500408 }
409 }
410
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600411 for (const auto& deletableIt : deletableVersions)
412 {
413 ItemUpdater::erase(deletableIt);
414 }
415
Lei YU56aaf452018-06-21 16:09:44 +0800416 helper.cleanup();
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500417}
418
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600419ItemUpdater::ActivationStatus
420 ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500421{
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800422 bool valid = true;
Saqib Khan35e83f32017-05-22 11:37:32 -0500423
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800424 // Record the images which are being updated
425 // First check for the fullimage, then check for images with partitions
426 imageUpdateList.push_back(bmcFullImages);
427 valid = checkImage(filePath, imageUpdateList);
428 if (!valid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500429 {
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800430 imageUpdateList.clear();
431 imageUpdateList.assign(bmcImages.begin(), bmcImages.end());
432 valid = checkImage(filePath, imageUpdateList);
433 if (!valid)
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500434 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500435 error("Failed to find the needed BMC images.");
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800436 return ItemUpdater::ActivationStatus::invalid;
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500437 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500438 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500439
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500440 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500441}
442
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500443void ItemUpdater::savePriority(const std::string& versionId, uint8_t value)
444{
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600445 storePriority(versionId, value);
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500446 helper.setEntry(versionId, value);
447}
448
Saqib Khanb9da6632017-09-13 09:48:37 -0500449void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500450{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500451 std::map<std::string, uint8_t> priorityMap;
452
453 // Insert the requested version and priority, it may not exist yet.
454 priorityMap.insert(std::make_pair(versionId, value));
455
Saqib Khan4c1aec02017-07-06 11:46:13 -0500456 for (const auto& intf : activations)
457 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500458 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500459 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500460 priorityMap.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600461 intf.first, intf.second->redundancyPriority.get()->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500462 }
463 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500464
465 // Lambda function to compare 2 priority values, use <= to allow duplicates
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600466 typedef std::function<bool(std::pair<std::string, uint8_t>,
467 std::pair<std::string, uint8_t>)>
468 cmpPriority;
469 cmpPriority cmpPriorityFunc =
470 [](std::pair<std::string, uint8_t> priority1,
471 std::pair<std::string, uint8_t> priority2) {
472 return priority1.second <= priority2.second;
473 };
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500474
475 // Sort versions by ascending priority
476 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600477 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500478
479 auto freePriorityValue = value;
480 for (auto& element : prioritySet)
481 {
482 if (element.first == versionId)
483 {
484 continue;
485 }
486 if (element.second == freePriorityValue)
487 {
488 ++freePriorityValue;
489 auto it = activations.find(element.first);
490 it->second->redundancyPriority.get()->sdbusPriority(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600491 freePriorityValue);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500492 }
493 }
494
495 auto lowestVersion = prioritySet.begin()->first;
496 if (value == prioritySet.begin()->second)
497 {
498 lowestVersion = versionId;
499 }
500 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500501}
502
Michael Tritz37a59042017-07-12 13:44:53 -0500503void ItemUpdater::reset()
504{
Lei YU56aaf452018-06-21 16:09:44 +0800505 helper.factoryReset();
Michael Tritz37a59042017-07-12 13:44:53 -0500506
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500507 info("BMC factory reset will take effect upon reboot.");
Michael Tritz37a59042017-07-12 13:44:53 -0500508}
509
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500510void ItemUpdater::removeReadOnlyPartition(std::string versionId)
511{
Lei YU56aaf452018-06-21 16:09:44 +0800512 helper.removeVersion(versionId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500513}
514
Michael Tritz0129d922017-08-10 19:33:46 -0500515bool ItemUpdater::fieldModeEnabled(bool value)
516{
517 // enabling field mode is intended to be one way: false -> true
518 if (value && !control::FieldMode::fieldModeEnabled())
519 {
520 control::FieldMode::fieldModeEnabled(value);
521
Adriana Kobylak22848ec2019-10-28 10:08:39 -0500522 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
523 SYSTEMD_INTERFACE, "StartUnit");
524 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
525 "replace");
526 bus.call_noreply(method);
527
528 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
529 SYSTEMD_INTERFACE, "StopUnit");
530 method.append("usr-local.mount", "replace");
531 bus.call_noreply(method);
532
533 std::vector<std::string> usrLocal = {"usr-local.mount"};
534
535 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
536 SYSTEMD_INTERFACE, "MaskUnitFiles");
537 method.append(usrLocal, false, true);
538 bus.call_noreply(method);
Michael Tritz0129d922017-08-10 19:33:46 -0500539 }
Adriana Kobylakd5b8f752019-05-01 11:52:35 -0500540 else if (!value && control::FieldMode::fieldModeEnabled())
541 {
542 elog<NotAllowed>(xyz::openbmc_project::Common::NotAllowed::REASON(
543 "FieldMode is not allowed to be cleared"));
544 }
Michael Tritz0129d922017-08-10 19:33:46 -0500545
546 return control::FieldMode::fieldModeEnabled();
547}
548
549void ItemUpdater::restoreFieldModeStatus()
550{
Michael Tritzff0b4212017-10-24 17:38:09 -0500551 std::ifstream input("/dev/mtd/u-boot-env");
Michael Tritz0129d922017-08-10 19:33:46 -0500552 std::string envVar;
553 std::getline(input, envVar);
554
Gunnar Mills9a782242017-08-22 16:23:15 -0500555 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500556 {
557 ItemUpdater::fieldModeEnabled(true);
558 }
559}
560
Gunnar Millsb60add12017-08-24 16:41:42 -0500561void ItemUpdater::setBMCInventoryPath()
562{
Gunnar Millsb60add12017-08-24 16:41:42 -0500563 auto depth = 0;
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600564 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
565 MAPPER_INTERFACE, "GetSubTreePaths");
Gunnar Millsb60add12017-08-24 16:41:42 -0500566
Adriana Kobylak1254c622017-12-07 12:24:56 -0600567 mapperCall.append(INVENTORY_PATH);
Gunnar Millsb60add12017-08-24 16:41:42 -0500568 mapperCall.append(depth);
Adriana Kobylak1254c622017-12-07 12:24:56 -0600569 std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE};
Gunnar Millsb60add12017-08-24 16:41:42 -0500570 mapperCall.append(filter);
571
Ed Tanous87c78172018-08-10 12:51:53 -0700572 try
573 {
574 auto response = bus.call(mapperCall);
575
576 using ObjectPaths = std::vector<std::string>;
577 ObjectPaths result;
578 response.read(result);
579
580 if (!result.empty())
581 {
582 bmcInventoryPath = result.front();
583 }
584 }
Patrick Williams4ce901c2021-09-02 09:34:45 -0500585 catch (const sdbusplus::exception::exception& e)
Gunnar Millsb60add12017-08-24 16:41:42 -0500586 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500587 error("Error in mapper GetSubTreePath: {ERROR}", "ERROR", e);
Gunnar Millsb60add12017-08-24 16:41:42 -0500588 return;
589 }
590
Adriana Kobylak1254c622017-12-07 12:24:56 -0600591 return;
Gunnar Millsb60add12017-08-24 16:41:42 -0500592}
593
Gunnar Millsf10b2322017-09-21 15:31:55 -0500594void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500595{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600596 assocs.emplace_back(
597 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
Gunnar Millsded875d2017-08-28 16:44:52 -0500598 associations(assocs);
599}
600
Gunnar Mills88e8a322017-09-13 11:09:28 -0500601void ItemUpdater::createFunctionalAssociation(const std::string& path)
602{
603 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600604 FUNCTIONAL_REV_ASSOCIATION, path));
Gunnar Mills88e8a322017-09-13 11:09:28 -0500605 associations(assocs);
606}
607
AppaRao Pulibbebec72020-01-28 23:57:41 +0530608void ItemUpdater::createUpdateableAssociation(const std::string& path)
609{
610 assocs.emplace_back(std::make_tuple(UPDATEABLE_FWD_ASSOCIATION,
611 UPDATEABLE_REV_ASSOCIATION, path));
612 associations(assocs);
613}
614
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600615void ItemUpdater::removeAssociations(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500616{
617 for (auto iter = assocs.begin(); iter != assocs.end();)
618 {
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600619 if ((std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500620 {
621 iter = assocs.erase(iter);
622 associations(assocs);
623 }
624 else
625 {
626 ++iter;
627 }
628 }
629}
630
Saqib Khanb9da6632017-09-13 09:48:37 -0500631bool ItemUpdater::isLowestPriority(uint8_t value)
632{
633 for (const auto& intf : activations)
634 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500635 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500636 {
637 if (intf.second->redundancyPriority.get()->priority() < value)
638 {
639 return false;
640 }
641 }
642 }
643 return true;
644}
645
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500646void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
647{
Lei YU56aaf452018-06-21 16:09:44 +0800648 helper.updateUbootVersionId(versionId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500649}
650
Saqib Khan49446ae2017-10-02 10:54:20 -0500651void ItemUpdater::resetUbootEnvVars()
652{
653 decltype(activations.begin()->second->redundancyPriority.get()->priority())
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600654 lowestPriority = std::numeric_limits<uint8_t>::max();
Saqib Khan49446ae2017-10-02 10:54:20 -0500655 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
656 for (const auto& intf : activations)
657 {
658 if (!intf.second->redundancyPriority.get())
659 {
660 // Skip this version if the redundancyPriority is not initialized.
661 continue;
662 }
663
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600664 if (intf.second->redundancyPriority.get()->priority() <= lowestPriority)
Saqib Khan49446ae2017-10-02 10:54:20 -0500665 {
666 lowestPriority = intf.second->redundancyPriority.get()->priority();
667 lowestPriorityVersion = intf.second->versionId;
668 }
669 }
670
Saqib Khanf0382c32017-10-24 13:36:22 -0500671 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500672 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500673}
674
Lei YU0cd6d842021-12-27 11:56:02 +0800675void ItemUpdater::freeSpace(const Activation& caller)
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600676{
677 // Versions with the highest priority in front
678 std::priority_queue<std::pair<int, std::string>,
679 std::vector<std::pair<int, std::string>>,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600680 std::less<std::pair<int, std::string>>>
681 versionsPQ;
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600682
683 std::size_t count = 0;
684 for (const auto& iter : activations)
685 {
686 if ((iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600687 server::Activation::Activations::Active) ||
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600688 (iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600689 server::Activation::Activations::Failed))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600690 {
691 count++;
692 // Don't put the functional version on the queue since we can't
693 // remove the "running" BMC version.
Lei YU0f88b5a2018-08-21 15:28:53 +0800694 // If ACTIVE_BMC_MAX_ALLOWED <= 1, there is only one active BMC,
695 // so remove functional version as well.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500696 // Don't delete the the Activation object that called this function.
697 if ((versions.find(iter.second->versionId)
698 ->second->isFunctional() &&
699 ACTIVE_BMC_MAX_ALLOWED > 1) ||
700 (iter.second->versionId == caller.versionId))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600701 {
702 continue;
703 }
Adriana Kobylaka6963592018-09-07 14:13:29 -0500704
705 // Failed activations don't have priority, assign them a large value
706 // for sorting purposes.
707 auto priority = 999;
708 if (iter.second.get()->activation() ==
Lei YUcfb4b202021-05-06 17:47:28 +0800709 server::Activation::Activations::Active &&
710 iter.second->redundancyPriority)
Adriana Kobylaka6963592018-09-07 14:13:29 -0500711 {
712 priority = iter.second->redundancyPriority.get()->priority();
713 }
714
715 versionsPQ.push(std::make_pair(priority, iter.second->versionId));
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600716 }
717 }
718
719 // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1,
720 // remove the highest priority one(s).
721 while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty()))
722 {
723 erase(versionsPQ.top().second);
724 versionsPQ.pop();
725 count--;
726 }
727}
728
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600729void ItemUpdater::mirrorUbootToAlt()
730{
Lei YU56aaf452018-06-21 16:09:44 +0800731 helper.mirrorAlt();
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600732}
733
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800734bool ItemUpdater::checkImage(const std::string& filePath,
735 const std::vector<std::string>& imageList)
736{
737 bool valid = true;
738
739 for (auto& bmcImage : imageList)
740 {
741 fs::path file(filePath);
742 file /= bmcImage;
743 std::ifstream efile(file.c_str());
744 if (efile.good() != 1)
745 {
746 valid = false;
747 break;
748 }
749 }
750
751 return valid;
752}
753
Lei YU6e9fb1d2021-02-19 18:01:40 +0800754#ifdef HOST_BIOS_UPGRADE
755void ItemUpdater::createBIOSObject()
756{
757 std::string path = BIOS_OBJPATH;
758 // Get version id from last item in the path
759 auto pos = path.rfind("/");
760 if (pos == std::string::npos)
761 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500762 error("No version id found in object path {PATH}", "PATH", path);
Lei YU6e9fb1d2021-02-19 18:01:40 +0800763 return;
764 }
765
766 createActiveAssociation(path);
767 createFunctionalAssociation(path);
768
769 auto versionId = path.substr(pos + 1);
770 auto version = "null";
771 AssociationList assocs = {};
772 biosActivation = std::make_unique<Activation>(
773 bus, path, *this, versionId, server::Activation::Activations::Active,
774 assocs);
775 auto dummyErase = [](std::string /*entryId*/) {
776 // Do nothing;
777 };
778 biosVersion = std::make_unique<VersionClass>(
779 bus, path, version, VersionPurpose::Host, "", "",
780 std::bind(dummyErase, std::placeholders::_1));
781 biosVersion->deleteObject =
782 std::make_unique<phosphor::software::manager::Delete>(bus, path,
783 *biosVersion);
784}
785#endif
786
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500787} // namespace updater
788} // namespace software
789} // namespace phosphor