blob: 48baf6d594d14a0bb18b784d410e2d77d5ef2330 [file] [log] [blame]
Adriana Kobylak692b5552017-04-17 14:02:58 -05001#include "config.h"
Gunnar Millsf6ed5892018-09-07 17:08:02 -05002
3#include "activation.hpp"
4
Saqib Khan81bac882017-06-08 12:17:01 -05005#include "item_updater.hpp"
Gunnar Millsf6ed5892018-09-07 17:08:02 -05006
7#include <experimental/filesystem>
Jayashankar Padath4d3d9122019-07-24 16:46:22 +05308#include <phosphor-logging/elog-errors.hpp>
9#include <phosphor-logging/elog.hpp>
Saqib Khan7f80e0b2017-10-22 11:29:07 -050010#include <phosphor-logging/log.hpp>
Gunnar Mills74b657e2018-07-13 09:27:31 -050011#include <sdbusplus/exception.hpp>
Jayashankar Padath4d3d9122019-07-24 16:46:22 +053012#include <sdbusplus/server.hpp>
13#include <xyz/openbmc_project/Common/error.hpp>
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -050014
Jayanth Othayoth4016e522018-03-20 09:39:06 -050015#ifdef WANT_SIGNATURE_VERIFY
Jayanth Othayoth4016e522018-03-20 09:39:06 -050016#include "image_verify.hpp"
Jayanth Othayoth4016e522018-03-20 09:39:06 -050017#endif
18
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -050019namespace openpower
20{
21namespace software
22{
23namespace updater
24{
25
Adriana Kobylak55f9e832017-05-14 16:13:00 -050026namespace fs = std::experimental::filesystem;
Adriana Kobylak99c8c0e2017-04-17 13:39:11 -050027namespace softwareServer = sdbusplus::xyz::openbmc_project::Software::server;
28
Saqib Khan7f80e0b2017-10-22 11:29:07 -050029using namespace phosphor::logging;
Gunnar Mills74b657e2018-07-13 09:27:31 -050030using sdbusplus::exception::SdBusError;
Jayanth Othayoth4016e522018-03-20 09:39:06 -050031using InternalFailure =
32 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Jayanth Othayoth11271fb2018-03-29 10:25:50 -050033
Jayashankar Padath4d3d9122019-07-24 16:46:22 +053034#ifdef WANT_SIGNATURE_VERIFY
Jayanth Othayoth11271fb2018-03-29 10:25:50 -050035// Field mode path and interface.
36constexpr auto FIELDMODE_PATH("/xyz/openbmc_project/software");
37constexpr auto FIELDMODE_INTERFACE("xyz.openbmc_project.Control.FieldMode");
Jayanth Othayoth4016e522018-03-20 09:39:06 -050038#endif
39
Adriana Kobylak70dcb632018-02-27 15:46:52 -060040constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
41constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
Michael Tritz9d25b602017-06-14 14:41:43 -050042
43void Activation::subscribeToSystemdSignals()
44{
Adriana Kobylak70dcb632018-02-27 15:46:52 -060045 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
46 SYSTEMD_INTERFACE, "Subscribe");
Gunnar Mills74b657e2018-07-13 09:27:31 -050047 try
48 {
49 this->bus.call_noreply(method);
50 }
51 catch (const SdBusError& e)
52 {
53 if (e.name() != nullptr &&
54 strcmp("org.freedesktop.systemd1.AlreadySubscribed", e.name()) == 0)
55 {
56 // If an Activation attempt fails, the Unsubscribe method is not
57 // called. This may lead to an AlreadySubscribed error if the
58 // Activation is re-attempted.
59 }
60 else
61 {
62 log<level::ERR>("Error subscribing to systemd",
63 entry("ERROR=%s", e.what()));
64 }
65 }
Michael Tritz9d25b602017-06-14 14:41:43 -050066 return;
67}
68
Michael Tritz1cb127f2017-07-26 15:40:38 -050069void Activation::unsubscribeFromSystemdSignals()
70{
Adriana Kobylak70dcb632018-02-27 15:46:52 -060071 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
72 SYSTEMD_INTERFACE, "Unsubscribe");
Michael Tritz1cb127f2017-07-26 15:40:38 -050073 this->bus.call_noreply(method);
74
75 return;
76}
77
Adriana Kobylak70dcb632018-02-27 15:46:52 -060078auto Activation::requestedActivation(RequestedActivations value)
79 -> RequestedActivations
Adriana Kobylak2fdb9312017-05-14 19:08:26 -050080{
81 if ((value == softwareServer::Activation::RequestedActivations::Active) &&
82 (softwareServer::Activation::requestedActivation() !=
Adriana Kobylak70dcb632018-02-27 15:46:52 -060083 softwareServer::Activation::RequestedActivations::Active))
Adriana Kobylak2fdb9312017-05-14 19:08:26 -050084 {
85 if ((softwareServer::Activation::activation() ==
Adriana Kobylak70dcb632018-02-27 15:46:52 -060086 softwareServer::Activation::Activations::Ready) ||
Adriana Kobylak2fdb9312017-05-14 19:08:26 -050087 (softwareServer::Activation::activation() ==
Adriana Kobylak70dcb632018-02-27 15:46:52 -060088 softwareServer::Activation::Activations::Failed))
Adriana Kobylak2fdb9312017-05-14 19:08:26 -050089 {
Lei YUa2e67162019-02-22 17:35:24 +080090 activation(softwareServer::Activation::Activations::Activating);
Adriana Kobylak2fdb9312017-05-14 19:08:26 -050091 }
92 }
Adriana Kobylak99c8c0e2017-04-17 13:39:11 -050093 return softwareServer::Activation::requestedActivation(value);
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -050094}
95
Saqib Khan7f80e0b2017-10-22 11:29:07 -050096void Activation::deleteImageManagerObject()
97{
98 // Get the Delete object for <versionID> inside image_manager
Lei YUc9caf862019-01-24 15:40:25 +080099 constexpr auto versionServiceStr = "xyz.openbmc_project.Software.Version";
100 constexpr auto deleteInterface = "xyz.openbmc_project.Object.Delete";
101 std::string versionService;
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600102 auto method = this->bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
103 MAPPER_INTERFACE, "GetObject");
Saqib Khan7f80e0b2017-10-22 11:29:07 -0500104
105 method.append(path);
Lei YUc9caf862019-01-24 15:40:25 +0800106 method.append(std::vector<std::string>({deleteInterface}));
Adriana Kobylakb8cb0cc2019-05-31 09:58:04 -0500107
108 std::map<std::string, std::vector<std::string>> mapperResponse;
109
110 try
111 {
112 auto mapperResponseMsg = bus.call(method);
113 mapperResponseMsg.read(mapperResponse);
114 if (mapperResponse.begin() == mapperResponse.end())
115 {
116 log<level::ERR>("ERROR in reading the mapper response",
117 entry("VERSIONPATH=%s", path.c_str()));
118 return;
119 }
120 }
121 catch (const SdBusError& e)
Saqib Khan7f80e0b2017-10-22 11:29:07 -0500122 {
123 log<level::ERR>("Error in Get Delete Object",
Joseph Reynoldsafd0a452018-05-30 11:16:03 -0500124 entry("VERSIONPATH=%s", path.c_str()));
Saqib Khan7f80e0b2017-10-22 11:29:07 -0500125 return;
126 }
Saqib Khan7f80e0b2017-10-22 11:29:07 -0500127
Lei YUc9caf862019-01-24 15:40:25 +0800128 // We need to find the phosphor-software-manager's version service
129 // to invoke the delete interface
130 for (auto resp : mapperResponse)
131 {
132 if (resp.first.find(versionServiceStr) != std::string::npos)
133 {
134 versionService = resp.first;
135 }
136 }
137
138 if (versionService.empty())
139 {
140 log<level::ERR>("Error finding version service");
141 return;
142 }
143
Saqib Khan7f80e0b2017-10-22 11:29:07 -0500144 // Call the Delete object for <versionID> inside image_manager
Lei YUc9caf862019-01-24 15:40:25 +0800145 method = this->bus.new_method_call(versionService.c_str(), path.c_str(),
146 deleteInterface, "Delete");
Adriana Kobylakab435df2018-07-16 11:37:19 -0500147 try
Saqib Khan7f80e0b2017-10-22 11:29:07 -0500148 {
Adriana Kobylakb8cb0cc2019-05-31 09:58:04 -0500149 bus.call(method);
Adriana Kobylakab435df2018-07-16 11:37:19 -0500150 }
151 catch (const SdBusError& e)
152 {
153 if (e.name() != nullptr && strcmp("System.Error.ELOOP", e.name()) == 0)
154 {
155 // TODO: Error being tracked with openbmc/openbmc#3311
156 }
157 else
158 {
159 log<level::ERR>("Error performing call to Delete object path",
160 entry("ERROR=%s", e.what()),
161 entry("PATH=%s", path.c_str()));
162 }
Saqib Khan7f80e0b2017-10-22 11:29:07 -0500163 return;
164 }
165}
166
Jayashankar Padath4d3d9122019-07-24 16:46:22 +0530167bool Activation::checkApplyTimeImmediate()
168{
169 auto service = utils::getService(bus, applyTimeObjPath, applyTimeIntf);
170 if (service.empty())
171 {
172 log<level::INFO>("Error getting the service name for Host image "
173 "ApplyTime. The Host needs to be manually rebooted to "
174 "complete the image activation if needed "
175 "immediately.");
176 }
177 else
178 {
179
180 auto method = bus.new_method_call(service.c_str(), applyTimeObjPath,
181 dbusPropIntf, "Get");
182 method.append(applyTimeIntf, applyTimeProp);
183
184 try
185 {
186 auto reply = bus.call(method);
187
188 sdbusplus::message::variant<std::string> result;
189 reply.read(result);
190 auto applyTime =
191 sdbusplus::message::variant_ns::get<std::string>(result);
192 if (applyTime == applyTimeImmediate)
193 {
194 return true;
195 }
196 }
197 catch (const SdBusError& e)
198 {
199 log<level::ERR>("Error in getting ApplyTime",
200 entry("ERROR=%s", e.what()));
201 }
202 }
203 return false;
204}
205
206void Activation::rebootHost()
207{
208 auto service = utils::getService(bus, hostStateObjPath, hostStateIntf);
209 if (service.empty())
210 {
211 log<level::ALERT>("Error in getting the service name to reboot the "
212 "Host. The Host needs to be manually rebooted to "
213 "complete the image activation.");
214 }
215
216 auto method = bus.new_method_call(service.c_str(), hostStateObjPath,
217 dbusPropIntf, "Set");
218 sdbusplus::message::variant<std::string> hostReboot = hostStateRebootVal;
219 method.append(hostStateIntf, hostStateRebootProp, hostReboot);
220
221 try
222 {
223 auto reply = bus.call(method);
224 }
225 catch (const SdBusError& e)
226 {
227 log<level::ALERT>("Error in trying to reboot the Host. "
228 "The Host needs to be manually rebooted to complete "
229 "the image activation.",
230 entry("ERROR=%s", e.what()));
231 report<InternalFailure>();
232 }
233}
234
Saqib Khan2021b4c2017-06-07 14:37:36 -0500235uint8_t RedundancyPriority::priority(uint8_t value)
236{
Saqib Khanb8e7f312017-08-12 10:24:10 -0500237 parent.parent.freePriority(value, parent.versionId);
Saqib Khan2021b4c2017-06-07 14:37:36 -0500238 return softwareServer::RedundancyPriority::priority(value);
239}
240
Jayanth Othayoth11271fb2018-03-29 10:25:50 -0500241#ifdef WANT_SIGNATURE_VERIFY
Lei YU2b2d2292019-03-18 15:22:56 +0800242bool Activation::validateSignature(const std::string& pnorFileName)
Jayanth Othayoth11271fb2018-03-29 10:25:50 -0500243{
244 using Signature = openpower::software::image::Signature;
245 fs::path imageDir(IMG_DIR);
246
Lei YU2b2d2292019-03-18 15:22:56 +0800247 Signature signature(imageDir / versionId, pnorFileName,
248 PNOR_SIGNED_IMAGE_CONF_PATH);
Jayanth Othayoth11271fb2018-03-29 10:25:50 -0500249
250 // Validate the signed image.
251 if (signature.verify())
252 {
253 return true;
254 }
255 // Log error and continue activation process, if field mode disabled.
256 log<level::ERR>("Error occurred during image validation");
257 report<InternalFailure>();
258
259 try
260 {
261 if (!fieldModeEnabled())
262 {
263 return true;
264 }
265 }
266 catch (const InternalFailure& e)
267 {
268 report<InternalFailure>();
269 }
270 return false;
271}
272
273bool Activation::fieldModeEnabled()
274{
Lei YUe4994462019-03-14 14:41:53 +0800275 auto fieldModeSvc =
276 utils::getService(bus, FIELDMODE_PATH, FIELDMODE_INTERFACE);
Jayanth Othayoth11271fb2018-03-29 10:25:50 -0500277
278 auto method = bus.new_method_call(fieldModeSvc.c_str(), FIELDMODE_PATH,
279 "org.freedesktop.DBus.Properties", "Get");
280
281 method.append(FIELDMODE_INTERFACE, "FieldModeEnabled");
Adriana Kobylakb8cb0cc2019-05-31 09:58:04 -0500282
283 sdbusplus::message::variant<bool> fieldMode;
284
285 try
286 {
287 auto reply = bus.call(method);
288 reply.read(fieldMode);
289 return sdbusplus::message::variant_ns::get<bool>(fieldMode);
290 }
291 catch (const SdBusError& e)
Jayanth Othayoth11271fb2018-03-29 10:25:50 -0500292 {
293 log<level::ERR>("Error in fieldModeEnabled getValue");
294 elog<InternalFailure>();
295 }
Jayanth Othayoth11271fb2018-03-29 10:25:50 -0500296}
297
Jayanth Othayoth11271fb2018-03-29 10:25:50 -0500298#endif
299
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -0500300} // namespace updater
301} // namespace software
302} // namespace openpower