blob: f3987d4c74fa6cef16346a5983c92200beb7e0dc [file] [log] [blame]
Jennifer Lee729dae72018-04-24 15:59:34 -07001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Tejas Patild61e5192021-06-04 15:49:35 +053018#include "bmcweb_config.h"
19
John Edward Broadbent7e860f12021-04-08 15:57:16 -070020#include <app.hpp>
Jennifer Lee729dae72018-04-24 15:59:34 -070021#include <boost/container/flat_map.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070022#include <registries/privilege_registry.hpp>
Andrew Geissler87d84722019-02-28 14:28:39 -060023#include <utils/fw_utils.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050024
Ed Tanousabf2add2019-01-22 16:40:12 -080025#include <variant>
Jennifer Lee729dae72018-04-24 15:59:34 -070026
Ed Tanous1abe55e2018-09-05 08:30:59 -070027namespace redfish
28{
Ed Tanous27826b52018-10-29 11:40:58 -070029
Andrew Geissler0e7de462019-03-04 19:11:54 -060030// Match signals added on software path
Jennifer Leeacb7cfb2018-06-07 16:08:15 -070031static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher;
James Feist4cde5d92020-06-11 10:39:55 -070032static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateErrorMatcher;
Andrew Geissler0e7de462019-03-04 19:11:54 -060033// Only allow one update at a time
34static bool fwUpdateInProgress = false;
Andrew Geissler86adcd62019-04-18 10:58:05 -050035// Timer for software available
Ed Tanous271584a2019-07-09 16:24:22 -070036static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer;
Andrew Geissler86adcd62019-04-18 10:58:05 -050037
John Edward Broadbent7e860f12021-04-08 15:57:16 -070038inline static void cleanUp()
Andrew Geissler86adcd62019-04-18 10:58:05 -050039{
40 fwUpdateInProgress = false;
41 fwUpdateMatcher = nullptr;
James Feist4cde5d92020-06-11 10:39:55 -070042 fwUpdateErrorMatcher = nullptr;
Andrew Geissler86adcd62019-04-18 10:58:05 -050043}
John Edward Broadbent7e860f12021-04-08 15:57:16 -070044inline static void activateImage(const std::string& objPath,
45 const std::string& service)
Andrew Geissler86adcd62019-04-18 10:58:05 -050046{
47 BMCWEB_LOG_DEBUG << "Activate image for " << objPath << " " << service;
48 crow::connections::systemBus->async_method_call(
Ed Tanous81ce6092020-12-17 16:54:55 +000049 [](const boost::system::error_code errorCode) {
50 if (errorCode)
Andrew Geissler86adcd62019-04-18 10:58:05 -050051 {
Ed Tanous81ce6092020-12-17 16:54:55 +000052 BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
53 BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message();
Andrew Geissler86adcd62019-04-18 10:58:05 -050054 }
55 },
56 service, objPath, "org.freedesktop.DBus.Properties", "Set",
57 "xyz.openbmc_project.Software.Activation", "RequestedActivation",
58 std::variant<std::string>(
George Liu0fda0f12021-11-16 10:06:17 +080059 "xyz.openbmc_project.Software.Activation.RequestedActivations.Active"));
Andrew Geissler86adcd62019-04-18 10:58:05 -050060}
Andrew Geissler0554c982019-04-23 14:40:12 -050061
62// Note that asyncResp can be either a valid pointer or nullptr. If nullptr
63// then no asyncResp updates will occur
zhanghch058d1b46d2021-04-01 11:18:24 +080064static void
65 softwareInterfaceAdded(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
66 sdbusplus::message::message& m,
Ed Tanousa3e65892021-09-16 14:13:20 -070067 task::Payload&& payload)
Andrew Geissler86adcd62019-04-18 10:58:05 -050068{
69 std::vector<std::pair<
70 std::string,
71 std::vector<std::pair<std::string, std::variant<std::string>>>>>
72 interfacesProperties;
73
74 sdbusplus::message::object_path objPath;
75
76 m.read(objPath, interfacesProperties);
77
78 BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
Gunnar Mills1214b7e2020-06-04 10:11:30 -050079 for (auto& interface : interfacesProperties)
Andrew Geissler86adcd62019-04-18 10:58:05 -050080 {
81 BMCWEB_LOG_DEBUG << "interface = " << interface.first;
82
83 if (interface.first == "xyz.openbmc_project.Software.Activation")
84 {
Andrew Geissler86adcd62019-04-18 10:58:05 -050085 // Retrieve service and activate
86 crow::connections::systemBus->async_method_call(
Ed Tanousa3e65892021-09-16 14:13:20 -070087 [objPath, asyncResp, payload(std::move(payload))](
88 const boost::system::error_code errorCode,
89 const std::vector<
90 std::pair<std::string, std::vector<std::string>>>&
91 objInfo) mutable {
Ed Tanous81ce6092020-12-17 16:54:55 +000092 if (errorCode)
Andrew Geissler86adcd62019-04-18 10:58:05 -050093 {
Ed Tanous81ce6092020-12-17 16:54:55 +000094 BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
Andrew Geissler86adcd62019-04-18 10:58:05 -050095 BMCWEB_LOG_DEBUG << "error msg = "
Ed Tanous81ce6092020-12-17 16:54:55 +000096 << errorCode.message();
Andrew Geissler0554c982019-04-23 14:40:12 -050097 if (asyncResp)
98 {
99 messages::internalError(asyncResp->res);
100 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500101 cleanUp();
102 return;
103 }
104 // Ensure we only got one service back
105 if (objInfo.size() != 1)
106 {
107 BMCWEB_LOG_ERROR << "Invalid Object Size "
108 << objInfo.size();
Andrew Geissler0554c982019-04-23 14:40:12 -0500109 if (asyncResp)
110 {
111 messages::internalError(asyncResp->res);
112 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500113 cleanUp();
114 return;
115 }
116 // cancel timer only when
117 // xyz.openbmc_project.Software.Activation interface
118 // is added
119 fwAvailableTimer = nullptr;
120
121 activateImage(objPath.str, objInfo[0].first);
Andrew Geissler0554c982019-04-23 14:40:12 -0500122 if (asyncResp)
123 {
James Feist32898ce2020-03-10 16:16:52 -0700124 std::shared_ptr<task::TaskData> task =
125 task::TaskData::createTask(
126 [](boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500127 sdbusplus::message::message& msg,
128 const std::shared_ptr<task::TaskData>&
129 taskData) {
James Feist32898ce2020-03-10 16:16:52 -0700130 if (ec)
131 {
132 return task::completed;
133 }
134
135 std::string iface;
136 boost::container::flat_map<
James Feistfd9ab9e2020-05-19 13:48:07 -0700137 std::string,
138 std::variant<std::string, uint8_t>>
James Feist32898ce2020-03-10 16:16:52 -0700139 values;
James Feist32898ce2020-03-10 16:16:52 -0700140
James Feiste5d50062020-05-11 17:29:00 -0700141 std::string index =
142 std::to_string(taskData->index);
James Feistfd9ab9e2020-05-19 13:48:07 -0700143 msg.read(iface, values);
James Feiste5d50062020-05-11 17:29:00 -0700144
George Liu0fda0f12021-11-16 10:06:17 +0800145 if (iface ==
146 "xyz.openbmc_project.Software.Activation")
James Feist32898ce2020-03-10 16:16:52 -0700147 {
James Feistfd9ab9e2020-05-19 13:48:07 -0700148 auto findActivation =
149 values.find("Activation");
150 if (findActivation == values.end())
151 {
152 return !task::completed;
153 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500154 std::string* state =
James Feistfd9ab9e2020-05-19 13:48:07 -0700155 std::get_if<std::string>(
156 &(findActivation->second));
157
158 if (state == nullptr)
159 {
160 taskData->messages.emplace_back(
161 messages::internalError());
162 return task::completed;
163 }
164
165 if (boost::ends_with(*state,
166 "Invalid") ||
167 boost::ends_with(*state, "Failed"))
168 {
169 taskData->state = "Exception";
170 taskData->status = "Warning";
171 taskData->messages.emplace_back(
172 messages::taskAborted(index));
173 return task::completed;
174 }
175
176 if (boost::ends_with(*state, "Staged"))
177 {
178 taskData->state = "Stopping";
179 taskData->messages.emplace_back(
180 messages::taskPaused(index));
181
182 // its staged, set a long timer to
183 // allow them time to complete the
184 // update (probably cycle the
185 // system) if this expires then
186 // task will be cancelled
187 taskData->extendTimer(
188 std::chrono::hours(5));
189 return !task::completed;
190 }
191
192 if (boost::ends_with(*state, "Active"))
193 {
194 taskData->messages.emplace_back(
195 messages::taskCompletedOK(
196 index));
197 taskData->state = "Completed";
198 return task::completed;
199 }
James Feist32898ce2020-03-10 16:16:52 -0700200 }
George Liu0fda0f12021-11-16 10:06:17 +0800201 else if (
202 iface ==
203 "xyz.openbmc_project.Software.ActivationProgress")
James Feist32898ce2020-03-10 16:16:52 -0700204 {
James Feistfd9ab9e2020-05-19 13:48:07 -0700205 auto findProgress =
206 values.find("Progress");
207 if (findProgress == values.end())
208 {
209 return !task::completed;
210 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500211 uint8_t* progress =
James Feistfd9ab9e2020-05-19 13:48:07 -0700212 std::get_if<uint8_t>(
213 &(findProgress->second));
James Feist32898ce2020-03-10 16:16:52 -0700214
James Feistfd9ab9e2020-05-19 13:48:07 -0700215 if (progress == nullptr)
216 {
217 taskData->messages.emplace_back(
218 messages::internalError());
219 return task::completed;
220 }
George Liu6868ff52021-01-02 11:37:41 +0800221 taskData->percentComplete =
222 static_cast<int>(*progress);
James Feist32898ce2020-03-10 16:16:52 -0700223 taskData->messages.emplace_back(
James Feistfd9ab9e2020-05-19 13:48:07 -0700224 messages::taskProgressChanged(
225 index, static_cast<size_t>(
226 *progress)));
227
228 // if we're getting status updates it's
229 // still alive, update timer
230 taskData->extendTimer(
231 std::chrono::minutes(5));
James Feist32898ce2020-03-10 16:16:52 -0700232 }
233
234 // as firmware update often results in a
235 // reboot, the task may never "complete"
236 // unless it is an error
237
238 return !task::completed;
239 },
George Liu0fda0f12021-11-16 10:06:17 +0800240 "type='signal',interface='org.freedesktop.DBus.Properties',"
James Feistfd9ab9e2020-05-19 13:48:07 -0700241 "member='PropertiesChanged',path='" +
James Feist32898ce2020-03-10 16:16:52 -0700242 objPath.str + "'");
243 task->startTimer(std::chrono::minutes(5));
244 task->populateResp(asyncResp->res);
Ed Tanousa3e65892021-09-16 14:13:20 -0700245 task->payload.emplace(std::move(payload));
Andrew Geissler0554c982019-04-23 14:40:12 -0500246 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500247 fwUpdateInProgress = false;
248 },
249 "xyz.openbmc_project.ObjectMapper",
250 "/xyz/openbmc_project/object_mapper",
251 "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500252 std::array<const char*, 1>{
Andrew Geissler86adcd62019-04-18 10:58:05 -0500253 "xyz.openbmc_project.Software.Activation"});
254 }
255 }
256}
257
Andrew Geissler0554c982019-04-23 14:40:12 -0500258// Note that asyncResp can be either a valid pointer or nullptr. If nullptr
259// then no asyncResp updates will occur
Ed Tanousb5a76932020-09-29 16:16:58 -0700260static void monitorForSoftwareAvailable(
zhanghch058d1b46d2021-04-01 11:18:24 +0800261 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
262 const crow::Request& req, const std::string& url,
263 int timeoutTimeSeconds = 10)
Andrew Geissler86adcd62019-04-18 10:58:05 -0500264{
265 // Only allow one FW update at a time
266 if (fwUpdateInProgress != false)
267 {
Andrew Geissler0554c982019-04-23 14:40:12 -0500268 if (asyncResp)
269 {
Andrew Geissler0554c982019-04-23 14:40:12 -0500270 messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
271 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500272 return;
273 }
274
Andrew Geissler0554c982019-04-23 14:40:12 -0500275 fwAvailableTimer =
Ed Tanous271584a2019-07-09 16:24:22 -0700276 std::make_unique<boost::asio::steady_timer>(*req.ioService);
Andrew Geissler86adcd62019-04-18 10:58:05 -0500277
Ed Tanous271584a2019-07-09 16:24:22 -0700278 fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds));
Andrew Geissler86adcd62019-04-18 10:58:05 -0500279
280 fwAvailableTimer->async_wait(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500281 [asyncResp](const boost::system::error_code& ec) {
Andrew Geissler86adcd62019-04-18 10:58:05 -0500282 cleanUp();
283 if (ec == boost::asio::error::operation_aborted)
284 {
285 // expected, we were canceled before the timer completed.
286 return;
287 }
288 BMCWEB_LOG_ERROR
289 << "Timed out waiting for firmware object being created";
290 BMCWEB_LOG_ERROR
291 << "FW image may has already been uploaded to server";
292 if (ec)
293 {
294 BMCWEB_LOG_ERROR << "Async_wait failed" << ec;
295 return;
296 }
Andrew Geissler0554c982019-04-23 14:40:12 -0500297 if (asyncResp)
298 {
299 redfish::messages::internalError(asyncResp->res);
300 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500301 });
Ed Tanousa3e65892021-09-16 14:13:20 -0700302 task::Payload payload(req);
303 auto callback = [asyncResp,
304 payload](sdbusplus::message::message& m) mutable {
Andrew Geissler86adcd62019-04-18 10:58:05 -0500305 BMCWEB_LOG_DEBUG << "Match fired";
Ed Tanousa3e65892021-09-16 14:13:20 -0700306 softwareInterfaceAdded(asyncResp, m, std::move(payload));
Andrew Geissler86adcd62019-04-18 10:58:05 -0500307 };
308
309 fwUpdateInProgress = true;
310
311 fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
312 *crow::connections::systemBus,
313 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
314 "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
315 callback);
James Feist4cde5d92020-06-11 10:39:55 -0700316
317 fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match::match>(
318 *crow::connections::systemBus,
319 "type='signal',member='PropertiesChanged',path_namespace='/xyz/"
320 "openbmc_project/logging/entry',"
321 "arg0='xyz.openbmc_project.Logging.Entry'",
322 [asyncResp, url](sdbusplus::message::message& m) {
323 BMCWEB_LOG_DEBUG << "Error Match fired";
324 boost::container::flat_map<std::string, std::variant<std::string>>
325 values;
326 std::string objName;
327 m.read(objName, values);
328 auto find = values.find("Message");
329 if (find == values.end())
330 {
331 return;
332 }
333 std::string* type = std::get_if<std::string>(&(find->second));
334 if (type == nullptr)
335 {
336 return; // if this was our message, timeout will cover it
337 }
Gunnar Mills88b3dd12020-11-20 14:26:04 -0600338 if (!boost::starts_with(*type, "xyz.openbmc_project.Software"))
James Feist4cde5d92020-06-11 10:39:55 -0700339 {
340 return;
341 }
342 if (*type ==
343 "xyz.openbmc_project.Software.Image.Error.UnTarFailure")
344 {
345 redfish::messages::invalidUpload(asyncResp->res, url,
346 "Invalid archive");
347 }
George Liu0fda0f12021-11-16 10:06:17 +0800348 else if (
349 *type ==
350 "xyz.openbmc_project.Software.Image.Error.ManifestFileFailure")
James Feist4cde5d92020-06-11 10:39:55 -0700351 {
352 redfish::messages::invalidUpload(asyncResp->res, url,
353 "Invalid manifest");
354 }
355 else if (*type ==
356 "xyz.openbmc_project.Software.Image.Error.ImageFailure")
357 {
358 redfish::messages::invalidUpload(asyncResp->res, url,
359 "Invalid image format");
360 }
George Liu0fda0f12021-11-16 10:06:17 +0800361 else if (*type ==
362 "xyz.openbmc_project.Software.Version.Error.AlreadyExists")
Gunnar Mills88b3dd12020-11-20 14:26:04 -0600363 {
364
365 redfish::messages::invalidUpload(
366 asyncResp->res, url, "Image version already exists");
367
368 redfish::messages::resourceAlreadyExists(
Chicago Duan0588a3b2021-06-10 18:20:36 +0800369 asyncResp->res, "UpdateService.v1_5_0.UpdateService",
Gunnar Mills88b3dd12020-11-20 14:26:04 -0600370 "Version", "uploaded version");
371 }
James Feist4cde5d92020-06-11 10:39:55 -0700372 else if (*type ==
373 "xyz.openbmc_project.Software.Image.Error.BusyFailure")
374 {
375 redfish::messages::resourceExhaustion(asyncResp->res, url);
376 }
377 else
378 {
379 redfish::messages::internalError(asyncResp->res);
380 }
381 fwAvailableTimer = nullptr;
382 });
Andrew Geissler86adcd62019-04-18 10:58:05 -0500383}
Jennifer Lee729dae72018-04-24 15:59:34 -0700384
Andrew Geissler0554c982019-04-23 14:40:12 -0500385/**
386 * UpdateServiceActionsSimpleUpdate class supports handle POST method for
387 * SimpleUpdate action.
388 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700389inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app)
Andrew Geissler0554c982019-04-23 14:40:12 -0500390{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700391 BMCWEB_ROUTE(
392 app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
Ed Tanoused398212021-06-09 17:05:54 -0700393 .privileges(redfish::privileges::postUpdateService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700394 .methods(
395 boost::beast::http::verb::
396 post)([](const crow::Request& req,
397 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
398 std::optional<std::string> transferProtocol;
399 std::string imageURI;
Andrew Geissler0554c982019-04-23 14:40:12 -0500400
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700401 BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost";
Andrew Geissler0554c982019-04-23 14:40:12 -0500402
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700403 // User can pass in both TransferProtocol and ImageURI parameters or
404 // they can pass in just the ImageURI with the transfer protocol
405 // embedded within it.
406 // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
407 // 2) ImageURI:tftp://1.1.1.1/myfile.bin
Andrew Geissler0554c982019-04-23 14:40:12 -0500408
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700409 if (!json_util::readJson(req, asyncResp->res, "TransferProtocol",
410 transferProtocol, "ImageURI", imageURI))
411 {
412 BMCWEB_LOG_DEBUG
413 << "Missing TransferProtocol or ImageURI parameter";
414 return;
415 }
416 if (!transferProtocol)
417 {
418 // Must be option 2
419 // Verify ImageURI has transfer protocol in it
420 size_t separator = imageURI.find(':');
421 if ((separator == std::string::npos) ||
422 ((separator + 1) > imageURI.size()))
423 {
424 messages::actionParameterValueTypeError(
425 asyncResp->res, imageURI, "ImageURI",
426 "UpdateService.SimpleUpdate");
427 BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: "
428 << imageURI;
429 return;
430 }
431 transferProtocol = imageURI.substr(0, separator);
432 // Ensure protocol is upper case for a common comparison path
433 // below
434 boost::to_upper(*transferProtocol);
435 BMCWEB_LOG_DEBUG << "Encoded transfer protocol "
436 << *transferProtocol;
Andrew Geissler0554c982019-04-23 14:40:12 -0500437
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700438 // Adjust imageURI to not have the protocol on it for parsing
439 // below
440 // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin
441 imageURI = imageURI.substr(separator + 3);
442 BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI;
443 }
444
445 // OpenBMC currently only supports TFTP
446 if (*transferProtocol != "TFTP")
447 {
448 messages::actionParameterNotSupported(
449 asyncResp->res, "TransferProtocol",
450 "UpdateService.SimpleUpdate");
451 BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: "
452 << *transferProtocol;
453 return;
454 }
455
456 // Format should be <IP or Hostname>/<file> for imageURI
457 size_t separator = imageURI.find('/');
Andrew Geissler0554c982019-04-23 14:40:12 -0500458 if ((separator == std::string::npos) ||
459 ((separator + 1) > imageURI.size()))
460 {
461 messages::actionParameterValueTypeError(
462 asyncResp->res, imageURI, "ImageURI",
463 "UpdateService.SimpleUpdate");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700464 BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI;
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530465 return;
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530466 }
467
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700468 std::string tftpServer = imageURI.substr(0, separator);
469 std::string fwFile = imageURI.substr(separator + 1);
470 BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile;
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530471
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700472 // Setup callback for when new software detected
473 // Give TFTP 10 minutes to complete
474 monitorForSoftwareAvailable(
475 asyncResp, req,
476 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate",
477 600);
478
479 // TFTP can take up to 10 minutes depending on image size and
480 // connection speed. Return to caller as soon as the TFTP operation
481 // has been started. The callback above will ensure the activate
482 // is started once the download has completed
483 redfish::messages::success(asyncResp->res);
484
485 // Call TFTP service
486 crow::connections::systemBus->async_method_call(
487 [](const boost::system::error_code ec) {
488 if (ec)
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530489 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700490 // messages::internalError(asyncResp->res);
491 cleanUp();
492 BMCWEB_LOG_DEBUG << "error_code = " << ec;
493 BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530494 }
495 else
496 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700497 BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success";
498 }
499 },
500 "xyz.openbmc_project.Software.Download",
501 "/xyz/openbmc_project/software",
502 "xyz.openbmc_project.Common.TFTP", "DownloadViaTFTP", fwFile,
503 tftpServer);
504
505 BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost";
506 });
507}
508
509inline void requestRoutesUpdateService(App& app)
510{
511 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
Ed Tanoused398212021-06-09 17:05:54 -0700512 .privileges(redfish::privileges::getUpdateService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700513 .methods(
514 boost::beast::http::verb::
515 get)([](const crow::Request&,
516 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
517 asyncResp->res.jsonValue["@odata.type"] =
Chicago Duan0588a3b2021-06-10 18:20:36 +0800518 "#UpdateService.v1_5_0.UpdateService";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700519 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
520 asyncResp->res.jsonValue["Id"] = "UpdateService";
521 asyncResp->res.jsonValue["Description"] =
522 "Service for Software Update";
523 asyncResp->res.jsonValue["Name"] = "Update Service";
524 asyncResp->res.jsonValue["HttpPushUri"] =
525 "/redfish/v1/UpdateService";
526 // UpdateService cannot be disabled
527 asyncResp->res.jsonValue["ServiceEnabled"] = true;
528 asyncResp->res.jsonValue["FirmwareInventory"] = {
529 {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}};
Tejas Patild61e5192021-06-04 15:49:35 +0530530 // Get the MaxImageSizeBytes
531 asyncResp->res.jsonValue["MaxImageSizeBytes"] =
532 bmcwebHttpReqBodyLimitMb * 1024 * 1024;
533
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700534#ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
535 // Update Actions object.
536 nlohmann::json& updateSvcSimpleUpdate =
537 asyncResp->res
538 .jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
539 updateSvcSimpleUpdate["target"] =
540 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
541 updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] =
542 {"TFTP"};
543#endif
544 // Get the current ApplyTime value
545 crow::connections::systemBus->async_method_call(
546 [asyncResp](const boost::system::error_code ec,
547 const std::variant<std::string>& applyTime) {
548 if (ec)
549 {
550 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
551 messages::internalError(asyncResp->res);
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530552 return;
553 }
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530554
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700555 const std::string* s = std::get_if<std::string>(&applyTime);
556 if (s == nullptr)
557 {
558 return;
559 }
560 // Store the ApplyTime Value
George Liu0fda0f12021-11-16 10:06:17 +0800561 if (*s ==
562 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate")
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700563 {
564 asyncResp->res
565 .jsonValue["HttpPushUriOptions"]
566 ["HttpPushUriApplyTime"]["ApplyTime"] =
567 "Immediate";
568 }
George Liu0fda0f12021-11-16 10:06:17 +0800569 else if (
570 *s ==
571 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset")
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700572 {
573 asyncResp->res
574 .jsonValue["HttpPushUriOptions"]
575 ["HttpPushUriApplyTime"]["ApplyTime"] =
576 "OnReset";
577 }
578 },
579 "xyz.openbmc_project.Settings",
580 "/xyz/openbmc_project/software/apply_time",
581 "org.freedesktop.DBus.Properties", "Get",
582 "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime");
583 });
584 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
Ed Tanoused398212021-06-09 17:05:54 -0700585 .privileges(redfish::privileges::patchUpdateService)
George Liu0fda0f12021-11-16 10:06:17 +0800586 .methods(
587 boost::beast::http::verb::
588 patch)([](const crow::Request& req,
589 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
590 BMCWEB_LOG_DEBUG << "doPatch...";
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530591
George Liu0fda0f12021-11-16 10:06:17 +0800592 std::optional<nlohmann::json> pushUriOptions;
593 if (!json_util::readJson(req, asyncResp->res, "HttpPushUriOptions",
594 pushUriOptions))
595 {
596 return;
597 }
598
599 if (pushUriOptions)
600 {
601 std::optional<nlohmann::json> pushUriApplyTime;
602 if (!json_util::readJson(*pushUriOptions, asyncResp->res,
603 "HttpPushUriApplyTime",
604 pushUriApplyTime))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700605 {
Ed Tanousc711bf82018-07-30 16:31:33 -0700606 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700607 }
Jennifer Lee6c4eb9d2018-05-22 10:58:31 -0700608
George Liu0fda0f12021-11-16 10:06:17 +0800609 if (pushUriApplyTime)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700610 {
George Liu0fda0f12021-11-16 10:06:17 +0800611 std::optional<std::string> applyTime;
612 if (!json_util::readJson(*pushUriApplyTime, asyncResp->res,
613 "ApplyTime", applyTime))
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700614 {
615 return;
616 }
617
George Liu0fda0f12021-11-16 10:06:17 +0800618 if (applyTime)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700619 {
George Liu0fda0f12021-11-16 10:06:17 +0800620 std::string applyTimeNewVal;
621 if (applyTime == "Immediate")
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700622 {
George Liu0fda0f12021-11-16 10:06:17 +0800623 applyTimeNewVal =
624 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate";
625 }
626 else if (applyTime == "OnReset")
627 {
628 applyTimeNewVal =
629 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
630 }
631 else
632 {
633 BMCWEB_LOG_INFO
634 << "ApplyTime value is not in the list of acceptable values";
635 messages::propertyValueNotInList(
636 asyncResp->res, *applyTime, "ApplyTime");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700637 return;
638 }
639
George Liu0fda0f12021-11-16 10:06:17 +0800640 // Set the requested image apply time value
641 crow::connections::systemBus->async_method_call(
642 [asyncResp](const boost::system::error_code ec) {
643 if (ec)
644 {
645 BMCWEB_LOG_ERROR
646 << "D-Bus responses error: " << ec;
647 messages::internalError(asyncResp->res);
648 return;
649 }
650 messages::success(asyncResp->res);
651 },
652 "xyz.openbmc_project.Settings",
653 "/xyz/openbmc_project/software/apply_time",
654 "org.freedesktop.DBus.Properties", "Set",
655 "xyz.openbmc_project.Software.ApplyTime",
656 "RequestedApplyTime",
657 std::variant<std::string>{applyTimeNewVal});
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700658 }
659 }
George Liu0fda0f12021-11-16 10:06:17 +0800660 }
661 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700662 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
Ed Tanoused398212021-06-09 17:05:54 -0700663 .privileges(redfish::privileges::postUpdateService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700664 .methods(boost::beast::http::verb::post)(
665 [](const crow::Request& req,
666 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
667 BMCWEB_LOG_DEBUG << "doPost...";
668
669 // Setup callback for when new software detected
670 monitorForSoftwareAvailable(asyncResp, req,
671 "/redfish/v1/UpdateService");
672
673 std::string filepath("/tmp/images/" +
674 boost::uuids::to_string(
675 boost::uuids::random_generator()()));
676 BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
677 std::ofstream out(filepath, std::ofstream::out |
678 std::ofstream::binary |
679 std::ofstream::trunc);
680 out << req.body;
681 out.close();
682 BMCWEB_LOG_DEBUG << "file upload complete!!";
683 });
684}
685
686inline void requestRoutesSoftwareInventoryCollection(App& app)
687{
688 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/")
Ed Tanoused398212021-06-09 17:05:54 -0700689 .privileges(redfish::privileges::getSoftwareInventoryCollection)
George Liu0fda0f12021-11-16 10:06:17 +0800690 .methods(
691 boost::beast::http::verb::
692 get)([](const crow::Request&,
693 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
694 asyncResp->res.jsonValue["@odata.type"] =
695 "#SoftwareInventoryCollection.SoftwareInventoryCollection";
696 asyncResp->res.jsonValue["@odata.id"] =
697 "/redfish/v1/UpdateService/FirmwareInventory";
698 asyncResp->res.jsonValue["Name"] = "Software Inventory Collection";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700699
George Liu0fda0f12021-11-16 10:06:17 +0800700 crow::connections::systemBus->async_method_call(
701 [asyncResp](
702 const boost::system::error_code ec,
703 const std::vector<
704 std::pair<std::string,
705 std::vector<std::pair<
706 std::string, std::vector<std::string>>>>>&
707 subtree) {
708 if (ec)
709 {
710 messages::internalError(asyncResp->res);
711 return;
712 }
713 asyncResp->res.jsonValue["Members"] =
714 nlohmann::json::array();
715 asyncResp->res.jsonValue["Members@odata.count"] = 0;
716
717 for (auto& obj : subtree)
718 {
719 sdbusplus::message::object_path path(obj.first);
720 std::string swId = path.filename();
721 if (swId.empty())
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700722 {
723 messages::internalError(asyncResp->res);
George Liu0fda0f12021-11-16 10:06:17 +0800724 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700725 return;
726 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700727
George Liu0fda0f12021-11-16 10:06:17 +0800728 nlohmann::json& members =
729 asyncResp->res.jsonValue["Members"];
730 members.push_back(
731 {{"@odata.id",
732 "/redfish/v1/UpdateService/FirmwareInventory/" +
733 swId}});
734 asyncResp->res.jsonValue["Members@odata.count"] =
735 members.size();
736 }
737 },
738 // Note that only firmware levels associated with a device
739 // are stored under /xyz/openbmc_project/software therefore
740 // to ensure only real FirmwareInventory items are returned,
741 // this full object path must be used here as input to
742 // mapper
743 "xyz.openbmc_project.ObjectMapper",
744 "/xyz/openbmc_project/object_mapper",
745 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
746 "/xyz/openbmc_project/software", static_cast<int32_t>(0),
747 std::array<const char*, 1>{
748 "xyz.openbmc_project.Software.Version"});
749 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700750}
751/* Fill related item links (i.e. bmc, bios) in for inventory */
752inline static void
753 getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
754 const std::string& purpose)
755{
756 if (purpose == fw_util::bmcPurpose)
757 {
758 nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
759 relatedItem.push_back({{"@odata.id", "/redfish/v1/Managers/bmc"}});
760 aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
761 }
762 else if (purpose == fw_util::biosPurpose)
763 {
764 nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
765 relatedItem.push_back(
766 {{"@odata.id", "/redfish/v1/Systems/system/Bios"}});
767 aResp->res.jsonValue["Members@odata.count"] = relatedItem.size();
768 }
769 else
770 {
771 BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
772 }
773}
774
775inline void requestRoutesSoftwareInventory(App& app)
776{
777 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700778 .privileges(redfish::privileges::getSoftwareInventory)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700779 .methods(
780 boost::beast::http::verb::get)([](const crow::Request&,
781 const std::shared_ptr<
782 bmcweb::AsyncResp>& asyncResp,
783 const std::string& param) {
784 std::shared_ptr<std::string> swId =
785 std::make_shared<std::string>(param);
786
787 asyncResp->res.jsonValue["@odata.id"] =
788 "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
789
790 crow::connections::systemBus->async_method_call(
791 [asyncResp, swId](
792 const boost::system::error_code ec,
793 const std::vector<
794 std::pair<std::string,
795 std::vector<std::pair<
796 std::string, std::vector<std::string>>>>>&
797 subtree) {
798 BMCWEB_LOG_DEBUG << "doGet callback...";
799 if (ec)
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700800 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700801 messages::internalError(asyncResp->res);
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700802 return;
803 }
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700804
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700805 // Ensure we find our input swId, otherwise return an error
806 bool found = false;
807 for (const std::pair<
808 std::string,
809 std::vector<std::pair<
810 std::string, std::vector<std::string>>>>& obj :
811 subtree)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700812 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700813 if (boost::ends_with(obj.first, *swId) != true)
814 {
815 continue;
816 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700817
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700818 if (obj.second.size() < 1)
819 {
820 continue;
821 }
822
823 found = true;
824 fw_util::getFwStatus(asyncResp, swId,
825 obj.second[0].first);
826
827 crow::connections::systemBus->async_method_call(
828 [asyncResp, swId](
829 const boost::system::error_code errorCode,
830 const boost::container::flat_map<
831 std::string, VariantType>& propertiesList) {
832 if (errorCode)
833 {
834 messages::internalError(asyncResp->res);
835 return;
836 }
837 boost::container::flat_map<
838 std::string, VariantType>::const_iterator
839 it = propertiesList.find("Purpose");
840 if (it == propertiesList.end())
841 {
842 BMCWEB_LOG_DEBUG
843 << "Can't find property \"Purpose\"!";
844 messages::propertyMissing(asyncResp->res,
845 "Purpose");
846 return;
847 }
848 const std::string* swInvPurpose =
849 std::get_if<std::string>(&it->second);
850 if (swInvPurpose == nullptr)
851 {
George Liu0fda0f12021-11-16 10:06:17 +0800852 BMCWEB_LOG_DEBUG
853 << "wrong types for property\"Purpose\"!";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700854 messages::propertyValueTypeError(
855 asyncResp->res, "", "Purpose");
856 return;
857 }
858
859 BMCWEB_LOG_DEBUG << "swInvPurpose = "
860 << *swInvPurpose;
861 it = propertiesList.find("Version");
862 if (it == propertiesList.end())
863 {
864 BMCWEB_LOG_DEBUG
865 << "Can't find property \"Version\"!";
866 messages::propertyMissing(asyncResp->res,
867 "Version");
868 return;
869 }
870
871 BMCWEB_LOG_DEBUG << "Version found!";
872
873 const std::string* version =
874 std::get_if<std::string>(&it->second);
875
876 if (version == nullptr)
877 {
878 BMCWEB_LOG_DEBUG
879 << "Can't find property \"Version\"!";
880
881 messages::propertyValueTypeError(
882 asyncResp->res, "", "Version");
883 return;
884 }
885 asyncResp->res.jsonValue["Version"] = *version;
886 asyncResp->res.jsonValue["Id"] = *swId;
887
888 // swInvPurpose is of format:
889 // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
890 // Translate this to "ABC image"
891 size_t endDesc = swInvPurpose->rfind('.');
892 if (endDesc == std::string::npos)
893 {
894 messages::internalError(asyncResp->res);
895 return;
896 }
897 endDesc++;
898 if (endDesc >= swInvPurpose->size())
899 {
900 messages::internalError(asyncResp->res);
901 return;
902 }
903
904 std::string formatDesc =
905 swInvPurpose->substr(endDesc);
906 asyncResp->res.jsonValue["Description"] =
907 formatDesc + " image";
908 getRelatedItems(asyncResp, *swInvPurpose);
909 },
910 obj.second[0].first, obj.first,
911 "org.freedesktop.DBus.Properties", "GetAll",
912 "xyz.openbmc_project.Software.Version");
913 }
914 if (!found)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700915 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700916 BMCWEB_LOG_ERROR
917 << "Input swID " + *swId + " not found!";
918 messages::resourceMissingAtURI(
919 asyncResp->res,
920 "/redfish/v1/UpdateService/FirmwareInventory/" +
921 *swId);
922 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700923 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700924 asyncResp->res.jsonValue["@odata.type"] =
925 "#SoftwareInventory.v1_1_0.SoftwareInventory";
926 asyncResp->res.jsonValue["Name"] = "Software Inventory";
927 asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700928
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700929 asyncResp->res.jsonValue["Updateable"] = false;
930 fw_util::getFwUpdateableStatus(asyncResp, swId);
931 },
932 "xyz.openbmc_project.ObjectMapper",
933 "/xyz/openbmc_project/object_mapper",
934 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
935 static_cast<int32_t>(0),
936 std::array<const char*, 1>{
937 "xyz.openbmc_project.Software.Version"});
938 });
939}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700940
941} // namespace redfish