blob: 151986f48dea2d62f2ef8543939a8a2514c360b5 [file] [log] [blame]
Saqib Khan35e83f32017-05-22 11:37:32 -05001#include <fstream>
Adriana Kobylakb77551c2017-10-27 12:46:23 -05002#include <set>
Gunnar Millsec1b41c2017-05-02 12:20:36 -05003#include <string>
Gunnar Mills2ce7da22017-05-04 15:37:56 -05004#include <phosphor-logging/log.hpp>
Saqib Khandcbfa042017-09-18 13:08:39 -05005#include <phosphor-logging/elog.hpp>
6#include <elog-errors.hpp>
7#include <xyz/openbmc_project/Software/Version/error.hpp>
Gunnar Millsec1b41c2017-05-02 12:20:36 -05008#include "config.h"
Gunnar Mills2ce7da22017-05-04 15:37:56 -05009#include "item_updater.hpp"
10#include "xyz/openbmc_project/Software/Version/server.hpp"
Saqib Khan35e83f32017-05-22 11:37:32 -050011#include <experimental/filesystem>
Saqib Khan705f1bf2017-06-09 23:58:38 -050012#include "version.hpp"
Saqib Khan5d532672017-08-09 10:44:50 -050013#include "serialize.hpp"
Gunnar Millsec1b41c2017-05-02 12:20:36 -050014
15namespace phosphor
16{
17namespace software
18{
19namespace updater
20{
21
Gunnar Mills2ce7da22017-05-04 15:37:56 -050022// When you see server:: you know we're referencing our base class
23namespace server = sdbusplus::xyz::openbmc_project::Software::server;
Michael Tritz0129d922017-08-10 19:33:46 -050024namespace control = sdbusplus::xyz::openbmc_project::Control::server;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050025
26using namespace phosphor::logging;
Saqib Khandcbfa042017-09-18 13:08:39 -050027using namespace sdbusplus::xyz::openbmc_project::Software::Version::Error;
Saqib Khan35e83f32017-05-22 11:37:32 -050028namespace fs = std::experimental::filesystem;
29
Gunnar Mills9a782242017-08-22 16:23:15 -050030const std::vector<std::string> bmcImages = { "image-kernel",
31 "image-rofs",
32 "image-rwfs",
33 "image-u-boot" };
Gunnar Mills2ce7da22017-05-04 15:37:56 -050034
Patrick Williamse75d10f2017-05-30 16:56:32 -050035void ItemUpdater::createActivation(sdbusplus::message::message& msg)
Gunnar Millsec1b41c2017-05-02 12:20:36 -050036{
Saqib Khan84a0e692017-06-28 17:27:01 -050037
38 using SVersion = server::Version;
39 using VersionPurpose = SVersion::VersionPurpose;
Gunnar Mills9a782242017-08-22 16:23:15 -050040 using VersionClass = phosphor::software::manager::Version;
Saqib Khan84a0e692017-06-28 17:27:01 -050041 namespace mesg = sdbusplus::message;
42 namespace variant_ns = mesg::variant_ns;
43
44 mesg::object_path objPath;
45 auto purpose = VersionPurpose::Unknown;
Saqib Khan705f1bf2017-06-09 23:58:38 -050046 std::string version;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050047 std::map<std::string,
Patrick Williamse75d10f2017-05-30 16:56:32 -050048 std::map<std::string,
Saqib Khan84a0e692017-06-28 17:27:01 -050049 mesg::variant<std::string>>> interfaces;
Patrick Williamse75d10f2017-05-30 16:56:32 -050050 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050051 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050052 std::string filePath;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050053
54 for (const auto& intf : interfaces)
55 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050056 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050057 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050058 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050059 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050060 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050061 {
Saqib Khan84a0e692017-06-28 17:27:01 -050062 auto value = SVersion::convertVersionPurposeFromString(
Gunnar Mills9a782242017-08-22 16:23:15 -050063 variant_ns::get<std::string>(property.second));
Saqib Khan84a0e692017-06-28 17:27:01 -050064 if (value == VersionPurpose::BMC ||
65 value == VersionPurpose::System)
66 {
67 purpose = value;
68 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050069 }
70 else if (property.first == "Version")
71 {
Saqib Khan84a0e692017-06-28 17:27:01 -050072 version = variant_ns::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050073 }
74 }
75 }
Saqib Khan19177d32017-06-20 08:11:49 -050076 else if (intf.first == FILEPATH_IFACE)
77 {
78 for (const auto& property : intf.second)
79 {
80 if (property.first == "Path")
81 {
Saqib Khan84a0e692017-06-28 17:27:01 -050082 filePath = variant_ns::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050083 }
84 }
85 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050086 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050087 if (version.empty() ||
Saqib Khan19177d32017-06-20 08:11:49 -050088 filePath.empty() ||
Saqib Khan84a0e692017-06-28 17:27:01 -050089 purpose == VersionPurpose::Unknown)
Saqib Khan705f1bf2017-06-09 23:58:38 -050090 {
91 return;
92 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050093
94 // Version id is the last item in the path
95 auto pos = path.rfind("/");
96 if (pos == std::string::npos)
97 {
98 log<level::ERR>("No version id found in object path",
99 entry("OBJPATH=%s", path));
Patrick Williamse75d10f2017-05-30 16:56:32 -0500100 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500101 }
102
103 auto versionId = path.substr(pos + 1);
104
Patrick Williamse75d10f2017-05-30 16:56:32 -0500105 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500106 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500107 // Determine the Activation state by processing the given image dir.
108 auto activationState = server::Activation::Activations::Invalid;
Gunnar Mills9a782242017-08-22 16:23:15 -0500109 ItemUpdater::ActivationStatus result =
110 ItemUpdater::validateSquashFSImage(filePath);
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500111 AssociationList associations = {};
112
Saqib Khan35e83f32017-05-22 11:37:32 -0500113 if (result == ItemUpdater::ActivationStatus::ready)
114 {
115 activationState = server::Activation::Activations::Ready;
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500116 // Create an association to the BMC inventory item
117 associations.emplace_back(std::make_tuple(
118 ACTIVATION_FWD_ASSOCIATION,
119 ACTIVATION_REV_ASSOCIATION,
120 bmcInventoryPath));
Saqib Khan35e83f32017-05-22 11:37:32 -0500121 }
Gunnar Millsb60add12017-08-24 16:41:42 -0500122
Michael Tritz4254bec2017-10-03 17:18:22 -0500123 auto activationPtr = std::make_unique<Activation>(
124 bus,
125 path,
126 *this,
127 versionId,
128 activationState,
129 associations);
130
131 activationPtr->deleteObject =
132 std::make_unique<Delete>(bus, path, *activationPtr);
133
134 activations.insert(std::make_pair(versionId, std::move(activationPtr)));
135
Saqib Khan705f1bf2017-06-09 23:58:38 -0500136 versions.insert(std::make_pair(
137 versionId,
Gunnar Mills9a782242017-08-22 16:23:15 -0500138 std::make_unique<VersionClass>(
Saqib Khan705f1bf2017-06-09 23:58:38 -0500139 bus,
140 path,
141 version,
142 purpose,
Eddie James9440f492017-08-30 11:34:16 -0500143 filePath)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500144 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500145 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500146}
147
Saqib Khanba239882017-05-26 08:41:54 -0500148void ItemUpdater::processBMCImage()
149{
Gunnar Mills88e8a322017-09-13 11:09:28 -0500150 using VersionClass = phosphor::software::manager::Version;
151 // Read os-release from /etc/ to get the functional BMC version
152 auto functionalVersion = VersionClass::getBMCVersion(OS_RELEASE_FILE);
153
Saqib Khan1eef62d2017-08-10 15:29:34 -0500154 // Read os-release from folders under /media/ to get
155 // BMC Software Versions.
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500156 for (const auto& iter : fs::directory_iterator(MEDIA_DIR))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500157 {
158 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500159 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500160
161 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
162 if (0 == iter.path().native().compare(0, BMC_RO_PREFIX_LEN,
Saqib Khan6fab70d2017-09-07 00:13:50 -0500163 BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500164 {
Saqib Khan021c3652017-09-26 12:11:02 -0500165 // The versionId is extracted from the path
166 // for example /media/ro-2a1022fe.
167 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500168 auto osRelease = iter.path() / OS_RELEASE_FILE;
169 if (!fs::is_regular_file(osRelease))
170 {
Gunnar Mills2ad1b552017-10-19 15:58:52 -0500171 log<level::ERR>("Failed to read osRelease",
172 entry("FILENAME=%s", osRelease.string()));
Saqib Khan021c3652017-09-26 12:11:02 -0500173 ItemUpdater::erase(id);
174 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500175 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500176 auto version = VersionClass::getBMCVersion(osRelease);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500177 if (version.empty())
178 {
179 log<level::ERR>("Failed to read version from osRelease",
180 entry("FILENAME=%s", osRelease.string()));
181 activationState = server::Activation::Activations::Invalid;
182 }
Saqib Khan021c3652017-09-26 12:11:02 -0500183
Saqib Khan1eef62d2017-08-10 15:29:34 -0500184 auto purpose = server::Version::VersionPurpose::BMC;
185 auto path = fs::path(SOFTWARE_OBJPATH) / id;
186
Gunnar Mills88e8a322017-09-13 11:09:28 -0500187 // Create functional association if this is the functional version
188 if (version.compare(functionalVersion) == 0)
189 {
190 createFunctionalAssociation(path);
191 }
192
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500193 AssociationList associations = {};
194
195 if (activationState == server::Activation::Activations::Active)
196 {
197 // Create an association to the BMC inventory item
198 associations.emplace_back(std::make_tuple(
199 ACTIVATION_FWD_ASSOCIATION,
200 ACTIVATION_REV_ASSOCIATION,
201 bmcInventoryPath));
202
203 // Create an active association since this image is active
204 createActiveAssociation(path);
205 }
206
Adriana Kobylakee590c72017-09-26 15:16:06 -0500207 // Create Version instance for this version.
208 auto versionPtr = std::make_unique<VersionClass>(
209 bus,
210 path,
211 version,
212 purpose,
213 "");
214 auto isVersionFunctional = versionPtr->isFunctional();
215 versions.insert(std::make_pair(
216 id,
217 std::move(versionPtr)));
218
Saqib Khan1eef62d2017-08-10 15:29:34 -0500219 // Create Activation instance for this version.
Michael Tritz4254bec2017-10-03 17:18:22 -0500220 auto activationPtr = std::make_unique<Activation>(
221 bus,
222 path,
223 *this,
224 id,
225 activationState,
226 associations);
227
228 // Add Delete() if this isn't the functional version
229 if (!isVersionFunctional)
230 {
231 activationPtr->deleteObject =
232 std::make_unique<Delete>(bus, path, *activationPtr);
233 }
234
235 activations.insert(std::make_pair(id, std::move(activationPtr)));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500236
237 // If Active, create RedundancyPriority instance for this version.
238 if (activationState == server::Activation::Activations::Active)
239 {
240 uint8_t priority = std::numeric_limits<uint8_t>::max();
241 if (!restoreFromFile(id, priority))
242 {
Adriana Kobylakee590c72017-09-26 15:16:06 -0500243 if (isVersionFunctional)
244 {
245 priority = 0;
246 }
247 else
248 {
249 log<level::ERR>("Unable to restore priority from file.",
250 entry("VERSIONID=%s", id));
251 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500252 }
253 activations.find(id)->second->redundancyPriority =
254 std::make_unique<RedundancyPriority>(
Saqib Khanba239882017-05-26 08:41:54 -0500255 bus,
256 path,
Saqib Khan1eef62d2017-08-10 15:29:34 -0500257 *(activations.find(id)->second),
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500258 priority,
259 false);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500260 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500261 }
262 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500263
264 // If there is no ubi volume for bmc version then read the /etc/os-release
265 // and create rofs-<versionId> under /media
266 if (activations.size() == 0)
267 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500268 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Saqib Khandcbfa042017-09-18 13:08:39 -0500269 auto id = phosphor::software::manager::Version::getId(version);
270 auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/";
271 try
272 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500273 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500274 {
275 fs::create_directories(versionFileDir);
276 }
277 auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE;
278 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
279 ItemUpdater::processBMCImage();
280 }
281 catch (const std::exception& e)
282 {
283 log<level::ERR>(e.what());
284 }
285 }
Saqib Khanba239882017-05-26 08:41:54 -0500286 return;
287}
288
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500289void ItemUpdater::erase(std::string entryId)
290{
Eddie James6d873712017-09-01 11:29:07 -0500291 // Find entry in versions map
292 auto it = versions.find(entryId);
293 if (it != versions.end())
294 {
295 if (it->second->isFunctional())
296 {
297 log<level::ERR>(("Error: Version " + entryId + \
298 " is currently running on the BMC." \
299 " Unable to remove.").c_str());
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500300 return;
Eddie James6d873712017-09-01 11:29:07 -0500301 }
302
303 // Delete ReadOnly partitions if it's not active
304 removeReadOnlyPartition(entryId);
305 removeFile(entryId);
306 }
307 else
308 {
309 // Delete ReadOnly partitions even if we can't find the version
310 removeReadOnlyPartition(entryId);
311 removeFile(entryId);
312
313 log<level::ERR>(("Error: Failed to find version " + entryId + \
314 " in item updater versions map." \
315 " Unable to remove.").c_str());
316 return;
317 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500318
319 // Remove the priority environment variable.
320 auto serviceFile = "obmc-flash-bmc-setenv@" + entryId + ".service";
321 auto method = bus.new_method_call(
322 SYSTEMD_BUSNAME,
323 SYSTEMD_PATH,
324 SYSTEMD_INTERFACE,
325 "StartUnit");
326 method.append(serviceFile, "replace");
327 bus.call_noreply(method);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500328
329 // Removing entry in versions map
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500330 this->versions.erase(entryId);
331
332 // Removing entry in activations map
333 auto ita = activations.find(entryId);
334 if (ita == activations.end())
335 {
336 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills9a782242017-08-22 16:23:15 -0500337 " in item updater activations map." \
338 " Unable to remove.").c_str());
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500339 return;
340 }
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500341
342 this->activations.erase(entryId);
Saqib Khan49446ae2017-10-02 10:54:20 -0500343 ItemUpdater::resetUbootEnvVars();
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500344}
345
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500346void ItemUpdater::deleteAll()
347{
348 std::vector<std::string> deletableVersions;
349
350 for (const auto& versionIt : versions)
351 {
352 if (!versionIt.second->isFunctional())
353 {
354 deletableVersions.push_back(versionIt.first);
355 }
356 }
357
358 for (const auto& deletableIt : deletableVersions)
359 {
360 ItemUpdater::erase(deletableIt);
361 }
362
363 // Remove any volumes that do not match current versions.
364 auto method = bus.new_method_call(
365 SYSTEMD_BUSNAME,
366 SYSTEMD_PATH,
367 SYSTEMD_INTERFACE,
368 "StartUnit");
369 method.append("obmc-flash-bmc-cleanup.service", "replace");
370 bus.call_noreply(method);
371}
372
Saqib Khan35e83f32017-05-22 11:37:32 -0500373ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
Gunnar Mills9a782242017-08-22 16:23:15 -0500374 const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500375{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500376 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500377
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500378 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500379 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500380 fs::path file(filePath);
381 file /= bmcImage;
382 std::ifstream efile(file.c_str());
383 if (efile.good() != 1)
384 {
385 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500386 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500387 invalid = true;
388 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500389 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500390
391 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500392 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500393 return ItemUpdater::ActivationStatus::invalid;
394 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500395
396 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500397}
398
Saqib Khanb9da6632017-09-13 09:48:37 -0500399void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500400{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500401 std::map<std::string, uint8_t> priorityMap;
402
403 // Insert the requested version and priority, it may not exist yet.
404 priorityMap.insert(std::make_pair(versionId, value));
405
Saqib Khan4c1aec02017-07-06 11:46:13 -0500406 for (const auto& intf : activations)
407 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500408 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500409 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500410 priorityMap.insert(std::make_pair(
411 intf.first,
412 intf.second->redundancyPriority.get()->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500413 }
414 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500415
416 // Lambda function to compare 2 priority values, use <= to allow duplicates
417 typedef std::function<bool(
418 std::pair<std::string, uint8_t>,
419 std::pair<std::string, uint8_t>)> cmpPriority;
420 cmpPriority cmpPriorityFunc = [](
421 std::pair<std::string, uint8_t> priority1,
422 std::pair<std::string, uint8_t> priority2)
423 {
424 return priority1.second <= priority2.second;
425 };
426
427 // Sort versions by ascending priority
428 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
429 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
430
431 auto freePriorityValue = value;
432 for (auto& element : prioritySet)
433 {
434 if (element.first == versionId)
435 {
436 continue;
437 }
438 if (element.second == freePriorityValue)
439 {
440 ++freePriorityValue;
441 auto it = activations.find(element.first);
442 it->second->redundancyPriority.get()->sdbusPriority(
443 freePriorityValue);
444 }
445 }
446
447 auto lowestVersion = prioritySet.begin()->first;
448 if (value == prioritySet.begin()->second)
449 {
450 lowestVersion = versionId;
451 }
452 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500453}
454
Michael Tritz37a59042017-07-12 13:44:53 -0500455void ItemUpdater::reset()
456{
457 // Mark the read-write partition for recreation upon reboot.
458 auto method = bus.new_method_call(
459 SYSTEMD_BUSNAME,
460 SYSTEMD_PATH,
461 SYSTEMD_INTERFACE,
462 "StartUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500463 method.append("obmc-flash-bmc-setenv@rwreset\\x3dtrue.service", "replace");
Michael Tritz37a59042017-07-12 13:44:53 -0500464 bus.call_noreply(method);
465
466 log<level::INFO>("BMC factory reset will take effect upon reboot.");
467
468 return;
469}
470
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500471void ItemUpdater::removeReadOnlyPartition(std::string versionId)
472{
473 auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId +
474 ".service";
475
476 // Remove the read-only partitions.
477 auto method = bus.new_method_call(
478 SYSTEMD_BUSNAME,
479 SYSTEMD_PATH,
480 SYSTEMD_INTERFACE,
481 "StartUnit");
482 method.append(serviceFile, "replace");
483 bus.call_noreply(method);
484}
485
Michael Tritz0129d922017-08-10 19:33:46 -0500486bool ItemUpdater::fieldModeEnabled(bool value)
487{
488 // enabling field mode is intended to be one way: false -> true
489 if (value && !control::FieldMode::fieldModeEnabled())
490 {
491 control::FieldMode::fieldModeEnabled(value);
492
493 auto method = bus.new_method_call(
494 SYSTEMD_BUSNAME,
495 SYSTEMD_PATH,
496 SYSTEMD_INTERFACE,
497 "StartUnit");
498 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
Gunnar Mills9a782242017-08-22 16:23:15 -0500499 "replace");
Michael Tritz0129d922017-08-10 19:33:46 -0500500 bus.call_noreply(method);
501
502 method = bus.new_method_call(
503 SYSTEMD_BUSNAME,
504 SYSTEMD_PATH,
505 SYSTEMD_INTERFACE,
506 "StopUnit");
507 method.append("usr-local.mount", "replace");
508 bus.call_noreply(method);
509
510 std::vector<std::string> usrLocal = {"usr-local.mount"};
511
512 method = bus.new_method_call(
513 SYSTEMD_BUSNAME,
514 SYSTEMD_PATH,
515 SYSTEMD_INTERFACE,
516 "MaskUnitFiles");
517 method.append(usrLocal, false, true);
518 bus.call_noreply(method);
519 }
520
521 return control::FieldMode::fieldModeEnabled();
522}
523
524void ItemUpdater::restoreFieldModeStatus()
525{
Michael Tritzff0b4212017-10-24 17:38:09 -0500526 std::ifstream input("/dev/mtd/u-boot-env");
Michael Tritz0129d922017-08-10 19:33:46 -0500527 std::string envVar;
528 std::getline(input, envVar);
529
Gunnar Mills9a782242017-08-22 16:23:15 -0500530 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500531 {
532 ItemUpdater::fieldModeEnabled(true);
533 }
534}
535
Gunnar Millsb60add12017-08-24 16:41:42 -0500536void ItemUpdater::setBMCInventoryPath()
537{
538 //TODO: openbmc/openbmc#1786 - Get the BMC path by looking for objects
539 // that implement the BMC inventory interface
540 auto depth = 0;
541 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
542 MAPPER_PATH,
543 MAPPER_INTERFACE,
544 "GetSubTreePaths");
545
546 mapperCall.append(CHASSIS_INVENTORY_PATH);
547 mapperCall.append(depth);
548
549 // TODO: openbmc/openbmc#2226 - Add Inventory Item filter when
550 // mapper is fixed.
551 std::vector<std::string> filter = {};
552 mapperCall.append(filter);
553
554 auto response = bus.call(mapperCall);
555 if (response.is_method_error())
556 {
557 log<level::ERR>("Error in mapper GetSubTreePath");
558 return;
559 }
560
561 using ObjectPaths = std::vector<std::string>;
562 ObjectPaths result;
563 response.read(result);
564
565 if (result.empty())
566 {
567 log<level::ERR>("Invalid response from mapper");
568 return;
569 }
570
571 for (auto& iter : result)
572 {
573 const auto& path = iter;
574 if (path.substr(path.find_last_of('/') + 1).compare("bmc") == 0)
575 {
576 bmcInventoryPath = path;
577 return;
578 }
579 }
580}
581
Gunnar Millsf10b2322017-09-21 15:31:55 -0500582void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500583{
584 assocs.emplace_back(std::make_tuple(ACTIVE_FWD_ASSOCIATION,
585 ACTIVE_REV_ASSOCIATION,
586 path));
587 associations(assocs);
588}
589
Gunnar Mills88e8a322017-09-13 11:09:28 -0500590void ItemUpdater::createFunctionalAssociation(const std::string& path)
591{
592 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
593 FUNCTIONAL_REV_ASSOCIATION,
594 path));
595 associations(assocs);
596}
597
Gunnar Millsf10b2322017-09-21 15:31:55 -0500598void ItemUpdater::removeActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500599{
600 for (auto iter = assocs.begin(); iter != assocs.end();)
601 {
Gunnar Mills88e8a322017-09-13 11:09:28 -0500602 // Since there could be multiple associations to the same path,
603 // only remove ones that have an active forward association.
604 if ((std::get<0>(*iter)).compare(ACTIVE_FWD_ASSOCIATION) == 0 &&
605 (std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500606 {
607 iter = assocs.erase(iter);
608 associations(assocs);
609 }
610 else
611 {
612 ++iter;
613 }
614 }
615}
616
Saqib Khanb9da6632017-09-13 09:48:37 -0500617bool ItemUpdater::isLowestPriority(uint8_t value)
618{
619 for (const auto& intf : activations)
620 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500621 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500622 {
623 if (intf.second->redundancyPriority.get()->priority() < value)
624 {
625 return false;
626 }
627 }
628 }
629 return true;
630}
631
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500632void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
633{
634 auto method = bus.new_method_call(
635 SYSTEMD_BUSNAME,
636 SYSTEMD_PATH,
637 SYSTEMD_INTERFACE,
638 "StartUnit");
639 auto updateEnvVarsFile = "obmc-flash-bmc-updateubootvars@" + versionId +
640 ".service";
641 method.append(updateEnvVarsFile, "replace");
642 auto result = bus.call(method);
643
644 //Check that the bus call didn't result in an error
645 if (result.is_method_error())
646 {
647 log<level::ERR>("Failed to update u-boot env variables",
648 entry("VERSIONID=%s", versionId));
649 }
650}
651
Saqib Khan49446ae2017-10-02 10:54:20 -0500652void ItemUpdater::resetUbootEnvVars()
653{
654 decltype(activations.begin()->second->redundancyPriority.get()->priority())
655 lowestPriority = std::numeric_limits<uint8_t>::max();
656 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
657 for (const auto& intf : activations)
658 {
659 if (!intf.second->redundancyPriority.get())
660 {
661 // Skip this version if the redundancyPriority is not initialized.
662 continue;
663 }
664
665 if (intf.second->redundancyPriority.get()->priority()
666 <= lowestPriority)
667 {
668 lowestPriority = intf.second->redundancyPriority.get()->priority();
669 lowestPriorityVersion = intf.second->versionId;
670 }
671 }
672
Saqib Khanf0382c32017-10-24 13:36:22 -0500673 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500674 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500675}
676
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500677} // namespace updater
678} // namespace software
679} // namespace phosphor