blob: 1596a4b250b346bfd2b78ecc8cbe88083cc0564d [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{
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500354 for (const auto& versionIt : versions)
355 {
356 if (!versionIt.second->isFunctional())
357 {
Saqib Khanee13e832017-10-23 12:53:11 -0500358 ItemUpdater::erase(versionIt.first);
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500359 }
360 }
361
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500362 // Remove any volumes that do not match current versions.
363 auto method = bus.new_method_call(
364 SYSTEMD_BUSNAME,
365 SYSTEMD_PATH,
366 SYSTEMD_INTERFACE,
367 "StartUnit");
368 method.append("obmc-flash-bmc-cleanup.service", "replace");
369 bus.call_noreply(method);
370}
371
Saqib Khan35e83f32017-05-22 11:37:32 -0500372ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
Gunnar Mills9a782242017-08-22 16:23:15 -0500373 const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500374{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500375 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500376
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500377 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500378 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500379 fs::path file(filePath);
380 file /= bmcImage;
381 std::ifstream efile(file.c_str());
382 if (efile.good() != 1)
383 {
384 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500385 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500386 invalid = true;
387 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500388 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500389
390 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500391 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500392 return ItemUpdater::ActivationStatus::invalid;
393 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500394
395 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500396}
397
Saqib Khanb9da6632017-09-13 09:48:37 -0500398void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500399{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500400 std::map<std::string, uint8_t> priorityMap;
401
402 // Insert the requested version and priority, it may not exist yet.
403 priorityMap.insert(std::make_pair(versionId, value));
404
Saqib Khan4c1aec02017-07-06 11:46:13 -0500405 for (const auto& intf : activations)
406 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500407 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500408 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500409 priorityMap.insert(std::make_pair(
410 intf.first,
411 intf.second->redundancyPriority.get()->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500412 }
413 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500414
415 // Lambda function to compare 2 priority values, use <= to allow duplicates
416 typedef std::function<bool(
417 std::pair<std::string, uint8_t>,
418 std::pair<std::string, uint8_t>)> cmpPriority;
419 cmpPriority cmpPriorityFunc = [](
420 std::pair<std::string, uint8_t> priority1,
421 std::pair<std::string, uint8_t> priority2)
422 {
423 return priority1.second <= priority2.second;
424 };
425
426 // Sort versions by ascending priority
427 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
428 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
429
430 auto freePriorityValue = value;
431 for (auto& element : prioritySet)
432 {
433 if (element.first == versionId)
434 {
435 continue;
436 }
437 if (element.second == freePriorityValue)
438 {
439 ++freePriorityValue;
440 auto it = activations.find(element.first);
441 it->second->redundancyPriority.get()->sdbusPriority(
442 freePriorityValue);
443 }
444 }
445
446 auto lowestVersion = prioritySet.begin()->first;
447 if (value == prioritySet.begin()->second)
448 {
449 lowestVersion = versionId;
450 }
451 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500452}
453
Michael Tritz37a59042017-07-12 13:44:53 -0500454void ItemUpdater::reset()
455{
456 // Mark the read-write partition for recreation upon reboot.
457 auto method = bus.new_method_call(
458 SYSTEMD_BUSNAME,
459 SYSTEMD_PATH,
460 SYSTEMD_INTERFACE,
461 "StartUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500462 method.append("obmc-flash-bmc-setenv@rwreset\\x3dtrue.service", "replace");
Michael Tritz37a59042017-07-12 13:44:53 -0500463 bus.call_noreply(method);
464
465 log<level::INFO>("BMC factory reset will take effect upon reboot.");
466
467 return;
468}
469
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500470void ItemUpdater::removeReadOnlyPartition(std::string versionId)
471{
472 auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId +
473 ".service";
474
475 // Remove the read-only partitions.
476 auto method = bus.new_method_call(
477 SYSTEMD_BUSNAME,
478 SYSTEMD_PATH,
479 SYSTEMD_INTERFACE,
480 "StartUnit");
481 method.append(serviceFile, "replace");
482 bus.call_noreply(method);
483}
484
Michael Tritz0129d922017-08-10 19:33:46 -0500485bool ItemUpdater::fieldModeEnabled(bool value)
486{
487 // enabling field mode is intended to be one way: false -> true
488 if (value && !control::FieldMode::fieldModeEnabled())
489 {
490 control::FieldMode::fieldModeEnabled(value);
491
492 auto method = bus.new_method_call(
493 SYSTEMD_BUSNAME,
494 SYSTEMD_PATH,
495 SYSTEMD_INTERFACE,
496 "StartUnit");
497 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
Gunnar Mills9a782242017-08-22 16:23:15 -0500498 "replace");
Michael Tritz0129d922017-08-10 19:33:46 -0500499 bus.call_noreply(method);
500
501 method = bus.new_method_call(
502 SYSTEMD_BUSNAME,
503 SYSTEMD_PATH,
504 SYSTEMD_INTERFACE,
505 "StopUnit");
506 method.append("usr-local.mount", "replace");
507 bus.call_noreply(method);
508
509 std::vector<std::string> usrLocal = {"usr-local.mount"};
510
511 method = bus.new_method_call(
512 SYSTEMD_BUSNAME,
513 SYSTEMD_PATH,
514 SYSTEMD_INTERFACE,
515 "MaskUnitFiles");
516 method.append(usrLocal, false, true);
517 bus.call_noreply(method);
518 }
519
520 return control::FieldMode::fieldModeEnabled();
521}
522
523void ItemUpdater::restoreFieldModeStatus()
524{
Michael Tritzff0b4212017-10-24 17:38:09 -0500525 std::ifstream input("/dev/mtd/u-boot-env");
Michael Tritz0129d922017-08-10 19:33:46 -0500526 std::string envVar;
527 std::getline(input, envVar);
528
Gunnar Mills9a782242017-08-22 16:23:15 -0500529 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500530 {
531 ItemUpdater::fieldModeEnabled(true);
532 }
533}
534
Gunnar Millsb60add12017-08-24 16:41:42 -0500535void ItemUpdater::setBMCInventoryPath()
536{
Gunnar Millsb60add12017-08-24 16:41:42 -0500537 auto depth = 0;
538 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
539 MAPPER_PATH,
540 MAPPER_INTERFACE,
541 "GetSubTreePaths");
542
Adriana Kobylak1254c622017-12-07 12:24:56 -0600543 mapperCall.append(INVENTORY_PATH);
Gunnar Millsb60add12017-08-24 16:41:42 -0500544 mapperCall.append(depth);
Adriana Kobylak1254c622017-12-07 12:24:56 -0600545 std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE};
Gunnar Millsb60add12017-08-24 16:41:42 -0500546 mapperCall.append(filter);
547
548 auto response = bus.call(mapperCall);
549 if (response.is_method_error())
550 {
551 log<level::ERR>("Error in mapper GetSubTreePath");
552 return;
553 }
554
555 using ObjectPaths = std::vector<std::string>;
556 ObjectPaths result;
557 response.read(result);
558
Adriana Kobylak1254c622017-12-07 12:24:56 -0600559 if (!result.empty())
Gunnar Millsb60add12017-08-24 16:41:42 -0500560 {
Adriana Kobylak1254c622017-12-07 12:24:56 -0600561 bmcInventoryPath = result.front();
Gunnar Millsb60add12017-08-24 16:41:42 -0500562 }
563
Adriana Kobylak1254c622017-12-07 12:24:56 -0600564 return;
Gunnar Millsb60add12017-08-24 16:41:42 -0500565}
566
Gunnar Millsf10b2322017-09-21 15:31:55 -0500567void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500568{
569 assocs.emplace_back(std::make_tuple(ACTIVE_FWD_ASSOCIATION,
570 ACTIVE_REV_ASSOCIATION,
571 path));
572 associations(assocs);
573}
574
Gunnar Mills88e8a322017-09-13 11:09:28 -0500575void ItemUpdater::createFunctionalAssociation(const std::string& path)
576{
577 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
578 FUNCTIONAL_REV_ASSOCIATION,
579 path));
580 associations(assocs);
581}
582
Gunnar Millsf10b2322017-09-21 15:31:55 -0500583void ItemUpdater::removeActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500584{
585 for (auto iter = assocs.begin(); iter != assocs.end();)
586 {
Gunnar Mills88e8a322017-09-13 11:09:28 -0500587 // Since there could be multiple associations to the same path,
588 // only remove ones that have an active forward association.
589 if ((std::get<0>(*iter)).compare(ACTIVE_FWD_ASSOCIATION) == 0 &&
590 (std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500591 {
592 iter = assocs.erase(iter);
593 associations(assocs);
594 }
595 else
596 {
597 ++iter;
598 }
599 }
600}
601
Saqib Khanb9da6632017-09-13 09:48:37 -0500602bool ItemUpdater::isLowestPriority(uint8_t value)
603{
604 for (const auto& intf : activations)
605 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500606 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500607 {
608 if (intf.second->redundancyPriority.get()->priority() < value)
609 {
610 return false;
611 }
612 }
613 }
614 return true;
615}
616
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500617void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
618{
619 auto method = bus.new_method_call(
620 SYSTEMD_BUSNAME,
621 SYSTEMD_PATH,
622 SYSTEMD_INTERFACE,
623 "StartUnit");
624 auto updateEnvVarsFile = "obmc-flash-bmc-updateubootvars@" + versionId +
625 ".service";
626 method.append(updateEnvVarsFile, "replace");
627 auto result = bus.call(method);
628
629 //Check that the bus call didn't result in an error
630 if (result.is_method_error())
631 {
632 log<level::ERR>("Failed to update u-boot env variables",
Adriana Kobylak596466b2018-02-13 14:48:53 -0600633 entry("VERSIONID=%s", versionId.c_str()));
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500634 }
635}
636
Saqib Khan49446ae2017-10-02 10:54:20 -0500637void ItemUpdater::resetUbootEnvVars()
638{
639 decltype(activations.begin()->second->redundancyPriority.get()->priority())
640 lowestPriority = std::numeric_limits<uint8_t>::max();
641 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
642 for (const auto& intf : activations)
643 {
644 if (!intf.second->redundancyPriority.get())
645 {
646 // Skip this version if the redundancyPriority is not initialized.
647 continue;
648 }
649
650 if (intf.second->redundancyPriority.get()->priority()
651 <= lowestPriority)
652 {
653 lowestPriority = intf.second->redundancyPriority.get()->priority();
654 lowestPriorityVersion = intf.second->versionId;
655 }
656 }
657
Saqib Khanf0382c32017-10-24 13:36:22 -0500658 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500659 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500660}
661
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600662void ItemUpdater::freeSpace()
663{
664 // Versions with the highest priority in front
665 std::priority_queue<std::pair<int, std::string>,
666 std::vector<std::pair<int, std::string>>,
667 std::less<std::pair<int, std::string>>> versionsPQ;
668
669 std::size_t count = 0;
670 for (const auto& iter : activations)
671 {
672 if ((iter.second.get()->activation() ==
673 server::Activation::Activations::Active) ||
674 (iter.second.get()->activation() ==
675 server::Activation::Activations::Failed))
676 {
677 count++;
678 // Don't put the functional version on the queue since we can't
679 // remove the "running" BMC version.
680 if (versions.find(iter.second->versionId)->second->isFunctional())
681 {
682 continue;
683 }
684 versionsPQ.push(std::make_pair(
685 iter.second->redundancyPriority.get()->priority(),
686 iter.second->versionId));
687 }
688 }
689
690 // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1,
691 // remove the highest priority one(s).
692 while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty()))
693 {
694 erase(versionsPQ.top().second);
695 versionsPQ.pop();
696 count--;
697 }
698}
699
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500700} // namespace updater
701} // namespace software
702} // namespace phosphor