blob: 79b5b1005285041b18db33ae57535b2ebb5c1afe [file] [log] [blame]
Saqib Khanb0774702017-05-23 16:02:41 -05001#include "activation.hpp"
Gunnar Millsb0ce9962018-09-07 13:39:10 -05002
Lei YU1be8d502018-06-20 11:48:36 +08003#include "images.hpp"
Saqib Khan4c1aec02017-07-06 11:46:13 -05004#include "item_updater.hpp"
Miguel Gomez21dad042020-06-26 20:54:48 +00005#include "msl_verify.hpp"
Saqib Khan5d532672017-08-09 10:44:50 -05006#include "serialize.hpp"
Gunnar Millsb0ce9962018-09-07 13:39:10 -05007
Jayashankar Padatha0135602019-04-22 16:22:58 +05308#include <phosphor-logging/elog-errors.hpp>
9#include <phosphor-logging/elog.hpp>
Saqib Khanb9da6632017-09-13 09:48:37 -050010#include <phosphor-logging/log.hpp>
Adriana Kobylakaea48f22018-07-10 10:20:56 -050011#include <sdbusplus/exception.hpp>
Jayashankar Padatha0135602019-04-22 16:22:58 +053012#include <xyz/openbmc_project/Common/error.hpp>
Miguel Gomez21dad042020-06-26 20:54:48 +000013#include <xyz/openbmc_project/Software/Version/error.hpp>
Saqib Khanb9da6632017-09-13 09:48:37 -050014
Jayanth Othayoth0e0c1272018-02-21 05:46:36 -060015#ifdef WANT_SIGNATURE_VERIFY
Jayanth Othayoth0e0c1272018-02-21 05:46:36 -060016#include "image_verify.hpp"
Jayanth Othayoth0e0c1272018-02-21 05:46:36 -060017#endif
18
Saqib Khanb0774702017-05-23 16:02:41 -050019namespace phosphor
20{
21namespace software
22{
23namespace updater
24{
25
26namespace softwareServer = sdbusplus::xyz::openbmc_project::Software::server;
27
Saqib Khanb9da6632017-09-13 09:48:37 -050028using namespace phosphor::logging;
Adriana Kobylakaea48f22018-07-10 10:20:56 -050029using sdbusplus::exception::SdBusError;
Jayanth Othayoth0e0c1272018-02-21 05:46:36 -060030using InternalFailure =
31 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Jayashankar Padatha0135602019-04-22 16:22:58 +053032
33#ifdef WANT_SIGNATURE_VERIFY
Jayanth Othayoth9a9d7c22018-03-28 10:05:26 -050034namespace control = sdbusplus::xyz::openbmc_project::Control::server;
Jayanth Othayoth0e0c1272018-02-21 05:46:36 -060035#endif
36
Michael Tritzbed88af2017-07-19 16:00:06 -050037void Activation::subscribeToSystemdSignals()
38{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060039 auto method = this->bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
40 SYSTEMD_INTERFACE, "Subscribe");
Adriana Kobylakaea48f22018-07-10 10:20:56 -050041 try
42 {
43 this->bus.call_noreply(method);
44 }
45 catch (const SdBusError& e)
46 {
47 if (e.name() != nullptr &&
48 strcmp("org.freedesktop.systemd1.AlreadySubscribed", e.name()) == 0)
49 {
50 // If an Activation attempt fails, the Unsubscribe method is not
51 // called. This may lead to an AlreadySubscribed error if the
52 // Activation is re-attempted.
53 }
54 else
55 {
56 log<level::ERR>("Error subscribing to systemd",
57 entry("ERROR=%s", e.what()));
58 }
59 }
Michael Tritzbed88af2017-07-19 16:00:06 -050060
61 return;
62}
63
Michael Tritzf2b5e0d2017-07-25 14:39:34 -050064void Activation::unsubscribeFromSystemdSignals()
65{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060066 auto method = this->bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
67 SYSTEMD_INTERFACE, "Unsubscribe");
Jayashankar Padatha0135602019-04-22 16:22:58 +053068 try
69 {
70 this->bus.call_noreply(method);
71 }
72 catch (const SdBusError& e)
73 {
74 log<level::ERR>("Error in unsubscribing from systemd signals",
75 entry("ERROR=%s", e.what()));
76 }
Michael Tritzf2b5e0d2017-07-25 14:39:34 -050077
78 return;
79}
80
Adriana Kobylak2285fe02018-02-27 15:36:59 -060081auto Activation::activation(Activations value) -> Activations
Saqib Khanb0774702017-05-23 16:02:41 -050082{
Adriana Kobylak8bd84c82018-01-24 14:19:24 -060083 if ((value != softwareServer::Activation::Activations::Active) &&
84 (value != softwareServer::Activation::Activations::Activating))
Saqib Khan4c1aec02017-07-06 11:46:13 -050085 {
86 redundancyPriority.reset(nullptr);
87 }
88
Saqib Khanb0774702017-05-23 16:02:41 -050089 if (value == softwareServer::Activation::Activations::Activating)
90 {
Vijay Khemkae9f6c842020-01-14 14:32:39 -080091
92#ifdef HOST_BIOS_UPGRADE
93 auto purpose = parent.versions.find(versionId)->second->purpose();
94 if (purpose == VersionPurpose::Host)
95 {
96 if (!activationProgress)
97 {
98 activationProgress =
99 std::make_unique<ActivationProgress>(bus, path);
100 }
101
102 // Enable systemd signals
103 subscribeToSystemdSignals();
104
105 // Set initial progress
106 activationProgress->progress(20);
107
108 // Initiate image writing to flash
109 flashWriteHost();
110
111 return softwareServer::Activation::activation(value);
112 }
113#endif
114
Miguel Gomez21dad042020-06-26 20:54:48 +0000115 auto versionStr = parent.versions.find(versionId)->second->version();
116
117 if (!minimum_ship_level::verify(versionStr))
118 {
119 using namespace phosphor::logging;
120 using IncompatibleErr = sdbusplus::xyz::openbmc_project::Software::
121 Version::Error::Incompatible;
122 using Incompatible =
123 xyz::openbmc_project::Software::Version::Incompatible;
124
125 report<IncompatibleErr>(
126 prev_entry<Incompatible::MIN_VERSION>(),
127 prev_entry<Incompatible::ACTUAL_VERSION>(),
128 prev_entry<Incompatible::VERSION_PURPOSE>());
129 return softwareServer::Activation::activation(
130 softwareServer::Activation::Activations::Failed);
131 }
132
Lei YU90532252018-05-24 11:15:24 +0800133#ifdef WANT_SIGNATURE_VERIFY
134 fs::path uploadDir(IMG_UPLOAD_DIR);
135 if (!verifySignature(uploadDir / versionId, SIGNED_IMAGE_CONF_PATH))
136 {
137 onVerifyFailed();
138 // Stop the activation process, if fieldMode is enabled.
139 if (parent.control::FieldMode::fieldModeEnabled())
140 {
141 return softwareServer::Activation::activation(
142 softwareServer::Activation::Activations::Failed);
143 }
144 }
145#endif
Adriana Kobylakb824b2f2020-05-14 14:57:53 -0500146
147 if (!activationProgress)
148 {
149 activationProgress =
150 std::make_unique<ActivationProgress>(bus, path);
151 }
152
153 if (!activationBlocksTransition)
154 {
155 activationBlocksTransition =
156 std::make_unique<ActivationBlocksTransition>(bus, path);
157 }
158
159 activationProgress->progress(10);
160
Adriana Kobylaka6963592018-09-07 14:13:29 -0500161 parent.freeSpace(*this);
Lei YUa7853ee2018-05-23 11:13:12 +0800162
Adriana Kobylakb824b2f2020-05-14 14:57:53 -0500163 // Enable systemd signals
164 Activation::subscribeToSystemdSignals();
165
Lei YUa7853ee2018-05-23 11:13:12 +0800166 flashWrite();
167
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500168#if defined UBIFS_LAYOUT || defined MMC_LAYOUT
Lei YUa7853ee2018-05-23 11:13:12 +0800169
Adriana Kobylakb824b2f2020-05-14 14:57:53 -0500170 return softwareServer::Activation::activation(value);
Lei YUa7853ee2018-05-23 11:13:12 +0800171
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500172#else // STATIC_LAYOUT
Lei YUa7853ee2018-05-23 11:13:12 +0800173
Adriana Kobylakb824b2f2020-05-14 14:57:53 -0500174 onFlashWriteSuccess();
Lei YUa7853ee2018-05-23 11:13:12 +0800175 return softwareServer::Activation::activation(
176 softwareServer::Activation::Activations::Active);
177#endif
Saqib Khanb0774702017-05-23 16:02:41 -0500178 }
179 else
180 {
181 activationBlocksTransition.reset(nullptr);
Michael Tritz0edd4ad2017-07-26 14:27:42 -0500182 activationProgress.reset(nullptr);
Saqib Khanb0774702017-05-23 16:02:41 -0500183 }
184 return softwareServer::Activation::activation(value);
185}
186
Adriana Kobylakb824b2f2020-05-14 14:57:53 -0500187void Activation::onFlashWriteSuccess()
188{
189 activationProgress->progress(100);
190
191 activationBlocksTransition.reset(nullptr);
192 activationProgress.reset(nullptr);
193
194 rwVolumeCreated = false;
195 roVolumeCreated = false;
196 ubootEnvVarsUpdated = false;
197 Activation::unsubscribeFromSystemdSignals();
198
199 storePurpose(versionId, parent.versions.find(versionId)->second->purpose());
200
201 if (!redundancyPriority)
202 {
203 redundancyPriority =
204 std::make_unique<RedundancyPriority>(bus, path, *this, 0);
205 }
206
207 // Remove version object from image manager
208 Activation::deleteImageManagerObject();
209
210 // Create active association
211 parent.createActiveAssociation(path);
212
213 // Create updateable association as this
214 // can be re-programmed.
215 parent.createUpdateableAssociation(path);
216
217 if (Activation::checkApplyTimeImmediate() == true)
218 {
219 log<level::INFO>("Image Active. ApplyTime is immediate, "
220 "rebooting BMC.");
221 Activation::rebootBmc();
222 }
223 else
224 {
225 log<level::INFO>("BMC image ready, need reboot to get activated.");
226 }
227
228 activation(softwareServer::Activation::Activations::Active);
229}
230
Saqib Khanee13e832017-10-23 12:53:11 -0500231void Activation::deleteImageManagerObject()
232{
Saqib Khanee13e832017-10-23 12:53:11 -0500233 // Call the Delete object for <versionID> inside image_manager
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600234 auto method = this->bus.new_method_call(VERSION_BUSNAME, path.c_str(),
235 "xyz.openbmc_project.Object.Delete",
236 "Delete");
Adriana Kobylak3b6a4cd2018-12-10 13:45:09 -0600237 try
238 {
239 bus.call_noreply(method);
240 }
241 catch (const SdBusError& e)
Saqib Khanee13e832017-10-23 12:53:11 -0500242 {
243 log<level::ERR>("Error in Deleting image from image manager",
Adriana Kobylak596466b2018-02-13 14:48:53 -0600244 entry("VERSIONPATH=%s", path.c_str()));
Saqib Khanee13e832017-10-23 12:53:11 -0500245 return;
246 }
247}
248
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600249auto Activation::requestedActivation(RequestedActivations value)
250 -> RequestedActivations
Saqib Khanb0774702017-05-23 16:02:41 -0500251{
Michael Tritzbed88af2017-07-19 16:00:06 -0500252 rwVolumeCreated = false;
253 roVolumeCreated = false;
Adriana Kobylak166bdf32018-04-09 14:24:06 -0500254 ubootEnvVarsUpdated = false;
Michael Tritzbed88af2017-07-19 16:00:06 -0500255
Saqib Khanb0774702017-05-23 16:02:41 -0500256 if ((value == softwareServer::Activation::RequestedActivations::Active) &&
257 (softwareServer::Activation::requestedActivation() !=
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600258 softwareServer::Activation::RequestedActivations::Active))
Saqib Khanb0774702017-05-23 16:02:41 -0500259 {
260 if ((softwareServer::Activation::activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600261 softwareServer::Activation::Activations::Ready) ||
Saqib Khanb0774702017-05-23 16:02:41 -0500262 (softwareServer::Activation::activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600263 softwareServer::Activation::Activations::Failed))
Saqib Khanb0774702017-05-23 16:02:41 -0500264 {
265 Activation::activation(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600266 softwareServer::Activation::Activations::Activating);
Saqib Khanb0774702017-05-23 16:02:41 -0500267 }
268 }
269 return softwareServer::Activation::requestedActivation(value);
270}
271
Saqib Khan4c1aec02017-07-06 11:46:13 -0500272uint8_t RedundancyPriority::priority(uint8_t value)
273{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500274 // Set the priority value so that the freePriority() function can order
275 // the versions by priority.
276 auto newPriority = softwareServer::RedundancyPriority::priority(value);
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500277 parent.parent.savePriority(parent.versionId, value);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500278 parent.parent.freePriority(value, parent.versionId);
279 return newPriority;
Saqib Khan4c1aec02017-07-06 11:46:13 -0500280}
281
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500282uint8_t RedundancyPriority::sdbusPriority(uint8_t value)
Saqib Khanf0382c32017-10-24 13:36:22 -0500283{
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500284 parent.parent.savePriority(parent.versionId, value);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500285 return softwareServer::RedundancyPriority::priority(value);
Saqib Khanf0382c32017-10-24 13:36:22 -0500286}
287
Michael Tritzbed88af2017-07-19 16:00:06 -0500288void Activation::unitStateChange(sdbusplus::message::message& msg)
289{
Michael Tritz0edd4ad2017-07-26 14:27:42 -0500290 if (softwareServer::Activation::activation() !=
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600291 softwareServer::Activation::Activations::Activating)
Michael Tritz0edd4ad2017-07-26 14:27:42 -0500292 {
293 return;
294 }
295
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800296#ifdef HOST_BIOS_UPGRADE
297 auto purpose = parent.versions.find(versionId)->second->purpose();
298 if (purpose == VersionPurpose::Host)
299 {
300 onStateChangesBios(msg);
301 return;
302 }
303#endif
304
Adriana Kobylak3ce563a2018-06-06 16:41:15 -0500305 onStateChanges(msg);
Michael Tritzbed88af2017-07-19 16:00:06 -0500306
307 return;
308}
309
Lei YU90532252018-05-24 11:15:24 +0800310#ifdef WANT_SIGNATURE_VERIFY
311bool Activation::verifySignature(const fs::path& imageDir,
312 const fs::path& confDir)
313{
314 using Signature = phosphor::software::image::Signature;
315
316 Signature signature(imageDir, confDir);
317
318 return signature.verify();
319}
320
321void Activation::onVerifyFailed()
322{
323 log<level::ERR>("Error occurred during image validation");
324 report<InternalFailure>();
325}
326#endif
327
Saqib Khanf37cefc2017-09-12 08:44:41 -0500328void ActivationBlocksTransition::enableRebootGuard()
329{
330 log<level::INFO>("BMC image activating - BMC reboots are disabled.");
331
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600332 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
333 SYSTEMD_INTERFACE, "StartUnit");
Saqib Khanf37cefc2017-09-12 08:44:41 -0500334 method.append("reboot-guard-enable.service", "replace");
335 bus.call_noreply(method);
336}
337
338void ActivationBlocksTransition::disableRebootGuard()
339{
340 log<level::INFO>("BMC activation has ended - BMC reboots are re-enabled.");
341
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600342 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
343 SYSTEMD_INTERFACE, "StartUnit");
Saqib Khanf37cefc2017-09-12 08:44:41 -0500344 method.append("reboot-guard-disable.service", "replace");
345 bus.call_noreply(method);
346}
Michael Tritzbed88af2017-07-19 16:00:06 -0500347
Jayashankar Padatha0135602019-04-22 16:22:58 +0530348bool Activation::checkApplyTimeImmediate()
349{
350 auto service = utils::getService(bus, applyTimeObjPath, applyTimeIntf);
351 if (service.empty())
352 {
353 log<level::INFO>("Error getting the service name for BMC image "
354 "ApplyTime. The BMC needs to be manually rebooted to "
355 "complete the image activation if needed "
356 "immediately.");
357 }
358 else
359 {
360
361 auto method = bus.new_method_call(service.c_str(), applyTimeObjPath,
362 dbusPropIntf, "Get");
363 method.append(applyTimeIntf, applyTimeProp);
364
365 try
366 {
367 auto reply = bus.call(method);
368
Patrick Williams24048b52020-05-13 17:51:30 -0500369 std::variant<std::string> result;
Jayashankar Padatha0135602019-04-22 16:22:58 +0530370 reply.read(result);
Patrick Williamse883fb82020-05-13 11:38:55 -0500371 auto applyTime = std::get<std::string>(result);
Jayashankar Padatha0135602019-04-22 16:22:58 +0530372 if (applyTime == applyTimeImmediate)
373 {
374 return true;
375 }
376 }
377 catch (const SdBusError& e)
378 {
379 log<level::ERR>("Error in getting ApplyTime",
380 entry("ERROR=%s", e.what()));
381 }
382 }
383 return false;
384}
385
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800386#ifdef HOST_BIOS_UPGRADE
387void Activation::flashWriteHost()
388{
389 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
390 SYSTEMD_INTERFACE, "StartUnit");
391 auto biosServiceFile = "obmc-flash-host-bios@" + versionId + ".service";
392 method.append(biosServiceFile, "replace");
393 try
394 {
395 auto reply = bus.call(method);
396 }
397 catch (const SdBusError& e)
398 {
399 log<level::ERR>("Error in trying to upgrade Host Bios.");
400 report<InternalFailure>();
401 }
402}
403
404void Activation::onStateChangesBios(sdbusplus::message::message& msg)
405{
406 uint32_t newStateID{};
407 sdbusplus::message::object_path newStateObjPath;
408 std::string newStateUnit{};
409 std::string newStateResult{};
410
411 // Read the msg and populate each variable
412 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
413
414 auto biosServiceFile = "obmc-flash-host-bios@" + versionId + ".service";
415
416 if (newStateUnit == biosServiceFile)
417 {
418 // unsubscribe to systemd signals
419 unsubscribeFromSystemdSignals();
420
421 // Remove version object from image manager
422 deleteImageManagerObject();
423
424 if (newStateResult == "done")
425 {
426 // Set activation progress to 100
427 activationProgress->progress(100);
428
429 // Set Activation value to active
430 activation(softwareServer::Activation::Activations::Active);
431
432 log<level::INFO>("Bios upgrade completed successfully.");
433 }
434 else if (newStateResult == "failed")
435 {
436 // Set Activation value to Failed
437 activation(softwareServer::Activation::Activations::Failed);
438
439 log<level::ERR>("Bios upgrade failed.");
440 }
441 }
442
443 return;
444}
445
446#endif
447
Jayashankar Padatha0135602019-04-22 16:22:58 +0530448void Activation::rebootBmc()
449{
450 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
451 SYSTEMD_INTERFACE, "StartUnit");
452 method.append("force-reboot.service", "replace");
453 try
454 {
455 auto reply = bus.call(method);
456 }
457 catch (const SdBusError& e)
458 {
459 log<level::ALERT>("Error in trying to reboot the BMC. "
460 "The BMC needs to be manually rebooted to complete "
461 "the image activation.");
462 report<InternalFailure>();
463 }
464}
465
Saqib Khanb0774702017-05-23 16:02:41 -0500466} // namespace updater
467} // namespace software
468} // namespace phosphor