blob: 4507bcfcb572b2c6ebabae2c210ebfcea53f1ab6 [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{
Saqib Khan0c2eb262017-08-19 11:01:18 -0500238 // Delete ReadOnly partitions
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500239 removeReadOnlyPartition(entryId);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500240 removeFile(entryId);
241
242 // Remove the priority environment variable.
243 auto serviceFile = "obmc-flash-bmc-setenv@" + entryId + ".service";
244 auto method = bus.new_method_call(
245 SYSTEMD_BUSNAME,
246 SYSTEMD_PATH,
247 SYSTEMD_INTERFACE,
248 "StartUnit");
249 method.append(serviceFile, "replace");
250 bus.call_noreply(method);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500251
252 // Removing entry in versions map
253 auto it = versions.find(entryId);
254 if (it == versions.end())
255 {
256 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills9a782242017-08-22 16:23:15 -0500257 " in item updater versions map." \
258 " Unable to remove.").c_str());
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500259 return;
260 }
261 this->versions.erase(entryId);
262
263 // Removing entry in activations map
264 auto ita = activations.find(entryId);
265 if (ita == activations.end())
266 {
267 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills9a782242017-08-22 16:23:15 -0500268 " in item updater activations map." \
269 " Unable to remove.").c_str());
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500270 return;
271 }
272 // TODO: openbmc/openbmc#1986
273 // Test if this is the currently running image
274 // If not, don't continue.
275
276 this->activations.erase(entryId);
277}
278
Saqib Khan35e83f32017-05-22 11:37:32 -0500279ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
Gunnar Mills9a782242017-08-22 16:23:15 -0500280 const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500281{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500282 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500283
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500284 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500285 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500286 fs::path file(filePath);
287 file /= bmcImage;
288 std::ifstream efile(file.c_str());
289 if (efile.good() != 1)
290 {
291 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500292 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500293 invalid = true;
294 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500295 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500296
297 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500298 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500299 return ItemUpdater::ActivationStatus::invalid;
300 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500301
302 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500303}
304
Saqib Khan4c1aec02017-07-06 11:46:13 -0500305void ItemUpdater::freePriority(uint8_t value)
306{
307 //TODO openbmc/openbmc#1896 Improve the performance of this function
308 for (const auto& intf : activations)
309 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500310 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500311 {
312 if (intf.second->redundancyPriority.get()->priority() == value)
313 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500314 intf.second->redundancyPriority.get()->priority(value + 1);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500315 }
316 }
317 }
318}
319
Michael Tritz37a59042017-07-12 13:44:53 -0500320void ItemUpdater::reset()
321{
322 // Mark the read-write partition for recreation upon reboot.
323 auto method = bus.new_method_call(
324 SYSTEMD_BUSNAME,
325 SYSTEMD_PATH,
326 SYSTEMD_INTERFACE,
327 "StartUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500328 method.append("obmc-flash-bmc-setenv@rwreset\\x3dtrue.service", "replace");
Michael Tritz37a59042017-07-12 13:44:53 -0500329 bus.call_noreply(method);
330
331 log<level::INFO>("BMC factory reset will take effect upon reboot.");
332
333 return;
334}
335
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500336void ItemUpdater::removeReadOnlyPartition(std::string versionId)
337{
338 auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId +
339 ".service";
340
341 // Remove the read-only partitions.
342 auto method = bus.new_method_call(
343 SYSTEMD_BUSNAME,
344 SYSTEMD_PATH,
345 SYSTEMD_INTERFACE,
346 "StartUnit");
347 method.append(serviceFile, "replace");
348 bus.call_noreply(method);
349}
350
Michael Tritz0129d922017-08-10 19:33:46 -0500351bool ItemUpdater::fieldModeEnabled(bool value)
352{
353 // enabling field mode is intended to be one way: false -> true
354 if (value && !control::FieldMode::fieldModeEnabled())
355 {
356 control::FieldMode::fieldModeEnabled(value);
357
358 auto method = bus.new_method_call(
359 SYSTEMD_BUSNAME,
360 SYSTEMD_PATH,
361 SYSTEMD_INTERFACE,
362 "StartUnit");
363 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
Gunnar Mills9a782242017-08-22 16:23:15 -0500364 "replace");
Michael Tritz0129d922017-08-10 19:33:46 -0500365 bus.call_noreply(method);
366
367 method = bus.new_method_call(
368 SYSTEMD_BUSNAME,
369 SYSTEMD_PATH,
370 SYSTEMD_INTERFACE,
371 "StopUnit");
372 method.append("usr-local.mount", "replace");
373 bus.call_noreply(method);
374
375 std::vector<std::string> usrLocal = {"usr-local.mount"};
376
377 method = bus.new_method_call(
378 SYSTEMD_BUSNAME,
379 SYSTEMD_PATH,
380 SYSTEMD_INTERFACE,
381 "MaskUnitFiles");
382 method.append(usrLocal, false, true);
383 bus.call_noreply(method);
384 }
385
386 return control::FieldMode::fieldModeEnabled();
387}
388
389void ItemUpdater::restoreFieldModeStatus()
390{
391 std::ifstream input("/run/fw_env");
392 std::string envVar;
393 std::getline(input, envVar);
394
Gunnar Mills9a782242017-08-22 16:23:15 -0500395 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500396 {
397 ItemUpdater::fieldModeEnabled(true);
398 }
399}
400
Gunnar Millsb60add12017-08-24 16:41:42 -0500401void ItemUpdater::setBMCInventoryPath()
402{
403 //TODO: openbmc/openbmc#1786 - Get the BMC path by looking for objects
404 // that implement the BMC inventory interface
405 auto depth = 0;
406 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
407 MAPPER_PATH,
408 MAPPER_INTERFACE,
409 "GetSubTreePaths");
410
411 mapperCall.append(CHASSIS_INVENTORY_PATH);
412 mapperCall.append(depth);
413
414 // TODO: openbmc/openbmc#2226 - Add Inventory Item filter when
415 // mapper is fixed.
416 std::vector<std::string> filter = {};
417 mapperCall.append(filter);
418
419 auto response = bus.call(mapperCall);
420 if (response.is_method_error())
421 {
422 log<level::ERR>("Error in mapper GetSubTreePath");
423 return;
424 }
425
426 using ObjectPaths = std::vector<std::string>;
427 ObjectPaths result;
428 response.read(result);
429
430 if (result.empty())
431 {
432 log<level::ERR>("Invalid response from mapper");
433 return;
434 }
435
436 for (auto& iter : result)
437 {
438 const auto& path = iter;
439 if (path.substr(path.find_last_of('/') + 1).compare("bmc") == 0)
440 {
441 bmcInventoryPath = path;
442 return;
443 }
444 }
445}
446
Gunnar Millsded875d2017-08-28 16:44:52 -0500447void ItemUpdater::createActiveAssociation(std::string path)
448{
449 assocs.emplace_back(std::make_tuple(ACTIVE_FWD_ASSOCIATION,
450 ACTIVE_REV_ASSOCIATION,
451 path));
452 associations(assocs);
453}
454
455void ItemUpdater::removeActiveAssociation(std::string path)
456{
457 for (auto iter = assocs.begin(); iter != assocs.end();)
458 {
459 if ((std::get<2>(*iter)).compare(path) == 0)
460 {
461 iter = assocs.erase(iter);
462 associations(assocs);
463 }
464 else
465 {
466 ++iter;
467 }
468 }
469}
470
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500471} // namespace updater
472} // namespace software
473} // namespace phosphor