blob: 75a1b3a5665d2eae26154f25aa7c03616a5b4d44 [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
Lei YU25868182021-05-14 14:50:51 +08008#include <boost/asio/io_context.hpp>
Lei YU7d2fa142021-08-06 10:58:19 +08009#include <boost/asio/post.hpp>
Jayashankar Padatha0135602019-04-22 16:22:58 +053010#include <phosphor-logging/elog-errors.hpp>
11#include <phosphor-logging/elog.hpp>
Patrick Williamsc9bb6422021-08-27 06:18:35 -050012#include <phosphor-logging/lg2.hpp>
Adriana Kobylakaea48f22018-07-10 10:20:56 -050013#include <sdbusplus/exception.hpp>
Jayashankar Padatha0135602019-04-22 16:22:58 +053014#include <xyz/openbmc_project/Common/error.hpp>
Miguel Gomez21dad042020-06-26 20:54:48 +000015#include <xyz/openbmc_project/Software/Version/error.hpp>
Saqib Khanb9da6632017-09-13 09:48:37 -050016
Jayanth Othayoth0e0c1272018-02-21 05:46:36 -060017#ifdef WANT_SIGNATURE_VERIFY
Jayanth Othayoth0e0c1272018-02-21 05:46:36 -060018#include "image_verify.hpp"
Jayanth Othayoth0e0c1272018-02-21 05:46:36 -060019#endif
20
Lei YU25868182021-05-14 14:50:51 +080021extern boost::asio::io_context& getIOContext();
22
Saqib Khanb0774702017-05-23 16:02:41 -050023namespace phosphor
24{
25namespace software
26{
27namespace updater
28{
29
30namespace softwareServer = sdbusplus::xyz::openbmc_project::Software::server;
31
Patrick Williamsc9bb6422021-08-27 06:18:35 -050032PHOSPHOR_LOG2_USING;
Saqib Khanb9da6632017-09-13 09:48:37 -050033using namespace phosphor::logging;
Adriana Kobylakaea48f22018-07-10 10:20:56 -050034using sdbusplus::exception::SdBusError;
Jayanth Othayoth0e0c1272018-02-21 05:46:36 -060035using InternalFailure =
36 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Jayashankar Padatha0135602019-04-22 16:22:58 +053037
38#ifdef WANT_SIGNATURE_VERIFY
Jayanth Othayoth9a9d7c22018-03-28 10:05:26 -050039namespace control = sdbusplus::xyz::openbmc_project::Control::server;
Jayanth Othayoth0e0c1272018-02-21 05:46:36 -060040#endif
41
Michael Tritzbed88af2017-07-19 16:00:06 -050042void Activation::subscribeToSystemdSignals()
43{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060044 auto method = this->bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
45 SYSTEMD_INTERFACE, "Subscribe");
Adriana Kobylakaea48f22018-07-10 10:20:56 -050046 try
47 {
48 this->bus.call_noreply(method);
49 }
50 catch (const SdBusError& e)
51 {
52 if (e.name() != nullptr &&
53 strcmp("org.freedesktop.systemd1.AlreadySubscribed", e.name()) == 0)
54 {
55 // If an Activation attempt fails, the Unsubscribe method is not
56 // called. This may lead to an AlreadySubscribed error if the
57 // Activation is re-attempted.
58 }
59 else
60 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -050061 error("Error subscribing to systemd: {ERROR}", "ERROR", e);
Adriana Kobylakaea48f22018-07-10 10:20:56 -050062 }
63 }
Michael Tritzbed88af2017-07-19 16:00:06 -050064
65 return;
66}
67
Michael Tritzf2b5e0d2017-07-25 14:39:34 -050068void Activation::unsubscribeFromSystemdSignals()
69{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060070 auto method = this->bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
71 SYSTEMD_INTERFACE, "Unsubscribe");
Jayashankar Padatha0135602019-04-22 16:22:58 +053072 try
73 {
74 this->bus.call_noreply(method);
75 }
76 catch (const SdBusError& e)
77 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -050078 error("Error unsubscribing from systemd signals: {ERROR}", "ERROR", e);
Jayashankar Padatha0135602019-04-22 16:22:58 +053079 }
Michael Tritzf2b5e0d2017-07-25 14:39:34 -050080
81 return;
82}
83
Adriana Kobylak2285fe02018-02-27 15:36:59 -060084auto Activation::activation(Activations value) -> Activations
Saqib Khanb0774702017-05-23 16:02:41 -050085{
Adriana Kobylak8bd84c82018-01-24 14:19:24 -060086 if ((value != softwareServer::Activation::Activations::Active) &&
87 (value != softwareServer::Activation::Activations::Activating))
Saqib Khan4c1aec02017-07-06 11:46:13 -050088 {
89 redundancyPriority.reset(nullptr);
90 }
91
Saqib Khanb0774702017-05-23 16:02:41 -050092 if (value == softwareServer::Activation::Activations::Activating)
93 {
Lei YU7ab55e22021-05-19 13:26:53 +080094#ifdef WANT_SIGNATURE_VERIFY
95 fs::path uploadDir(IMG_UPLOAD_DIR);
96 if (!verifySignature(uploadDir / versionId, SIGNED_IMAGE_CONF_PATH))
97 {
98 onVerifyFailed();
99 // Stop the activation process, if fieldMode is enabled.
100 if (parent.control::FieldMode::fieldModeEnabled())
101 {
102 return softwareServer::Activation::activation(
103 softwareServer::Activation::Activations::Failed);
104 }
105 }
106#endif
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800107
108#ifdef HOST_BIOS_UPGRADE
109 auto purpose = parent.versions.find(versionId)->second->purpose();
110 if (purpose == VersionPurpose::Host)
111 {
112 if (!activationProgress)
113 {
114 activationProgress =
115 std::make_unique<ActivationProgress>(bus, path);
116 }
117
118 // Enable systemd signals
119 subscribeToSystemdSignals();
120
121 // Set initial progress
122 activationProgress->progress(20);
123
124 // Initiate image writing to flash
125 flashWriteHost();
126
127 return softwareServer::Activation::activation(value);
128 }
129#endif
130
Miguel Gomez21dad042020-06-26 20:54:48 +0000131 auto versionStr = parent.versions.find(versionId)->second->version();
132
133 if (!minimum_ship_level::verify(versionStr))
134 {
135 using namespace phosphor::logging;
136 using IncompatibleErr = sdbusplus::xyz::openbmc_project::Software::
137 Version::Error::Incompatible;
138 using Incompatible =
139 xyz::openbmc_project::Software::Version::Incompatible;
140
141 report<IncompatibleErr>(
142 prev_entry<Incompatible::MIN_VERSION>(),
143 prev_entry<Incompatible::ACTUAL_VERSION>(),
144 prev_entry<Incompatible::VERSION_PURPOSE>());
145 return softwareServer::Activation::activation(
146 softwareServer::Activation::Activations::Failed);
147 }
148
Adriana Kobylakb824b2f2020-05-14 14:57:53 -0500149 if (!activationProgress)
150 {
151 activationProgress =
152 std::make_unique<ActivationProgress>(bus, path);
153 }
154
155 if (!activationBlocksTransition)
156 {
157 activationBlocksTransition =
158 std::make_unique<ActivationBlocksTransition>(bus, path);
159 }
160
161 activationProgress->progress(10);
162
Adriana Kobylaka6963592018-09-07 14:13:29 -0500163 parent.freeSpace(*this);
Lei YUa7853ee2018-05-23 11:13:12 +0800164
Adriana Kobylakb824b2f2020-05-14 14:57:53 -0500165 // Enable systemd signals
166 Activation::subscribeToSystemdSignals();
167
Lei YUa7853ee2018-05-23 11:13:12 +0800168 flashWrite();
169
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500170#if defined UBIFS_LAYOUT || defined MMC_LAYOUT
Lei YUa7853ee2018-05-23 11:13:12 +0800171
Adriana Kobylakb824b2f2020-05-14 14:57:53 -0500172 return softwareServer::Activation::activation(value);
Lei YUa7853ee2018-05-23 11:13:12 +0800173
Adriana Kobylak70f5bc02020-05-13 14:08:14 -0500174#else // STATIC_LAYOUT
Lei YUa7853ee2018-05-23 11:13:12 +0800175
Adriana Kobylakb824b2f2020-05-14 14:57:53 -0500176 onFlashWriteSuccess();
Lei YUa7853ee2018-05-23 11:13:12 +0800177 return softwareServer::Activation::activation(
178 softwareServer::Activation::Activations::Active);
179#endif
Saqib Khanb0774702017-05-23 16:02:41 -0500180 }
181 else
182 {
183 activationBlocksTransition.reset(nullptr);
Michael Tritz0edd4ad2017-07-26 14:27:42 -0500184 activationProgress.reset(nullptr);
Saqib Khanb0774702017-05-23 16:02:41 -0500185 }
186 return softwareServer::Activation::activation(value);
187}
188
Adriana Kobylakb824b2f2020-05-14 14:57:53 -0500189void Activation::onFlashWriteSuccess()
190{
191 activationProgress->progress(100);
192
193 activationBlocksTransition.reset(nullptr);
194 activationProgress.reset(nullptr);
195
196 rwVolumeCreated = false;
197 roVolumeCreated = false;
198 ubootEnvVarsUpdated = false;
199 Activation::unsubscribeFromSystemdSignals();
200
201 storePurpose(versionId, parent.versions.find(versionId)->second->purpose());
202
203 if (!redundancyPriority)
204 {
205 redundancyPriority =
206 std::make_unique<RedundancyPriority>(bus, path, *this, 0);
207 }
208
209 // Remove version object from image manager
210 Activation::deleteImageManagerObject();
211
212 // Create active association
213 parent.createActiveAssociation(path);
214
215 // Create updateable association as this
216 // can be re-programmed.
217 parent.createUpdateableAssociation(path);
218
219 if (Activation::checkApplyTimeImmediate() == true)
220 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500221 info("Image Active and ApplyTime is immediate; rebooting BMC.");
Adriana Kobylakb824b2f2020-05-14 14:57:53 -0500222 Activation::rebootBmc();
223 }
224 else
225 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500226 info("BMC image ready; need reboot to get activated.");
Adriana Kobylakb824b2f2020-05-14 14:57:53 -0500227 }
228
229 activation(softwareServer::Activation::Activations::Active);
230}
231
Saqib Khanee13e832017-10-23 12:53:11 -0500232void Activation::deleteImageManagerObject()
233{
Saqib Khanee13e832017-10-23 12:53:11 -0500234 // Call the Delete object for <versionID> inside image_manager
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600235 auto method = this->bus.new_method_call(VERSION_BUSNAME, path.c_str(),
236 "xyz.openbmc_project.Object.Delete",
237 "Delete");
Adriana Kobylak3b6a4cd2018-12-10 13:45:09 -0600238 try
239 {
240 bus.call_noreply(method);
241 }
242 catch (const SdBusError& e)
Saqib Khanee13e832017-10-23 12:53:11 -0500243 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500244 error("Error deleting image ({PATH}) from image manager: {ERROR}",
245 "PATH", path, "ERROR", e);
Saqib Khanee13e832017-10-23 12:53:11 -0500246 return;
247 }
248}
249
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600250auto Activation::requestedActivation(RequestedActivations value)
251 -> RequestedActivations
Saqib Khanb0774702017-05-23 16:02:41 -0500252{
Michael Tritzbed88af2017-07-19 16:00:06 -0500253 rwVolumeCreated = false;
254 roVolumeCreated = false;
Adriana Kobylak166bdf32018-04-09 14:24:06 -0500255 ubootEnvVarsUpdated = false;
Michael Tritzbed88af2017-07-19 16:00:06 -0500256
Saqib Khanb0774702017-05-23 16:02:41 -0500257 if ((value == softwareServer::Activation::RequestedActivations::Active) &&
258 (softwareServer::Activation::requestedActivation() !=
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600259 softwareServer::Activation::RequestedActivations::Active))
Saqib Khanb0774702017-05-23 16:02:41 -0500260 {
261 if ((softwareServer::Activation::activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600262 softwareServer::Activation::Activations::Ready) ||
Saqib Khanb0774702017-05-23 16:02:41 -0500263 (softwareServer::Activation::activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600264 softwareServer::Activation::Activations::Failed))
Saqib Khanb0774702017-05-23 16:02:41 -0500265 {
266 Activation::activation(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600267 softwareServer::Activation::Activations::Activating);
Saqib Khanb0774702017-05-23 16:02:41 -0500268 }
269 }
270 return softwareServer::Activation::requestedActivation(value);
271}
272
Saqib Khan4c1aec02017-07-06 11:46:13 -0500273uint8_t RedundancyPriority::priority(uint8_t value)
274{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500275 // Set the priority value so that the freePriority() function can order
276 // the versions by priority.
277 auto newPriority = softwareServer::RedundancyPriority::priority(value);
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500278 parent.parent.savePriority(parent.versionId, value);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500279 parent.parent.freePriority(value, parent.versionId);
280 return newPriority;
Saqib Khan4c1aec02017-07-06 11:46:13 -0500281}
282
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500283uint8_t RedundancyPriority::sdbusPriority(uint8_t value)
Saqib Khanf0382c32017-10-24 13:36:22 -0500284{
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500285 parent.parent.savePriority(parent.versionId, value);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500286 return softwareServer::RedundancyPriority::priority(value);
Saqib Khanf0382c32017-10-24 13:36:22 -0500287}
288
Michael Tritzbed88af2017-07-19 16:00:06 -0500289void Activation::unitStateChange(sdbusplus::message::message& msg)
290{
Michael Tritz0edd4ad2017-07-26 14:27:42 -0500291 if (softwareServer::Activation::activation() !=
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600292 softwareServer::Activation::Activations::Activating)
Michael Tritz0edd4ad2017-07-26 14:27:42 -0500293 {
294 return;
295 }
296
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800297#ifdef HOST_BIOS_UPGRADE
298 auto purpose = parent.versions.find(versionId)->second->purpose();
299 if (purpose == VersionPurpose::Host)
300 {
301 onStateChangesBios(msg);
302 return;
303 }
304#endif
305
Adriana Kobylak3ce563a2018-06-06 16:41:15 -0500306 onStateChanges(msg);
Michael Tritzbed88af2017-07-19 16:00:06 -0500307
308 return;
309}
310
Lei YU90532252018-05-24 11:15:24 +0800311#ifdef WANT_SIGNATURE_VERIFY
312bool Activation::verifySignature(const fs::path& imageDir,
313 const fs::path& confDir)
314{
315 using Signature = phosphor::software::image::Signature;
316
317 Signature signature(imageDir, confDir);
318
319 return signature.verify();
320}
321
322void Activation::onVerifyFailed()
323{
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500324 error("Error occurred during image validation");
Lei YU90532252018-05-24 11:15:24 +0800325 report<InternalFailure>();
326}
327#endif
328
Saqib Khanf37cefc2017-09-12 08:44:41 -0500329void ActivationBlocksTransition::enableRebootGuard()
330{
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500331 info("BMC image activating - BMC reboots are disabled.");
Saqib Khanf37cefc2017-09-12 08:44:41 -0500332
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600333 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
334 SYSTEMD_INTERFACE, "StartUnit");
Saqib Khanf37cefc2017-09-12 08:44:41 -0500335 method.append("reboot-guard-enable.service", "replace");
336 bus.call_noreply(method);
337}
338
339void ActivationBlocksTransition::disableRebootGuard()
340{
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500341 info("BMC activation has ended - BMC reboots are re-enabled.");
Saqib Khanf37cefc2017-09-12 08:44:41 -0500342
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600343 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
344 SYSTEMD_INTERFACE, "StartUnit");
Saqib Khanf37cefc2017-09-12 08:44:41 -0500345 method.append("reboot-guard-disable.service", "replace");
346 bus.call_noreply(method);
347}
Michael Tritzbed88af2017-07-19 16:00:06 -0500348
Jayashankar Padatha0135602019-04-22 16:22:58 +0530349bool Activation::checkApplyTimeImmediate()
350{
351 auto service = utils::getService(bus, applyTimeObjPath, applyTimeIntf);
352 if (service.empty())
353 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500354 info("Error getting the service name for BMC image ApplyTime. "
355 "The BMC needs to be manually rebooted to complete the image "
356 "activation if needed immediately.");
Jayashankar Padatha0135602019-04-22 16:22:58 +0530357 }
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 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500379 error("Error in getting ApplyTime: {ERROR}", "ERROR", e);
Jayashankar Padatha0135602019-04-22 16:22:58 +0530380 }
381 }
382 return false;
383}
384
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800385#ifdef HOST_BIOS_UPGRADE
386void Activation::flashWriteHost()
387{
388 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
389 SYSTEMD_INTERFACE, "StartUnit");
390 auto biosServiceFile = "obmc-flash-host-bios@" + versionId + ".service";
391 method.append(biosServiceFile, "replace");
392 try
393 {
394 auto reply = bus.call(method);
395 }
396 catch (const SdBusError& e)
397 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500398 error("Error in trying to upgrade Host Bios: {ERROR}", "ERROR", e);
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800399 report<InternalFailure>();
400 }
401}
402
403void Activation::onStateChangesBios(sdbusplus::message::message& msg)
404{
405 uint32_t newStateID{};
406 sdbusplus::message::object_path newStateObjPath;
407 std::string newStateUnit{};
408 std::string newStateResult{};
409
410 // Read the msg and populate each variable
411 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
412
413 auto biosServiceFile = "obmc-flash-host-bios@" + versionId + ".service";
414
415 if (newStateUnit == biosServiceFile)
416 {
417 // unsubscribe to systemd signals
418 unsubscribeFromSystemdSignals();
419
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800420 if (newStateResult == "done")
421 {
Lei YU837de882020-11-03 19:21:14 +0800422 // Remove version object from image manager
423 deleteImageManagerObject();
424
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800425 // Set activation progress to 100
426 activationProgress->progress(100);
427
428 // Set Activation value to active
429 activation(softwareServer::Activation::Activations::Active);
430
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500431 info("Bios upgrade completed successfully.");
Lei YU16aa28a2021-05-07 10:17:30 +0800432 parent.biosVersion->version(
433 parent.versions.find(versionId)->second->version());
Lei YU25868182021-05-14 14:50:51 +0800434
435 // Delete the uploaded activation
Lei YU7d2fa142021-08-06 10:58:19 +0800436 boost::asio::post(getIOContext(), [this]() {
437 this->parent.erase(this->versionId);
Lei YU25868182021-05-14 14:50:51 +0800438 });
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800439 }
440 else if (newStateResult == "failed")
441 {
442 // Set Activation value to Failed
443 activation(softwareServer::Activation::Activations::Failed);
444
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500445 error("Bios upgrade failed.");
Vijay Khemkae9f6c842020-01-14 14:32:39 -0800446 }
447 }
448
449 return;
450}
451
452#endif
453
Jayashankar Padatha0135602019-04-22 16:22:58 +0530454void Activation::rebootBmc()
455{
456 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
457 SYSTEMD_INTERFACE, "StartUnit");
458 method.append("force-reboot.service", "replace");
459 try
460 {
461 auto reply = bus.call(method);
462 }
463 catch (const SdBusError& e)
464 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500465 alert("Error in trying to reboot the BMC. The BMC needs to be manually "
466 "rebooted to complete the image activation. {ERROR}",
467 "ERROR", e);
Jayashankar Padatha0135602019-04-22 16:22:58 +0530468 report<InternalFailure>();
469 }
470}
471
Saqib Khanb0774702017-05-23 16:02:41 -0500472} // namespace updater
473} // namespace software
474} // namespace phosphor