blob: df73fc5c6e1d1276c7c16873e9196d5eeef944e4 [file] [log] [blame]
Saqib Khan35e83f32017-05-22 11:37:32 -05001#include <fstream>
Adriana Kobylak204e1e72018-01-24 16:00:05 -06002#include <queue>
Adriana Kobylakb77551c2017-10-27 12:46:23 -05003#include <set>
Gunnar Millsec1b41c2017-05-02 12:20:36 -05004#include <string>
Gunnar Mills2ce7da22017-05-04 15:37:56 -05005#include <phosphor-logging/log.hpp>
Saqib Khandcbfa042017-09-18 13:08:39 -05006#include <phosphor-logging/elog.hpp>
7#include <elog-errors.hpp>
8#include <xyz/openbmc_project/Software/Version/error.hpp>
Gunnar Millsec1b41c2017-05-02 12:20:36 -05009#include "config.h"
Gunnar Mills2ce7da22017-05-04 15:37:56 -050010#include "item_updater.hpp"
11#include "xyz/openbmc_project/Software/Version/server.hpp"
Saqib Khan35e83f32017-05-22 11:37:32 -050012#include <experimental/filesystem>
Saqib Khan705f1bf2017-06-09 23:58:38 -050013#include "version.hpp"
Saqib Khan5d532672017-08-09 10:44:50 -050014#include "serialize.hpp"
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060015#include "image_verify.hpp"
Gunnar Millsec1b41c2017-05-02 12:20:36 -050016
17namespace phosphor
18{
19namespace software
20{
21namespace updater
22{
23
Gunnar Mills2ce7da22017-05-04 15:37:56 -050024// When you see server:: you know we're referencing our base class
25namespace server = sdbusplus::xyz::openbmc_project::Software::server;
Michael Tritz0129d922017-08-10 19:33:46 -050026namespace control = sdbusplus::xyz::openbmc_project::Control::server;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050027
28using namespace phosphor::logging;
Saqib Khandcbfa042017-09-18 13:08:39 -050029using namespace sdbusplus::xyz::openbmc_project::Software::Version::Error;
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060030using namespace phosphor::software::image;
Saqib Khan35e83f32017-05-22 11:37:32 -050031namespace fs = std::experimental::filesystem;
32
Patrick Williamse75d10f2017-05-30 16:56:32 -050033void ItemUpdater::createActivation(sdbusplus::message::message& msg)
Gunnar Millsec1b41c2017-05-02 12:20:36 -050034{
Saqib Khan84a0e692017-06-28 17:27:01 -050035
36 using SVersion = server::Version;
37 using VersionPurpose = SVersion::VersionPurpose;
Gunnar Mills9a782242017-08-22 16:23:15 -050038 using VersionClass = phosphor::software::manager::Version;
Saqib Khan84a0e692017-06-28 17:27:01 -050039 namespace mesg = sdbusplus::message;
40 namespace variant_ns = mesg::variant_ns;
41
42 mesg::object_path objPath;
43 auto purpose = VersionPurpose::Unknown;
Saqib Khan705f1bf2017-06-09 23:58:38 -050044 std::string version;
Adriana Kobylak2285fe02018-02-27 15:36:59 -060045 std::map<std::string, std::map<std::string, mesg::variant<std::string>>>
46 interfaces;
Patrick Williamse75d10f2017-05-30 16:56:32 -050047 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050048 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050049 std::string filePath;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050050
51 for (const auto& intf : interfaces)
52 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050053 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050054 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050055 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050056 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050057 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050058 {
Saqib Khan84a0e692017-06-28 17:27:01 -050059 auto value = SVersion::convertVersionPurposeFromString(
Adriana Kobylak2285fe02018-02-27 15:36:59 -060060 variant_ns::get<std::string>(property.second));
Saqib Khan84a0e692017-06-28 17:27:01 -050061 if (value == VersionPurpose::BMC ||
62 value == VersionPurpose::System)
63 {
64 purpose = value;
65 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050066 }
67 else if (property.first == "Version")
68 {
Saqib Khan84a0e692017-06-28 17:27:01 -050069 version = variant_ns::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050070 }
71 }
72 }
Saqib Khan19177d32017-06-20 08:11:49 -050073 else if (intf.first == FILEPATH_IFACE)
74 {
75 for (const auto& property : intf.second)
76 {
77 if (property.first == "Path")
78 {
Saqib Khan84a0e692017-06-28 17:27:01 -050079 filePath = variant_ns::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050080 }
81 }
82 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050083 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -060084 if (version.empty() || filePath.empty() ||
Saqib Khan84a0e692017-06-28 17:27:01 -050085 purpose == VersionPurpose::Unknown)
Saqib Khan705f1bf2017-06-09 23:58:38 -050086 {
87 return;
88 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050089
90 // Version id is the last item in the path
91 auto pos = path.rfind("/");
92 if (pos == std::string::npos)
93 {
94 log<level::ERR>("No version id found in object path",
Adriana Kobylak596466b2018-02-13 14:48:53 -060095 entry("OBJPATH=%s", path.c_str()));
Patrick Williamse75d10f2017-05-30 16:56:32 -050096 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050097 }
98
99 auto versionId = path.substr(pos + 1);
100
Patrick Williamse75d10f2017-05-30 16:56:32 -0500101 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500102 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500103 // Determine the Activation state by processing the given image dir.
104 auto activationState = server::Activation::Activations::Invalid;
Gunnar Mills9a782242017-08-22 16:23:15 -0500105 ItemUpdater::ActivationStatus result =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600106 ItemUpdater::validateSquashFSImage(filePath);
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500107 AssociationList associations = {};
108
Saqib Khan35e83f32017-05-22 11:37:32 -0500109 if (result == ItemUpdater::ActivationStatus::ready)
110 {
111 activationState = server::Activation::Activations::Ready;
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500112 // Create an association to the BMC inventory item
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600113 associations.emplace_back(
114 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
115 ACTIVATION_REV_ASSOCIATION, bmcInventoryPath));
Saqib Khan35e83f32017-05-22 11:37:32 -0500116 }
Gunnar Millsb60add12017-08-24 16:41:42 -0500117
Saqib Khanee13e832017-10-23 12:53:11 -0500118 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600119 versionId,
120 std::make_unique<Activation>(bus, path, *this, versionId,
121 activationState, associations)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500122
Saqib Khanee13e832017-10-23 12:53:11 -0500123 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600124 bus, path, version, purpose, filePath,
125 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Saqib Khanee13e832017-10-23 12:53:11 -0500126 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600127 std::make_unique<phosphor::software::manager::Delete>(bus, path,
128 *versionPtr);
Saqib Khanee13e832017-10-23 12:53:11 -0500129 versions.insert(std::make_pair(versionId, std::move(versionPtr)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500130 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500131 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500132}
133
Saqib Khanba239882017-05-26 08:41:54 -0500134void ItemUpdater::processBMCImage()
135{
Gunnar Mills88e8a322017-09-13 11:09:28 -0500136 using VersionClass = phosphor::software::manager::Version;
137 // Read os-release from /etc/ to get the functional BMC version
138 auto functionalVersion = VersionClass::getBMCVersion(OS_RELEASE_FILE);
139
Saqib Khan1eef62d2017-08-10 15:29:34 -0500140 // Read os-release from folders under /media/ to get
141 // BMC Software Versions.
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500142 for (const auto& iter : fs::directory_iterator(MEDIA_DIR))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500143 {
144 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500145 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500146
147 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600148 if (0 ==
149 iter.path().native().compare(0, BMC_RO_PREFIX_LEN, BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500150 {
Saqib Khan021c3652017-09-26 12:11:02 -0500151 // The versionId is extracted from the path
152 // for example /media/ro-2a1022fe.
153 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500154 auto osRelease = iter.path() / OS_RELEASE_FILE;
155 if (!fs::is_regular_file(osRelease))
156 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600157 log<level::ERR>(
158 "Failed to read osRelease",
159 entry("FILENAME=%s", osRelease.string().c_str()));
Saqib Khan021c3652017-09-26 12:11:02 -0500160 ItemUpdater::erase(id);
161 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500162 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500163 auto version = VersionClass::getBMCVersion(osRelease);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500164 if (version.empty())
165 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600166 log<level::ERR>(
167 "Failed to read version from osRelease",
168 entry("FILENAME=%s", osRelease.string().c_str()));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500169 activationState = server::Activation::Activations::Invalid;
170 }
Saqib Khan021c3652017-09-26 12:11:02 -0500171
Saqib Khan1eef62d2017-08-10 15:29:34 -0500172 auto purpose = server::Version::VersionPurpose::BMC;
173 auto path = fs::path(SOFTWARE_OBJPATH) / id;
174
Gunnar Mills88e8a322017-09-13 11:09:28 -0500175 // Create functional association if this is the functional version
176 if (version.compare(functionalVersion) == 0)
177 {
178 createFunctionalAssociation(path);
179 }
180
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500181 AssociationList associations = {};
182
183 if (activationState == server::Activation::Activations::Active)
184 {
185 // Create an association to the BMC inventory item
186 associations.emplace_back(std::make_tuple(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600187 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
188 bmcInventoryPath));
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500189
190 // Create an active association since this image is active
191 createActiveAssociation(path);
192 }
193
Adriana Kobylakee590c72017-09-26 15:16:06 -0500194 // Create Version instance for this version.
195 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600196 bus, path, version, purpose, "",
197 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500198 auto isVersionFunctional = versionPtr->isFunctional();
Michael Tritz4254bec2017-10-03 17:18:22 -0500199 if (!isVersionFunctional)
200 {
Saqib Khanee13e832017-10-23 12:53:11 -0500201 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600202 std::make_unique<phosphor::software::manager::Delete>(
203 bus, path, *versionPtr);
Michael Tritz4254bec2017-10-03 17:18:22 -0500204 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600205 versions.insert(std::make_pair(id, std::move(versionPtr)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500206
Saqib Khanee13e832017-10-23 12:53:11 -0500207 // Create Activation instance for this version.
208 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600209 id, std::make_unique<Activation>(
210 bus, path, *this, id, activationState, associations)));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500211
212 // If Active, create RedundancyPriority instance for this version.
213 if (activationState == server::Activation::Activations::Active)
214 {
215 uint8_t priority = std::numeric_limits<uint8_t>::max();
216 if (!restoreFromFile(id, priority))
217 {
Adriana Kobylakee590c72017-09-26 15:16:06 -0500218 if (isVersionFunctional)
219 {
220 priority = 0;
221 }
222 else
223 {
224 log<level::ERR>("Unable to restore priority from file.",
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600225 entry("VERSIONID=%s", id.c_str()));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500226 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500227 }
228 activations.find(id)->second->redundancyPriority =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600229 std::make_unique<RedundancyPriority>(
230 bus, path, *(activations.find(id)->second), priority,
231 false);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500232 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500233 }
234 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500235
236 // If there is no ubi volume for bmc version then read the /etc/os-release
237 // and create rofs-<versionId> under /media
238 if (activations.size() == 0)
239 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500240 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Saqib Khandcbfa042017-09-18 13:08:39 -0500241 auto id = phosphor::software::manager::Version::getId(version);
242 auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/";
243 try
244 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500245 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500246 {
247 fs::create_directories(versionFileDir);
248 }
249 auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE;
250 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
251 ItemUpdater::processBMCImage();
252 }
253 catch (const std::exception& e)
254 {
255 log<level::ERR>(e.what());
256 }
257 }
Saqib Khanba239882017-05-26 08:41:54 -0500258 return;
259}
260
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500261void ItemUpdater::erase(std::string entryId)
262{
Eddie James6d873712017-09-01 11:29:07 -0500263 // Find entry in versions map
264 auto it = versions.find(entryId);
265 if (it != versions.end())
266 {
267 if (it->second->isFunctional())
268 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600269 log<level::ERR>("Error: Version is currently running on the BMC. "
270 "Unable to remove.",
271 entry("VERSIONID=%s", entryId.c_str()));
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500272 return;
Eddie James6d873712017-09-01 11:29:07 -0500273 }
274
275 // Delete ReadOnly partitions if it's not active
276 removeReadOnlyPartition(entryId);
277 removeFile(entryId);
Saqib Khanee13e832017-10-23 12:53:11 -0500278
279 // Removing entry in versions map
280 this->versions.erase(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500281 }
282 else
283 {
284 // Delete ReadOnly partitions even if we can't find the version
285 removeReadOnlyPartition(entryId);
286 removeFile(entryId);
287
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600288 log<level::ERR>("Error: Failed to find version in item updater "
289 "versions map. Unable to remove.",
290 entry("VERSIONID=%s", entryId.c_str()));
Eddie James6d873712017-09-01 11:29:07 -0500291 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500292
293 // Remove the priority environment variable.
294 auto serviceFile = "obmc-flash-bmc-setenv@" + entryId + ".service";
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600295 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
296 SYSTEMD_INTERFACE, "StartUnit");
Saqib Khan1eef62d2017-08-10 15:29:34 -0500297 method.append(serviceFile, "replace");
298 bus.call_noreply(method);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500299
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500300 // Removing entry in activations map
301 auto ita = activations.find(entryId);
302 if (ita == activations.end())
303 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600304 log<level::ERR>("Error: Failed to find version in item updater "
305 "activations map. Unable to remove.",
306 entry("VERSIONID=%s", entryId.c_str()));
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500307 }
Saqib Khanee13e832017-10-23 12:53:11 -0500308 else
309 {
310 this->activations.erase(entryId);
311 }
Saqib Khan49446ae2017-10-02 10:54:20 -0500312 ItemUpdater::resetUbootEnvVars();
Saqib Khanee13e832017-10-23 12:53:11 -0500313 return;
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500314}
315
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500316void ItemUpdater::deleteAll()
317{
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600318 std::vector<std::string> deletableVersions;
319
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500320 for (const auto& versionIt : versions)
321 {
322 if (!versionIt.second->isFunctional())
323 {
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600324 deletableVersions.push_back(versionIt.first);
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500325 }
326 }
327
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600328 for (const auto& deletableIt : deletableVersions)
329 {
330 ItemUpdater::erase(deletableIt);
331 }
332
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500333 // Remove any volumes that do not match current versions.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600334 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
335 SYSTEMD_INTERFACE, "StartUnit");
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500336 method.append("obmc-flash-bmc-cleanup.service", "replace");
337 bus.call_noreply(method);
338}
339
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600340ItemUpdater::ActivationStatus
341 ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500342{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500343 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500344
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500345 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500346 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500347 fs::path file(filePath);
348 file /= bmcImage;
349 std::ifstream efile(file.c_str());
350 if (efile.good() != 1)
351 {
352 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500353 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500354 invalid = true;
355 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500356 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500357
358 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500359 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500360 return ItemUpdater::ActivationStatus::invalid;
361 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500362
363 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500364}
365
Saqib Khanb9da6632017-09-13 09:48:37 -0500366void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500367{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500368 std::map<std::string, uint8_t> priorityMap;
369
370 // Insert the requested version and priority, it may not exist yet.
371 priorityMap.insert(std::make_pair(versionId, value));
372
Saqib Khan4c1aec02017-07-06 11:46:13 -0500373 for (const auto& intf : activations)
374 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500375 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500376 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500377 priorityMap.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600378 intf.first, intf.second->redundancyPriority.get()->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500379 }
380 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500381
382 // Lambda function to compare 2 priority values, use <= to allow duplicates
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600383 typedef std::function<bool(std::pair<std::string, uint8_t>,
384 std::pair<std::string, uint8_t>)>
385 cmpPriority;
386 cmpPriority cmpPriorityFunc =
387 [](std::pair<std::string, uint8_t> priority1,
388 std::pair<std::string, uint8_t> priority2) {
389 return priority1.second <= priority2.second;
390 };
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500391
392 // Sort versions by ascending priority
393 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600394 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500395
396 auto freePriorityValue = value;
397 for (auto& element : prioritySet)
398 {
399 if (element.first == versionId)
400 {
401 continue;
402 }
403 if (element.second == freePriorityValue)
404 {
405 ++freePriorityValue;
406 auto it = activations.find(element.first);
407 it->second->redundancyPriority.get()->sdbusPriority(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600408 freePriorityValue);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500409 }
410 }
411
412 auto lowestVersion = prioritySet.begin()->first;
413 if (value == prioritySet.begin()->second)
414 {
415 lowestVersion = versionId;
416 }
417 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500418}
419
Michael Tritz37a59042017-07-12 13:44:53 -0500420void ItemUpdater::reset()
421{
422 // Mark the read-write partition for recreation upon reboot.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600423 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
424 SYSTEMD_INTERFACE, "StartUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500425 method.append("obmc-flash-bmc-setenv@rwreset\\x3dtrue.service", "replace");
Michael Tritz37a59042017-07-12 13:44:53 -0500426 bus.call_noreply(method);
427
428 log<level::INFO>("BMC factory reset will take effect upon reboot.");
429
430 return;
431}
432
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500433void ItemUpdater::removeReadOnlyPartition(std::string versionId)
434{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600435 auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId + ".service";
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500436
437 // Remove the read-only partitions.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600438 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
439 SYSTEMD_INTERFACE, "StartUnit");
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500440 method.append(serviceFile, "replace");
441 bus.call_noreply(method);
442}
443
Michael Tritz0129d922017-08-10 19:33:46 -0500444bool ItemUpdater::fieldModeEnabled(bool value)
445{
446 // enabling field mode is intended to be one way: false -> true
447 if (value && !control::FieldMode::fieldModeEnabled())
448 {
449 control::FieldMode::fieldModeEnabled(value);
450
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600451 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
452 SYSTEMD_INTERFACE, "StartUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500453 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
Gunnar Mills9a782242017-08-22 16:23:15 -0500454 "replace");
Michael Tritz0129d922017-08-10 19:33:46 -0500455 bus.call_noreply(method);
456
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600457 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
458 SYSTEMD_INTERFACE, "StopUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500459 method.append("usr-local.mount", "replace");
460 bus.call_noreply(method);
461
462 std::vector<std::string> usrLocal = {"usr-local.mount"};
463
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600464 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
465 SYSTEMD_INTERFACE, "MaskUnitFiles");
Michael Tritz0129d922017-08-10 19:33:46 -0500466 method.append(usrLocal, false, true);
467 bus.call_noreply(method);
468 }
469
470 return control::FieldMode::fieldModeEnabled();
471}
472
473void ItemUpdater::restoreFieldModeStatus()
474{
Michael Tritzff0b4212017-10-24 17:38:09 -0500475 std::ifstream input("/dev/mtd/u-boot-env");
Michael Tritz0129d922017-08-10 19:33:46 -0500476 std::string envVar;
477 std::getline(input, envVar);
478
Gunnar Mills9a782242017-08-22 16:23:15 -0500479 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500480 {
481 ItemUpdater::fieldModeEnabled(true);
482 }
483}
484
Gunnar Millsb60add12017-08-24 16:41:42 -0500485void ItemUpdater::setBMCInventoryPath()
486{
Gunnar Millsb60add12017-08-24 16:41:42 -0500487 auto depth = 0;
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600488 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
489 MAPPER_INTERFACE, "GetSubTreePaths");
Gunnar Millsb60add12017-08-24 16:41:42 -0500490
Adriana Kobylak1254c622017-12-07 12:24:56 -0600491 mapperCall.append(INVENTORY_PATH);
Gunnar Millsb60add12017-08-24 16:41:42 -0500492 mapperCall.append(depth);
Adriana Kobylak1254c622017-12-07 12:24:56 -0600493 std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE};
Gunnar Millsb60add12017-08-24 16:41:42 -0500494 mapperCall.append(filter);
495
496 auto response = bus.call(mapperCall);
497 if (response.is_method_error())
498 {
499 log<level::ERR>("Error in mapper GetSubTreePath");
500 return;
501 }
502
503 using ObjectPaths = std::vector<std::string>;
504 ObjectPaths result;
505 response.read(result);
506
Adriana Kobylak1254c622017-12-07 12:24:56 -0600507 if (!result.empty())
Gunnar Millsb60add12017-08-24 16:41:42 -0500508 {
Adriana Kobylak1254c622017-12-07 12:24:56 -0600509 bmcInventoryPath = result.front();
Gunnar Millsb60add12017-08-24 16:41:42 -0500510 }
511
Adriana Kobylak1254c622017-12-07 12:24:56 -0600512 return;
Gunnar Millsb60add12017-08-24 16:41:42 -0500513}
514
Gunnar Millsf10b2322017-09-21 15:31:55 -0500515void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500516{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600517 assocs.emplace_back(
518 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
Gunnar Millsded875d2017-08-28 16:44:52 -0500519 associations(assocs);
520}
521
Gunnar Mills88e8a322017-09-13 11:09:28 -0500522void ItemUpdater::createFunctionalAssociation(const std::string& path)
523{
524 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600525 FUNCTIONAL_REV_ASSOCIATION, path));
Gunnar Mills88e8a322017-09-13 11:09:28 -0500526 associations(assocs);
527}
528
Gunnar Millsf10b2322017-09-21 15:31:55 -0500529void ItemUpdater::removeActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500530{
531 for (auto iter = assocs.begin(); iter != assocs.end();)
532 {
Gunnar Mills88e8a322017-09-13 11:09:28 -0500533 // Since there could be multiple associations to the same path,
534 // only remove ones that have an active forward association.
535 if ((std::get<0>(*iter)).compare(ACTIVE_FWD_ASSOCIATION) == 0 &&
536 (std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500537 {
538 iter = assocs.erase(iter);
539 associations(assocs);
540 }
541 else
542 {
543 ++iter;
544 }
545 }
546}
547
Saqib Khanb9da6632017-09-13 09:48:37 -0500548bool ItemUpdater::isLowestPriority(uint8_t value)
549{
550 for (const auto& intf : activations)
551 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500552 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500553 {
554 if (intf.second->redundancyPriority.get()->priority() < value)
555 {
556 return false;
557 }
558 }
559 }
560 return true;
561}
562
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500563void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
564{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600565 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
566 SYSTEMD_INTERFACE, "StartUnit");
567 auto updateEnvVarsFile =
568 "obmc-flash-bmc-updateubootvars@" + versionId + ".service";
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500569 method.append(updateEnvVarsFile, "replace");
570 auto result = bus.call(method);
571
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600572 // Check that the bus call didn't result in an error
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500573 if (result.is_method_error())
574 {
575 log<level::ERR>("Failed to update u-boot env variables",
Adriana Kobylak596466b2018-02-13 14:48:53 -0600576 entry("VERSIONID=%s", versionId.c_str()));
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500577 }
578}
579
Saqib Khan49446ae2017-10-02 10:54:20 -0500580void ItemUpdater::resetUbootEnvVars()
581{
582 decltype(activations.begin()->second->redundancyPriority.get()->priority())
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600583 lowestPriority = std::numeric_limits<uint8_t>::max();
Saqib Khan49446ae2017-10-02 10:54:20 -0500584 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
585 for (const auto& intf : activations)
586 {
587 if (!intf.second->redundancyPriority.get())
588 {
589 // Skip this version if the redundancyPriority is not initialized.
590 continue;
591 }
592
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600593 if (intf.second->redundancyPriority.get()->priority() <= lowestPriority)
Saqib Khan49446ae2017-10-02 10:54:20 -0500594 {
595 lowestPriority = intf.second->redundancyPriority.get()->priority();
596 lowestPriorityVersion = intf.second->versionId;
597 }
598 }
599
Saqib Khanf0382c32017-10-24 13:36:22 -0500600 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500601 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500602}
603
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600604void ItemUpdater::freeSpace()
605{
606 // Versions with the highest priority in front
607 std::priority_queue<std::pair<int, std::string>,
608 std::vector<std::pair<int, std::string>>,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600609 std::less<std::pair<int, std::string>>>
610 versionsPQ;
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600611
612 std::size_t count = 0;
613 for (const auto& iter : activations)
614 {
615 if ((iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600616 server::Activation::Activations::Active) ||
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600617 (iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600618 server::Activation::Activations::Failed))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600619 {
620 count++;
621 // Don't put the functional version on the queue since we can't
622 // remove the "running" BMC version.
623 if (versions.find(iter.second->versionId)->second->isFunctional())
624 {
625 continue;
626 }
627 versionsPQ.push(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600628 iter.second->redundancyPriority.get()->priority(),
629 iter.second->versionId));
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600630 }
631 }
632
633 // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1,
634 // remove the highest priority one(s).
635 while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty()))
636 {
637 erase(versionsPQ.top().second);
638 versionsPQ.pop();
639 count--;
640 }
641}
642
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500643} // namespace updater
644} // namespace software
645} // namespace phosphor