blob: e6dd298737d5261baeb77a52614b46a0384b9ba8 [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"
8#include "xyz/openbmc_project/Software/Version/server.hpp"
9
Adriana Kobylakd5b8f752019-05-01 11:52:35 -050010#include <phosphor-logging/elog-errors.hpp>
Gunnar Millsb0ce9962018-09-07 13:39:10 -050011#include <phosphor-logging/elog.hpp>
12#include <phosphor-logging/log.hpp>
Adriana Kobylak58aa7502020-06-08 11:12:11 -050013#include <xyz/openbmc_project/Common/error.hpp>
14#include <xyz/openbmc_project/Software/Image/error.hpp>
15
16#include <filesystem>
17#include <fstream>
Adriana Kobylak204e1e72018-01-24 16:00:05 -060018#include <queue>
Adriana Kobylakb77551c2017-10-27 12:46:23 -050019#include <set>
Gunnar Millsec1b41c2017-05-02 12:20:36 -050020#include <string>
Gunnar Mills60f5ccf2020-05-28 12:26:39 -050021#include <thread>
Gunnar Millsec1b41c2017-05-02 12:20:36 -050022
23namespace phosphor
24{
25namespace software
26{
27namespace updater
28{
29
Gunnar Mills2ce7da22017-05-04 15:37:56 -050030// When you see server:: you know we're referencing our base class
31namespace server = sdbusplus::xyz::openbmc_project::Software::server;
Michael Tritz0129d922017-08-10 19:33:46 -050032namespace control = sdbusplus::xyz::openbmc_project::Control::server;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050033
34using namespace phosphor::logging;
Adriana Kobylak43699ca2018-10-17 14:56:29 -050035using namespace sdbusplus::xyz::openbmc_project::Software::Image::Error;
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060036using namespace phosphor::software::image;
Adriana Kobylakc98d9122020-05-05 10:36:01 -050037namespace fs = std::filesystem;
Adriana Kobylakd5b8f752019-05-01 11:52:35 -050038using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
Saqib Khan35e83f32017-05-22 11:37:32 -050039
Patrick Williamse75d10f2017-05-30 16:56:32 -050040void ItemUpdater::createActivation(sdbusplus::message::message& msg)
Gunnar Millsec1b41c2017-05-02 12:20:36 -050041{
Saqib Khan84a0e692017-06-28 17:27:01 -050042
43 using SVersion = server::Version;
44 using VersionPurpose = SVersion::VersionPurpose;
Gunnar Mills9a782242017-08-22 16:23:15 -050045 using VersionClass = phosphor::software::manager::Version;
Saqib Khan84a0e692017-06-28 17:27:01 -050046
Patrick Williamsbc1facd2020-06-03 05:58:27 -050047 sdbusplus::message::object_path objPath;
Saqib Khan84a0e692017-06-28 17:27:01 -050048 auto purpose = VersionPurpose::Unknown;
Saqib Khan705f1bf2017-06-09 23:58:38 -050049 std::string version;
Patrick Williamsbc1facd2020-06-03 05:58:27 -050050 std::map<std::string, std::map<std::string, std::variant<std::string>>>
Adriana Kobylak2285fe02018-02-27 15:36:59 -060051 interfaces;
Patrick Williamse75d10f2017-05-30 16:56:32 -050052 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050053 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050054 std::string filePath;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050055
56 for (const auto& intf : interfaces)
57 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050058 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050059 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050060 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050061 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050062 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050063 {
Saqib Khan84a0e692017-06-28 17:27:01 -050064 auto value = SVersion::convertVersionPurposeFromString(
Patrick Williamse883fb82020-05-13 11:38:55 -050065 std::get<std::string>(property.second));
Saqib Khan84a0e692017-06-28 17:27:01 -050066 if (value == VersionPurpose::BMC ||
Vijay Khemkae9f6c842020-01-14 14:32:39 -080067#ifdef HOST_BIOS_UPGRADE
68 value == VersionPurpose::Host ||
69#endif
Saqib Khan84a0e692017-06-28 17:27:01 -050070 value == VersionPurpose::System)
71 {
72 purpose = value;
73 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050074 }
75 else if (property.first == "Version")
76 {
Patrick Williamse883fb82020-05-13 11:38:55 -050077 version = std::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050078 }
79 }
80 }
Saqib Khan19177d32017-06-20 08:11:49 -050081 else if (intf.first == FILEPATH_IFACE)
82 {
83 for (const auto& property : intf.second)
84 {
85 if (property.first == "Path")
86 {
Patrick Williamse883fb82020-05-13 11:38:55 -050087 filePath = std::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050088 }
89 }
90 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050091 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -060092 if (version.empty() || filePath.empty() ||
Saqib Khan84a0e692017-06-28 17:27:01 -050093 purpose == VersionPurpose::Unknown)
Saqib Khan705f1bf2017-06-09 23:58:38 -050094 {
95 return;
96 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050097
98 // Version id is the last item in the path
99 auto pos = path.rfind("/");
100 if (pos == std::string::npos)
101 {
102 log<level::ERR>("No version id found in object path",
Adriana Kobylak596466b2018-02-13 14:48:53 -0600103 entry("OBJPATH=%s", path.c_str()));
Patrick Williamse75d10f2017-05-30 16:56:32 -0500104 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500105 }
106
107 auto versionId = path.substr(pos + 1);
108
Patrick Williamse75d10f2017-05-30 16:56:32 -0500109 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500110 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500111 // Determine the Activation state by processing the given image dir.
112 auto activationState = server::Activation::Activations::Invalid;
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800113 ItemUpdater::ActivationStatus result;
114 if (purpose == VersionPurpose::BMC || purpose == VersionPurpose::System)
115 result = ItemUpdater::validateSquashFSImage(filePath);
116 else
117 result = ItemUpdater::ActivationStatus::ready;
118
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500119 AssociationList associations = {};
120
Saqib Khan35e83f32017-05-22 11:37:32 -0500121 if (result == ItemUpdater::ActivationStatus::ready)
122 {
123 activationState = server::Activation::Activations::Ready;
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500124 // Create an association to the BMC inventory item
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600125 associations.emplace_back(
126 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
127 ACTIVATION_REV_ASSOCIATION, bmcInventoryPath));
Saqib Khan35e83f32017-05-22 11:37:32 -0500128 }
Gunnar Millsb60add12017-08-24 16:41:42 -0500129
Saqib Khanee13e832017-10-23 12:53:11 -0500130 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600131 versionId,
132 std::make_unique<Activation>(bus, path, *this, versionId,
133 activationState, associations)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500134
Saqib Khanee13e832017-10-23 12:53:11 -0500135 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600136 bus, path, version, purpose, filePath,
137 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Saqib Khanee13e832017-10-23 12:53:11 -0500138 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600139 std::make_unique<phosphor::software::manager::Delete>(bus, path,
140 *versionPtr);
Saqib Khanee13e832017-10-23 12:53:11 -0500141 versions.insert(std::make_pair(versionId, std::move(versionPtr)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500142 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500143 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500144}
145
Saqib Khanba239882017-05-26 08:41:54 -0500146void ItemUpdater::processBMCImage()
147{
Gunnar Mills88e8a322017-09-13 11:09:28 -0500148 using VersionClass = phosphor::software::manager::Version;
Lei YU269bff32018-08-21 15:21:40 +0800149
150 // Check MEDIA_DIR and create if it does not exist
151 try
152 {
153 if (!fs::is_directory(MEDIA_DIR))
154 {
155 fs::create_directory(MEDIA_DIR);
156 }
157 }
158 catch (const fs::filesystem_error& e)
159 {
160 log<level::ERR>("Failed to prepare dir", entry("ERR=%s", e.what()));
161 return;
162 }
163
Gunnar Mills88e8a322017-09-13 11:09:28 -0500164 // Read os-release from /etc/ to get the functional BMC version
165 auto functionalVersion = VersionClass::getBMCVersion(OS_RELEASE_FILE);
166
Saqib Khan1eef62d2017-08-10 15:29:34 -0500167 // Read os-release from folders under /media/ to get
168 // BMC Software Versions.
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500169 for (const auto& iter : fs::directory_iterator(MEDIA_DIR))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500170 {
171 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500172 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500173
174 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600175 if (0 ==
176 iter.path().native().compare(0, BMC_RO_PREFIX_LEN, BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500177 {
Adriana Kobylak716cd782020-06-08 13:27:43 -0500178 // Get the version to calculate the id
Adriana Kobylak24a8d832020-06-10 08:29:36 -0500179 fs::path releaseFile(OS_RELEASE_FILE);
180 auto osRelease = iter.path() / releaseFile.relative_path();
Saqib Khan1eef62d2017-08-10 15:29:34 -0500181 if (!fs::is_regular_file(osRelease))
182 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600183 log<level::ERR>(
184 "Failed to read osRelease",
185 entry("FILENAME=%s", osRelease.string().c_str()));
Adriana Kobylak716cd782020-06-08 13:27:43 -0500186
187 // Try to get the version id from the mount directory name and
188 // call to delete it as this version may be corrupted. Dynamic
189 // volumes created by the UBI layout for example have the id in
190 // the mount directory name. The worst that can happen is that
191 // erase() is called with an non-existent id and returns.
192 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
Saqib Khan021c3652017-09-26 12:11:02 -0500193 ItemUpdater::erase(id);
Adriana Kobylak716cd782020-06-08 13:27:43 -0500194
Saqib Khan021c3652017-09-26 12:11:02 -0500195 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500196 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500197 auto version = VersionClass::getBMCVersion(osRelease);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500198 if (version.empty())
199 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600200 log<level::ERR>(
201 "Failed to read version from osRelease",
202 entry("FILENAME=%s", osRelease.string().c_str()));
Adriana Kobylak716cd782020-06-08 13:27:43 -0500203
204 // Try to delete the version, same as above if the
205 // OS_RELEASE_FILE does not exist.
206 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
207 ItemUpdater::erase(id);
208
209 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500210 }
Saqib Khan021c3652017-09-26 12:11:02 -0500211
Adriana Kobylak716cd782020-06-08 13:27:43 -0500212 auto id = VersionClass::getId(version);
213
Adriana Kobylakf383d272020-06-16 15:17:22 -0500214 // Check if the id has already been added. This can happen if the
215 // BMC partitions / devices were manually flashed with the same
216 // image.
217 if (versions.find(id) != versions.end())
218 {
219 continue;
220 }
221
Saqib Khan1eef62d2017-08-10 15:29:34 -0500222 auto purpose = server::Version::VersionPurpose::BMC;
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600223 restorePurpose(id, purpose);
224
Saqib Khan1eef62d2017-08-10 15:29:34 -0500225 auto path = fs::path(SOFTWARE_OBJPATH) / id;
226
Lei YU269bff32018-08-21 15:21:40 +0800227 // Create functional association if this is the functional
228 // version
Gunnar Mills88e8a322017-09-13 11:09:28 -0500229 if (version.compare(functionalVersion) == 0)
230 {
231 createFunctionalAssociation(path);
232 }
233
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500234 AssociationList associations = {};
235
236 if (activationState == server::Activation::Activations::Active)
237 {
238 // Create an association to the BMC inventory item
239 associations.emplace_back(std::make_tuple(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600240 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
241 bmcInventoryPath));
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500242
243 // Create an active association since this image is active
244 createActiveAssociation(path);
245 }
246
AppaRao Pulibbebec72020-01-28 23:57:41 +0530247 // All updateable firmware components must expose the updateable
248 // association.
249 createUpdateableAssociation(path);
250
Adriana Kobylakee590c72017-09-26 15:16:06 -0500251 // Create Version instance for this version.
252 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600253 bus, path, version, purpose, "",
254 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500255 auto isVersionFunctional = versionPtr->isFunctional();
Michael Tritz4254bec2017-10-03 17:18:22 -0500256 if (!isVersionFunctional)
257 {
Saqib Khanee13e832017-10-23 12:53:11 -0500258 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600259 std::make_unique<phosphor::software::manager::Delete>(
260 bus, path, *versionPtr);
Michael Tritz4254bec2017-10-03 17:18:22 -0500261 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600262 versions.insert(std::make_pair(id, std::move(versionPtr)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500263
Saqib Khanee13e832017-10-23 12:53:11 -0500264 // Create Activation instance for this version.
265 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600266 id, std::make_unique<Activation>(
267 bus, path, *this, id, activationState, associations)));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500268
Lei YU269bff32018-08-21 15:21:40 +0800269 // If Active, create RedundancyPriority instance for this
270 // version.
Saqib Khan1eef62d2017-08-10 15:29:34 -0500271 if (activationState == server::Activation::Activations::Active)
272 {
273 uint8_t priority = std::numeric_limits<uint8_t>::max();
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600274 if (!restorePriority(id, priority))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500275 {
Adriana Kobylakee590c72017-09-26 15:16:06 -0500276 if (isVersionFunctional)
277 {
278 priority = 0;
279 }
280 else
281 {
282 log<level::ERR>("Unable to restore priority from file.",
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600283 entry("VERSIONID=%s", id.c_str()));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500284 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500285 }
286 activations.find(id)->second->redundancyPriority =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600287 std::make_unique<RedundancyPriority>(
288 bus, path, *(activations.find(id)->second), priority,
289 false);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500290 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500291 }
292 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500293
Adriana Kobylak716cd782020-06-08 13:27:43 -0500294 // If there are no bmc versions mounted under MEDIA_DIR, then read the
295 // /etc/os-release and create rofs-<versionId> under MEDIA_DIR, then call
296 // again processBMCImage() to create the D-Bus interface for it.
Saqib Khandcbfa042017-09-18 13:08:39 -0500297 if (activations.size() == 0)
298 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500299 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Saqib Khandcbfa042017-09-18 13:08:39 -0500300 auto id = phosphor::software::manager::Version::getId(version);
301 auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/";
302 try
303 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500304 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500305 {
306 fs::create_directories(versionFileDir);
307 }
308 auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE;
309 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
310 ItemUpdater::processBMCImage();
311 }
312 catch (const std::exception& e)
313 {
314 log<level::ERR>(e.what());
315 }
316 }
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600317
318 mirrorUbootToAlt();
Saqib Khanba239882017-05-26 08:41:54 -0500319 return;
320}
321
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500322void ItemUpdater::erase(std::string entryId)
323{
Eddie James6d873712017-09-01 11:29:07 -0500324 // Find entry in versions map
325 auto it = versions.find(entryId);
326 if (it != versions.end())
327 {
Lei YU0f88b5a2018-08-21 15:28:53 +0800328 if (it->second->isFunctional() && ACTIVE_BMC_MAX_ALLOWED > 1)
Eddie James6d873712017-09-01 11:29:07 -0500329 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600330 log<level::ERR>("Error: Version is currently running on the BMC. "
331 "Unable to remove.",
332 entry("VERSIONID=%s", entryId.c_str()));
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500333 return;
Eddie James6d873712017-09-01 11:29:07 -0500334 }
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500335 }
Eddie James6d873712017-09-01 11:29:07 -0500336
Adriana Kobylakd1a55ad2020-06-11 14:01:28 -0500337 // First call resetUbootEnvVars() so that the BMC points to a valid image to
338 // boot from. If resetUbootEnvVars() is called after the image is actually
339 // deleted from the BMC flash, there'd be a time window where the BMC would
340 // be pointing to a non-existent image to boot from.
341 // Need to remove the entries from the activations map before that call so
342 // that resetUbootEnvVars() doesn't use the version to be deleted.
343 auto iteratorActivations = activations.find(entryId);
344 if (iteratorActivations == activations.end())
345 {
346 log<level::ERR>("Error: Failed to find version in item updater "
347 "activations map. Unable to remove.",
348 entry("VERSIONID=%s", entryId.c_str()));
349 }
350 else
351 {
352 removeAssociations(iteratorActivations->second->path);
353 this->activations.erase(entryId);
354 }
355 ItemUpdater::resetUbootEnvVars();
356
357 if (it != versions.end())
358 {
Eddie James6d873712017-09-01 11:29:07 -0500359 // Delete ReadOnly partitions if it's not active
360 removeReadOnlyPartition(entryId);
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600361 removePersistDataDirectory(entryId);
Saqib Khanee13e832017-10-23 12:53:11 -0500362
363 // Removing entry in versions map
364 this->versions.erase(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500365 }
366 else
367 {
368 // Delete ReadOnly partitions even if we can't find the version
369 removeReadOnlyPartition(entryId);
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600370 removePersistDataDirectory(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500371
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600372 log<level::ERR>("Error: Failed to find version in item updater "
373 "versions map. Unable to remove.",
374 entry("VERSIONID=%s", entryId.c_str()));
Eddie James6d873712017-09-01 11:29:07 -0500375 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500376
Lei YU56aaf452018-06-21 16:09:44 +0800377 helper.clearEntry(entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500378
Saqib Khanee13e832017-10-23 12:53:11 -0500379 return;
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500380}
381
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500382void ItemUpdater::deleteAll()
383{
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600384 std::vector<std::string> deletableVersions;
385
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500386 for (const auto& versionIt : versions)
387 {
388 if (!versionIt.second->isFunctional())
389 {
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600390 deletableVersions.push_back(versionIt.first);
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500391 }
392 }
393
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600394 for (const auto& deletableIt : deletableVersions)
395 {
396 ItemUpdater::erase(deletableIt);
397 }
398
Lei YU56aaf452018-06-21 16:09:44 +0800399 helper.cleanup();
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500400}
401
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600402ItemUpdater::ActivationStatus
403 ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500404{
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800405 bool valid = true;
Saqib Khan35e83f32017-05-22 11:37:32 -0500406
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800407 // Record the images which are being updated
408 // First check for the fullimage, then check for images with partitions
409 imageUpdateList.push_back(bmcFullImages);
410 valid = checkImage(filePath, imageUpdateList);
411 if (!valid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500412 {
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800413 imageUpdateList.clear();
414 imageUpdateList.assign(bmcImages.begin(), bmcImages.end());
415 valid = checkImage(filePath, imageUpdateList);
416 if (!valid)
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500417 {
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800418 log<level::ERR>("Failed to find the needed BMC images.");
419 return ItemUpdater::ActivationStatus::invalid;
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500420 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500421 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500422
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500423 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500424}
425
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500426void ItemUpdater::savePriority(const std::string& versionId, uint8_t value)
427{
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600428 storePriority(versionId, value);
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500429 helper.setEntry(versionId, value);
430}
431
Saqib Khanb9da6632017-09-13 09:48:37 -0500432void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500433{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500434 std::map<std::string, uint8_t> priorityMap;
435
436 // Insert the requested version and priority, it may not exist yet.
437 priorityMap.insert(std::make_pair(versionId, value));
438
Saqib Khan4c1aec02017-07-06 11:46:13 -0500439 for (const auto& intf : activations)
440 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500441 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500442 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500443 priorityMap.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600444 intf.first, intf.second->redundancyPriority.get()->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500445 }
446 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500447
448 // Lambda function to compare 2 priority values, use <= to allow duplicates
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600449 typedef std::function<bool(std::pair<std::string, uint8_t>,
450 std::pair<std::string, uint8_t>)>
451 cmpPriority;
452 cmpPriority cmpPriorityFunc =
453 [](std::pair<std::string, uint8_t> priority1,
454 std::pair<std::string, uint8_t> priority2) {
455 return priority1.second <= priority2.second;
456 };
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500457
458 // Sort versions by ascending priority
459 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600460 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500461
462 auto freePriorityValue = value;
463 for (auto& element : prioritySet)
464 {
465 if (element.first == versionId)
466 {
467 continue;
468 }
469 if (element.second == freePriorityValue)
470 {
471 ++freePriorityValue;
472 auto it = activations.find(element.first);
473 it->second->redundancyPriority.get()->sdbusPriority(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600474 freePriorityValue);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500475 }
476 }
477
478 auto lowestVersion = prioritySet.begin()->first;
479 if (value == prioritySet.begin()->second)
480 {
481 lowestVersion = versionId;
482 }
483 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500484}
485
Michael Tritz37a59042017-07-12 13:44:53 -0500486void ItemUpdater::reset()
487{
Gunnar Mills60f5ccf2020-05-28 12:26:39 -0500488 constexpr auto setFactoryResetWait = std::chrono::seconds(3);
Lei YU56aaf452018-06-21 16:09:44 +0800489 helper.factoryReset();
Michael Tritz37a59042017-07-12 13:44:53 -0500490
Gunnar Mills60f5ccf2020-05-28 12:26:39 -0500491 // Need to wait for env variables to complete, otherwise an immediate reboot
492 // will not factory reset.
493 std::this_thread::sleep_for(setFactoryResetWait);
494
Michael Tritz37a59042017-07-12 13:44:53 -0500495 log<level::INFO>("BMC factory reset will take effect upon reboot.");
Michael Tritz37a59042017-07-12 13:44:53 -0500496}
497
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500498void ItemUpdater::removeReadOnlyPartition(std::string versionId)
499{
Lei YU56aaf452018-06-21 16:09:44 +0800500 helper.removeVersion(versionId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500501}
502
Michael Tritz0129d922017-08-10 19:33:46 -0500503bool ItemUpdater::fieldModeEnabled(bool value)
504{
505 // enabling field mode is intended to be one way: false -> true
506 if (value && !control::FieldMode::fieldModeEnabled())
507 {
508 control::FieldMode::fieldModeEnabled(value);
509
Adriana Kobylak22848ec2019-10-28 10:08:39 -0500510 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
511 SYSTEMD_INTERFACE, "StartUnit");
512 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
513 "replace");
514 bus.call_noreply(method);
515
516 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
517 SYSTEMD_INTERFACE, "StopUnit");
518 method.append("usr-local.mount", "replace");
519 bus.call_noreply(method);
520
521 std::vector<std::string> usrLocal = {"usr-local.mount"};
522
523 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
524 SYSTEMD_INTERFACE, "MaskUnitFiles");
525 method.append(usrLocal, false, true);
526 bus.call_noreply(method);
Michael Tritz0129d922017-08-10 19:33:46 -0500527 }
Adriana Kobylakd5b8f752019-05-01 11:52:35 -0500528 else if (!value && control::FieldMode::fieldModeEnabled())
529 {
530 elog<NotAllowed>(xyz::openbmc_project::Common::NotAllowed::REASON(
531 "FieldMode is not allowed to be cleared"));
532 }
Michael Tritz0129d922017-08-10 19:33:46 -0500533
534 return control::FieldMode::fieldModeEnabled();
535}
536
537void ItemUpdater::restoreFieldModeStatus()
538{
Michael Tritzff0b4212017-10-24 17:38:09 -0500539 std::ifstream input("/dev/mtd/u-boot-env");
Michael Tritz0129d922017-08-10 19:33:46 -0500540 std::string envVar;
541 std::getline(input, envVar);
542
Gunnar Mills9a782242017-08-22 16:23:15 -0500543 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500544 {
545 ItemUpdater::fieldModeEnabled(true);
546 }
547}
548
Gunnar Millsb60add12017-08-24 16:41:42 -0500549void ItemUpdater::setBMCInventoryPath()
550{
Gunnar Millsb60add12017-08-24 16:41:42 -0500551 auto depth = 0;
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600552 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
553 MAPPER_INTERFACE, "GetSubTreePaths");
Gunnar Millsb60add12017-08-24 16:41:42 -0500554
Adriana Kobylak1254c622017-12-07 12:24:56 -0600555 mapperCall.append(INVENTORY_PATH);
Gunnar Millsb60add12017-08-24 16:41:42 -0500556 mapperCall.append(depth);
Adriana Kobylak1254c622017-12-07 12:24:56 -0600557 std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE};
Gunnar Millsb60add12017-08-24 16:41:42 -0500558 mapperCall.append(filter);
559
Ed Tanous87c78172018-08-10 12:51:53 -0700560 try
561 {
562 auto response = bus.call(mapperCall);
563
564 using ObjectPaths = std::vector<std::string>;
565 ObjectPaths result;
566 response.read(result);
567
568 if (!result.empty())
569 {
570 bmcInventoryPath = result.front();
571 }
572 }
573 catch (const sdbusplus::exception::SdBusError& e)
Gunnar Millsb60add12017-08-24 16:41:42 -0500574 {
575 log<level::ERR>("Error in mapper GetSubTreePath");
576 return;
577 }
578
Adriana Kobylak1254c622017-12-07 12:24:56 -0600579 return;
Gunnar Millsb60add12017-08-24 16:41:42 -0500580}
581
Gunnar Millsf10b2322017-09-21 15:31:55 -0500582void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500583{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600584 assocs.emplace_back(
585 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
Gunnar Millsded875d2017-08-28 16:44:52 -0500586 associations(assocs);
587}
588
Gunnar Mills88e8a322017-09-13 11:09:28 -0500589void ItemUpdater::createFunctionalAssociation(const std::string& path)
590{
591 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600592 FUNCTIONAL_REV_ASSOCIATION, path));
Gunnar Mills88e8a322017-09-13 11:09:28 -0500593 associations(assocs);
594}
595
AppaRao Pulibbebec72020-01-28 23:57:41 +0530596void ItemUpdater::createUpdateableAssociation(const std::string& path)
597{
598 assocs.emplace_back(std::make_tuple(UPDATEABLE_FWD_ASSOCIATION,
599 UPDATEABLE_REV_ASSOCIATION, path));
600 associations(assocs);
601}
602
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600603void ItemUpdater::removeAssociations(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500604{
605 for (auto iter = assocs.begin(); iter != assocs.end();)
606 {
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600607 if ((std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500608 {
609 iter = assocs.erase(iter);
610 associations(assocs);
611 }
612 else
613 {
614 ++iter;
615 }
616 }
617}
618
Saqib Khanb9da6632017-09-13 09:48:37 -0500619bool ItemUpdater::isLowestPriority(uint8_t value)
620{
621 for (const auto& intf : activations)
622 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500623 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500624 {
625 if (intf.second->redundancyPriority.get()->priority() < value)
626 {
627 return false;
628 }
629 }
630 }
631 return true;
632}
633
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500634void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
635{
Lei YU56aaf452018-06-21 16:09:44 +0800636 helper.updateUbootVersionId(versionId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500637}
638
Saqib Khan49446ae2017-10-02 10:54:20 -0500639void ItemUpdater::resetUbootEnvVars()
640{
641 decltype(activations.begin()->second->redundancyPriority.get()->priority())
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600642 lowestPriority = std::numeric_limits<uint8_t>::max();
Saqib Khan49446ae2017-10-02 10:54:20 -0500643 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
644 for (const auto& intf : activations)
645 {
646 if (!intf.second->redundancyPriority.get())
647 {
648 // Skip this version if the redundancyPriority is not initialized.
649 continue;
650 }
651
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600652 if (intf.second->redundancyPriority.get()->priority() <= lowestPriority)
Saqib Khan49446ae2017-10-02 10:54:20 -0500653 {
654 lowestPriority = intf.second->redundancyPriority.get()->priority();
655 lowestPriorityVersion = intf.second->versionId;
656 }
657 }
658
Saqib Khanf0382c32017-10-24 13:36:22 -0500659 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500660 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500661}
662
Adriana Kobylaka6963592018-09-07 14:13:29 -0500663void ItemUpdater::freeSpace(Activation& caller)
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600664{
665 // Versions with the highest priority in front
666 std::priority_queue<std::pair<int, std::string>,
667 std::vector<std::pair<int, std::string>>,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600668 std::less<std::pair<int, std::string>>>
669 versionsPQ;
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600670
671 std::size_t count = 0;
672 for (const auto& iter : activations)
673 {
674 if ((iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600675 server::Activation::Activations::Active) ||
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600676 (iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600677 server::Activation::Activations::Failed))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600678 {
679 count++;
680 // Don't put the functional version on the queue since we can't
681 // remove the "running" BMC version.
Lei YU0f88b5a2018-08-21 15:28:53 +0800682 // If ACTIVE_BMC_MAX_ALLOWED <= 1, there is only one active BMC,
683 // so remove functional version as well.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500684 // Don't delete the the Activation object that called this function.
685 if ((versions.find(iter.second->versionId)
686 ->second->isFunctional() &&
687 ACTIVE_BMC_MAX_ALLOWED > 1) ||
688 (iter.second->versionId == caller.versionId))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600689 {
690 continue;
691 }
Adriana Kobylaka6963592018-09-07 14:13:29 -0500692
693 // Failed activations don't have priority, assign them a large value
694 // for sorting purposes.
695 auto priority = 999;
696 if (iter.second.get()->activation() ==
697 server::Activation::Activations::Active)
698 {
699 priority = iter.second->redundancyPriority.get()->priority();
700 }
701
702 versionsPQ.push(std::make_pair(priority, iter.second->versionId));
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600703 }
704 }
705
706 // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1,
707 // remove the highest priority one(s).
708 while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty()))
709 {
710 erase(versionsPQ.top().second);
711 versionsPQ.pop();
712 count--;
713 }
714}
715
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600716void ItemUpdater::mirrorUbootToAlt()
717{
Lei YU56aaf452018-06-21 16:09:44 +0800718 helper.mirrorAlt();
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600719}
720
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800721bool ItemUpdater::checkImage(const std::string& filePath,
722 const std::vector<std::string>& imageList)
723{
724 bool valid = true;
725
726 for (auto& bmcImage : imageList)
727 {
728 fs::path file(filePath);
729 file /= bmcImage;
730 std::ifstream efile(file.c_str());
731 if (efile.good() != 1)
732 {
733 valid = false;
734 break;
735 }
736 }
737
738 return valid;
739}
740
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500741} // namespace updater
742} // namespace software
743} // namespace phosphor