blob: 30e6b0aaaf66e70c57661ae57a957b804e68d14a [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
Gunnar Mills9a782242017-08-22 16:23:15 -050031const std::vector<std::string> bmcImages = { "image-kernel",
32 "image-rofs",
33 "image-rwfs",
34 "image-u-boot" };
Gunnar Mills2ce7da22017-05-04 15:37:56 -050035
Patrick Williamse75d10f2017-05-30 16:56:32 -050036void ItemUpdater::createActivation(sdbusplus::message::message& msg)
Gunnar Millsec1b41c2017-05-02 12:20:36 -050037{
Saqib Khan84a0e692017-06-28 17:27:01 -050038
39 using SVersion = server::Version;
40 using VersionPurpose = SVersion::VersionPurpose;
Gunnar Mills9a782242017-08-22 16:23:15 -050041 using VersionClass = phosphor::software::manager::Version;
Saqib Khan84a0e692017-06-28 17:27:01 -050042 namespace mesg = sdbusplus::message;
43 namespace variant_ns = mesg::variant_ns;
44
45 mesg::object_path objPath;
46 auto purpose = VersionPurpose::Unknown;
Saqib Khan705f1bf2017-06-09 23:58:38 -050047 std::string version;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050048 std::map<std::string,
Patrick Williamse75d10f2017-05-30 16:56:32 -050049 std::map<std::string,
Saqib Khan84a0e692017-06-28 17:27:01 -050050 mesg::variant<std::string>>> interfaces;
Patrick Williamse75d10f2017-05-30 16:56:32 -050051 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050052 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050053 std::string filePath;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050054
55 for (const auto& intf : interfaces)
56 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050057 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050058 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050059 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050060 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050061 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050062 {
Saqib Khan84a0e692017-06-28 17:27:01 -050063 auto value = SVersion::convertVersionPurposeFromString(
Gunnar Mills9a782242017-08-22 16:23:15 -050064 variant_ns::get<std::string>(property.second));
Saqib Khan84a0e692017-06-28 17:27:01 -050065 if (value == VersionPurpose::BMC ||
66 value == VersionPurpose::System)
67 {
68 purpose = value;
69 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050070 }
71 else if (property.first == "Version")
72 {
Saqib Khan84a0e692017-06-28 17:27:01 -050073 version = variant_ns::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050074 }
75 }
76 }
Saqib Khan19177d32017-06-20 08:11:49 -050077 else if (intf.first == FILEPATH_IFACE)
78 {
79 for (const auto& property : intf.second)
80 {
81 if (property.first == "Path")
82 {
Saqib Khan84a0e692017-06-28 17:27:01 -050083 filePath = variant_ns::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050084 }
85 }
86 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050087 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050088 if (version.empty() ||
Saqib Khan19177d32017-06-20 08:11:49 -050089 filePath.empty() ||
Saqib Khan84a0e692017-06-28 17:27:01 -050090 purpose == VersionPurpose::Unknown)
Saqib Khan705f1bf2017-06-09 23:58:38 -050091 {
92 return;
93 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050094
95 // Version id is the last item in the path
96 auto pos = path.rfind("/");
97 if (pos == std::string::npos)
98 {
99 log<level::ERR>("No version id found in object path",
Adriana Kobylak596466b2018-02-13 14:48:53 -0600100 entry("OBJPATH=%s", path.c_str()));
Patrick Williamse75d10f2017-05-30 16:56:32 -0500101 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500102 }
103
104 auto versionId = path.substr(pos + 1);
105
Patrick Williamse75d10f2017-05-30 16:56:32 -0500106 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500107 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500108 // Determine the Activation state by processing the given image dir.
109 auto activationState = server::Activation::Activations::Invalid;
Gunnar Mills9a782242017-08-22 16:23:15 -0500110 ItemUpdater::ActivationStatus result =
111 ItemUpdater::validateSquashFSImage(filePath);
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500112 AssociationList associations = {};
113
Saqib Khan35e83f32017-05-22 11:37:32 -0500114 if (result == ItemUpdater::ActivationStatus::ready)
115 {
116 activationState = server::Activation::Activations::Ready;
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500117 // Create an association to the BMC inventory item
118 associations.emplace_back(std::make_tuple(
119 ACTIVATION_FWD_ASSOCIATION,
120 ACTIVATION_REV_ASSOCIATION,
121 bmcInventoryPath));
Saqib Khan35e83f32017-05-22 11:37:32 -0500122 }
Gunnar Millsb60add12017-08-24 16:41:42 -0500123
Saqib Khanee13e832017-10-23 12:53:11 -0500124 activations.insert(std::make_pair(
125 versionId,
126 std::make_unique<Activation>(
127 bus,
128 path,
129 *this,
130 versionId,
131 activationState,
132 associations)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500133
Saqib Khanee13e832017-10-23 12:53:11 -0500134 auto versionPtr = std::make_unique<VersionClass>(
135 bus,
136 path,
137 version,
138 purpose,
139 filePath,
140 std::bind(&ItemUpdater::erase,
141 this,
142 std::placeholders::_1));
143 versionPtr->deleteObject =
144 std::make_unique<phosphor::software::manager::Delete>(
145 bus, path, *versionPtr);
146 versions.insert(std::make_pair(versionId, std::move(versionPtr)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500147 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500148 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500149}
150
Saqib Khanba239882017-05-26 08:41:54 -0500151void ItemUpdater::processBMCImage()
152{
Gunnar Mills88e8a322017-09-13 11:09:28 -0500153 using VersionClass = phosphor::software::manager::Version;
154 // Read os-release from /etc/ to get the functional BMC version
155 auto functionalVersion = VersionClass::getBMCVersion(OS_RELEASE_FILE);
156
Saqib Khan1eef62d2017-08-10 15:29:34 -0500157 // Read os-release from folders under /media/ to get
158 // BMC Software Versions.
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500159 for (const auto& iter : fs::directory_iterator(MEDIA_DIR))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500160 {
161 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500162 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500163
164 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
165 if (0 == iter.path().native().compare(0, BMC_RO_PREFIX_LEN,
Saqib Khan6fab70d2017-09-07 00:13:50 -0500166 BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500167 {
Saqib Khan021c3652017-09-26 12:11:02 -0500168 // The versionId is extracted from the path
169 // for example /media/ro-2a1022fe.
170 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500171 auto osRelease = iter.path() / OS_RELEASE_FILE;
172 if (!fs::is_regular_file(osRelease))
173 {
Gunnar Mills2ad1b552017-10-19 15:58:52 -0500174 log<level::ERR>("Failed to read osRelease",
Adriana Kobylak596466b2018-02-13 14:48:53 -0600175 entry("FILENAME=%s", osRelease.string().c_str()));
Saqib Khan021c3652017-09-26 12:11:02 -0500176 ItemUpdater::erase(id);
177 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500178 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500179 auto version = VersionClass::getBMCVersion(osRelease);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500180 if (version.empty())
181 {
182 log<level::ERR>("Failed to read version from osRelease",
Adriana Kobylak596466b2018-02-13 14:48:53 -0600183 entry("FILENAME=%s", osRelease.string().c_str()));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500184 activationState = server::Activation::Activations::Invalid;
185 }
Saqib Khan021c3652017-09-26 12:11:02 -0500186
Saqib Khan1eef62d2017-08-10 15:29:34 -0500187 auto purpose = server::Version::VersionPurpose::BMC;
188 auto path = fs::path(SOFTWARE_OBJPATH) / id;
189
Gunnar Mills88e8a322017-09-13 11:09:28 -0500190 // Create functional association if this is the functional version
191 if (version.compare(functionalVersion) == 0)
192 {
193 createFunctionalAssociation(path);
194 }
195
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500196 AssociationList associations = {};
197
198 if (activationState == server::Activation::Activations::Active)
199 {
200 // Create an association to the BMC inventory item
201 associations.emplace_back(std::make_tuple(
202 ACTIVATION_FWD_ASSOCIATION,
203 ACTIVATION_REV_ASSOCIATION,
204 bmcInventoryPath));
205
206 // Create an active association since this image is active
207 createActiveAssociation(path);
208 }
209
Adriana Kobylakee590c72017-09-26 15:16:06 -0500210 // Create Version instance for this version.
211 auto versionPtr = std::make_unique<VersionClass>(
Saqib Khanee13e832017-10-23 12:53:11 -0500212 bus,
213 path,
214 version,
215 purpose,
216 "",
217 std::bind(&ItemUpdater::erase,
218 this,
219 std::placeholders::_1));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500220 auto isVersionFunctional = versionPtr->isFunctional();
Michael Tritz4254bec2017-10-03 17:18:22 -0500221 if (!isVersionFunctional)
222 {
Saqib Khanee13e832017-10-23 12:53:11 -0500223 versionPtr->deleteObject =
224 std::make_unique<phosphor::software::manager::Delete>(
225 bus, path, *versionPtr);
Michael Tritz4254bec2017-10-03 17:18:22 -0500226 }
Saqib Khanee13e832017-10-23 12:53:11 -0500227 versions.insert(std::make_pair(
228 id,
229 std::move(versionPtr)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500230
Saqib Khanee13e832017-10-23 12:53:11 -0500231 // Create Activation instance for this version.
232 activations.insert(std::make_pair(
233 id,
234 std::make_unique<Activation>(
235 bus,
236 path,
237 *this,
238 id,
239 activationState,
240 associations)));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500241
242 // If Active, create RedundancyPriority instance for this version.
243 if (activationState == server::Activation::Activations::Active)
244 {
245 uint8_t priority = std::numeric_limits<uint8_t>::max();
246 if (!restoreFromFile(id, priority))
247 {
Adriana Kobylakee590c72017-09-26 15:16:06 -0500248 if (isVersionFunctional)
249 {
250 priority = 0;
251 }
252 else
253 {
254 log<level::ERR>("Unable to restore priority from file.",
Adriana Kobylak596466b2018-02-13 14:48:53 -0600255 entry("VERSIONID=%s", id.c_str()));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500256 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500257 }
258 activations.find(id)->second->redundancyPriority =
259 std::make_unique<RedundancyPriority>(
Saqib Khanba239882017-05-26 08:41:54 -0500260 bus,
261 path,
Saqib Khan1eef62d2017-08-10 15:29:34 -0500262 *(activations.find(id)->second),
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500263 priority,
264 false);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500265 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500266 }
267 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500268
269 // If there is no ubi volume for bmc version then read the /etc/os-release
270 // and create rofs-<versionId> under /media
271 if (activations.size() == 0)
272 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500273 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Saqib Khandcbfa042017-09-18 13:08:39 -0500274 auto id = phosphor::software::manager::Version::getId(version);
275 auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/";
276 try
277 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500278 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500279 {
280 fs::create_directories(versionFileDir);
281 }
282 auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE;
283 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
284 ItemUpdater::processBMCImage();
285 }
286 catch (const std::exception& e)
287 {
288 log<level::ERR>(e.what());
289 }
290 }
Saqib Khanba239882017-05-26 08:41:54 -0500291 return;
292}
293
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500294void ItemUpdater::erase(std::string entryId)
295{
Eddie James6d873712017-09-01 11:29:07 -0500296 // Find entry in versions map
297 auto it = versions.find(entryId);
298 if (it != versions.end())
299 {
300 if (it->second->isFunctional())
301 {
302 log<level::ERR>(("Error: Version " + entryId + \
303 " is currently running on the BMC." \
304 " Unable to remove.").c_str());
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500305 return;
Eddie James6d873712017-09-01 11:29:07 -0500306 }
307
308 // Delete ReadOnly partitions if it's not active
309 removeReadOnlyPartition(entryId);
310 removeFile(entryId);
Saqib Khanee13e832017-10-23 12:53:11 -0500311
312 // Removing entry in versions map
313 this->versions.erase(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500314 }
315 else
316 {
317 // Delete ReadOnly partitions even if we can't find the version
318 removeReadOnlyPartition(entryId);
319 removeFile(entryId);
320
321 log<level::ERR>(("Error: Failed to find version " + entryId + \
322 " in item updater versions map." \
323 " Unable to remove.").c_str());
Eddie James6d873712017-09-01 11:29:07 -0500324 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500325
326 // Remove the priority environment variable.
327 auto serviceFile = "obmc-flash-bmc-setenv@" + entryId + ".service";
328 auto method = bus.new_method_call(
329 SYSTEMD_BUSNAME,
330 SYSTEMD_PATH,
331 SYSTEMD_INTERFACE,
332 "StartUnit");
333 method.append(serviceFile, "replace");
334 bus.call_noreply(method);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500335
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500336 // Removing entry in activations map
337 auto ita = activations.find(entryId);
338 if (ita == activations.end())
339 {
340 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills9a782242017-08-22 16:23:15 -0500341 " in item updater activations map." \
342 " Unable to remove.").c_str());
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500343 }
Saqib Khanee13e832017-10-23 12:53:11 -0500344 else
345 {
346 this->activations.erase(entryId);
347 }
Saqib Khan49446ae2017-10-02 10:54:20 -0500348 ItemUpdater::resetUbootEnvVars();
Saqib Khanee13e832017-10-23 12:53:11 -0500349 return;
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500350}
351
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500352void ItemUpdater::deleteAll()
353{
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600354 std::vector<std::string> deletableVersions;
355
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500356 for (const auto& versionIt : versions)
357 {
358 if (!versionIt.second->isFunctional())
359 {
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600360 deletableVersions.push_back(versionIt.first);
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500361 }
362 }
363
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600364 for (const auto& deletableIt : deletableVersions)
365 {
366 ItemUpdater::erase(deletableIt);
367 }
368
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500369 // Remove any volumes that do not match current versions.
370 auto method = bus.new_method_call(
371 SYSTEMD_BUSNAME,
372 SYSTEMD_PATH,
373 SYSTEMD_INTERFACE,
374 "StartUnit");
375 method.append("obmc-flash-bmc-cleanup.service", "replace");
376 bus.call_noreply(method);
377}
378
Saqib Khan35e83f32017-05-22 11:37:32 -0500379ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
Gunnar Mills9a782242017-08-22 16:23:15 -0500380 const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500381{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500382 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500383
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500384 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500385 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500386 fs::path file(filePath);
387 file /= bmcImage;
388 std::ifstream efile(file.c_str());
389 if (efile.good() != 1)
390 {
391 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500392 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500393 invalid = true;
394 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500395 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500396
397 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500398 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500399 return ItemUpdater::ActivationStatus::invalid;
400 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500401
402 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500403}
404
Saqib Khanb9da6632017-09-13 09:48:37 -0500405void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500406{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500407 std::map<std::string, uint8_t> priorityMap;
408
409 // Insert the requested version and priority, it may not exist yet.
410 priorityMap.insert(std::make_pair(versionId, value));
411
Saqib Khan4c1aec02017-07-06 11:46:13 -0500412 for (const auto& intf : activations)
413 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500414 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500415 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500416 priorityMap.insert(std::make_pair(
417 intf.first,
418 intf.second->redundancyPriority.get()->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500419 }
420 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500421
422 // Lambda function to compare 2 priority values, use <= to allow duplicates
423 typedef std::function<bool(
424 std::pair<std::string, uint8_t>,
425 std::pair<std::string, uint8_t>)> cmpPriority;
426 cmpPriority cmpPriorityFunc = [](
427 std::pair<std::string, uint8_t> priority1,
428 std::pair<std::string, uint8_t> priority2)
429 {
430 return priority1.second <= priority2.second;
431 };
432
433 // Sort versions by ascending priority
434 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
435 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
436
437 auto freePriorityValue = value;
438 for (auto& element : prioritySet)
439 {
440 if (element.first == versionId)
441 {
442 continue;
443 }
444 if (element.second == freePriorityValue)
445 {
446 ++freePriorityValue;
447 auto it = activations.find(element.first);
448 it->second->redundancyPriority.get()->sdbusPriority(
449 freePriorityValue);
450 }
451 }
452
453 auto lowestVersion = prioritySet.begin()->first;
454 if (value == prioritySet.begin()->second)
455 {
456 lowestVersion = versionId;
457 }
458 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500459}
460
Michael Tritz37a59042017-07-12 13:44:53 -0500461void ItemUpdater::reset()
462{
463 // Mark the read-write partition for recreation upon reboot.
464 auto method = bus.new_method_call(
465 SYSTEMD_BUSNAME,
466 SYSTEMD_PATH,
467 SYSTEMD_INTERFACE,
468 "StartUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500469 method.append("obmc-flash-bmc-setenv@rwreset\\x3dtrue.service", "replace");
Michael Tritz37a59042017-07-12 13:44:53 -0500470 bus.call_noreply(method);
471
472 log<level::INFO>("BMC factory reset will take effect upon reboot.");
473
474 return;
475}
476
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500477void ItemUpdater::removeReadOnlyPartition(std::string versionId)
478{
479 auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId +
480 ".service";
481
482 // Remove the read-only partitions.
483 auto method = bus.new_method_call(
484 SYSTEMD_BUSNAME,
485 SYSTEMD_PATH,
486 SYSTEMD_INTERFACE,
487 "StartUnit");
488 method.append(serviceFile, "replace");
489 bus.call_noreply(method);
490}
491
Michael Tritz0129d922017-08-10 19:33:46 -0500492bool ItemUpdater::fieldModeEnabled(bool value)
493{
494 // enabling field mode is intended to be one way: false -> true
495 if (value && !control::FieldMode::fieldModeEnabled())
496 {
497 control::FieldMode::fieldModeEnabled(value);
498
499 auto method = bus.new_method_call(
500 SYSTEMD_BUSNAME,
501 SYSTEMD_PATH,
502 SYSTEMD_INTERFACE,
503 "StartUnit");
504 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
Gunnar Mills9a782242017-08-22 16:23:15 -0500505 "replace");
Michael Tritz0129d922017-08-10 19:33:46 -0500506 bus.call_noreply(method);
507
508 method = bus.new_method_call(
509 SYSTEMD_BUSNAME,
510 SYSTEMD_PATH,
511 SYSTEMD_INTERFACE,
512 "StopUnit");
513 method.append("usr-local.mount", "replace");
514 bus.call_noreply(method);
515
516 std::vector<std::string> usrLocal = {"usr-local.mount"};
517
518 method = bus.new_method_call(
519 SYSTEMD_BUSNAME,
520 SYSTEMD_PATH,
521 SYSTEMD_INTERFACE,
522 "MaskUnitFiles");
523 method.append(usrLocal, false, true);
524 bus.call_noreply(method);
525 }
526
527 return control::FieldMode::fieldModeEnabled();
528}
529
530void ItemUpdater::restoreFieldModeStatus()
531{
Michael Tritzff0b4212017-10-24 17:38:09 -0500532 std::ifstream input("/dev/mtd/u-boot-env");
Michael Tritz0129d922017-08-10 19:33:46 -0500533 std::string envVar;
534 std::getline(input, envVar);
535
Gunnar Mills9a782242017-08-22 16:23:15 -0500536 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500537 {
538 ItemUpdater::fieldModeEnabled(true);
539 }
540}
541
Gunnar Millsb60add12017-08-24 16:41:42 -0500542void ItemUpdater::setBMCInventoryPath()
543{
Gunnar Millsb60add12017-08-24 16:41:42 -0500544 auto depth = 0;
545 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
546 MAPPER_PATH,
547 MAPPER_INTERFACE,
548 "GetSubTreePaths");
549
Adriana Kobylak1254c622017-12-07 12:24:56 -0600550 mapperCall.append(INVENTORY_PATH);
Gunnar Millsb60add12017-08-24 16:41:42 -0500551 mapperCall.append(depth);
Adriana Kobylak1254c622017-12-07 12:24:56 -0600552 std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE};
Gunnar Millsb60add12017-08-24 16:41:42 -0500553 mapperCall.append(filter);
554
555 auto response = bus.call(mapperCall);
556 if (response.is_method_error())
557 {
558 log<level::ERR>("Error in mapper GetSubTreePath");
559 return;
560 }
561
562 using ObjectPaths = std::vector<std::string>;
563 ObjectPaths result;
564 response.read(result);
565
Adriana Kobylak1254c622017-12-07 12:24:56 -0600566 if (!result.empty())
Gunnar Millsb60add12017-08-24 16:41:42 -0500567 {
Adriana Kobylak1254c622017-12-07 12:24:56 -0600568 bmcInventoryPath = result.front();
Gunnar Millsb60add12017-08-24 16:41:42 -0500569 }
570
Adriana Kobylak1254c622017-12-07 12:24:56 -0600571 return;
Gunnar Millsb60add12017-08-24 16:41:42 -0500572}
573
Gunnar Millsf10b2322017-09-21 15:31:55 -0500574void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500575{
576 assocs.emplace_back(std::make_tuple(ACTIVE_FWD_ASSOCIATION,
577 ACTIVE_REV_ASSOCIATION,
578 path));
579 associations(assocs);
580}
581
Gunnar Mills88e8a322017-09-13 11:09:28 -0500582void ItemUpdater::createFunctionalAssociation(const std::string& path)
583{
584 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
585 FUNCTIONAL_REV_ASSOCIATION,
586 path));
587 associations(assocs);
588}
589
Gunnar Millsf10b2322017-09-21 15:31:55 -0500590void ItemUpdater::removeActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500591{
592 for (auto iter = assocs.begin(); iter != assocs.end();)
593 {
Gunnar Mills88e8a322017-09-13 11:09:28 -0500594 // Since there could be multiple associations to the same path,
595 // only remove ones that have an active forward association.
596 if ((std::get<0>(*iter)).compare(ACTIVE_FWD_ASSOCIATION) == 0 &&
597 (std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500598 {
599 iter = assocs.erase(iter);
600 associations(assocs);
601 }
602 else
603 {
604 ++iter;
605 }
606 }
607}
608
Saqib Khanb9da6632017-09-13 09:48:37 -0500609bool ItemUpdater::isLowestPriority(uint8_t value)
610{
611 for (const auto& intf : activations)
612 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500613 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500614 {
615 if (intf.second->redundancyPriority.get()->priority() < value)
616 {
617 return false;
618 }
619 }
620 }
621 return true;
622}
623
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500624void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
625{
626 auto method = bus.new_method_call(
627 SYSTEMD_BUSNAME,
628 SYSTEMD_PATH,
629 SYSTEMD_INTERFACE,
630 "StartUnit");
631 auto updateEnvVarsFile = "obmc-flash-bmc-updateubootvars@" + versionId +
632 ".service";
633 method.append(updateEnvVarsFile, "replace");
634 auto result = bus.call(method);
635
636 //Check that the bus call didn't result in an error
637 if (result.is_method_error())
638 {
639 log<level::ERR>("Failed to update u-boot env variables",
Adriana Kobylak596466b2018-02-13 14:48:53 -0600640 entry("VERSIONID=%s", versionId.c_str()));
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500641 }
642}
643
Saqib Khan49446ae2017-10-02 10:54:20 -0500644void ItemUpdater::resetUbootEnvVars()
645{
646 decltype(activations.begin()->second->redundancyPriority.get()->priority())
647 lowestPriority = std::numeric_limits<uint8_t>::max();
648 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
649 for (const auto& intf : activations)
650 {
651 if (!intf.second->redundancyPriority.get())
652 {
653 // Skip this version if the redundancyPriority is not initialized.
654 continue;
655 }
656
657 if (intf.second->redundancyPriority.get()->priority()
658 <= lowestPriority)
659 {
660 lowestPriority = intf.second->redundancyPriority.get()->priority();
661 lowestPriorityVersion = intf.second->versionId;
662 }
663 }
664
Saqib Khanf0382c32017-10-24 13:36:22 -0500665 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500666 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500667}
668
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600669void ItemUpdater::freeSpace()
670{
671 // Versions with the highest priority in front
672 std::priority_queue<std::pair<int, std::string>,
673 std::vector<std::pair<int, std::string>>,
674 std::less<std::pair<int, std::string>>> versionsPQ;
675
676 std::size_t count = 0;
677 for (const auto& iter : activations)
678 {
679 if ((iter.second.get()->activation() ==
680 server::Activation::Activations::Active) ||
681 (iter.second.get()->activation() ==
682 server::Activation::Activations::Failed))
683 {
684 count++;
685 // Don't put the functional version on the queue since we can't
686 // remove the "running" BMC version.
687 if (versions.find(iter.second->versionId)->second->isFunctional())
688 {
689 continue;
690 }
691 versionsPQ.push(std::make_pair(
692 iter.second->redundancyPriority.get()->priority(),
693 iter.second->versionId));
694 }
695 }
696
697 // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1,
698 // remove the highest priority one(s).
699 while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty()))
700 {
701 erase(versionsPQ.top().second);
702 versionsPQ.pop();
703 count--;
704 }
705}
706
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500707} // namespace updater
708} // namespace software
709} // namespace phosphor