blob: 64bd270ba449d7ea62b6bf11de17e91d82b7dd27 [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);
Saqib Khan35e83f32017-05-22 11:37:32 -0500106 if (result == ItemUpdater::ActivationStatus::ready)
107 {
108 activationState = server::Activation::Activations::Ready;
109 }
Gunnar Millsb60add12017-08-24 16:41:42 -0500110
111 // Create an association to the BMC inventory item
112 AssociationList associations{(std::make_tuple(
113 ACTIVATION_FWD_ASSOCIATION,
114 ACTIVATION_REV_ASSOCIATION,
115 bmcInventoryPath))};
116
Saqib Khan35e83f32017-05-22 11:37:32 -0500117 activations.insert(std::make_pair(
Saqib Khan705f1bf2017-06-09 23:58:38 -0500118 versionId,
119 std::make_unique<Activation>(
120 bus,
121 path,
Saqib Khan4c1aec02017-07-06 11:46:13 -0500122 *this,
Saqib Khan35e83f32017-05-22 11:37:32 -0500123 versionId,
Gunnar Millsb60add12017-08-24 16:41:42 -0500124 activationState,
125 associations)));
Saqib Khan705f1bf2017-06-09 23:58:38 -0500126 versions.insert(std::make_pair(
127 versionId,
Gunnar Mills9a782242017-08-22 16:23:15 -0500128 std::make_unique<VersionClass>(
Saqib Khan705f1bf2017-06-09 23:58:38 -0500129 bus,
130 path,
131 version,
132 purpose,
Eddie James9440f492017-08-30 11:34:16 -0500133 filePath)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500134 }
Saqib Khan7b5010f2017-08-09 10:03:11 -0500135 else
136 {
137 log<level::INFO>("Software Object with the same version already exists",
Gunnar Mills9a782242017-08-22 16:23:15 -0500138 entry("VERSION_ID=%s", versionId));
Saqib Khan7b5010f2017-08-09 10:03:11 -0500139 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500140 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500141}
142
Saqib Khanba239882017-05-26 08:41:54 -0500143void ItemUpdater::processBMCImage()
144{
Gunnar Millsb60add12017-08-24 16:41:42 -0500145 // Create an association to the BMC inventory item
146 AssociationList associations{(std::make_tuple(
147 ACTIVATION_FWD_ASSOCIATION,
148 ACTIVATION_REV_ASSOCIATION,
149 bmcInventoryPath))};
150
Saqib Khan1eef62d2017-08-10 15:29:34 -0500151 // Read os-release from folders under /media/ to get
152 // BMC Software Versions.
153 for(const auto& iter : fs::directory_iterator(MEDIA_DIR))
154 {
155 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500156 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500157
158 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
159 if (0 == iter.path().native().compare(0, BMC_RO_PREFIX_LEN,
Saqib Khan6fab70d2017-09-07 00:13:50 -0500160 BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500161 {
162 auto osRelease = iter.path() / OS_RELEASE_FILE;
163 if (!fs::is_regular_file(osRelease))
164 {
165 log<level::ERR>("Failed to read osRelease\n",
166 entry("FileName=%s", osRelease.string()));
167 activationState = server::Activation::Activations::Invalid;
168 }
169 auto version =
170 phosphor::software::manager::Version::
171 getBMCVersion(osRelease);
172 if (version.empty())
173 {
174 log<level::ERR>("Failed to read version from osRelease",
175 entry("FILENAME=%s", osRelease.string()));
176 activationState = server::Activation::Activations::Invalid;
177 }
178 // The versionId is extracted from the path
179 // for example /media/ro-2a1022fe
180 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
181 auto purpose = server::Version::VersionPurpose::BMC;
182 auto path = fs::path(SOFTWARE_OBJPATH) / id;
183
184 // Create Activation instance for this version.
185 activations.insert(std::make_pair(
186 id,
187 std::make_unique<Activation>(
188 bus,
189 path,
190 *this,
191 id,
192 server::Activation::Activations::Active,
193 associations)));
194
195 // If Active, create RedundancyPriority instance for this version.
196 if (activationState == server::Activation::Activations::Active)
197 {
198 uint8_t priority = std::numeric_limits<uint8_t>::max();
199 if (!restoreFromFile(id, priority))
200 {
201 log<level::ERR>("Unable to restore priority from file.",
202 entry("VERSIONID=%s", id));
203 }
204 activations.find(id)->second->redundancyPriority =
205 std::make_unique<RedundancyPriority>(
Saqib Khanba239882017-05-26 08:41:54 -0500206 bus,
207 path,
Saqib Khan1eef62d2017-08-10 15:29:34 -0500208 *(activations.find(id)->second),
209 priority);
210 }
211
212 // Create Version instance for this version.
213 versions.insert(std::make_pair(
214 id,
215 std::make_unique<
216 phosphor::software::manager::Version>(
217 bus,
218 path,
219 version,
220 purpose,
Eddie James9440f492017-08-30 11:34:16 -0500221 "")));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500222 }
223 }
Saqib Khanba239882017-05-26 08:41:54 -0500224 return;
225}
226
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500227void ItemUpdater::erase(std::string entryId)
228{
Saqib Khan0c2eb262017-08-19 11:01:18 -0500229 // Delete ReadOnly partitions
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500230 removeReadOnlyPartition(entryId);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500231 removeFile(entryId);
232
233 // Remove the priority environment variable.
234 auto serviceFile = "obmc-flash-bmc-setenv@" + entryId + ".service";
235 auto method = bus.new_method_call(
236 SYSTEMD_BUSNAME,
237 SYSTEMD_PATH,
238 SYSTEMD_INTERFACE,
239 "StartUnit");
240 method.append(serviceFile, "replace");
241 bus.call_noreply(method);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500242
243 // Removing entry in versions map
244 auto it = versions.find(entryId);
245 if (it == versions.end())
246 {
247 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills9a782242017-08-22 16:23:15 -0500248 " in item updater versions map." \
249 " Unable to remove.").c_str());
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500250 return;
251 }
252 this->versions.erase(entryId);
253
254 // Removing entry in activations map
255 auto ita = activations.find(entryId);
256 if (ita == activations.end())
257 {
258 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills9a782242017-08-22 16:23:15 -0500259 " in item updater activations map." \
260 " Unable to remove.").c_str());
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500261 return;
262 }
263 // TODO: openbmc/openbmc#1986
264 // Test if this is the currently running image
265 // If not, don't continue.
266
267 this->activations.erase(entryId);
268}
269
Saqib Khan35e83f32017-05-22 11:37:32 -0500270ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
Gunnar Mills9a782242017-08-22 16:23:15 -0500271 const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500272{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500273 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500274
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500275 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500276 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500277 fs::path file(filePath);
278 file /= bmcImage;
279 std::ifstream efile(file.c_str());
280 if (efile.good() != 1)
281 {
282 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500283 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500284 invalid = true;
285 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500286 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500287
288 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500289 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500290 return ItemUpdater::ActivationStatus::invalid;
291 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500292
293 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500294}
295
Saqib Khan4c1aec02017-07-06 11:46:13 -0500296void ItemUpdater::freePriority(uint8_t value)
297{
298 //TODO openbmc/openbmc#1896 Improve the performance of this function
299 for (const auto& intf : activations)
300 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500301 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500302 {
303 if (intf.second->redundancyPriority.get()->priority() == value)
304 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500305 intf.second->redundancyPriority.get()->priority(value + 1);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500306 }
307 }
308 }
309}
310
Michael Tritz37a59042017-07-12 13:44:53 -0500311void ItemUpdater::reset()
312{
313 // Mark the read-write partition for recreation upon reboot.
314 auto method = bus.new_method_call(
315 SYSTEMD_BUSNAME,
316 SYSTEMD_PATH,
317 SYSTEMD_INTERFACE,
318 "StartUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500319 method.append("obmc-flash-bmc-setenv@rwreset\\x3dtrue.service", "replace");
Michael Tritz37a59042017-07-12 13:44:53 -0500320 bus.call_noreply(method);
321
322 log<level::INFO>("BMC factory reset will take effect upon reboot.");
323
324 return;
325}
326
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500327void ItemUpdater::removeReadOnlyPartition(std::string versionId)
328{
329 auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId +
330 ".service";
331
332 // Remove the read-only partitions.
333 auto method = bus.new_method_call(
334 SYSTEMD_BUSNAME,
335 SYSTEMD_PATH,
336 SYSTEMD_INTERFACE,
337 "StartUnit");
338 method.append(serviceFile, "replace");
339 bus.call_noreply(method);
340}
341
Michael Tritz0129d922017-08-10 19:33:46 -0500342bool ItemUpdater::fieldModeEnabled(bool value)
343{
344 // enabling field mode is intended to be one way: false -> true
345 if (value && !control::FieldMode::fieldModeEnabled())
346 {
347 control::FieldMode::fieldModeEnabled(value);
348
349 auto method = bus.new_method_call(
350 SYSTEMD_BUSNAME,
351 SYSTEMD_PATH,
352 SYSTEMD_INTERFACE,
353 "StartUnit");
354 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
Gunnar Mills9a782242017-08-22 16:23:15 -0500355 "replace");
Michael Tritz0129d922017-08-10 19:33:46 -0500356 bus.call_noreply(method);
357
358 method = bus.new_method_call(
359 SYSTEMD_BUSNAME,
360 SYSTEMD_PATH,
361 SYSTEMD_INTERFACE,
362 "StopUnit");
363 method.append("usr-local.mount", "replace");
364 bus.call_noreply(method);
365
366 std::vector<std::string> usrLocal = {"usr-local.mount"};
367
368 method = bus.new_method_call(
369 SYSTEMD_BUSNAME,
370 SYSTEMD_PATH,
371 SYSTEMD_INTERFACE,
372 "MaskUnitFiles");
373 method.append(usrLocal, false, true);
374 bus.call_noreply(method);
375 }
376
377 return control::FieldMode::fieldModeEnabled();
378}
379
380void ItemUpdater::restoreFieldModeStatus()
381{
382 std::ifstream input("/run/fw_env");
383 std::string envVar;
384 std::getline(input, envVar);
385
Gunnar Mills9a782242017-08-22 16:23:15 -0500386 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500387 {
388 ItemUpdater::fieldModeEnabled(true);
389 }
390}
391
Gunnar Millsb60add12017-08-24 16:41:42 -0500392void ItemUpdater::setBMCInventoryPath()
393{
394 //TODO: openbmc/openbmc#1786 - Get the BMC path by looking for objects
395 // that implement the BMC inventory interface
396 auto depth = 0;
397 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
398 MAPPER_PATH,
399 MAPPER_INTERFACE,
400 "GetSubTreePaths");
401
402 mapperCall.append(CHASSIS_INVENTORY_PATH);
403 mapperCall.append(depth);
404
405 // TODO: openbmc/openbmc#2226 - Add Inventory Item filter when
406 // mapper is fixed.
407 std::vector<std::string> filter = {};
408 mapperCall.append(filter);
409
410 auto response = bus.call(mapperCall);
411 if (response.is_method_error())
412 {
413 log<level::ERR>("Error in mapper GetSubTreePath");
414 return;
415 }
416
417 using ObjectPaths = std::vector<std::string>;
418 ObjectPaths result;
419 response.read(result);
420
421 if (result.empty())
422 {
423 log<level::ERR>("Invalid response from mapper");
424 return;
425 }
426
427 for (auto& iter : result)
428 {
429 const auto& path = iter;
430 if (path.substr(path.find_last_of('/') + 1).compare("bmc") == 0)
431 {
432 bmcInventoryPath = path;
433 return;
434 }
435 }
436}
437
Gunnar Millsded875d2017-08-28 16:44:52 -0500438void ItemUpdater::createActiveAssociation(std::string path)
439{
440 assocs.emplace_back(std::make_tuple(ACTIVE_FWD_ASSOCIATION,
441 ACTIVE_REV_ASSOCIATION,
442 path));
443 associations(assocs);
444}
445
446void ItemUpdater::removeActiveAssociation(std::string path)
447{
448 for (auto iter = assocs.begin(); iter != assocs.end();)
449 {
450 if ((std::get<2>(*iter)).compare(path) == 0)
451 {
452 iter = assocs.erase(iter);
453 associations(assocs);
454 }
455 else
456 {
457 ++iter;
458 }
459 }
460}
461
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500462} // namespace updater
463} // namespace software
464} // namespace phosphor