blob: 6b4ff5ef810eb7187e250c5ad667fb8cb24c0976 [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>
Gunnar Millsec1b41c2017-05-02 12:20:36 -05004#include "config.h"
Gunnar Mills2ce7da22017-05-04 15:37:56 -05005#include "item_updater.hpp"
6#include "xyz/openbmc_project/Software/Version/server.hpp"
Saqib Khan35e83f32017-05-22 11:37:32 -05007#include <experimental/filesystem>
Saqib Khan705f1bf2017-06-09 23:58:38 -05008#include "version.hpp"
Saqib Khan5d532672017-08-09 10:44:50 -05009#include "serialize.hpp"
Gunnar Millsec1b41c2017-05-02 12:20:36 -050010
11namespace phosphor
12{
13namespace software
14{
15namespace updater
16{
17
Gunnar Mills2ce7da22017-05-04 15:37:56 -050018// When you see server:: you know we're referencing our base class
19namespace server = sdbusplus::xyz::openbmc_project::Software::server;
Michael Tritz0129d922017-08-10 19:33:46 -050020namespace control = sdbusplus::xyz::openbmc_project::Control::server;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050021
22using namespace phosphor::logging;
Saqib Khan35e83f32017-05-22 11:37:32 -050023namespace fs = std::experimental::filesystem;
24
Gunnar Mills9a782242017-08-22 16:23:15 -050025const std::vector<std::string> bmcImages = { "image-kernel",
26 "image-rofs",
27 "image-rwfs",
28 "image-u-boot" };
Gunnar Mills2ce7da22017-05-04 15:37:56 -050029
Patrick Williamse75d10f2017-05-30 16:56:32 -050030void ItemUpdater::createActivation(sdbusplus::message::message& msg)
Gunnar Millsec1b41c2017-05-02 12:20:36 -050031{
Saqib Khan84a0e692017-06-28 17:27:01 -050032
33 using SVersion = server::Version;
34 using VersionPurpose = SVersion::VersionPurpose;
Gunnar Mills9a782242017-08-22 16:23:15 -050035 using VersionClass = phosphor::software::manager::Version;
Saqib Khan84a0e692017-06-28 17:27:01 -050036 namespace mesg = sdbusplus::message;
37 namespace variant_ns = mesg::variant_ns;
38
39 mesg::object_path objPath;
40 auto purpose = VersionPurpose::Unknown;
Saqib Khan705f1bf2017-06-09 23:58:38 -050041 std::string version;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050042 std::map<std::string,
Patrick Williamse75d10f2017-05-30 16:56:32 -050043 std::map<std::string,
Saqib Khan84a0e692017-06-28 17:27:01 -050044 mesg::variant<std::string>>> interfaces;
Patrick Williamse75d10f2017-05-30 16:56:32 -050045 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050046 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050047 std::string filePath;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050048
49 for (const auto& intf : interfaces)
50 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050051 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050052 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050053 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050054 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050055 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050056 {
Saqib Khan84a0e692017-06-28 17:27:01 -050057 auto value = SVersion::convertVersionPurposeFromString(
Gunnar Mills9a782242017-08-22 16:23:15 -050058 variant_ns::get<std::string>(property.second));
Saqib Khan84a0e692017-06-28 17:27:01 -050059 if (value == VersionPurpose::BMC ||
60 value == VersionPurpose::System)
61 {
62 purpose = value;
63 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050064 }
65 else if (property.first == "Version")
66 {
Saqib Khan84a0e692017-06-28 17:27:01 -050067 version = variant_ns::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050068 }
69 }
70 }
Saqib Khan19177d32017-06-20 08:11:49 -050071 else if (intf.first == FILEPATH_IFACE)
72 {
73 for (const auto& property : intf.second)
74 {
75 if (property.first == "Path")
76 {
Saqib Khan84a0e692017-06-28 17:27:01 -050077 filePath = variant_ns::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050078 }
79 }
80 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050081 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050082 if (version.empty() ||
Saqib Khan19177d32017-06-20 08:11:49 -050083 filePath.empty() ||
Saqib Khan84a0e692017-06-28 17:27:01 -050084 purpose == VersionPurpose::Unknown)
Saqib Khan705f1bf2017-06-09 23:58:38 -050085 {
86 return;
87 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050088
89 // Version id is the last item in the path
90 auto pos = path.rfind("/");
91 if (pos == std::string::npos)
92 {
93 log<level::ERR>("No version id found in object path",
94 entry("OBJPATH=%s", path));
Patrick Williamse75d10f2017-05-30 16:56:32 -050095 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050096 }
97
98 auto versionId = path.substr(pos + 1);
99
Patrick Williamse75d10f2017-05-30 16:56:32 -0500100 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500101 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500102 // Determine the Activation state by processing the given image dir.
103 auto activationState = server::Activation::Activations::Invalid;
Gunnar Mills9a782242017-08-22 16:23:15 -0500104 ItemUpdater::ActivationStatus result =
105 ItemUpdater::validateSquashFSImage(filePath);
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500106 AssociationList associations = {};
107
Saqib Khan35e83f32017-05-22 11:37:32 -0500108 if (result == ItemUpdater::ActivationStatus::ready)
109 {
110 activationState = server::Activation::Activations::Ready;
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500111 // Create an association to the BMC inventory item
112 associations.emplace_back(std::make_tuple(
113 ACTIVATION_FWD_ASSOCIATION,
114 ACTIVATION_REV_ASSOCIATION,
115 bmcInventoryPath));
Saqib Khan35e83f32017-05-22 11:37:32 -0500116 }
Gunnar Millsb60add12017-08-24 16:41:42 -0500117
Saqib Khan35e83f32017-05-22 11:37:32 -0500118 activations.insert(std::make_pair(
Saqib Khan705f1bf2017-06-09 23:58:38 -0500119 versionId,
120 std::make_unique<Activation>(
121 bus,
122 path,
Saqib Khan4c1aec02017-07-06 11:46:13 -0500123 *this,
Saqib Khan35e83f32017-05-22 11:37:32 -0500124 versionId,
Gunnar Millsb60add12017-08-24 16:41:42 -0500125 activationState,
126 associations)));
Saqib Khan705f1bf2017-06-09 23:58:38 -0500127 versions.insert(std::make_pair(
128 versionId,
Gunnar Mills9a782242017-08-22 16:23:15 -0500129 std::make_unique<VersionClass>(
Saqib Khan705f1bf2017-06-09 23:58:38 -0500130 bus,
131 path,
132 version,
133 purpose,
Eddie James9440f492017-08-30 11:34:16 -0500134 filePath)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500135 }
Saqib Khan7b5010f2017-08-09 10:03:11 -0500136 else
137 {
138 log<level::INFO>("Software Object with the same version already exists",
Gunnar Mills9a782242017-08-22 16:23:15 -0500139 entry("VERSION_ID=%s", versionId));
Saqib Khan7b5010f2017-08-09 10:03:11 -0500140 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500141 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500142}
143
Saqib Khanba239882017-05-26 08:41:54 -0500144void ItemUpdater::processBMCImage()
145{
Saqib Khan1eef62d2017-08-10 15:29:34 -0500146 // Read os-release from folders under /media/ to get
147 // BMC Software Versions.
148 for(const auto& iter : fs::directory_iterator(MEDIA_DIR))
149 {
150 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500151 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500152
153 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
154 if (0 == iter.path().native().compare(0, BMC_RO_PREFIX_LEN,
Saqib Khan6fab70d2017-09-07 00:13:50 -0500155 BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500156 {
157 auto osRelease = iter.path() / OS_RELEASE_FILE;
158 if (!fs::is_regular_file(osRelease))
159 {
160 log<level::ERR>("Failed to read osRelease\n",
161 entry("FileName=%s", osRelease.string()));
162 activationState = server::Activation::Activations::Invalid;
163 }
164 auto version =
165 phosphor::software::manager::Version::
166 getBMCVersion(osRelease);
167 if (version.empty())
168 {
169 log<level::ERR>("Failed to read version from osRelease",
170 entry("FILENAME=%s", osRelease.string()));
171 activationState = server::Activation::Activations::Invalid;
172 }
173 // The versionId is extracted from the path
174 // for example /media/ro-2a1022fe
175 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
176 auto purpose = server::Version::VersionPurpose::BMC;
177 auto path = fs::path(SOFTWARE_OBJPATH) / id;
178
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500179 AssociationList associations = {};
180
181 if (activationState == server::Activation::Activations::Active)
182 {
183 // Create an association to the BMC inventory item
184 associations.emplace_back(std::make_tuple(
185 ACTIVATION_FWD_ASSOCIATION,
186 ACTIVATION_REV_ASSOCIATION,
187 bmcInventoryPath));
188
189 // Create an active association since this image is active
190 createActiveAssociation(path);
191 }
192
Saqib Khan1eef62d2017-08-10 15:29:34 -0500193 // Create Activation instance for this version.
194 activations.insert(std::make_pair(
195 id,
196 std::make_unique<Activation>(
197 bus,
198 path,
199 *this,
200 id,
201 server::Activation::Activations::Active,
202 associations)));
203
204 // If Active, create RedundancyPriority instance for this version.
205 if (activationState == server::Activation::Activations::Active)
206 {
207 uint8_t priority = std::numeric_limits<uint8_t>::max();
208 if (!restoreFromFile(id, priority))
209 {
210 log<level::ERR>("Unable to restore priority from file.",
211 entry("VERSIONID=%s", id));
212 }
213 activations.find(id)->second->redundancyPriority =
214 std::make_unique<RedundancyPriority>(
Saqib Khanba239882017-05-26 08:41:54 -0500215 bus,
216 path,
Saqib Khan1eef62d2017-08-10 15:29:34 -0500217 *(activations.find(id)->second),
218 priority);
219 }
220
221 // Create Version instance for this version.
222 versions.insert(std::make_pair(
223 id,
224 std::make_unique<
225 phosphor::software::manager::Version>(
226 bus,
227 path,
228 version,
229 purpose,
Eddie James9440f492017-08-30 11:34:16 -0500230 "")));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500231 }
232 }
Saqib Khanba239882017-05-26 08:41:54 -0500233 return;
234}
235
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500236void ItemUpdater::erase(std::string entryId)
237{
Eddie James6d873712017-09-01 11:29:07 -0500238 // Find entry in versions map
239 auto it = versions.find(entryId);
240 if (it != versions.end())
241 {
242 if (it->second->isFunctional())
243 {
244 log<level::ERR>(("Error: Version " + entryId + \
245 " is currently running on the BMC." \
246 " Unable to remove.").c_str());
247 return;
248 }
249
250 // Delete ReadOnly partitions if it's not active
251 removeReadOnlyPartition(entryId);
252 removeFile(entryId);
253 }
254 else
255 {
256 // Delete ReadOnly partitions even if we can't find the version
257 removeReadOnlyPartition(entryId);
258 removeFile(entryId);
259
260 log<level::ERR>(("Error: Failed to find version " + entryId + \
261 " in item updater versions map." \
262 " Unable to remove.").c_str());
263 return;
264 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500265
266 // Remove the priority environment variable.
267 auto serviceFile = "obmc-flash-bmc-setenv@" + entryId + ".service";
268 auto method = bus.new_method_call(
269 SYSTEMD_BUSNAME,
270 SYSTEMD_PATH,
271 SYSTEMD_INTERFACE,
272 "StartUnit");
273 method.append(serviceFile, "replace");
274 bus.call_noreply(method);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500275
276 // Removing entry in versions map
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500277 this->versions.erase(entryId);
278
279 // Removing entry in activations map
280 auto ita = activations.find(entryId);
281 if (ita == activations.end())
282 {
283 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills9a782242017-08-22 16:23:15 -0500284 " in item updater activations map." \
285 " Unable to remove.").c_str());
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500286 return;
287 }
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500288
289 this->activations.erase(entryId);
290}
291
Saqib Khan35e83f32017-05-22 11:37:32 -0500292ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
Gunnar Mills9a782242017-08-22 16:23:15 -0500293 const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500294{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500295 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500296
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500297 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500298 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500299 fs::path file(filePath);
300 file /= bmcImage;
301 std::ifstream efile(file.c_str());
302 if (efile.good() != 1)
303 {
304 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500305 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500306 invalid = true;
307 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500308 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500309
310 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500311 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500312 return ItemUpdater::ActivationStatus::invalid;
313 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500314
315 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500316}
317
Saqib Khan4c1aec02017-07-06 11:46:13 -0500318void ItemUpdater::freePriority(uint8_t value)
319{
320 //TODO openbmc/openbmc#1896 Improve the performance of this function
321 for (const auto& intf : activations)
322 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500323 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500324 {
325 if (intf.second->redundancyPriority.get()->priority() == value)
326 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500327 intf.second->redundancyPriority.get()->priority(value + 1);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500328 }
329 }
330 }
331}
332
Michael Tritz37a59042017-07-12 13:44:53 -0500333void ItemUpdater::reset()
334{
335 // Mark the read-write partition for recreation upon reboot.
336 auto method = bus.new_method_call(
337 SYSTEMD_BUSNAME,
338 SYSTEMD_PATH,
339 SYSTEMD_INTERFACE,
340 "StartUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500341 method.append("obmc-flash-bmc-setenv@rwreset\\x3dtrue.service", "replace");
Michael Tritz37a59042017-07-12 13:44:53 -0500342 bus.call_noreply(method);
343
344 log<level::INFO>("BMC factory reset will take effect upon reboot.");
345
346 return;
347}
348
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500349void ItemUpdater::removeReadOnlyPartition(std::string versionId)
350{
351 auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId +
352 ".service";
353
354 // Remove the read-only partitions.
355 auto method = bus.new_method_call(
356 SYSTEMD_BUSNAME,
357 SYSTEMD_PATH,
358 SYSTEMD_INTERFACE,
359 "StartUnit");
360 method.append(serviceFile, "replace");
361 bus.call_noreply(method);
362}
363
Michael Tritz0129d922017-08-10 19:33:46 -0500364bool ItemUpdater::fieldModeEnabled(bool value)
365{
366 // enabling field mode is intended to be one way: false -> true
367 if (value && !control::FieldMode::fieldModeEnabled())
368 {
369 control::FieldMode::fieldModeEnabled(value);
370
371 auto method = bus.new_method_call(
372 SYSTEMD_BUSNAME,
373 SYSTEMD_PATH,
374 SYSTEMD_INTERFACE,
375 "StartUnit");
376 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
Gunnar Mills9a782242017-08-22 16:23:15 -0500377 "replace");
Michael Tritz0129d922017-08-10 19:33:46 -0500378 bus.call_noreply(method);
379
380 method = bus.new_method_call(
381 SYSTEMD_BUSNAME,
382 SYSTEMD_PATH,
383 SYSTEMD_INTERFACE,
384 "StopUnit");
385 method.append("usr-local.mount", "replace");
386 bus.call_noreply(method);
387
388 std::vector<std::string> usrLocal = {"usr-local.mount"};
389
390 method = bus.new_method_call(
391 SYSTEMD_BUSNAME,
392 SYSTEMD_PATH,
393 SYSTEMD_INTERFACE,
394 "MaskUnitFiles");
395 method.append(usrLocal, false, true);
396 bus.call_noreply(method);
397 }
398
399 return control::FieldMode::fieldModeEnabled();
400}
401
402void ItemUpdater::restoreFieldModeStatus()
403{
404 std::ifstream input("/run/fw_env");
405 std::string envVar;
406 std::getline(input, envVar);
407
Gunnar Mills9a782242017-08-22 16:23:15 -0500408 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500409 {
410 ItemUpdater::fieldModeEnabled(true);
411 }
412}
413
Gunnar Millsb60add12017-08-24 16:41:42 -0500414void ItemUpdater::setBMCInventoryPath()
415{
416 //TODO: openbmc/openbmc#1786 - Get the BMC path by looking for objects
417 // that implement the BMC inventory interface
418 auto depth = 0;
419 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
420 MAPPER_PATH,
421 MAPPER_INTERFACE,
422 "GetSubTreePaths");
423
424 mapperCall.append(CHASSIS_INVENTORY_PATH);
425 mapperCall.append(depth);
426
427 // TODO: openbmc/openbmc#2226 - Add Inventory Item filter when
428 // mapper is fixed.
429 std::vector<std::string> filter = {};
430 mapperCall.append(filter);
431
432 auto response = bus.call(mapperCall);
433 if (response.is_method_error())
434 {
435 log<level::ERR>("Error in mapper GetSubTreePath");
436 return;
437 }
438
439 using ObjectPaths = std::vector<std::string>;
440 ObjectPaths result;
441 response.read(result);
442
443 if (result.empty())
444 {
445 log<level::ERR>("Invalid response from mapper");
446 return;
447 }
448
449 for (auto& iter : result)
450 {
451 const auto& path = iter;
452 if (path.substr(path.find_last_of('/') + 1).compare("bmc") == 0)
453 {
454 bmcInventoryPath = path;
455 return;
456 }
457 }
458}
459
Gunnar Millsded875d2017-08-28 16:44:52 -0500460void ItemUpdater::createActiveAssociation(std::string path)
461{
462 assocs.emplace_back(std::make_tuple(ACTIVE_FWD_ASSOCIATION,
463 ACTIVE_REV_ASSOCIATION,
464 path));
465 associations(assocs);
466}
467
468void ItemUpdater::removeActiveAssociation(std::string path)
469{
470 for (auto iter = assocs.begin(); iter != assocs.end();)
471 {
472 if ((std::get<2>(*iter)).compare(path) == 0)
473 {
474 iter = assocs.erase(iter);
475 associations(assocs);
476 }
477 else
478 {
479 ++iter;
480 }
481 }
482}
483
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500484} // namespace updater
485} // namespace software
486} // namespace phosphor