blob: 4d07ac93791f63880d0dc67ddbd810b470fe8c21 [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"
Gunnar Millsec1b41c2017-05-02 12:20:36 -050015
16namespace phosphor
17{
18namespace software
19{
20namespace updater
21{
22
Gunnar Mills2ce7da22017-05-04 15:37:56 -050023// When you see server:: you know we're referencing our base class
24namespace server = sdbusplus::xyz::openbmc_project::Software::server;
Michael Tritz0129d922017-08-10 19:33:46 -050025namespace control = sdbusplus::xyz::openbmc_project::Control::server;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050026
27using namespace phosphor::logging;
Saqib Khandcbfa042017-09-18 13:08:39 -050028using namespace sdbusplus::xyz::openbmc_project::Software::Version::Error;
Saqib Khan35e83f32017-05-22 11:37:32 -050029namespace fs = std::experimental::filesystem;
30
Adriana Kobylak2285fe02018-02-27 15:36:59 -060031const std::vector<std::string> bmcImages = {"image-kernel", "image-rofs",
32 "image-rwfs", "image-u-boot"};
Gunnar Mills2ce7da22017-05-04 15:37:56 -050033
Patrick Williamse75d10f2017-05-30 16:56:32 -050034void ItemUpdater::createActivation(sdbusplus::message::message& msg)
Gunnar Millsec1b41c2017-05-02 12:20:36 -050035{
Saqib Khan84a0e692017-06-28 17:27:01 -050036
37 using SVersion = server::Version;
38 using VersionPurpose = SVersion::VersionPurpose;
Gunnar Mills9a782242017-08-22 16:23:15 -050039 using VersionClass = phosphor::software::manager::Version;
Saqib Khan84a0e692017-06-28 17:27:01 -050040 namespace mesg = sdbusplus::message;
41 namespace variant_ns = mesg::variant_ns;
42
43 mesg::object_path objPath;
44 auto purpose = VersionPurpose::Unknown;
Saqib Khan705f1bf2017-06-09 23:58:38 -050045 std::string version;
Adriana Kobylak2285fe02018-02-27 15:36:59 -060046 std::map<std::string, std::map<std::string, mesg::variant<std::string>>>
47 interfaces;
Patrick Williamse75d10f2017-05-30 16:56:32 -050048 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050049 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050050 std::string filePath;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050051
52 for (const auto& intf : interfaces)
53 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050054 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050055 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050056 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050057 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050058 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050059 {
Saqib Khan84a0e692017-06-28 17:27:01 -050060 auto value = SVersion::convertVersionPurposeFromString(
Adriana Kobylak2285fe02018-02-27 15:36:59 -060061 variant_ns::get<std::string>(property.second));
Saqib Khan84a0e692017-06-28 17:27:01 -050062 if (value == VersionPurpose::BMC ||
63 value == VersionPurpose::System)
64 {
65 purpose = value;
66 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050067 }
68 else if (property.first == "Version")
69 {
Saqib Khan84a0e692017-06-28 17:27:01 -050070 version = variant_ns::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050071 }
72 }
73 }
Saqib Khan19177d32017-06-20 08:11:49 -050074 else if (intf.first == FILEPATH_IFACE)
75 {
76 for (const auto& property : intf.second)
77 {
78 if (property.first == "Path")
79 {
Saqib Khan84a0e692017-06-28 17:27:01 -050080 filePath = variant_ns::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050081 }
82 }
83 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050084 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -060085 if (version.empty() || filePath.empty() ||
Saqib Khan84a0e692017-06-28 17:27:01 -050086 purpose == VersionPurpose::Unknown)
Saqib Khan705f1bf2017-06-09 23:58:38 -050087 {
88 return;
89 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050090
91 // Version id is the last item in the path
92 auto pos = path.rfind("/");
93 if (pos == std::string::npos)
94 {
95 log<level::ERR>("No version id found in object path",
Adriana Kobylak596466b2018-02-13 14:48:53 -060096 entry("OBJPATH=%s", path.c_str()));
Patrick Williamse75d10f2017-05-30 16:56:32 -050097 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050098 }
99
100 auto versionId = path.substr(pos + 1);
101
Patrick Williamse75d10f2017-05-30 16:56:32 -0500102 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500103 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500104 // Determine the Activation state by processing the given image dir.
105 auto activationState = server::Activation::Activations::Invalid;
Gunnar Mills9a782242017-08-22 16:23:15 -0500106 ItemUpdater::ActivationStatus result =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600107 ItemUpdater::validateSquashFSImage(filePath);
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500108 AssociationList associations = {};
109
Saqib Khan35e83f32017-05-22 11:37:32 -0500110 if (result == ItemUpdater::ActivationStatus::ready)
111 {
112 activationState = server::Activation::Activations::Ready;
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500113 // Create an association to the BMC inventory item
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600114 associations.emplace_back(
115 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
116 ACTIVATION_REV_ASSOCIATION, bmcInventoryPath));
Saqib Khan35e83f32017-05-22 11:37:32 -0500117 }
Gunnar Millsb60add12017-08-24 16:41:42 -0500118
Saqib Khanee13e832017-10-23 12:53:11 -0500119 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600120 versionId,
121 std::make_unique<Activation>(bus, path, *this, versionId,
122 activationState, associations)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500123
Saqib Khanee13e832017-10-23 12:53:11 -0500124 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600125 bus, path, version, purpose, filePath,
126 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Saqib Khanee13e832017-10-23 12:53:11 -0500127 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600128 std::make_unique<phosphor::software::manager::Delete>(bus, path,
129 *versionPtr);
Saqib Khanee13e832017-10-23 12:53:11 -0500130 versions.insert(std::make_pair(versionId, std::move(versionPtr)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500131 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500132 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500133}
134
Saqib Khanba239882017-05-26 08:41:54 -0500135void ItemUpdater::processBMCImage()
136{
Gunnar Mills88e8a322017-09-13 11:09:28 -0500137 using VersionClass = phosphor::software::manager::Version;
138 // Read os-release from /etc/ to get the functional BMC version
139 auto functionalVersion = VersionClass::getBMCVersion(OS_RELEASE_FILE);
140
Saqib Khan1eef62d2017-08-10 15:29:34 -0500141 // Read os-release from folders under /media/ to get
142 // BMC Software Versions.
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500143 for (const auto& iter : fs::directory_iterator(MEDIA_DIR))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500144 {
145 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500146 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500147
148 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600149 if (0 ==
150 iter.path().native().compare(0, BMC_RO_PREFIX_LEN, BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500151 {
Saqib Khan021c3652017-09-26 12:11:02 -0500152 // The versionId is extracted from the path
153 // for example /media/ro-2a1022fe.
154 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500155 auto osRelease = iter.path() / OS_RELEASE_FILE;
156 if (!fs::is_regular_file(osRelease))
157 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600158 log<level::ERR>(
159 "Failed to read osRelease",
160 entry("FILENAME=%s", osRelease.string().c_str()));
Saqib Khan021c3652017-09-26 12:11:02 -0500161 ItemUpdater::erase(id);
162 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500163 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500164 auto version = VersionClass::getBMCVersion(osRelease);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500165 if (version.empty())
166 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600167 log<level::ERR>(
168 "Failed to read version from osRelease",
169 entry("FILENAME=%s", osRelease.string().c_str()));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500170 activationState = server::Activation::Activations::Invalid;
171 }
Saqib Khan021c3652017-09-26 12:11:02 -0500172
Saqib Khan1eef62d2017-08-10 15:29:34 -0500173 auto purpose = server::Version::VersionPurpose::BMC;
174 auto path = fs::path(SOFTWARE_OBJPATH) / id;
175
Gunnar Mills88e8a322017-09-13 11:09:28 -0500176 // Create functional association if this is the functional version
177 if (version.compare(functionalVersion) == 0)
178 {
179 createFunctionalAssociation(path);
180 }
181
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500182 AssociationList associations = {};
183
184 if (activationState == server::Activation::Activations::Active)
185 {
186 // Create an association to the BMC inventory item
187 associations.emplace_back(std::make_tuple(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600188 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
189 bmcInventoryPath));
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500190
191 // Create an active association since this image is active
192 createActiveAssociation(path);
193 }
194
Adriana Kobylakee590c72017-09-26 15:16:06 -0500195 // Create Version instance for this version.
196 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600197 bus, path, version, purpose, "",
198 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500199 auto isVersionFunctional = versionPtr->isFunctional();
Michael Tritz4254bec2017-10-03 17:18:22 -0500200 if (!isVersionFunctional)
201 {
Saqib Khanee13e832017-10-23 12:53:11 -0500202 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600203 std::make_unique<phosphor::software::manager::Delete>(
204 bus, path, *versionPtr);
Michael Tritz4254bec2017-10-03 17:18:22 -0500205 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600206 versions.insert(std::make_pair(id, std::move(versionPtr)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500207
Saqib Khanee13e832017-10-23 12:53:11 -0500208 // Create Activation instance for this version.
209 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600210 id, std::make_unique<Activation>(
211 bus, path, *this, id, activationState, associations)));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500212
213 // If Active, create RedundancyPriority instance for this version.
214 if (activationState == server::Activation::Activations::Active)
215 {
216 uint8_t priority = std::numeric_limits<uint8_t>::max();
217 if (!restoreFromFile(id, priority))
218 {
Adriana Kobylakee590c72017-09-26 15:16:06 -0500219 if (isVersionFunctional)
220 {
221 priority = 0;
222 }
223 else
224 {
225 log<level::ERR>("Unable to restore priority from file.",
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600226 entry("VERSIONID=%s", id.c_str()));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500227 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500228 }
229 activations.find(id)->second->redundancyPriority =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600230 std::make_unique<RedundancyPriority>(
231 bus, path, *(activations.find(id)->second), priority,
232 false);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500233 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500234 }
235 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500236
237 // If there is no ubi volume for bmc version then read the /etc/os-release
238 // and create rofs-<versionId> under /media
239 if (activations.size() == 0)
240 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500241 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Saqib Khandcbfa042017-09-18 13:08:39 -0500242 auto id = phosphor::software::manager::Version::getId(version);
243 auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/";
244 try
245 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500246 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500247 {
248 fs::create_directories(versionFileDir);
249 }
250 auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE;
251 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
252 ItemUpdater::processBMCImage();
253 }
254 catch (const std::exception& e)
255 {
256 log<level::ERR>(e.what());
257 }
258 }
Saqib Khanba239882017-05-26 08:41:54 -0500259 return;
260}
261
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500262void ItemUpdater::erase(std::string entryId)
263{
Eddie James6d873712017-09-01 11:29:07 -0500264 // Find entry in versions map
265 auto it = versions.find(entryId);
266 if (it != versions.end())
267 {
268 if (it->second->isFunctional())
269 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600270 log<level::ERR>("Error: Version is currently running on the BMC. "
271 "Unable to remove.",
272 entry("VERSIONID=%s", entryId.c_str()));
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500273 return;
Eddie James6d873712017-09-01 11:29:07 -0500274 }
275
276 // Delete ReadOnly partitions if it's not active
277 removeReadOnlyPartition(entryId);
278 removeFile(entryId);
Saqib Khanee13e832017-10-23 12:53:11 -0500279
280 // Removing entry in versions map
281 this->versions.erase(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500282 }
283 else
284 {
285 // Delete ReadOnly partitions even if we can't find the version
286 removeReadOnlyPartition(entryId);
287 removeFile(entryId);
288
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600289 log<level::ERR>("Error: Failed to find version in item updater "
290 "versions map. Unable to remove.",
291 entry("VERSIONID=%s", entryId.c_str()));
Eddie James6d873712017-09-01 11:29:07 -0500292 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500293
294 // Remove the priority environment variable.
295 auto serviceFile = "obmc-flash-bmc-setenv@" + entryId + ".service";
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600296 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
297 SYSTEMD_INTERFACE, "StartUnit");
Saqib Khan1eef62d2017-08-10 15:29:34 -0500298 method.append(serviceFile, "replace");
299 bus.call_noreply(method);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500300
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500301 // Removing entry in activations map
302 auto ita = activations.find(entryId);
303 if (ita == activations.end())
304 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600305 log<level::ERR>("Error: Failed to find version in item updater "
306 "activations map. Unable to remove.",
307 entry("VERSIONID=%s", entryId.c_str()));
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500308 }
Saqib Khanee13e832017-10-23 12:53:11 -0500309 else
310 {
311 this->activations.erase(entryId);
312 }
Saqib Khan49446ae2017-10-02 10:54:20 -0500313 ItemUpdater::resetUbootEnvVars();
Saqib Khanee13e832017-10-23 12:53:11 -0500314 return;
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500315}
316
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500317void ItemUpdater::deleteAll()
318{
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600319 std::vector<std::string> deletableVersions;
320
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500321 for (const auto& versionIt : versions)
322 {
323 if (!versionIt.second->isFunctional())
324 {
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600325 deletableVersions.push_back(versionIt.first);
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500326 }
327 }
328
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600329 for (const auto& deletableIt : deletableVersions)
330 {
331 ItemUpdater::erase(deletableIt);
332 }
333
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500334 // Remove any volumes that do not match current versions.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600335 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
336 SYSTEMD_INTERFACE, "StartUnit");
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500337 method.append("obmc-flash-bmc-cleanup.service", "replace");
338 bus.call_noreply(method);
339}
340
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600341ItemUpdater::ActivationStatus
342 ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500343{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500344 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500345
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500346 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500347 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500348 fs::path file(filePath);
349 file /= bmcImage;
350 std::ifstream efile(file.c_str());
351 if (efile.good() != 1)
352 {
353 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500354 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500355 invalid = true;
356 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500357 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500358
359 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500360 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500361 return ItemUpdater::ActivationStatus::invalid;
362 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500363
364 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500365}
366
Saqib Khanb9da6632017-09-13 09:48:37 -0500367void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500368{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500369 std::map<std::string, uint8_t> priorityMap;
370
371 // Insert the requested version and priority, it may not exist yet.
372 priorityMap.insert(std::make_pair(versionId, value));
373
Saqib Khan4c1aec02017-07-06 11:46:13 -0500374 for (const auto& intf : activations)
375 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500376 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500377 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500378 priorityMap.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600379 intf.first, intf.second->redundancyPriority.get()->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500380 }
381 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500382
383 // Lambda function to compare 2 priority values, use <= to allow duplicates
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600384 typedef std::function<bool(std::pair<std::string, uint8_t>,
385 std::pair<std::string, uint8_t>)>
386 cmpPriority;
387 cmpPriority cmpPriorityFunc =
388 [](std::pair<std::string, uint8_t> priority1,
389 std::pair<std::string, uint8_t> priority2) {
390 return priority1.second <= priority2.second;
391 };
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500392
393 // Sort versions by ascending priority
394 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600395 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500396
397 auto freePriorityValue = value;
398 for (auto& element : prioritySet)
399 {
400 if (element.first == versionId)
401 {
402 continue;
403 }
404 if (element.second == freePriorityValue)
405 {
406 ++freePriorityValue;
407 auto it = activations.find(element.first);
408 it->second->redundancyPriority.get()->sdbusPriority(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600409 freePriorityValue);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500410 }
411 }
412
413 auto lowestVersion = prioritySet.begin()->first;
414 if (value == prioritySet.begin()->second)
415 {
416 lowestVersion = versionId;
417 }
418 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500419}
420
Michael Tritz37a59042017-07-12 13:44:53 -0500421void ItemUpdater::reset()
422{
423 // Mark the read-write partition for recreation upon reboot.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600424 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
425 SYSTEMD_INTERFACE, "StartUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500426 method.append("obmc-flash-bmc-setenv@rwreset\\x3dtrue.service", "replace");
Michael Tritz37a59042017-07-12 13:44:53 -0500427 bus.call_noreply(method);
428
429 log<level::INFO>("BMC factory reset will take effect upon reboot.");
430
431 return;
432}
433
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500434void ItemUpdater::removeReadOnlyPartition(std::string versionId)
435{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600436 auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId + ".service";
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500437
438 // Remove the read-only partitions.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600439 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
440 SYSTEMD_INTERFACE, "StartUnit");
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500441 method.append(serviceFile, "replace");
442 bus.call_noreply(method);
443}
444
Michael Tritz0129d922017-08-10 19:33:46 -0500445bool ItemUpdater::fieldModeEnabled(bool value)
446{
447 // enabling field mode is intended to be one way: false -> true
448 if (value && !control::FieldMode::fieldModeEnabled())
449 {
450 control::FieldMode::fieldModeEnabled(value);
451
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600452 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
453 SYSTEMD_INTERFACE, "StartUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500454 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
Gunnar Mills9a782242017-08-22 16:23:15 -0500455 "replace");
Michael Tritz0129d922017-08-10 19:33:46 -0500456 bus.call_noreply(method);
457
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600458 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
459 SYSTEMD_INTERFACE, "StopUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500460 method.append("usr-local.mount", "replace");
461 bus.call_noreply(method);
462
463 std::vector<std::string> usrLocal = {"usr-local.mount"};
464
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600465 method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
466 SYSTEMD_INTERFACE, "MaskUnitFiles");
Michael Tritz0129d922017-08-10 19:33:46 -0500467 method.append(usrLocal, false, true);
468 bus.call_noreply(method);
469 }
470
471 return control::FieldMode::fieldModeEnabled();
472}
473
474void ItemUpdater::restoreFieldModeStatus()
475{
Michael Tritzff0b4212017-10-24 17:38:09 -0500476 std::ifstream input("/dev/mtd/u-boot-env");
Michael Tritz0129d922017-08-10 19:33:46 -0500477 std::string envVar;
478 std::getline(input, envVar);
479
Gunnar Mills9a782242017-08-22 16:23:15 -0500480 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500481 {
482 ItemUpdater::fieldModeEnabled(true);
483 }
484}
485
Gunnar Millsb60add12017-08-24 16:41:42 -0500486void ItemUpdater::setBMCInventoryPath()
487{
Gunnar Millsb60add12017-08-24 16:41:42 -0500488 auto depth = 0;
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600489 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
490 MAPPER_INTERFACE, "GetSubTreePaths");
Gunnar Millsb60add12017-08-24 16:41:42 -0500491
Adriana Kobylak1254c622017-12-07 12:24:56 -0600492 mapperCall.append(INVENTORY_PATH);
Gunnar Millsb60add12017-08-24 16:41:42 -0500493 mapperCall.append(depth);
Adriana Kobylak1254c622017-12-07 12:24:56 -0600494 std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE};
Gunnar Millsb60add12017-08-24 16:41:42 -0500495 mapperCall.append(filter);
496
497 auto response = bus.call(mapperCall);
498 if (response.is_method_error())
499 {
500 log<level::ERR>("Error in mapper GetSubTreePath");
501 return;
502 }
503
504 using ObjectPaths = std::vector<std::string>;
505 ObjectPaths result;
506 response.read(result);
507
Adriana Kobylak1254c622017-12-07 12:24:56 -0600508 if (!result.empty())
Gunnar Millsb60add12017-08-24 16:41:42 -0500509 {
Adriana Kobylak1254c622017-12-07 12:24:56 -0600510 bmcInventoryPath = result.front();
Gunnar Millsb60add12017-08-24 16:41:42 -0500511 }
512
Adriana Kobylak1254c622017-12-07 12:24:56 -0600513 return;
Gunnar Millsb60add12017-08-24 16:41:42 -0500514}
515
Gunnar Millsf10b2322017-09-21 15:31:55 -0500516void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500517{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600518 assocs.emplace_back(
519 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
Gunnar Millsded875d2017-08-28 16:44:52 -0500520 associations(assocs);
521}
522
Gunnar Mills88e8a322017-09-13 11:09:28 -0500523void ItemUpdater::createFunctionalAssociation(const std::string& path)
524{
525 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600526 FUNCTIONAL_REV_ASSOCIATION, path));
Gunnar Mills88e8a322017-09-13 11:09:28 -0500527 associations(assocs);
528}
529
Gunnar Millsf10b2322017-09-21 15:31:55 -0500530void ItemUpdater::removeActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500531{
532 for (auto iter = assocs.begin(); iter != assocs.end();)
533 {
Gunnar Mills88e8a322017-09-13 11:09:28 -0500534 // Since there could be multiple associations to the same path,
535 // only remove ones that have an active forward association.
536 if ((std::get<0>(*iter)).compare(ACTIVE_FWD_ASSOCIATION) == 0 &&
537 (std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500538 {
539 iter = assocs.erase(iter);
540 associations(assocs);
541 }
542 else
543 {
544 ++iter;
545 }
546 }
547}
548
Saqib Khanb9da6632017-09-13 09:48:37 -0500549bool ItemUpdater::isLowestPriority(uint8_t value)
550{
551 for (const auto& intf : activations)
552 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500553 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500554 {
555 if (intf.second->redundancyPriority.get()->priority() < value)
556 {
557 return false;
558 }
559 }
560 }
561 return true;
562}
563
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500564void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
565{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600566 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
567 SYSTEMD_INTERFACE, "StartUnit");
568 auto updateEnvVarsFile =
569 "obmc-flash-bmc-updateubootvars@" + versionId + ".service";
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500570 method.append(updateEnvVarsFile, "replace");
571 auto result = bus.call(method);
572
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600573 // Check that the bus call didn't result in an error
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500574 if (result.is_method_error())
575 {
576 log<level::ERR>("Failed to update u-boot env variables",
Adriana Kobylak596466b2018-02-13 14:48:53 -0600577 entry("VERSIONID=%s", versionId.c_str()));
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500578 }
579}
580
Saqib Khan49446ae2017-10-02 10:54:20 -0500581void ItemUpdater::resetUbootEnvVars()
582{
583 decltype(activations.begin()->second->redundancyPriority.get()->priority())
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600584 lowestPriority = std::numeric_limits<uint8_t>::max();
Saqib Khan49446ae2017-10-02 10:54:20 -0500585 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
586 for (const auto& intf : activations)
587 {
588 if (!intf.second->redundancyPriority.get())
589 {
590 // Skip this version if the redundancyPriority is not initialized.
591 continue;
592 }
593
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600594 if (intf.second->redundancyPriority.get()->priority() <= lowestPriority)
Saqib Khan49446ae2017-10-02 10:54:20 -0500595 {
596 lowestPriority = intf.second->redundancyPriority.get()->priority();
597 lowestPriorityVersion = intf.second->versionId;
598 }
599 }
600
Saqib Khanf0382c32017-10-24 13:36:22 -0500601 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500602 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500603}
604
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600605void ItemUpdater::freeSpace()
606{
607 // Versions with the highest priority in front
608 std::priority_queue<std::pair<int, std::string>,
609 std::vector<std::pair<int, std::string>>,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600610 std::less<std::pair<int, std::string>>>
611 versionsPQ;
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600612
613 std::size_t count = 0;
614 for (const auto& iter : activations)
615 {
616 if ((iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600617 server::Activation::Activations::Active) ||
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600618 (iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600619 server::Activation::Activations::Failed))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600620 {
621 count++;
622 // Don't put the functional version on the queue since we can't
623 // remove the "running" BMC version.
624 if (versions.find(iter.second->versionId)->second->isFunctional())
625 {
626 continue;
627 }
628 versionsPQ.push(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600629 iter.second->redundancyPriority.get()->priority(),
630 iter.second->versionId));
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600631 }
632 }
633
634 // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1,
635 // remove the highest priority one(s).
636 while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty()))
637 {
638 erase(versionsPQ.top().second);
639 versionsPQ.pop();
640 count--;
641 }
642}
643
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500644} // namespace updater
645} // namespace software
646} // namespace phosphor