blob: 5f56e353d7bdc480697cf980f4a76906047a6eb7 [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 Tanous168e20c2021-12-13 14:39:53 -080022#include <dbus_utility.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070023#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070024#include <sdbusplus/asio/property.hpp>
Andrew Geissler87d84722019-02-28 14:28:39 -060025#include <utils/fw_utils.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050026
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",
Ed Tanous168e20c2021-12-13 14:39:53 -080058 dbus::utility::DbusVariantType(
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,
Ed Tanous168e20c2021-12-13 14:39:53 -080071 std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>>>
Andrew Geissler86adcd62019-04-18 10:58:05 -050072 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,
Ed Tanous168e20c2021-12-13 14:39:53 -0800138 dbus::utility::DbusVariantType>
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,
Brian Mae1cc4822021-12-01 17:05:54 +0800319 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
320 "member='InterfacesAdded',"
321 "path='/xyz/openbmc_project/logging'",
James Feist4cde5d92020-06-11 10:39:55 -0700322 [asyncResp, url](sdbusplus::message::message& m) {
Brian Mae1cc4822021-12-01 17:05:54 +0800323 std::vector<
324 std::pair<std::string, dbus::utility::DBusPropertiesMap>>
325 interfacesProperties;
326 sdbusplus::message::object_path objPath;
327 m.read(objPath, interfacesProperties);
328 BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
329 for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>&
330 interface : interfacesProperties)
James Feist4cde5d92020-06-11 10:39:55 -0700331 {
Brian Mae1cc4822021-12-01 17:05:54 +0800332 if (interface.first == "xyz.openbmc_project.Logging.Entry")
333 {
334 BMCWEB_LOG_DEBUG << "Error Match fired";
335 const dbus::utility::DBusPropertiesMap& values =
336 interface.second;
337 auto find = values.find("Message");
338 if (find == values.end())
339 {
340 return;
341 }
342 const std::string* type =
343 std::get_if<std::string>(&(find->second));
344 if (type == nullptr)
345 {
346 // if this was our message, timeout will cover it
347 return;
348 }
349 fwAvailableTimer = nullptr;
350 if (*type ==
351 "xyz.openbmc_project.Software.Image.Error.UnTarFailure")
352 {
353 redfish::messages::invalidUpload(asyncResp->res, url,
354 "Invalid archive");
355 }
356 else if (*type ==
357 "xyz.openbmc_project.Software.Image.Error."
358 "ManifestFileFailure")
359 {
360 redfish::messages::invalidUpload(asyncResp->res, url,
361 "Invalid manifest");
362 }
363 else if (
364 *type ==
365 "xyz.openbmc_project.Software.Image.Error.ImageFailure")
366 {
367 redfish::messages::invalidUpload(
368 asyncResp->res, url, "Invalid image format");
369 }
370 else if (
371 *type ==
372 "xyz.openbmc_project.Software.Version.Error.AlreadyExists")
373 {
374 redfish::messages::invalidUpload(
375 asyncResp->res, url,
376 "Image version already exists");
Gunnar Mills88b3dd12020-11-20 14:26:04 -0600377
Brian Mae1cc4822021-12-01 17:05:54 +0800378 redfish::messages::resourceAlreadyExists(
379 asyncResp->res,
380 "UpdateService.v1_5_0.UpdateService", "Version",
381 "uploaded version");
382 }
383 else if (
384 *type ==
385 "xyz.openbmc_project.Software.Image.Error.BusyFailure")
386 {
387 redfish::messages::resourceExhaustion(asyncResp->res,
388 url);
389 }
390 else
391 {
392 redfish::messages::internalError(asyncResp->res);
393 }
394 }
Gunnar Mills88b3dd12020-11-20 14:26:04 -0600395 }
James Feist4cde5d92020-06-11 10:39:55 -0700396 });
Andrew Geissler86adcd62019-04-18 10:58:05 -0500397}
Jennifer Lee729dae72018-04-24 15:59:34 -0700398
Andrew Geissler0554c982019-04-23 14:40:12 -0500399/**
400 * UpdateServiceActionsSimpleUpdate class supports handle POST method for
401 * SimpleUpdate action.
402 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700403inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app)
Andrew Geissler0554c982019-04-23 14:40:12 -0500404{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700405 BMCWEB_ROUTE(
406 app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
Ed Tanoused398212021-06-09 17:05:54 -0700407 .privileges(redfish::privileges::postUpdateService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700408 .methods(
409 boost::beast::http::verb::
410 post)([](const crow::Request& req,
411 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
412 std::optional<std::string> transferProtocol;
413 std::string imageURI;
Andrew Geissler0554c982019-04-23 14:40:12 -0500414
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700415 BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost";
Andrew Geissler0554c982019-04-23 14:40:12 -0500416
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700417 // User can pass in both TransferProtocol and ImageURI parameters or
418 // they can pass in just the ImageURI with the transfer protocol
419 // embedded within it.
420 // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
421 // 2) ImageURI:tftp://1.1.1.1/myfile.bin
Andrew Geissler0554c982019-04-23 14:40:12 -0500422
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700423 if (!json_util::readJson(req, asyncResp->res, "TransferProtocol",
424 transferProtocol, "ImageURI", imageURI))
425 {
426 BMCWEB_LOG_DEBUG
427 << "Missing TransferProtocol or ImageURI parameter";
428 return;
429 }
430 if (!transferProtocol)
431 {
432 // Must be option 2
433 // Verify ImageURI has transfer protocol in it
434 size_t separator = imageURI.find(':');
435 if ((separator == std::string::npos) ||
436 ((separator + 1) > imageURI.size()))
437 {
438 messages::actionParameterValueTypeError(
439 asyncResp->res, imageURI, "ImageURI",
440 "UpdateService.SimpleUpdate");
441 BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: "
442 << imageURI;
443 return;
444 }
445 transferProtocol = imageURI.substr(0, separator);
446 // Ensure protocol is upper case for a common comparison path
447 // below
448 boost::to_upper(*transferProtocol);
449 BMCWEB_LOG_DEBUG << "Encoded transfer protocol "
450 << *transferProtocol;
Andrew Geissler0554c982019-04-23 14:40:12 -0500451
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700452 // Adjust imageURI to not have the protocol on it for parsing
453 // below
454 // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin
455 imageURI = imageURI.substr(separator + 3);
456 BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI;
457 }
458
459 // OpenBMC currently only supports TFTP
460 if (*transferProtocol != "TFTP")
461 {
462 messages::actionParameterNotSupported(
463 asyncResp->res, "TransferProtocol",
464 "UpdateService.SimpleUpdate");
465 BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: "
466 << *transferProtocol;
467 return;
468 }
469
470 // Format should be <IP or Hostname>/<file> for imageURI
471 size_t separator = imageURI.find('/');
Andrew Geissler0554c982019-04-23 14:40:12 -0500472 if ((separator == std::string::npos) ||
473 ((separator + 1) > imageURI.size()))
474 {
475 messages::actionParameterValueTypeError(
476 asyncResp->res, imageURI, "ImageURI",
477 "UpdateService.SimpleUpdate");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700478 BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI;
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530479 return;
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530480 }
481
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700482 std::string tftpServer = imageURI.substr(0, separator);
483 std::string fwFile = imageURI.substr(separator + 1);
484 BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile;
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530485
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700486 // Setup callback for when new software detected
487 // Give TFTP 10 minutes to complete
488 monitorForSoftwareAvailable(
489 asyncResp, req,
490 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate",
491 600);
492
493 // TFTP can take up to 10 minutes depending on image size and
494 // connection speed. Return to caller as soon as the TFTP operation
495 // has been started. The callback above will ensure the activate
496 // is started once the download has completed
497 redfish::messages::success(asyncResp->res);
498
499 // Call TFTP service
500 crow::connections::systemBus->async_method_call(
501 [](const boost::system::error_code ec) {
502 if (ec)
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530503 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700504 // messages::internalError(asyncResp->res);
505 cleanUp();
506 BMCWEB_LOG_DEBUG << "error_code = " << ec;
507 BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530508 }
509 else
510 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700511 BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success";
512 }
513 },
514 "xyz.openbmc_project.Software.Download",
515 "/xyz/openbmc_project/software",
516 "xyz.openbmc_project.Common.TFTP", "DownloadViaTFTP", fwFile,
517 tftpServer);
518
519 BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost";
520 });
521}
522
523inline void requestRoutesUpdateService(App& app)
524{
525 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
Ed Tanoused398212021-06-09 17:05:54 -0700526 .privileges(redfish::privileges::getUpdateService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700527 .methods(
528 boost::beast::http::verb::
529 get)([](const crow::Request&,
530 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
531 asyncResp->res.jsonValue["@odata.type"] =
Chicago Duan0588a3b2021-06-10 18:20:36 +0800532 "#UpdateService.v1_5_0.UpdateService";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700533 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
534 asyncResp->res.jsonValue["Id"] = "UpdateService";
535 asyncResp->res.jsonValue["Description"] =
536 "Service for Software Update";
537 asyncResp->res.jsonValue["Name"] = "Update Service";
538 asyncResp->res.jsonValue["HttpPushUri"] =
539 "/redfish/v1/UpdateService";
540 // UpdateService cannot be disabled
541 asyncResp->res.jsonValue["ServiceEnabled"] = true;
542 asyncResp->res.jsonValue["FirmwareInventory"] = {
543 {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}};
Tejas Patild61e5192021-06-04 15:49:35 +0530544 // Get the MaxImageSizeBytes
545 asyncResp->res.jsonValue["MaxImageSizeBytes"] =
546 bmcwebHttpReqBodyLimitMb * 1024 * 1024;
547
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700548#ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
549 // Update Actions object.
550 nlohmann::json& updateSvcSimpleUpdate =
551 asyncResp->res
552 .jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
553 updateSvcSimpleUpdate["target"] =
554 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
555 updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] =
556 {"TFTP"};
557#endif
558 // Get the current ApplyTime value
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700559 sdbusplus::asio::getProperty<std::string>(
560 *crow::connections::systemBus, "xyz.openbmc_project.Settings",
561 "/xyz/openbmc_project/software/apply_time",
562 "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime",
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700563 [asyncResp](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700564 const std::string& applyTime) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700565 if (ec)
566 {
567 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
568 messages::internalError(asyncResp->res);
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530569 return;
570 }
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530571
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700572 // Store the ApplyTime Value
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700573 if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
574 "RequestedApplyTimes.Immediate")
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700575 {
576 asyncResp->res
577 .jsonValue["HttpPushUriOptions"]
578 ["HttpPushUriApplyTime"]["ApplyTime"] =
579 "Immediate";
580 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700581 else if (applyTime ==
582 "xyz.openbmc_project.Software.ApplyTime."
583 "RequestedApplyTimes.OnReset")
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700584 {
585 asyncResp->res
586 .jsonValue["HttpPushUriOptions"]
587 ["HttpPushUriApplyTime"]["ApplyTime"] =
588 "OnReset";
589 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700590 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700591 });
592 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
Ed Tanoused398212021-06-09 17:05:54 -0700593 .privileges(redfish::privileges::patchUpdateService)
George Liu0fda0f12021-11-16 10:06:17 +0800594 .methods(
595 boost::beast::http::verb::
596 patch)([](const crow::Request& req,
597 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
598 BMCWEB_LOG_DEBUG << "doPatch...";
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530599
George Liu0fda0f12021-11-16 10:06:17 +0800600 std::optional<nlohmann::json> pushUriOptions;
601 if (!json_util::readJson(req, asyncResp->res, "HttpPushUriOptions",
602 pushUriOptions))
603 {
604 return;
605 }
606
607 if (pushUriOptions)
608 {
609 std::optional<nlohmann::json> pushUriApplyTime;
610 if (!json_util::readJson(*pushUriOptions, asyncResp->res,
611 "HttpPushUriApplyTime",
612 pushUriApplyTime))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700613 {
Ed Tanousc711bf82018-07-30 16:31:33 -0700614 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700615 }
Jennifer Lee6c4eb9d2018-05-22 10:58:31 -0700616
George Liu0fda0f12021-11-16 10:06:17 +0800617 if (pushUriApplyTime)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700618 {
George Liu0fda0f12021-11-16 10:06:17 +0800619 std::optional<std::string> applyTime;
620 if (!json_util::readJson(*pushUriApplyTime, asyncResp->res,
621 "ApplyTime", applyTime))
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700622 {
623 return;
624 }
625
George Liu0fda0f12021-11-16 10:06:17 +0800626 if (applyTime)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700627 {
George Liu0fda0f12021-11-16 10:06:17 +0800628 std::string applyTimeNewVal;
629 if (applyTime == "Immediate")
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700630 {
George Liu0fda0f12021-11-16 10:06:17 +0800631 applyTimeNewVal =
632 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate";
633 }
634 else if (applyTime == "OnReset")
635 {
636 applyTimeNewVal =
637 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
638 }
639 else
640 {
641 BMCWEB_LOG_INFO
642 << "ApplyTime value is not in the list of acceptable values";
643 messages::propertyValueNotInList(
644 asyncResp->res, *applyTime, "ApplyTime");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700645 return;
646 }
647
George Liu0fda0f12021-11-16 10:06:17 +0800648 // Set the requested image apply time value
649 crow::connections::systemBus->async_method_call(
650 [asyncResp](const boost::system::error_code ec) {
651 if (ec)
652 {
653 BMCWEB_LOG_ERROR
654 << "D-Bus responses error: " << ec;
655 messages::internalError(asyncResp->res);
656 return;
657 }
658 messages::success(asyncResp->res);
659 },
660 "xyz.openbmc_project.Settings",
661 "/xyz/openbmc_project/software/apply_time",
662 "org.freedesktop.DBus.Properties", "Set",
663 "xyz.openbmc_project.Software.ApplyTime",
664 "RequestedApplyTime",
Ed Tanous168e20c2021-12-13 14:39:53 -0800665 dbus::utility::DbusVariantType{applyTimeNewVal});
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700666 }
667 }
George Liu0fda0f12021-11-16 10:06:17 +0800668 }
669 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700670 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
Ed Tanoused398212021-06-09 17:05:54 -0700671 .privileges(redfish::privileges::postUpdateService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700672 .methods(boost::beast::http::verb::post)(
673 [](const crow::Request& req,
674 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
675 BMCWEB_LOG_DEBUG << "doPost...";
676
677 // Setup callback for when new software detected
678 monitorForSoftwareAvailable(asyncResp, req,
679 "/redfish/v1/UpdateService");
680
681 std::string filepath("/tmp/images/" +
682 boost::uuids::to_string(
683 boost::uuids::random_generator()()));
684 BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
685 std::ofstream out(filepath, std::ofstream::out |
686 std::ofstream::binary |
687 std::ofstream::trunc);
688 out << req.body;
689 out.close();
690 BMCWEB_LOG_DEBUG << "file upload complete!!";
691 });
692}
693
694inline void requestRoutesSoftwareInventoryCollection(App& app)
695{
696 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/")
Ed Tanoused398212021-06-09 17:05:54 -0700697 .privileges(redfish::privileges::getSoftwareInventoryCollection)
George Liu0fda0f12021-11-16 10:06:17 +0800698 .methods(
699 boost::beast::http::verb::
700 get)([](const crow::Request&,
701 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
702 asyncResp->res.jsonValue["@odata.type"] =
703 "#SoftwareInventoryCollection.SoftwareInventoryCollection";
704 asyncResp->res.jsonValue["@odata.id"] =
705 "/redfish/v1/UpdateService/FirmwareInventory";
706 asyncResp->res.jsonValue["Name"] = "Software Inventory Collection";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700707
George Liu0fda0f12021-11-16 10:06:17 +0800708 crow::connections::systemBus->async_method_call(
709 [asyncResp](
710 const boost::system::error_code ec,
711 const std::vector<
712 std::pair<std::string,
713 std::vector<std::pair<
714 std::string, std::vector<std::string>>>>>&
715 subtree) {
716 if (ec)
717 {
718 messages::internalError(asyncResp->res);
719 return;
720 }
721 asyncResp->res.jsonValue["Members"] =
722 nlohmann::json::array();
723 asyncResp->res.jsonValue["Members@odata.count"] = 0;
724
725 for (auto& obj : subtree)
726 {
727 sdbusplus::message::object_path path(obj.first);
728 std::string swId = path.filename();
729 if (swId.empty())
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700730 {
731 messages::internalError(asyncResp->res);
George Liu0fda0f12021-11-16 10:06:17 +0800732 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700733 return;
734 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700735
George Liu0fda0f12021-11-16 10:06:17 +0800736 nlohmann::json& members =
737 asyncResp->res.jsonValue["Members"];
738 members.push_back(
739 {{"@odata.id",
740 "/redfish/v1/UpdateService/FirmwareInventory/" +
741 swId}});
742 asyncResp->res.jsonValue["Members@odata.count"] =
743 members.size();
744 }
745 },
746 // Note that only firmware levels associated with a device
747 // are stored under /xyz/openbmc_project/software therefore
748 // to ensure only real FirmwareInventory items are returned,
749 // this full object path must be used here as input to
750 // mapper
751 "xyz.openbmc_project.ObjectMapper",
752 "/xyz/openbmc_project/object_mapper",
753 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
754 "/xyz/openbmc_project/software", static_cast<int32_t>(0),
755 std::array<const char*, 1>{
756 "xyz.openbmc_project.Software.Version"});
757 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700758}
759/* Fill related item links (i.e. bmc, bios) in for inventory */
760inline static void
761 getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
762 const std::string& purpose)
763{
764 if (purpose == fw_util::bmcPurpose)
765 {
766 nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
767 relatedItem.push_back({{"@odata.id", "/redfish/v1/Managers/bmc"}});
768 aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
769 }
770 else if (purpose == fw_util::biosPurpose)
771 {
772 nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
773 relatedItem.push_back(
774 {{"@odata.id", "/redfish/v1/Systems/system/Bios"}});
775 aResp->res.jsonValue["Members@odata.count"] = relatedItem.size();
776 }
777 else
778 {
779 BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
780 }
781}
782
783inline void requestRoutesSoftwareInventory(App& app)
784{
785 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700786 .privileges(redfish::privileges::getSoftwareInventory)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700787 .methods(
788 boost::beast::http::verb::get)([](const crow::Request&,
789 const std::shared_ptr<
790 bmcweb::AsyncResp>& asyncResp,
791 const std::string& param) {
792 std::shared_ptr<std::string> swId =
793 std::make_shared<std::string>(param);
794
795 asyncResp->res.jsonValue["@odata.id"] =
796 "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
797
798 crow::connections::systemBus->async_method_call(
799 [asyncResp, swId](
800 const boost::system::error_code ec,
801 const std::vector<
802 std::pair<std::string,
803 std::vector<std::pair<
804 std::string, std::vector<std::string>>>>>&
805 subtree) {
806 BMCWEB_LOG_DEBUG << "doGet callback...";
807 if (ec)
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700808 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700809 messages::internalError(asyncResp->res);
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700810 return;
811 }
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700812
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700813 // Ensure we find our input swId, otherwise return an error
814 bool found = false;
815 for (const std::pair<
816 std::string,
817 std::vector<std::pair<
818 std::string, std::vector<std::string>>>>& obj :
819 subtree)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700820 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700821 if (boost::ends_with(obj.first, *swId) != true)
822 {
823 continue;
824 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700825
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700826 if (obj.second.size() < 1)
827 {
828 continue;
829 }
830
831 found = true;
832 fw_util::getFwStatus(asyncResp, swId,
833 obj.second[0].first);
834
835 crow::connections::systemBus->async_method_call(
Ed Tanous168e20c2021-12-13 14:39:53 -0800836 [asyncResp,
837 swId](const boost::system::error_code errorCode,
838 const boost::container::flat_map<
839 std::string,
840 dbus::utility::DbusVariantType>&
841 propertiesList) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700842 if (errorCode)
843 {
844 messages::internalError(asyncResp->res);
845 return;
846 }
847 boost::container::flat_map<
Ed Tanous168e20c2021-12-13 14:39:53 -0800848 std::string,
849 dbus::utility::DbusVariantType>::
850 const_iterator it =
851 propertiesList.find("Purpose");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700852 if (it == propertiesList.end())
853 {
854 BMCWEB_LOG_DEBUG
855 << "Can't find property \"Purpose\"!";
856 messages::propertyMissing(asyncResp->res,
857 "Purpose");
858 return;
859 }
860 const std::string* swInvPurpose =
861 std::get_if<std::string>(&it->second);
862 if (swInvPurpose == nullptr)
863 {
George Liu0fda0f12021-11-16 10:06:17 +0800864 BMCWEB_LOG_DEBUG
865 << "wrong types for property\"Purpose\"!";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700866 messages::propertyValueTypeError(
867 asyncResp->res, "", "Purpose");
868 return;
869 }
870
871 BMCWEB_LOG_DEBUG << "swInvPurpose = "
872 << *swInvPurpose;
873 it = propertiesList.find("Version");
874 if (it == propertiesList.end())
875 {
876 BMCWEB_LOG_DEBUG
877 << "Can't find property \"Version\"!";
878 messages::propertyMissing(asyncResp->res,
879 "Version");
880 return;
881 }
882
883 BMCWEB_LOG_DEBUG << "Version found!";
884
885 const std::string* version =
886 std::get_if<std::string>(&it->second);
887
888 if (version == nullptr)
889 {
890 BMCWEB_LOG_DEBUG
891 << "Can't find property \"Version\"!";
892
893 messages::propertyValueTypeError(
894 asyncResp->res, "", "Version");
895 return;
896 }
897 asyncResp->res.jsonValue["Version"] = *version;
898 asyncResp->res.jsonValue["Id"] = *swId;
899
900 // swInvPurpose is of format:
901 // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
902 // Translate this to "ABC image"
903 size_t endDesc = swInvPurpose->rfind('.');
904 if (endDesc == std::string::npos)
905 {
906 messages::internalError(asyncResp->res);
907 return;
908 }
909 endDesc++;
910 if (endDesc >= swInvPurpose->size())
911 {
912 messages::internalError(asyncResp->res);
913 return;
914 }
915
916 std::string formatDesc =
917 swInvPurpose->substr(endDesc);
918 asyncResp->res.jsonValue["Description"] =
919 formatDesc + " image";
920 getRelatedItems(asyncResp, *swInvPurpose);
921 },
922 obj.second[0].first, obj.first,
923 "org.freedesktop.DBus.Properties", "GetAll",
924 "xyz.openbmc_project.Software.Version");
925 }
926 if (!found)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700927 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700928 BMCWEB_LOG_ERROR
929 << "Input swID " + *swId + " not found!";
930 messages::resourceMissingAtURI(
931 asyncResp->res,
932 "/redfish/v1/UpdateService/FirmwareInventory/" +
933 *swId);
934 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700935 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700936 asyncResp->res.jsonValue["@odata.type"] =
937 "#SoftwareInventory.v1_1_0.SoftwareInventory";
938 asyncResp->res.jsonValue["Name"] = "Software Inventory";
939 asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700940
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700941 asyncResp->res.jsonValue["Updateable"] = false;
942 fw_util::getFwUpdateableStatus(asyncResp, swId);
943 },
944 "xyz.openbmc_project.ObjectMapper",
945 "/xyz/openbmc_project/object_mapper",
946 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
947 static_cast<int32_t>(0),
948 std::array<const char*, 1>{
949 "xyz.openbmc_project.Software.Version"});
950 });
951}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700952
953} // namespace redfish