blob: 92de6a6280428ea3080b40dc413232e1ffc98b5a [file] [log] [blame]
Saqib Khanb0774702017-05-23 16:02:41 -05001#include "activation.hpp"
Saqib Khan4c1aec02017-07-06 11:46:13 -05002#include "item_updater.hpp"
Michael Tritzf2b5e0d2017-07-25 14:39:34 -05003#include "config.h"
Saqib Khan5d532672017-08-09 10:44:50 -05004#include "serialize.hpp"
Saqib Khanb9da6632017-09-13 09:48:37 -05005#include <phosphor-logging/log.hpp>
Adriana Kobylakaea48f22018-07-10 10:20:56 -05006#include <sdbusplus/exception.hpp>
Saqib Khanb9da6632017-09-13 09:48:37 -05007
Jayanth Othayoth0e0c1272018-02-21 05:46:36 -06008#ifdef WANT_SIGNATURE_VERIFY
9#include <phosphor-logging/elog.hpp>
10#include <phosphor-logging/elog-errors.hpp>
11#include <xyz/openbmc_project/Common/error.hpp>
12#include "image_verify.hpp"
Jayanth Othayoth0e0c1272018-02-21 05:46:36 -060013#endif
14
Saqib Khanb0774702017-05-23 16:02:41 -050015namespace phosphor
16{
17namespace software
18{
19namespace updater
20{
21
22namespace softwareServer = sdbusplus::xyz::openbmc_project::Software::server;
23
Saqib Khanb9da6632017-09-13 09:48:37 -050024using namespace phosphor::logging;
Adriana Kobylakaea48f22018-07-10 10:20:56 -050025using sdbusplus::exception::SdBusError;
Saqib Khanb9da6632017-09-13 09:48:37 -050026
Jayanth Othayoth0e0c1272018-02-21 05:46:36 -060027#ifdef WANT_SIGNATURE_VERIFY
28using InternalFailure =
29 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Jayanth Othayoth9a9d7c22018-03-28 10:05:26 -050030namespace control = sdbusplus::xyz::openbmc_project::Control::server;
Jayanth Othayoth0e0c1272018-02-21 05:46:36 -060031#endif
32
Michael Tritzbed88af2017-07-19 16:00:06 -050033void Activation::subscribeToSystemdSignals()
34{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060035 auto method = this->bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
36 SYSTEMD_INTERFACE, "Subscribe");
Adriana Kobylakaea48f22018-07-10 10:20:56 -050037 try
38 {
39 this->bus.call_noreply(method);
40 }
41 catch (const SdBusError& e)
42 {
43 if (e.name() != nullptr &&
44 strcmp("org.freedesktop.systemd1.AlreadySubscribed", e.name()) == 0)
45 {
46 // If an Activation attempt fails, the Unsubscribe method is not
47 // called. This may lead to an AlreadySubscribed error if the
48 // Activation is re-attempted.
49 }
50 else
51 {
52 log<level::ERR>("Error subscribing to systemd",
53 entry("ERROR=%s", e.what()));
54 }
55 }
Michael Tritzbed88af2017-07-19 16:00:06 -050056
57 return;
58}
59
Michael Tritzf2b5e0d2017-07-25 14:39:34 -050060void Activation::unsubscribeFromSystemdSignals()
61{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060062 auto method = this->bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
63 SYSTEMD_INTERFACE, "Unsubscribe");
Michael Tritzf2b5e0d2017-07-25 14:39:34 -050064 this->bus.call_noreply(method);
65
66 return;
67}
68
Adriana Kobylak2285fe02018-02-27 15:36:59 -060069auto Activation::activation(Activations value) -> Activations
Saqib Khanb0774702017-05-23 16:02:41 -050070{
Saqib Khan4c1aec02017-07-06 11:46:13 -050071
Adriana Kobylak8bd84c82018-01-24 14:19:24 -060072 if ((value != softwareServer::Activation::Activations::Active) &&
73 (value != softwareServer::Activation::Activations::Activating))
Saqib Khan4c1aec02017-07-06 11:46:13 -050074 {
75 redundancyPriority.reset(nullptr);
76 }
77
Saqib Khanb0774702017-05-23 16:02:41 -050078 if (value == softwareServer::Activation::Activations::Activating)
79 {
Michael Tritzbed88af2017-07-19 16:00:06 -050080 if (rwVolumeCreated == false && roVolumeCreated == false)
Saqib Khanb0774702017-05-23 16:02:41 -050081 {
Adriana Kobylakaea48f22018-07-10 10:20:56 -050082 // Enable systemd signals
83 Activation::subscribeToSystemdSignals();
84
Adriana Kobylak204e1e72018-01-24 16:00:05 -060085 parent.freeSpace();
86
Michael Tritz0edd4ad2017-07-26 14:27:42 -050087 if (!activationProgress)
88 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -060089 activationProgress =
90 std::make_unique<ActivationProgress>(bus, path);
Michael Tritz0edd4ad2017-07-26 14:27:42 -050091 }
92
Michael Tritzbed88af2017-07-19 16:00:06 -050093 if (!activationBlocksTransition)
94 {
95 activationBlocksTransition =
Adriana Kobylak2285fe02018-02-27 15:36:59 -060096 std::make_unique<ActivationBlocksTransition>(bus, path);
Michael Tritzbed88af2017-07-19 16:00:06 -050097 }
Saqib Khan4c1aec02017-07-06 11:46:13 -050098
Jayanth Othayoth0e0c1272018-02-21 05:46:36 -060099#ifdef WANT_SIGNATURE_VERIFY
100 using Signature = phosphor::software::image::Signature;
101
102 fs::path uploadDir(IMG_UPLOAD_DIR);
103
104 Signature signature(uploadDir / versionId, SIGNED_IMAGE_CONF_PATH);
105
106 // Validate the signed image.
107 if (!signature.verify())
108 {
109 log<level::ERR>("Error occurred during image validation");
110 report<InternalFailure>();
111
Jayanth Othayoth9a9d7c22018-03-28 10:05:26 -0500112 // Stop the activation process, if fieldMode is enabled.
113 if (parent.control::FieldMode::fieldModeEnabled())
114 {
115 // Cleanup
116 activationBlocksTransition.reset(nullptr);
117 activationProgress.reset(nullptr);
118
119 return softwareServer::Activation::activation(
120 softwareServer::Activation::Activations::Failed);
121 }
Jayanth Othayoth0e0c1272018-02-21 05:46:36 -0600122 }
123#endif
124
Adriana Kobylak9f89e2e2018-05-30 13:16:20 -0500125 flashWrite();
Michael Tritz0edd4ad2017-07-26 14:27:42 -0500126
127 activationProgress->progress(10);
Michael Tritzbed88af2017-07-19 16:00:06 -0500128 }
129 else if (rwVolumeCreated == true && roVolumeCreated == true)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500130 {
Adriana Kobylak166bdf32018-04-09 14:24:06 -0500131 if (ubootEnvVarsUpdated == false)
Michael Tritzbed88af2017-07-19 16:00:06 -0500132 {
Adriana Kobylak166bdf32018-04-09 14:24:06 -0500133 activationProgress->progress(90);
134
135 if (!redundancyPriority)
136 {
137 redundancyPriority = std::make_unique<RedundancyPriority>(
138 bus, path, *this, 0);
139 }
Michael Tritzbed88af2017-07-19 16:00:06 -0500140 }
Adriana Kobylak166bdf32018-04-09 14:24:06 -0500141 else
142 {
143 activationProgress->progress(100);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500144
Adriana Kobylak166bdf32018-04-09 14:24:06 -0500145 activationBlocksTransition.reset(nullptr);
146 activationProgress.reset(nullptr);
Michael Tritz0edd4ad2017-07-26 14:27:42 -0500147
Adriana Kobylak166bdf32018-04-09 14:24:06 -0500148 rwVolumeCreated = false;
149 roVolumeCreated = false;
150 ubootEnvVarsUpdated = false;
151 Activation::unsubscribeFromSystemdSignals();
Michael Tritzf2b5e0d2017-07-25 14:39:34 -0500152
Adriana Kobylak166bdf32018-04-09 14:24:06 -0500153 // Remove version object from image manager
154 Activation::deleteImageManagerObject();
Michael Tritzf2b5e0d2017-07-25 14:39:34 -0500155
Adriana Kobylak166bdf32018-04-09 14:24:06 -0500156 // Create active association
157 parent.createActiveAssociation(path);
Saqib Khanee13e832017-10-23 12:53:11 -0500158
Adriana Kobylak166bdf32018-04-09 14:24:06 -0500159 return softwareServer::Activation::activation(
160 softwareServer::Activation::Activations::Active);
161 }
Michael Tritzbed88af2017-07-19 16:00:06 -0500162 }
Saqib Khanb0774702017-05-23 16:02:41 -0500163 }
164 else
165 {
166 activationBlocksTransition.reset(nullptr);
Michael Tritz0edd4ad2017-07-26 14:27:42 -0500167 activationProgress.reset(nullptr);
Saqib Khanb0774702017-05-23 16:02:41 -0500168 }
169 return softwareServer::Activation::activation(value);
170}
171
Saqib Khanee13e832017-10-23 12:53:11 -0500172void Activation::deleteImageManagerObject()
173{
Saqib Khanee13e832017-10-23 12:53:11 -0500174 // Call the Delete object for <versionID> inside image_manager
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600175 auto method = this->bus.new_method_call(VERSION_BUSNAME, path.c_str(),
176 "xyz.openbmc_project.Object.Delete",
177 "Delete");
Adriana Kobylak00bd9022018-01-24 13:55:58 -0600178 auto mapperResponseMsg = bus.call(method);
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600179 // Check that the bus call didn't result in an error
Saqib Khanee13e832017-10-23 12:53:11 -0500180 if (mapperResponseMsg.is_method_error())
181 {
182 log<level::ERR>("Error in Deleting image from image manager",
Adriana Kobylak596466b2018-02-13 14:48:53 -0600183 entry("VERSIONPATH=%s", path.c_str()));
Saqib Khanee13e832017-10-23 12:53:11 -0500184 return;
185 }
186}
187
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600188auto Activation::requestedActivation(RequestedActivations value)
189 -> RequestedActivations
Saqib Khanb0774702017-05-23 16:02:41 -0500190{
Michael Tritzbed88af2017-07-19 16:00:06 -0500191 rwVolumeCreated = false;
192 roVolumeCreated = false;
Adriana Kobylak166bdf32018-04-09 14:24:06 -0500193 ubootEnvVarsUpdated = false;
Michael Tritzbed88af2017-07-19 16:00:06 -0500194
Saqib Khanb0774702017-05-23 16:02:41 -0500195 if ((value == softwareServer::Activation::RequestedActivations::Active) &&
196 (softwareServer::Activation::requestedActivation() !=
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600197 softwareServer::Activation::RequestedActivations::Active))
Saqib Khanb0774702017-05-23 16:02:41 -0500198 {
199 if ((softwareServer::Activation::activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600200 softwareServer::Activation::Activations::Ready) ||
Saqib Khanb0774702017-05-23 16:02:41 -0500201 (softwareServer::Activation::activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600202 softwareServer::Activation::Activations::Failed))
Saqib Khanb0774702017-05-23 16:02:41 -0500203 {
204 Activation::activation(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600205 softwareServer::Activation::Activations::Activating);
Saqib Khanb0774702017-05-23 16:02:41 -0500206 }
207 }
208 return softwareServer::Activation::requestedActivation(value);
209}
210
Saqib Khan4c1aec02017-07-06 11:46:13 -0500211uint8_t RedundancyPriority::priority(uint8_t value)
212{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500213 // Set the priority value so that the freePriority() function can order
214 // the versions by priority.
215 auto newPriority = softwareServer::RedundancyPriority::priority(value);
Saqib Khan5d532672017-08-09 10:44:50 -0500216 storeToFile(parent.versionId, value);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500217 parent.parent.freePriority(value, parent.versionId);
218 return newPriority;
Saqib Khan4c1aec02017-07-06 11:46:13 -0500219}
220
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500221uint8_t RedundancyPriority::sdbusPriority(uint8_t value)
Saqib Khanf0382c32017-10-24 13:36:22 -0500222{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500223 storeToFile(parent.versionId, value);
224 return softwareServer::RedundancyPriority::priority(value);
Saqib Khanf0382c32017-10-24 13:36:22 -0500225}
226
Michael Tritzbed88af2017-07-19 16:00:06 -0500227void Activation::unitStateChange(sdbusplus::message::message& msg)
228{
Michael Tritz0edd4ad2017-07-26 14:27:42 -0500229 if (softwareServer::Activation::activation() !=
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600230 softwareServer::Activation::Activations::Activating)
Michael Tritz0edd4ad2017-07-26 14:27:42 -0500231 {
232 return;
233 }
234
Adriana Kobylak3ce563a2018-06-06 16:41:15 -0500235 onStateChanges(msg);
Michael Tritzbed88af2017-07-19 16:00:06 -0500236
237 return;
238}
239
Saqib Khanf37cefc2017-09-12 08:44:41 -0500240void ActivationBlocksTransition::enableRebootGuard()
241{
242 log<level::INFO>("BMC image activating - BMC reboots are disabled.");
243
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600244 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
245 SYSTEMD_INTERFACE, "StartUnit");
Saqib Khanf37cefc2017-09-12 08:44:41 -0500246 method.append("reboot-guard-enable.service", "replace");
247 bus.call_noreply(method);
248}
249
250void ActivationBlocksTransition::disableRebootGuard()
251{
252 log<level::INFO>("BMC activation has ended - BMC reboots are re-enabled.");
253
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600254 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
255 SYSTEMD_INTERFACE, "StartUnit");
Saqib Khanf37cefc2017-09-12 08:44:41 -0500256 method.append("reboot-guard-disable.service", "replace");
257 bus.call_noreply(method);
258}
Michael Tritzbed88af2017-07-19 16:00:06 -0500259
Saqib Khanb0774702017-05-23 16:02:41 -0500260} // namespace updater
261} // namespace software
262} // namespace phosphor