blob: e2de36ffe044e89c69ae8c13437f1124a57a6e65 [file] [log] [blame]
Saqib Khan35e83f32017-05-22 11:37:32 -05001#include <fstream>
Gunnar Millsec1b41c2017-05-02 12:20:36 -05002#include <string>
Gunnar Mills2ce7da22017-05-04 15:37:56 -05003#include <phosphor-logging/log.hpp>
Saqib Khandcbfa042017-09-18 13:08:39 -05004#include <phosphor-logging/elog.hpp>
5#include <elog-errors.hpp>
6#include <xyz/openbmc_project/Software/Version/error.hpp>
Gunnar Millsec1b41c2017-05-02 12:20:36 -05007#include "config.h"
Gunnar Mills2ce7da22017-05-04 15:37:56 -05008#include "item_updater.hpp"
9#include "xyz/openbmc_project/Software/Version/server.hpp"
Saqib Khan35e83f32017-05-22 11:37:32 -050010#include <experimental/filesystem>
Saqib Khan705f1bf2017-06-09 23:58:38 -050011#include "version.hpp"
Saqib Khan5d532672017-08-09 10:44:50 -050012#include "serialize.hpp"
Gunnar Millsec1b41c2017-05-02 12:20:36 -050013
14namespace phosphor
15{
16namespace software
17{
18namespace updater
19{
20
Gunnar Mills2ce7da22017-05-04 15:37:56 -050021// When you see server:: you know we're referencing our base class
22namespace server = sdbusplus::xyz::openbmc_project::Software::server;
Michael Tritz0129d922017-08-10 19:33:46 -050023namespace control = sdbusplus::xyz::openbmc_project::Control::server;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050024
25using namespace phosphor::logging;
Saqib Khandcbfa042017-09-18 13:08:39 -050026using namespace sdbusplus::xyz::openbmc_project::Software::Version::Error;
Saqib Khan35e83f32017-05-22 11:37:32 -050027namespace fs = std::experimental::filesystem;
28
Gunnar Mills9a782242017-08-22 16:23:15 -050029const std::vector<std::string> bmcImages = { "image-kernel",
30 "image-rofs",
31 "image-rwfs",
32 "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;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050046 std::map<std::string,
Patrick Williamse75d10f2017-05-30 16:56:32 -050047 std::map<std::string,
Saqib Khan84a0e692017-06-28 17:27:01 -050048 mesg::variant<std::string>>> interfaces;
Patrick Williamse75d10f2017-05-30 16:56:32 -050049 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050050 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050051 std::string filePath;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050052
53 for (const auto& intf : interfaces)
54 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050055 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050056 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050057 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050058 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050059 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050060 {
Saqib Khan84a0e692017-06-28 17:27:01 -050061 auto value = SVersion::convertVersionPurposeFromString(
Gunnar Mills9a782242017-08-22 16:23:15 -050062 variant_ns::get<std::string>(property.second));
Saqib Khan84a0e692017-06-28 17:27:01 -050063 if (value == VersionPurpose::BMC ||
64 value == VersionPurpose::System)
65 {
66 purpose = value;
67 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050068 }
69 else if (property.first == "Version")
70 {
Saqib Khan84a0e692017-06-28 17:27:01 -050071 version = variant_ns::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050072 }
73 }
74 }
Saqib Khan19177d32017-06-20 08:11:49 -050075 else if (intf.first == FILEPATH_IFACE)
76 {
77 for (const auto& property : intf.second)
78 {
79 if (property.first == "Path")
80 {
Saqib Khan84a0e692017-06-28 17:27:01 -050081 filePath = variant_ns::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050082 }
83 }
84 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050085 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050086 if (version.empty() ||
Saqib Khan19177d32017-06-20 08:11:49 -050087 filePath.empty() ||
Saqib Khan84a0e692017-06-28 17:27:01 -050088 purpose == VersionPurpose::Unknown)
Saqib Khan705f1bf2017-06-09 23:58:38 -050089 {
90 return;
91 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050092
93 // Version id is the last item in the path
94 auto pos = path.rfind("/");
95 if (pos == std::string::npos)
96 {
97 log<level::ERR>("No version id found in object path",
98 entry("OBJPATH=%s", path));
Patrick Williamse75d10f2017-05-30 16:56:32 -050099 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500100 }
101
102 auto versionId = path.substr(pos + 1);
103
Patrick Williamse75d10f2017-05-30 16:56:32 -0500104 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500105 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500106 // Determine the Activation state by processing the given image dir.
107 auto activationState = server::Activation::Activations::Invalid;
Gunnar Mills9a782242017-08-22 16:23:15 -0500108 ItemUpdater::ActivationStatus result =
109 ItemUpdater::validateSquashFSImage(filePath);
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500110 AssociationList associations = {};
111
Saqib Khan35e83f32017-05-22 11:37:32 -0500112 if (result == ItemUpdater::ActivationStatus::ready)
113 {
114 activationState = server::Activation::Activations::Ready;
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500115 // Create an association to the BMC inventory item
116 associations.emplace_back(std::make_tuple(
117 ACTIVATION_FWD_ASSOCIATION,
118 ACTIVATION_REV_ASSOCIATION,
119 bmcInventoryPath));
Saqib Khan35e83f32017-05-22 11:37:32 -0500120 }
Gunnar Millsb60add12017-08-24 16:41:42 -0500121
Saqib Khan35e83f32017-05-22 11:37:32 -0500122 activations.insert(std::make_pair(
Saqib Khan705f1bf2017-06-09 23:58:38 -0500123 versionId,
124 std::make_unique<Activation>(
125 bus,
126 path,
Saqib Khan4c1aec02017-07-06 11:46:13 -0500127 *this,
Saqib Khan35e83f32017-05-22 11:37:32 -0500128 versionId,
Gunnar Millsb60add12017-08-24 16:41:42 -0500129 activationState,
130 associations)));
Saqib Khan705f1bf2017-06-09 23:58:38 -0500131 versions.insert(std::make_pair(
132 versionId,
Gunnar Mills9a782242017-08-22 16:23:15 -0500133 std::make_unique<VersionClass>(
Saqib Khan705f1bf2017-06-09 23:58:38 -0500134 bus,
135 path,
136 version,
137 purpose,
Eddie James9440f492017-08-30 11:34:16 -0500138 filePath)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500139 }
Saqib Khan7b5010f2017-08-09 10:03:11 -0500140 else
141 {
142 log<level::INFO>("Software Object with the same version already exists",
Gunnar Mills9a782242017-08-22 16:23:15 -0500143 entry("VERSION_ID=%s", versionId));
Saqib Khan7b5010f2017-08-09 10:03:11 -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 {
171 log<level::ERR>("Failed to read osRelease\n",
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.
220 activations.insert(std::make_pair(
221 id,
222 std::make_unique<Activation>(
223 bus,
224 path,
225 *this,
226 id,
227 server::Activation::Activations::Active,
228 associations)));
229
230 // If Active, create RedundancyPriority instance for this version.
231 if (activationState == server::Activation::Activations::Active)
232 {
233 uint8_t priority = std::numeric_limits<uint8_t>::max();
234 if (!restoreFromFile(id, priority))
235 {
Adriana Kobylakee590c72017-09-26 15:16:06 -0500236 if (isVersionFunctional)
237 {
238 priority = 0;
239 }
240 else
241 {
242 log<level::ERR>("Unable to restore priority from file.",
243 entry("VERSIONID=%s", id));
244 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500245 }
246 activations.find(id)->second->redundancyPriority =
247 std::make_unique<RedundancyPriority>(
Saqib Khanba239882017-05-26 08:41:54 -0500248 bus,
249 path,
Saqib Khan1eef62d2017-08-10 15:29:34 -0500250 *(activations.find(id)->second),
251 priority);
252 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500253 }
254 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500255
256 // If there is no ubi volume for bmc version then read the /etc/os-release
257 // and create rofs-<versionId> under /media
258 if (activations.size() == 0)
259 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500260 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Saqib Khandcbfa042017-09-18 13:08:39 -0500261 auto id = phosphor::software::manager::Version::getId(version);
262 auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/";
263 try
264 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500265 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500266 {
267 fs::create_directories(versionFileDir);
268 }
269 auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE;
270 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
271 ItemUpdater::processBMCImage();
272 }
273 catch (const std::exception& e)
274 {
275 log<level::ERR>(e.what());
276 }
277 }
Saqib Khanba239882017-05-26 08:41:54 -0500278 return;
279}
280
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500281void ItemUpdater::erase(std::string entryId)
282{
Eddie James6d873712017-09-01 11:29:07 -0500283 // Find entry in versions map
284 auto it = versions.find(entryId);
285 if (it != versions.end())
286 {
287 if (it->second->isFunctional())
288 {
289 log<level::ERR>(("Error: Version " + entryId + \
290 " is currently running on the BMC." \
291 " Unable to remove.").c_str());
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500292 return;
Eddie James6d873712017-09-01 11:29:07 -0500293 }
294
295 // Delete ReadOnly partitions if it's not active
296 removeReadOnlyPartition(entryId);
297 removeFile(entryId);
298 }
299 else
300 {
301 // Delete ReadOnly partitions even if we can't find the version
302 removeReadOnlyPartition(entryId);
303 removeFile(entryId);
304
305 log<level::ERR>(("Error: Failed to find version " + entryId + \
306 " in item updater versions map." \
307 " Unable to remove.").c_str());
308 return;
309 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500310
311 // Remove the priority environment variable.
312 auto serviceFile = "obmc-flash-bmc-setenv@" + entryId + ".service";
313 auto method = bus.new_method_call(
314 SYSTEMD_BUSNAME,
315 SYSTEMD_PATH,
316 SYSTEMD_INTERFACE,
317 "StartUnit");
318 method.append(serviceFile, "replace");
319 bus.call_noreply(method);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500320
321 // Removing entry in versions map
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500322 this->versions.erase(entryId);
323
324 // Removing entry in activations map
325 auto ita = activations.find(entryId);
326 if (ita == activations.end())
327 {
328 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills9a782242017-08-22 16:23:15 -0500329 " in item updater activations map." \
330 " Unable to remove.").c_str());
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500331 return;
332 }
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500333
334 this->activations.erase(entryId);
Saqib Khan49446ae2017-10-02 10:54:20 -0500335 ItemUpdater::resetUbootEnvVars();
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500336}
337
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500338void ItemUpdater::deleteAll()
339{
340 std::vector<std::string> deletableVersions;
341
342 for (const auto& versionIt : versions)
343 {
344 if (!versionIt.second->isFunctional())
345 {
346 deletableVersions.push_back(versionIt.first);
347 }
348 }
349
350 for (const auto& deletableIt : deletableVersions)
351 {
352 ItemUpdater::erase(deletableIt);
353 }
354
355 // Remove any volumes that do not match current versions.
356 auto method = bus.new_method_call(
357 SYSTEMD_BUSNAME,
358 SYSTEMD_PATH,
359 SYSTEMD_INTERFACE,
360 "StartUnit");
361 method.append("obmc-flash-bmc-cleanup.service", "replace");
362 bus.call_noreply(method);
363}
364
Saqib Khan35e83f32017-05-22 11:37:32 -0500365ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
Gunnar Mills9a782242017-08-22 16:23:15 -0500366 const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500367{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500368 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500369
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500370 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500371 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500372 fs::path file(filePath);
373 file /= bmcImage;
374 std::ifstream efile(file.c_str());
375 if (efile.good() != 1)
376 {
377 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500378 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500379 invalid = true;
380 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500381 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500382
383 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500384 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500385 return ItemUpdater::ActivationStatus::invalid;
386 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500387
388 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500389}
390
Saqib Khanb9da6632017-09-13 09:48:37 -0500391void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500392{
393 //TODO openbmc/openbmc#1896 Improve the performance of this function
394 for (const auto& intf : activations)
395 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500396 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500397 {
Saqib Khanb9da6632017-09-13 09:48:37 -0500398 if (intf.second->redundancyPriority.get()->priority() == value &&
399 intf.second->versionId != versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500400 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500401 intf.second->redundancyPriority.get()->priority(value + 1);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500402 }
403 }
404 }
405}
406
Michael Tritz37a59042017-07-12 13:44:53 -0500407void ItemUpdater::reset()
408{
409 // Mark the read-write partition for recreation upon reboot.
410 auto method = bus.new_method_call(
411 SYSTEMD_BUSNAME,
412 SYSTEMD_PATH,
413 SYSTEMD_INTERFACE,
414 "StartUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500415 method.append("obmc-flash-bmc-setenv@rwreset\\x3dtrue.service", "replace");
Michael Tritz37a59042017-07-12 13:44:53 -0500416 bus.call_noreply(method);
417
418 log<level::INFO>("BMC factory reset will take effect upon reboot.");
419
420 return;
421}
422
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500423void ItemUpdater::removeReadOnlyPartition(std::string versionId)
424{
425 auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId +
426 ".service";
427
428 // Remove the read-only partitions.
429 auto method = bus.new_method_call(
430 SYSTEMD_BUSNAME,
431 SYSTEMD_PATH,
432 SYSTEMD_INTERFACE,
433 "StartUnit");
434 method.append(serviceFile, "replace");
435 bus.call_noreply(method);
436}
437
Michael Tritz0129d922017-08-10 19:33:46 -0500438bool ItemUpdater::fieldModeEnabled(bool value)
439{
440 // enabling field mode is intended to be one way: false -> true
441 if (value && !control::FieldMode::fieldModeEnabled())
442 {
443 control::FieldMode::fieldModeEnabled(value);
444
445 auto method = bus.new_method_call(
446 SYSTEMD_BUSNAME,
447 SYSTEMD_PATH,
448 SYSTEMD_INTERFACE,
449 "StartUnit");
450 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
Gunnar Mills9a782242017-08-22 16:23:15 -0500451 "replace");
Michael Tritz0129d922017-08-10 19:33:46 -0500452 bus.call_noreply(method);
453
454 method = bus.new_method_call(
455 SYSTEMD_BUSNAME,
456 SYSTEMD_PATH,
457 SYSTEMD_INTERFACE,
458 "StopUnit");
459 method.append("usr-local.mount", "replace");
460 bus.call_noreply(method);
461
462 std::vector<std::string> usrLocal = {"usr-local.mount"};
463
464 method = bus.new_method_call(
465 SYSTEMD_BUSNAME,
466 SYSTEMD_PATH,
467 SYSTEMD_INTERFACE,
468 "MaskUnitFiles");
469 method.append(usrLocal, false, true);
470 bus.call_noreply(method);
471 }
472
473 return control::FieldMode::fieldModeEnabled();
474}
475
476void ItemUpdater::restoreFieldModeStatus()
477{
478 std::ifstream input("/run/fw_env");
479 std::string envVar;
480 std::getline(input, envVar);
481
Gunnar Mills9a782242017-08-22 16:23:15 -0500482 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500483 {
484 ItemUpdater::fieldModeEnabled(true);
485 }
486}
487
Gunnar Millsb60add12017-08-24 16:41:42 -0500488void ItemUpdater::setBMCInventoryPath()
489{
490 //TODO: openbmc/openbmc#1786 - Get the BMC path by looking for objects
491 // that implement the BMC inventory interface
492 auto depth = 0;
493 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
494 MAPPER_PATH,
495 MAPPER_INTERFACE,
496 "GetSubTreePaths");
497
498 mapperCall.append(CHASSIS_INVENTORY_PATH);
499 mapperCall.append(depth);
500
501 // TODO: openbmc/openbmc#2226 - Add Inventory Item filter when
502 // mapper is fixed.
503 std::vector<std::string> filter = {};
504 mapperCall.append(filter);
505
506 auto response = bus.call(mapperCall);
507 if (response.is_method_error())
508 {
509 log<level::ERR>("Error in mapper GetSubTreePath");
510 return;
511 }
512
513 using ObjectPaths = std::vector<std::string>;
514 ObjectPaths result;
515 response.read(result);
516
517 if (result.empty())
518 {
519 log<level::ERR>("Invalid response from mapper");
520 return;
521 }
522
523 for (auto& iter : result)
524 {
525 const auto& path = iter;
526 if (path.substr(path.find_last_of('/') + 1).compare("bmc") == 0)
527 {
528 bmcInventoryPath = path;
529 return;
530 }
531 }
532}
533
Gunnar Millsf10b2322017-09-21 15:31:55 -0500534void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500535{
536 assocs.emplace_back(std::make_tuple(ACTIVE_FWD_ASSOCIATION,
537 ACTIVE_REV_ASSOCIATION,
538 path));
539 associations(assocs);
540}
541
Gunnar Mills88e8a322017-09-13 11:09:28 -0500542void ItemUpdater::createFunctionalAssociation(const std::string& path)
543{
544 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
545 FUNCTIONAL_REV_ASSOCIATION,
546 path));
547 associations(assocs);
548}
549
Gunnar Millsf10b2322017-09-21 15:31:55 -0500550void ItemUpdater::removeActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500551{
552 for (auto iter = assocs.begin(); iter != assocs.end();)
553 {
Gunnar Mills88e8a322017-09-13 11:09:28 -0500554 // Since there could be multiple associations to the same path,
555 // only remove ones that have an active forward association.
556 if ((std::get<0>(*iter)).compare(ACTIVE_FWD_ASSOCIATION) == 0 &&
557 (std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500558 {
559 iter = assocs.erase(iter);
560 associations(assocs);
561 }
562 else
563 {
564 ++iter;
565 }
566 }
567}
568
Saqib Khanb9da6632017-09-13 09:48:37 -0500569bool ItemUpdater::isLowestPriority(uint8_t value)
570{
571 for (const auto& intf : activations)
572 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500573 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500574 {
575 if (intf.second->redundancyPriority.get()->priority() < value)
576 {
577 return false;
578 }
579 }
580 }
581 return true;
582}
583
Saqib Khan49446ae2017-10-02 10:54:20 -0500584void ItemUpdater::resetUbootEnvVars()
585{
586 decltype(activations.begin()->second->redundancyPriority.get()->priority())
587 lowestPriority = std::numeric_limits<uint8_t>::max();
588 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
589 for (const auto& intf : activations)
590 {
591 if (!intf.second->redundancyPriority.get())
592 {
593 // Skip this version if the redundancyPriority is not initialized.
594 continue;
595 }
596
597 if (intf.second->redundancyPriority.get()->priority()
598 <= lowestPriority)
599 {
600 lowestPriority = intf.second->redundancyPriority.get()->priority();
601 lowestPriorityVersion = intf.second->versionId;
602 }
603 }
604
605 // TODO: openbmc/openbmc#2369 Add recovery policy to updateubootvars
606 // unit template.
607 auto method = bus.new_method_call(
608 SYSTEMD_BUSNAME,
609 SYSTEMD_PATH,
610 SYSTEMD_INTERFACE,
611 "StartUnit");
612 auto updateEnvVarsFile = "obmc-flash-bmc-updateubootvars@" +
613 lowestPriorityVersion + ".service";
614 method.append(updateEnvVarsFile, "replace");
615 auto result = bus.call(method);
616
617 //Check that the bus call didn't result in an error
618 if (result.is_method_error())
619 {
620 log<level::ERR>("Failed to update u-boot env variables");
621 }
622}
623
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500624} // namespace updater
625} // namespace software
626} // namespace phosphor