blob: 9a07d3c334dbe9fccba806c892c20bd3045b58ee [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
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080020#include "app.hpp"
21#include "dbus_utility.hpp"
George Liu0ed80c82020-05-12 16:06:27 +080022#include "multipart_parser.hpp"
Ed Tanous2c6ffdb2023-06-28 11:28:38 -070023#include "ossl_random.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080024#include "query.hpp"
25#include "registries/privilege_registry.hpp"
Ed Tanousa8e884f2023-01-13 17:40:03 -080026#include "task.hpp"
John Edward Broadbent08d81ad2022-05-17 20:00:23 -070027#include "utils/collection.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080028#include "utils/dbus_utils.hpp"
29#include "utils/sw_utils.hpp"
30
Ed Tanousd093c992023-01-19 19:01:49 -080031#include <boost/algorithm/string/case_conv.hpp>
George Liue99073f2022-12-09 11:06:16 +080032#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070033#include <boost/url/format.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070034#include <sdbusplus/asio/property.hpp>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080035#include <sdbusplus/bus/match.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020036#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050037
George Liu2b731192023-01-11 16:27:13 +080038#include <array>
George Liu0ed80c82020-05-12 16:06:27 +080039#include <filesystem>
George Liu2b731192023-01-11 16:27:13 +080040#include <string_view>
41
Ed Tanous1abe55e2018-09-05 08:30:59 -070042namespace redfish
43{
Ed Tanous27826b52018-10-29 11:40:58 -070044
Andrew Geissler0e7de462019-03-04 19:11:54 -060045// Match signals added on software path
Ed Tanouscf9e4172022-12-21 09:30:16 -080046// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Patrick Williams59d494e2022-07-22 19:26:55 -050047static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateMatcher;
Ed Tanouscf9e4172022-12-21 09:30:16 -080048// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Patrick Williams59d494e2022-07-22 19:26:55 -050049static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateErrorMatcher;
Andrew Geissler0e7de462019-03-04 19:11:54 -060050// Only allow one update at a time
Ed Tanouscf9e4172022-12-21 09:30:16 -080051// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Andrew Geissler0e7de462019-03-04 19:11:54 -060052static bool fwUpdateInProgress = false;
Andrew Geissler86adcd62019-04-18 10:58:05 -050053// Timer for software available
Ed Tanouscf9e4172022-12-21 09:30:16 -080054// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Ed Tanous271584a2019-07-09 16:24:22 -070055static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer;
Andrew Geissler86adcd62019-04-18 10:58:05 -050056
John Edward Broadbent7e860f12021-04-08 15:57:16 -070057inline static void cleanUp()
Andrew Geissler86adcd62019-04-18 10:58:05 -050058{
59 fwUpdateInProgress = false;
60 fwUpdateMatcher = nullptr;
James Feist4cde5d92020-06-11 10:39:55 -070061 fwUpdateErrorMatcher = nullptr;
Andrew Geissler86adcd62019-04-18 10:58:05 -050062}
John Edward Broadbent7e860f12021-04-08 15:57:16 -070063inline static void activateImage(const std::string& objPath,
64 const std::string& service)
Andrew Geissler86adcd62019-04-18 10:58:05 -050065{
Ed Tanous62598e32023-07-17 17:06:25 -070066 BMCWEB_LOG_DEBUG("Activate image for {} {}", objPath, service);
George Liu9ae226f2023-06-21 17:56:46 +080067 sdbusplus::asio::setProperty(
68 *crow::connections::systemBus, service, objPath,
69 "xyz.openbmc_project.Software.Activation", "RequestedActivation",
70 "xyz.openbmc_project.Software.Activation.RequestedActivations.Active",
Ed Tanous8b242752023-06-27 17:17:13 -070071 [](const boost::system::error_code& ec) {
72 if (ec)
Ed Tanous002d39b2022-05-31 08:59:27 -070073 {
Ed Tanous62598e32023-07-17 17:06:25 -070074 BMCWEB_LOG_DEBUG("error_code = {}", ec);
75 BMCWEB_LOG_DEBUG("error msg = {}", ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -070076 }
Patrick Williams5a39f772023-10-20 11:20:21 -050077 });
Andrew Geissler86adcd62019-04-18 10:58:05 -050078}
Andrew Geissler0554c982019-04-23 14:40:12 -050079
80// Note that asyncResp can be either a valid pointer or nullptr. If nullptr
81// then no asyncResp updates will occur
zhanghch058d1b46d2021-04-01 11:18:24 +080082static void
83 softwareInterfaceAdded(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Patrick Williams59d494e2022-07-22 19:26:55 -050084 sdbusplus::message_t& m, task::Payload&& payload)
Andrew Geissler86adcd62019-04-18 10:58:05 -050085{
Michael Shen80f79a42023-08-24 13:41:53 +000086 dbus::utility::DBusInterfacesMap interfacesProperties;
Andrew Geissler86adcd62019-04-18 10:58:05 -050087
88 sdbusplus::message::object_path objPath;
89
90 m.read(objPath, interfacesProperties);
91
Ed Tanous62598e32023-07-17 17:06:25 -070092 BMCWEB_LOG_DEBUG("obj path = {}", objPath.str);
Ed Tanouse3eb3d62022-12-21 11:56:07 -080093 for (const auto& interface : interfacesProperties)
Andrew Geissler86adcd62019-04-18 10:58:05 -050094 {
Ed Tanous62598e32023-07-17 17:06:25 -070095 BMCWEB_LOG_DEBUG("interface = {}", interface.first);
Andrew Geissler86adcd62019-04-18 10:58:05 -050096
97 if (interface.first == "xyz.openbmc_project.Software.Activation")
98 {
Andrew Geissler86adcd62019-04-18 10:58:05 -050099 // Retrieve service and activate
George Liu2b731192023-01-11 16:27:13 +0800100 constexpr std::array<std::string_view, 1> interfaces = {
101 "xyz.openbmc_project.Software.Activation"};
102 dbus::utility::getDbusObject(
103 objPath.str, interfaces,
Ed Tanousa3e65892021-09-16 14:13:20 -0700104 [objPath, asyncResp, payload(std::move(payload))](
Ed Tanous8b242752023-06-27 17:17:13 -0700105 const boost::system::error_code& ec,
Ed Tanousa3e65892021-09-16 14:13:20 -0700106 const std::vector<
107 std::pair<std::string, std::vector<std::string>>>&
108 objInfo) mutable {
Ed Tanous8b242752023-06-27 17:17:13 -0700109 if (ec)
Ed Tanous002d39b2022-05-31 08:59:27 -0700110 {
Ed Tanous62598e32023-07-17 17:06:25 -0700111 BMCWEB_LOG_DEBUG("error_code = {}", ec);
112 BMCWEB_LOG_DEBUG("error msg = {}", ec.message());
Andrew Geissler0554c982019-04-23 14:40:12 -0500113 if (asyncResp)
114 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700115 messages::internalError(asyncResp->res);
116 }
117 cleanUp();
118 return;
119 }
120 // Ensure we only got one service back
121 if (objInfo.size() != 1)
122 {
Ed Tanous62598e32023-07-17 17:06:25 -0700123 BMCWEB_LOG_ERROR("Invalid Object Size {}", objInfo.size());
Ed Tanous002d39b2022-05-31 08:59:27 -0700124 if (asyncResp)
125 {
126 messages::internalError(asyncResp->res);
127 }
128 cleanUp();
129 return;
130 }
131 // cancel timer only when
132 // xyz.openbmc_project.Software.Activation interface
133 // is added
134 fwAvailableTimer = nullptr;
135
136 activateImage(objPath.str, objInfo[0].first);
137 if (asyncResp)
138 {
139 std::shared_ptr<task::TaskData> task =
140 task::TaskData::createTask(
Ed Tanous8b242752023-06-27 17:17:13 -0700141 [](const boost::system::error_code& ec2,
Patrick Williams59d494e2022-07-22 19:26:55 -0500142 sdbusplus::message_t& msg,
Ed Tanous002d39b2022-05-31 08:59:27 -0700143 const std::shared_ptr<task::TaskData>&
144 taskData) {
Ed Tanous8b242752023-06-27 17:17:13 -0700145 if (ec2)
Ed Tanous002d39b2022-05-31 08:59:27 -0700146 {
147 return task::completed;
148 }
149
150 std::string iface;
151 dbus::utility::DBusPropertiesMap values;
152
153 std::string index = std::to_string(taskData->index);
154 msg.read(iface, values);
155
156 if (iface == "xyz.openbmc_project.Software.Activation")
157 {
Gayathri Leburu0fb5b502022-11-17 04:51:55 +0000158 const std::string* state = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -0700159 for (const auto& property : values)
160 {
161 if (property.first == "Activation")
162 {
Gayathri Leburu0fb5b502022-11-17 04:51:55 +0000163 state = std::get_if<std::string>(
164 &property.second);
165 if (state == nullptr)
James Feist32898ce2020-03-10 16:16:52 -0700166 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700167 taskData->messages.emplace_back(
168 messages::internalError());
James Feist32898ce2020-03-10 16:16:52 -0700169 return task::completed;
170 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700171 }
172 }
James Feist32898ce2020-03-10 16:16:52 -0700173
Ed Tanous002d39b2022-05-31 08:59:27 -0700174 if (state == nullptr)
175 {
176 return !task::completed;
177 }
James Feist32898ce2020-03-10 16:16:52 -0700178
Ed Tanous11ba3972022-07-11 09:50:41 -0700179 if (state->ends_with("Invalid") ||
180 state->ends_with("Failed"))
Ed Tanous002d39b2022-05-31 08:59:27 -0700181 {
182 taskData->state = "Exception";
183 taskData->status = "Warning";
184 taskData->messages.emplace_back(
185 messages::taskAborted(index));
186 return task::completed;
187 }
James Feiste5d50062020-05-11 17:29:00 -0700188
Ed Tanous11ba3972022-07-11 09:50:41 -0700189 if (state->ends_with("Staged"))
Ed Tanous002d39b2022-05-31 08:59:27 -0700190 {
191 taskData->state = "Stopping";
192 taskData->messages.emplace_back(
193 messages::taskPaused(index));
194
195 // its staged, set a long timer to
196 // allow them time to complete the
197 // update (probably cycle the
198 // system) if this expires then
Ed Tanous8ece0e42024-01-02 13:16:50 -0800199 // task will be canceled
Ed Tanous002d39b2022-05-31 08:59:27 -0700200 taskData->extendTimer(std::chrono::hours(5));
201 return !task::completed;
202 }
203
Ed Tanous11ba3972022-07-11 09:50:41 -0700204 if (state->ends_with("Active"))
Ed Tanous002d39b2022-05-31 08:59:27 -0700205 {
206 taskData->messages.emplace_back(
207 messages::taskCompletedOK(index));
208 taskData->state = "Completed";
209 return task::completed;
210 }
211 }
212 else if (
213 iface ==
214 "xyz.openbmc_project.Software.ActivationProgress")
215 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700216 const uint8_t* progress = nullptr;
217 for (const auto& property : values)
218 {
219 if (property.first == "Progress")
220 {
Gayathri Leburu0fb5b502022-11-17 04:51:55 +0000221 progress =
222 std::get_if<uint8_t>(&property.second);
223 if (progress == nullptr)
James Feist32898ce2020-03-10 16:16:52 -0700224 {
James Feist32898ce2020-03-10 16:16:52 -0700225 taskData->messages.emplace_back(
Ed Tanous002d39b2022-05-31 08:59:27 -0700226 messages::internalError());
227 return task::completed;
James Feist32898ce2020-03-10 16:16:52 -0700228 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700229 }
230 }
James Feist32898ce2020-03-10 16:16:52 -0700231
Ed Tanous002d39b2022-05-31 08:59:27 -0700232 if (progress == nullptr)
233 {
234 return !task::completed;
235 }
Gayathri Leburu0fb5b502022-11-17 04:51:55 +0000236 taskData->percentComplete = *progress;
Ed Tanous002d39b2022-05-31 08:59:27 -0700237 taskData->messages.emplace_back(
Gayathri Leburu0fb5b502022-11-17 04:51:55 +0000238 messages::taskProgressChanged(index,
239 *progress));
James Feist32898ce2020-03-10 16:16:52 -0700240
Ed Tanous002d39b2022-05-31 08:59:27 -0700241 // if we're getting status updates it's
242 // still alive, update timer
243 taskData->extendTimer(std::chrono::minutes(5));
244 }
245
246 // as firmware update often results in a
247 // reboot, the task may never "complete"
248 // unless it is an error
249
250 return !task::completed;
Patrick Williams5a39f772023-10-20 11:20:21 -0500251 },
Ed Tanous002d39b2022-05-31 08:59:27 -0700252 "type='signal',interface='org.freedesktop.DBus.Properties',"
253 "member='PropertiesChanged',path='" +
254 objPath.str + "'");
255 task->startTimer(std::chrono::minutes(5));
256 task->populateResp(asyncResp->res);
257 task->payload.emplace(std::move(payload));
258 }
259 fwUpdateInProgress = false;
Patrick Williams5a39f772023-10-20 11:20:21 -0500260 });
Patrick Williams62bafc02022-09-08 17:35:35 -0500261
262 break;
Andrew Geissler86adcd62019-04-18 10:58:05 -0500263 }
264 }
265}
266
Myung Bae8549b952023-08-16 15:18:19 -0400267inline void afterAvailbleTimerAsyncWait(
268 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
269 const boost::system::error_code& ec)
270{
271 cleanUp();
272 if (ec == boost::asio::error::operation_aborted)
273 {
274 // expected, we were canceled before the timer completed.
275 return;
276 }
277 BMCWEB_LOG_ERROR("Timed out waiting for firmware object being created");
278 BMCWEB_LOG_ERROR("FW image may has already been uploaded to server");
279 if (ec)
280 {
281 BMCWEB_LOG_ERROR("Async_wait failed{}", ec);
282 return;
283 }
284 if (asyncResp)
285 {
286 redfish::messages::internalError(asyncResp->res);
287 }
288}
289
290inline void
291 handleUpdateErrorType(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
292 const std::string& url, const std::string& type)
293{
294 if (type == "xyz.openbmc_project.Software.Image.Error.UnTarFailure")
295 {
296 redfish::messages::invalidUpload(asyncResp->res, url,
297 "Invalid archive");
298 }
299 else if (type ==
300 "xyz.openbmc_project.Software.Image.Error.ManifestFileFailure")
301 {
302 redfish::messages::invalidUpload(asyncResp->res, url,
303 "Invalid manifest");
304 }
305 else if (type == "xyz.openbmc_project.Software.Image.Error.ImageFailure")
306 {
307 redfish::messages::invalidUpload(asyncResp->res, url,
308 "Invalid image format");
309 }
310 else if (type == "xyz.openbmc_project.Software.Version.Error.AlreadyExists")
311 {
312 redfish::messages::invalidUpload(asyncResp->res, url,
313 "Image version already exists");
314
315 redfish::messages::resourceAlreadyExists(
316 asyncResp->res, "UpdateService", "Version", "uploaded version");
317 }
318 else if (type == "xyz.openbmc_project.Software.Image.Error.BusyFailure")
319 {
320 redfish::messages::resourceExhaustion(asyncResp->res, url);
321 }
Myung Bae4034a652023-08-17 08:47:35 -0400322 else if (type == "xyz.openbmc_project.Software.Version.Error.Incompatible")
Myung Bae8549b952023-08-16 15:18:19 -0400323 {
Myung Bae4034a652023-08-17 08:47:35 -0400324 redfish::messages::invalidUpload(asyncResp->res, url,
325 "Incompatible image version");
326 }
327 else if (type ==
328 "xyz.openbmc_project.Software.Version.Error.ExpiredAccessKey")
329 {
330 redfish::messages::invalidUpload(asyncResp->res, url,
331 "Update Access Key Expired");
332 }
333 else if (type ==
334 "xyz.openbmc_project.Software.Version.Error.InvalidSignature")
335 {
336 redfish::messages::invalidUpload(asyncResp->res, url,
337 "Invalid image signature");
338 }
339 else if (type ==
340 "xyz.openbmc_project.Software.Image.Error.InternalFailure" ||
341 type == "xyz.openbmc_project.Software.Version.Error.HostFile")
342 {
343 BMCWEB_LOG_ERROR("Software Image Error type={}", type);
Myung Bae8549b952023-08-16 15:18:19 -0400344 redfish::messages::internalError(asyncResp->res);
345 }
Myung Bae4034a652023-08-17 08:47:35 -0400346 else
347 {
348 // Unrelated error types. Ignored
349 BMCWEB_LOG_INFO("Non-Software-related Error type={}. Ignored", type);
350 return;
351 }
352 // Clear the timer
353 fwAvailableTimer = nullptr;
Myung Bae8549b952023-08-16 15:18:19 -0400354}
355
356inline void
357 afterUpdateErrorMatcher(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
358 const std::string& url, sdbusplus::message_t& m)
359{
Michael Shen80f79a42023-08-24 13:41:53 +0000360 dbus::utility::DBusInterfacesMap interfacesProperties;
Myung Bae8549b952023-08-16 15:18:19 -0400361 sdbusplus::message::object_path objPath;
362 m.read(objPath, interfacesProperties);
363 BMCWEB_LOG_DEBUG("obj path = {}", objPath.str);
364 for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>&
365 interface : interfacesProperties)
366 {
367 if (interface.first == "xyz.openbmc_project.Logging.Entry")
368 {
369 for (const std::pair<std::string, dbus::utility::DbusVariantType>&
370 value : interface.second)
371 {
372 if (value.first != "Message")
373 {
374 continue;
375 }
376 const std::string* type =
377 std::get_if<std::string>(&value.second);
378 if (type == nullptr)
379 {
380 // if this was our message, timeout will cover it
381 return;
382 }
Myung Bae8549b952023-08-16 15:18:19 -0400383 handleUpdateErrorType(asyncResp, url, *type);
384 }
385 }
386 }
387}
388
Andrew Geissler0554c982019-04-23 14:40:12 -0500389// Note that asyncResp can be either a valid pointer or nullptr. If nullptr
390// then no asyncResp updates will occur
Ed Tanousb5a76932020-09-29 16:16:58 -0700391static void monitorForSoftwareAvailable(
zhanghch058d1b46d2021-04-01 11:18:24 +0800392 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
393 const crow::Request& req, const std::string& url,
Gunnar Mills5d138942022-09-07 10:26:21 -0500394 int timeoutTimeSeconds = 25)
Andrew Geissler86adcd62019-04-18 10:58:05 -0500395{
396 // Only allow one FW update at a time
Ed Tanouse05aec52022-01-25 10:28:56 -0800397 if (fwUpdateInProgress)
Andrew Geissler86adcd62019-04-18 10:58:05 -0500398 {
Andrew Geissler0554c982019-04-23 14:40:12 -0500399 if (asyncResp)
400 {
Andrew Geissler0554c982019-04-23 14:40:12 -0500401 messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
402 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500403 return;
404 }
405
Andrew Geissler0554c982019-04-23 14:40:12 -0500406 fwAvailableTimer =
Ed Tanous271584a2019-07-09 16:24:22 -0700407 std::make_unique<boost::asio::steady_timer>(*req.ioService);
Andrew Geissler86adcd62019-04-18 10:58:05 -0500408
Ed Tanous271584a2019-07-09 16:24:22 -0700409 fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds));
Andrew Geissler86adcd62019-04-18 10:58:05 -0500410
411 fwAvailableTimer->async_wait(
Myung Bae8549b952023-08-16 15:18:19 -0400412 std::bind_front(afterAvailbleTimerAsyncWait, asyncResp));
413
Ed Tanousa3e65892021-09-16 14:13:20 -0700414 task::Payload payload(req);
Patrick Williams59d494e2022-07-22 19:26:55 -0500415 auto callback = [asyncResp, payload](sdbusplus::message_t& m) mutable {
Ed Tanous62598e32023-07-17 17:06:25 -0700416 BMCWEB_LOG_DEBUG("Match fired");
Ed Tanousa3e65892021-09-16 14:13:20 -0700417 softwareInterfaceAdded(asyncResp, m, std::move(payload));
Andrew Geissler86adcd62019-04-18 10:58:05 -0500418 };
419
420 fwUpdateInProgress = true;
421
Patrick Williams59d494e2022-07-22 19:26:55 -0500422 fwUpdateMatcher = std::make_unique<sdbusplus::bus::match_t>(
Andrew Geissler86adcd62019-04-18 10:58:05 -0500423 *crow::connections::systemBus,
424 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
425 "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
426 callback);
James Feist4cde5d92020-06-11 10:39:55 -0700427
Patrick Williams59d494e2022-07-22 19:26:55 -0500428 fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match_t>(
James Feist4cde5d92020-06-11 10:39:55 -0700429 *crow::connections::systemBus,
Brian Mae1cc4822021-12-01 17:05:54 +0800430 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
431 "member='InterfacesAdded',"
432 "path='/xyz/openbmc_project/logging'",
Myung Bae8549b952023-08-16 15:18:19 -0400433 std::bind_front(afterUpdateErrorMatcher, asyncResp, url));
Andrew Geissler86adcd62019-04-18 10:58:05 -0500434}
Jennifer Lee729dae72018-04-24 15:59:34 -0700435
Ed Tanousf86bcc82023-08-25 09:34:07 -0700436struct TftpUrl
437{
438 std::string fwFile;
439 std::string tftpServer;
440};
441
442inline std::optional<TftpUrl>
443 parseTftpUrl(std::string imageURI,
444 std::optional<std::string> transferProtocol,
445 crow::Response& res)
446{
447 if (imageURI.find("://") == std::string::npos)
448 {
449 if (imageURI.starts_with("/"))
450 {
451 messages::actionParameterValueTypeError(
452 res, imageURI, "ImageURI", "UpdateService.SimpleUpdate");
453 return std::nullopt;
454 }
455 if (!transferProtocol)
456 {
457 messages::actionParameterValueTypeError(
458 res, imageURI, "ImageURI", "UpdateService.SimpleUpdate");
459 return std::nullopt;
460 }
461 // OpenBMC currently only supports TFTP
462 if (*transferProtocol != "TFTP")
463 {
464 messages::actionParameterNotSupported(res, "TransferProtocol",
465 *transferProtocol);
466 BMCWEB_LOG_ERROR("Request incorrect protocol parameter: {}",
467 *transferProtocol);
468 return std::nullopt;
469 }
470 imageURI = "tftp://" + imageURI;
471 }
472
473 boost::system::result<boost::urls::url> url =
474 boost::urls::parse_absolute_uri(imageURI);
475 if (!url)
476 {
477 messages::actionParameterValueTypeError(res, imageURI, "ImageURI",
478 "UpdateService.SimpleUpdate");
479
480 return std::nullopt;
481 }
482 url->normalize();
483
484 if (url->scheme() != "tftp")
485 {
486 messages::actionParameterNotSupported(res, "ImageURI", imageURI);
487 return std::nullopt;
488 }
489 std::string path(url->encoded_path());
490 if (path.size() < 2)
491 {
492 messages::actionParameterNotSupported(res, "ImageURI", imageURI);
493 return std::nullopt;
494 }
495 path.erase(0, 1);
496 std::string host(url->encoded_host_and_port());
497 return TftpUrl{path, host};
498}
499
Andrew Geissler0554c982019-04-23 14:40:12 -0500500/**
501 * UpdateServiceActionsSimpleUpdate class supports handle POST method for
502 * SimpleUpdate action.
503 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700504inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app)
Andrew Geissler0554c982019-04-23 14:40:12 -0500505{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700506 BMCWEB_ROUTE(
507 app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
Ed Tanoused398212021-06-09 17:05:54 -0700508 .privileges(redfish::privileges::postUpdateService)
Ed Tanous002d39b2022-05-31 08:59:27 -0700509 .methods(boost::beast::http::verb::post)(
510 [&app](const crow::Request& req,
511 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000512 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700513 {
514 return;
515 }
Ed Tanous45ca1b82022-03-25 13:07:27 -0700516
Ed Tanous002d39b2022-05-31 08:59:27 -0700517 std::optional<std::string> transferProtocol;
518 std::string imageURI;
Andrew Geissler0554c982019-04-23 14:40:12 -0500519
Ed Tanous62598e32023-07-17 17:06:25 -0700520 BMCWEB_LOG_DEBUG("Enter UpdateService.SimpleUpdate doPost");
Andrew Geissler0554c982019-04-23 14:40:12 -0500521
Ed Tanous002d39b2022-05-31 08:59:27 -0700522 // User can pass in both TransferProtocol and ImageURI parameters or
523 // they can pass in just the ImageURI with the transfer protocol
524 // embedded within it.
525 // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
526 // 2) ImageURI:tftp://1.1.1.1/myfile.bin
Andrew Geissler0554c982019-04-23 14:40:12 -0500527
Ed Tanous002d39b2022-05-31 08:59:27 -0700528 if (!json_util::readJsonAction(req, asyncResp->res, "TransferProtocol",
529 transferProtocol, "ImageURI", imageURI))
530 {
Ed Tanous62598e32023-07-17 17:06:25 -0700531 BMCWEB_LOG_DEBUG("Missing TransferProtocol or ImageURI parameter");
Ed Tanous002d39b2022-05-31 08:59:27 -0700532 return;
533 }
Ed Tanousf86bcc82023-08-25 09:34:07 -0700534 std::optional<TftpUrl> ret = parseTftpUrl(imageURI, transferProtocol,
535 asyncResp->res);
536 if (!ret)
Ed Tanous002d39b2022-05-31 08:59:27 -0700537 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700538 return;
539 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700540
Ed Tanousf86bcc82023-08-25 09:34:07 -0700541 BMCWEB_LOG_DEBUG("Server: {} File: {}", ret->tftpServer, ret->fwFile);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700542
Ed Tanous002d39b2022-05-31 08:59:27 -0700543 // Setup callback for when new software detected
544 // Give TFTP 10 minutes to complete
545 monitorForSoftwareAvailable(
546 asyncResp, req,
547 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate",
548 600);
549
550 // TFTP can take up to 10 minutes depending on image size and
551 // connection speed. Return to caller as soon as the TFTP operation
552 // has been started. The callback above will ensure the activate
553 // is started once the download has completed
554 redfish::messages::success(asyncResp->res);
555
556 // Call TFTP service
557 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800558 [](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700559 if (ec)
560 {
561 // messages::internalError(asyncResp->res);
562 cleanUp();
Ed Tanous62598e32023-07-17 17:06:25 -0700563 BMCWEB_LOG_DEBUG("error_code = {}", ec);
564 BMCWEB_LOG_DEBUG("error msg = {}", ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -0700565 }
566 else
567 {
Ed Tanous62598e32023-07-17 17:06:25 -0700568 BMCWEB_LOG_DEBUG("Call to DownloaViaTFTP Success");
Ed Tanous002d39b2022-05-31 08:59:27 -0700569 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500570 },
Ed Tanous002d39b2022-05-31 08:59:27 -0700571 "xyz.openbmc_project.Software.Download",
572 "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP",
Ed Tanousf86bcc82023-08-25 09:34:07 -0700573 "DownloadViaTFTP", ret->fwFile, ret->tftpServer);
Ed Tanous002d39b2022-05-31 08:59:27 -0700574
Ed Tanous62598e32023-07-17 17:06:25 -0700575 BMCWEB_LOG_DEBUG("Exit UpdateService.SimpleUpdate doPost");
Patrick Williams5a39f772023-10-20 11:20:21 -0500576 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700577}
578
George Liu0ed80c82020-05-12 16:06:27 +0800579inline void uploadImageFile(crow::Response& res, std::string_view body)
580{
Ed Tanous2c6ffdb2023-06-28 11:28:38 -0700581 std::filesystem::path filepath("/tmp/images/" + bmcweb::getRandomUUID());
582
Ed Tanous62598e32023-07-17 17:06:25 -0700583 BMCWEB_LOG_DEBUG("Writing file to {}", filepath.string());
George Liu0ed80c82020-05-12 16:06:27 +0800584 std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
585 std::ofstream::trunc);
586 // set the permission of the file to 640
Patrick Williams89492a12023-05-10 07:51:34 -0500587 std::filesystem::perms permission = std::filesystem::perms::owner_read |
588 std::filesystem::perms::group_read;
George Liu0ed80c82020-05-12 16:06:27 +0800589 std::filesystem::permissions(filepath, permission);
590 out << body;
591
592 if (out.bad())
593 {
594 messages::internalError(res);
595 cleanUp();
596 }
597}
598
599inline void setApplyTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
600 const std::string& applyTime)
601{
602 std::string applyTimeNewVal;
603 if (applyTime == "Immediate")
604 {
605 applyTimeNewVal =
606 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate";
607 }
608 else if (applyTime == "OnReset")
609 {
610 applyTimeNewVal =
611 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
612 }
613 else
614 {
Ed Tanous62598e32023-07-17 17:06:25 -0700615 BMCWEB_LOG_INFO(
616 "ApplyTime value is not in the list of acceptable values");
George Liu0ed80c82020-05-12 16:06:27 +0800617 messages::propertyValueNotInList(asyncResp->res, applyTime,
618 "ApplyTime");
619 return;
620 }
621
622 // Set the requested image apply time value
George Liu9ae226f2023-06-21 17:56:46 +0800623 sdbusplus::asio::setProperty(
624 *crow::connections::systemBus, "xyz.openbmc_project.Settings",
George Liu0ed80c82020-05-12 16:06:27 +0800625 "/xyz/openbmc_project/software/apply_time",
George Liu0ed80c82020-05-12 16:06:27 +0800626 "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime",
George Liu9ae226f2023-06-21 17:56:46 +0800627 applyTimeNewVal, [asyncResp](const boost::system::error_code& ec) {
Patrick Williams5a39f772023-10-20 11:20:21 -0500628 if (ec)
629 {
630 BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
631 messages::internalError(asyncResp->res);
632 return;
633 }
634 messages::success(asyncResp->res);
635 });
George Liu0ed80c82020-05-12 16:06:27 +0800636}
637
638inline void
639 updateMultipartContext(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
640 const MultipartParser& parser)
641{
642 const std::string* uploadData = nullptr;
643 std::optional<std::string> applyTime = "OnReset";
644 bool targetFound = false;
645 for (const FormPart& formpart : parser.mime_fields)
646 {
647 boost::beast::http::fields::const_iterator it =
648 formpart.fields.find("Content-Disposition");
649 if (it == formpart.fields.end())
650 {
Ed Tanous62598e32023-07-17 17:06:25 -0700651 BMCWEB_LOG_ERROR("Couldn't find Content-Disposition");
George Liu0ed80c82020-05-12 16:06:27 +0800652 return;
653 }
Ed Tanous62598e32023-07-17 17:06:25 -0700654 BMCWEB_LOG_INFO("Parsing value {}", it->value());
George Liu0ed80c82020-05-12 16:06:27 +0800655
656 // The construction parameters of param_list must start with `;`
657 size_t index = it->value().find(';');
658 if (index == std::string::npos)
659 {
660 continue;
661 }
662
Patrick Williams89492a12023-05-10 07:51:34 -0500663 for (const auto& param :
George Liu0ed80c82020-05-12 16:06:27 +0800664 boost::beast::http::param_list{it->value().substr(index)})
665 {
666 if (param.first != "name" || param.second.empty())
667 {
668 continue;
669 }
670
671 if (param.second == "UpdateParameters")
672 {
673 std::vector<std::string> targets;
674 nlohmann::json content =
675 nlohmann::json::parse(formpart.content);
676 if (!json_util::readJson(content, asyncResp->res, "Targets",
677 targets, "@Redfish.OperationApplyTime",
678 applyTime))
679 {
680 return;
681 }
682 if (targets.size() != 1)
683 {
684 messages::propertyValueFormatError(asyncResp->res,
685 "Targets", "");
686 return;
687 }
688 if (targets[0] != "/redfish/v1/Managers/bmc")
689 {
690 messages::propertyValueNotInList(asyncResp->res,
691 "Targets/0", targets[0]);
692 return;
693 }
694 targetFound = true;
695 }
696 else if (param.second == "UpdateFile")
697 {
698 uploadData = &(formpart.content);
699 }
700 }
701 }
702
703 if (uploadData == nullptr)
704 {
Ed Tanous62598e32023-07-17 17:06:25 -0700705 BMCWEB_LOG_ERROR("Upload data is NULL");
George Liu0ed80c82020-05-12 16:06:27 +0800706 messages::propertyMissing(asyncResp->res, "UpdateFile");
707 return;
708 }
709 if (!targetFound)
710 {
711 messages::propertyMissing(asyncResp->res, "targets");
712 return;
713 }
714
715 setApplyTime(asyncResp, *applyTime);
716
717 uploadImageFile(asyncResp->res, *uploadData);
718}
719
Ed Tanousc2051d12022-05-11 12:21:55 -0700720inline void
721 handleUpdateServicePost(App& app, const crow::Request& req,
722 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
723{
Carson Labrado3ba00072022-06-06 19:40:56 +0000724 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanousc2051d12022-05-11 12:21:55 -0700725 {
726 return;
727 }
Ninad Palsuleb33a4322023-06-09 09:19:18 -0500728 std::string_view contentType = req.getHeaderValue("Content-Type");
Ed Tanousc2051d12022-05-11 12:21:55 -0700729
Ed Tanous62598e32023-07-17 17:06:25 -0700730 BMCWEB_LOG_DEBUG("doPost: contentType={}", contentType);
Ed Tanousc2051d12022-05-11 12:21:55 -0700731
Ninad Palsuleb33a4322023-06-09 09:19:18 -0500732 // Make sure that content type is application/octet-stream or
733 // multipart/form-data
734 if (boost::iequals(contentType, "application/octet-stream"))
George Liu0ed80c82020-05-12 16:06:27 +0800735 {
Ninad Palsuleb33a4322023-06-09 09:19:18 -0500736 // Setup callback for when new software detected
737 monitorForSoftwareAvailable(asyncResp, req,
738 "/redfish/v1/UpdateService");
739
George Liu0ed80c82020-05-12 16:06:27 +0800740 uploadImageFile(asyncResp->res, req.body());
George Liu0ed80c82020-05-12 16:06:27 +0800741 }
Ninad Palsuleb33a4322023-06-09 09:19:18 -0500742 else if (contentType.starts_with("multipart/form-data"))
George Liu0ed80c82020-05-12 16:06:27 +0800743 {
Ninad Palsuleb33a4322023-06-09 09:19:18 -0500744 MultipartParser parser;
745
746 // Setup callback for when new software detected
747 monitorForSoftwareAvailable(asyncResp, req,
748 "/redfish/v1/UpdateService");
749
750 ParserError ec = parser.parse(req);
751 if (ec != ParserError::PARSER_SUCCESS)
752 {
753 // handle error
Ed Tanous62598e32023-07-17 17:06:25 -0700754 BMCWEB_LOG_ERROR("MIME parse failed, ec : {}",
755 static_cast<int>(ec));
Ninad Palsuleb33a4322023-06-09 09:19:18 -0500756 messages::internalError(asyncResp->res);
757 return;
758 }
759 updateMultipartContext(asyncResp, parser);
George Liu0ed80c82020-05-12 16:06:27 +0800760 }
Ninad Palsuleb33a4322023-06-09 09:19:18 -0500761 else
762 {
Ed Tanous62598e32023-07-17 17:06:25 -0700763 BMCWEB_LOG_DEBUG("Bad content type specified:{}", contentType);
Ninad Palsuleb33a4322023-06-09 09:19:18 -0500764 asyncResp->res.result(boost::beast::http::status::bad_request);
765 }
Ed Tanousc2051d12022-05-11 12:21:55 -0700766}
767
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700768inline void requestRoutesUpdateService(App& app)
769{
770 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
Ed Tanoused398212021-06-09 17:05:54 -0700771 .privileges(redfish::privileges::getUpdateService)
Ed Tanous002d39b2022-05-31 08:59:27 -0700772 .methods(boost::beast::http::verb::get)(
773 [&app](const crow::Request& req,
774 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000775 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700776 {
777 return;
778 }
779 asyncResp->res.jsonValue["@odata.type"] =
George Liu0ed80c82020-05-12 16:06:27 +0800780 "#UpdateService.v1_11_1.UpdateService";
Ed Tanous002d39b2022-05-31 08:59:27 -0700781 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
782 asyncResp->res.jsonValue["Id"] = "UpdateService";
783 asyncResp->res.jsonValue["Description"] = "Service for Software Update";
784 asyncResp->res.jsonValue["Name"] = "Update Service";
Ed Tanous4dc23f32022-05-11 11:32:19 -0700785
Ed Tanous002d39b2022-05-31 08:59:27 -0700786 asyncResp->res.jsonValue["HttpPushUri"] =
787 "/redfish/v1/UpdateService/update";
George Liu0ed80c82020-05-12 16:06:27 +0800788 asyncResp->res.jsonValue["MultipartHttpPushUri"] =
789 "/redfish/v1/UpdateService/update";
Ed Tanous4dc23f32022-05-11 11:32:19 -0700790
Ed Tanous002d39b2022-05-31 08:59:27 -0700791 // UpdateService cannot be disabled
792 asyncResp->res.jsonValue["ServiceEnabled"] = true;
793 asyncResp->res.jsonValue["FirmwareInventory"]["@odata.id"] =
794 "/redfish/v1/UpdateService/FirmwareInventory";
795 // Get the MaxImageSizeBytes
796 asyncResp->res.jsonValue["MaxImageSizeBytes"] =
797 bmcwebHttpReqBodyLimitMb * 1024 * 1024;
Tejas Patild61e5192021-06-04 15:49:35 +0530798
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700799#ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
Ed Tanous002d39b2022-05-31 08:59:27 -0700800 // Update Actions object.
801 nlohmann::json& updateSvcSimpleUpdate =
802 asyncResp->res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
803 updateSvcSimpleUpdate["target"] =
804 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
805 updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = {
806 "TFTP"};
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700807#endif
Ed Tanous002d39b2022-05-31 08:59:27 -0700808 // Get the current ApplyTime value
809 sdbusplus::asio::getProperty<std::string>(
810 *crow::connections::systemBus, "xyz.openbmc_project.Settings",
811 "/xyz/openbmc_project/software/apply_time",
812 "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800813 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700814 const std::string& applyTime) {
815 if (ec)
816 {
Ed Tanous62598e32023-07-17 17:06:25 -0700817 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700818 messages::internalError(asyncResp->res);
819 return;
820 }
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530821
Ed Tanous002d39b2022-05-31 08:59:27 -0700822 // Store the ApplyTime Value
823 if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
824 "RequestedApplyTimes.Immediate")
825 {
826 asyncResp->res.jsonValue["HttpPushUriOptions"]
827 ["HttpPushUriApplyTime"]["ApplyTime"] =
828 "Immediate";
829 }
830 else if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
831 "RequestedApplyTimes.OnReset")
832 {
833 asyncResp->res.jsonValue["HttpPushUriOptions"]
834 ["HttpPushUriApplyTime"]["ApplyTime"] =
835 "OnReset";
836 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700837 });
Patrick Williams5a39f772023-10-20 11:20:21 -0500838 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700839 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
Ed Tanoused398212021-06-09 17:05:54 -0700840 .privileges(redfish::privileges::patchUpdateService)
Ed Tanous002d39b2022-05-31 08:59:27 -0700841 .methods(boost::beast::http::verb::patch)(
842 [&app](const crow::Request& req,
843 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000844 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700845 {
846 return;
847 }
Ed Tanous62598e32023-07-17 17:06:25 -0700848 BMCWEB_LOG_DEBUG("doPatch...");
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530849
Ed Tanous002d39b2022-05-31 08:59:27 -0700850 std::optional<nlohmann::json> pushUriOptions;
851 if (!json_util::readJsonPatch(req, asyncResp->res, "HttpPushUriOptions",
852 pushUriOptions))
853 {
854 return;
855 }
856
857 if (pushUriOptions)
858 {
859 std::optional<nlohmann::json> pushUriApplyTime;
860 if (!json_util::readJson(*pushUriOptions, asyncResp->res,
861 "HttpPushUriApplyTime", pushUriApplyTime))
George Liu0fda0f12021-11-16 10:06:17 +0800862 {
863 return;
864 }
865
Ed Tanous002d39b2022-05-31 08:59:27 -0700866 if (pushUriApplyTime)
George Liu0fda0f12021-11-16 10:06:17 +0800867 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700868 std::optional<std::string> applyTime;
869 if (!json_util::readJson(*pushUriApplyTime, asyncResp->res,
870 "ApplyTime", applyTime))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700871 {
Ed Tanousc711bf82018-07-30 16:31:33 -0700872 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700873 }
Jennifer Lee6c4eb9d2018-05-22 10:58:31 -0700874
Ed Tanous002d39b2022-05-31 08:59:27 -0700875 if (applyTime)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700876 {
George Liu0ed80c82020-05-12 16:06:27 +0800877 setApplyTime(asyncResp, *applyTime);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700878 }
George Liu0fda0f12021-11-16 10:06:17 +0800879 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700880 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500881 });
Ed Tanousc2051d12022-05-11 12:21:55 -0700882
Ed Tanous4dc23f32022-05-11 11:32:19 -0700883 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/update/")
884 .privileges(redfish::privileges::postUpdateService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700885 .methods(boost::beast::http::verb::post)(
Ed Tanousc2051d12022-05-11 12:21:55 -0700886 std::bind_front(handleUpdateServicePost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700887}
888
889inline void requestRoutesSoftwareInventoryCollection(App& app)
890{
891 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/")
Ed Tanoused398212021-06-09 17:05:54 -0700892 .privileges(redfish::privileges::getSoftwareInventoryCollection)
Ed Tanous14766872022-03-15 10:44:42 -0700893 .methods(boost::beast::http::verb::get)(
894 [&app](const crow::Request& req,
895 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000896 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700897 {
898 return;
899 }
900 asyncResp->res.jsonValue["@odata.type"] =
901 "#SoftwareInventoryCollection.SoftwareInventoryCollection";
902 asyncResp->res.jsonValue["@odata.id"] =
903 "/redfish/v1/UpdateService/FirmwareInventory";
904 asyncResp->res.jsonValue["Name"] = "Software Inventory Collection";
John Edward Broadbent08d81ad2022-05-17 20:00:23 -0700905 const std::array<const std::string_view, 1> iface = {
George Liue99073f2022-12-09 11:06:16 +0800906 "xyz.openbmc_project.Software.Version"};
Ed Tanous002d39b2022-05-31 08:59:27 -0700907
John Edward Broadbent08d81ad2022-05-17 20:00:23 -0700908 redfish::collection_util::getCollectionMembers(
909 asyncResp,
910 boost::urls::url("/redfish/v1/UpdateService/FirmwareInventory"),
911 iface, "/xyz/openbmc_project/software");
Patrick Williams5a39f772023-10-20 11:20:21 -0500912 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700913}
914/* Fill related item links (i.e. bmc, bios) in for inventory */
915inline static void
Ed Tanousac106bf2023-06-07 09:24:59 -0700916 getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700917 const std::string& purpose)
918{
Willy Tueee00132022-06-14 14:53:17 -0700919 if (purpose == sw_util::bmcPurpose)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700920 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700921 nlohmann::json& relatedItem = asyncResp->res.jsonValue["RelatedItem"];
Ed Tanous14766872022-03-15 10:44:42 -0700922 nlohmann::json::object_t item;
923 item["@odata.id"] = "/redfish/v1/Managers/bmc";
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500924 relatedItem.emplace_back(std::move(item));
Ed Tanousac106bf2023-06-07 09:24:59 -0700925 asyncResp->res.jsonValue["RelatedItem@odata.count"] =
926 relatedItem.size();
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700927 }
Willy Tueee00132022-06-14 14:53:17 -0700928 else if (purpose == sw_util::biosPurpose)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700929 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700930 nlohmann::json& relatedItem = asyncResp->res.jsonValue["RelatedItem"];
Ed Tanous14766872022-03-15 10:44:42 -0700931 nlohmann::json::object_t item;
932 item["@odata.id"] = "/redfish/v1/Systems/system/Bios";
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500933 relatedItem.emplace_back(std::move(item));
Ed Tanousac106bf2023-06-07 09:24:59 -0700934 asyncResp->res.jsonValue["RelatedItem@odata.count"] =
935 relatedItem.size();
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700936 }
937 else
938 {
Carson Labradobf2dded2023-08-10 00:37:06 +0000939 BMCWEB_LOG_DEBUG("Unknown software purpose {}", purpose);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700940 }
941}
942
Willy Tuaf246602022-06-14 15:51:53 -0700943inline void
944 getSoftwareVersion(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
945 const std::string& service, const std::string& path,
946 const std::string& swId)
947{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200948 sdbusplus::asio::getAllProperties(
949 *crow::connections::systemBus, service, path,
950 "xyz.openbmc_project.Software.Version",
Willy Tuaf246602022-06-14 15:51:53 -0700951 [asyncResp,
Ed Tanous8b242752023-06-27 17:17:13 -0700952 swId](const boost::system::error_code& ec,
Willy Tuaf246602022-06-14 15:51:53 -0700953 const dbus::utility::DBusPropertiesMap& propertiesList) {
Ed Tanous8b242752023-06-27 17:17:13 -0700954 if (ec)
Willy Tuaf246602022-06-14 15:51:53 -0700955 {
956 messages::internalError(asyncResp->res);
957 return;
958 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200959
Willy Tuaf246602022-06-14 15:51:53 -0700960 const std::string* swInvPurpose = nullptr;
961 const std::string* version = nullptr;
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200962
963 const bool success = sdbusplus::unpackPropertiesNoThrow(
964 dbus_utils::UnpackErrorPrinter(), propertiesList, "Purpose",
965 swInvPurpose, "Version", version);
966
967 if (!success)
Willy Tuaf246602022-06-14 15:51:53 -0700968 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200969 messages::internalError(asyncResp->res);
970 return;
Willy Tuaf246602022-06-14 15:51:53 -0700971 }
972
973 if (swInvPurpose == nullptr)
974 {
Ed Tanous62598e32023-07-17 17:06:25 -0700975 BMCWEB_LOG_DEBUG("Can't find property \"Purpose\"!");
Willy Tuaf246602022-06-14 15:51:53 -0700976 messages::internalError(asyncResp->res);
977 return;
978 }
979
Ed Tanous62598e32023-07-17 17:06:25 -0700980 BMCWEB_LOG_DEBUG("swInvPurpose = {}", *swInvPurpose);
Willy Tuaf246602022-06-14 15:51:53 -0700981
982 if (version == nullptr)
983 {
Ed Tanous62598e32023-07-17 17:06:25 -0700984 BMCWEB_LOG_DEBUG("Can't find property \"Version\"!");
Willy Tuaf246602022-06-14 15:51:53 -0700985
986 messages::internalError(asyncResp->res);
987
988 return;
989 }
990 asyncResp->res.jsonValue["Version"] = *version;
991 asyncResp->res.jsonValue["Id"] = swId;
992
993 // swInvPurpose is of format:
994 // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
995 // Translate this to "ABC image"
996 size_t endDesc = swInvPurpose->rfind('.');
997 if (endDesc == std::string::npos)
998 {
999 messages::internalError(asyncResp->res);
1000 return;
1001 }
1002 endDesc++;
1003 if (endDesc >= swInvPurpose->size())
1004 {
1005 messages::internalError(asyncResp->res);
1006 return;
1007 }
1008
1009 std::string formatDesc = swInvPurpose->substr(endDesc);
1010 asyncResp->res.jsonValue["Description"] = formatDesc + " image";
1011 getRelatedItems(asyncResp, *swInvPurpose);
Patrick Williams5a39f772023-10-20 11:20:21 -05001012 });
Willy Tuaf246602022-06-14 15:51:53 -07001013}
1014
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001015inline void requestRoutesSoftwareInventory(App& app)
1016{
1017 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001018 .privileges(redfish::privileges::getSoftwareInventory)
Ed Tanous002d39b2022-05-31 08:59:27 -07001019 .methods(boost::beast::http::verb::get)(
1020 [&app](const crow::Request& req,
1021 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1022 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001023 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001024 {
1025 return;
1026 }
1027 std::shared_ptr<std::string> swId =
1028 std::make_shared<std::string>(param);
1029
Ed Tanousef4c65b2023-04-24 15:28:50 -07001030 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1031 "/redfish/v1/UpdateService/FirmwareInventory/{}", *swId);
Ed Tanous002d39b2022-05-31 08:59:27 -07001032
George Liue99073f2022-12-09 11:06:16 +08001033 constexpr std::array<std::string_view, 1> interfaces = {
1034 "xyz.openbmc_project.Software.Version"};
1035 dbus::utility::getSubTree(
1036 "/", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001037 [asyncResp,
George Liue99073f2022-12-09 11:06:16 +08001038 swId](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001039 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous62598e32023-07-17 17:06:25 -07001040 BMCWEB_LOG_DEBUG("doGet callback...");
Ed Tanous002d39b2022-05-31 08:59:27 -07001041 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001042 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001043 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001044 return;
1045 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001046
Ed Tanous002d39b2022-05-31 08:59:27 -07001047 // Ensure we find our input swId, otherwise return an error
1048 bool found = false;
1049 for (const std::pair<std::string,
1050 std::vector<std::pair<
1051 std::string, std::vector<std::string>>>>&
1052 obj : subtree)
1053 {
Ed Tanous11ba3972022-07-11 09:50:41 -07001054 if (!obj.first.ends_with(*swId))
Ed Tanous002d39b2022-05-31 08:59:27 -07001055 {
1056 continue;
1057 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001058
Ed Tanous002d39b2022-05-31 08:59:27 -07001059 if (obj.second.empty())
1060 {
1061 continue;
1062 }
1063
1064 found = true;
Willy Tueee00132022-06-14 14:53:17 -07001065 sw_util::getSwStatus(asyncResp, swId, obj.second[0].first);
Willy Tuaf246602022-06-14 15:51:53 -07001066 getSoftwareVersion(asyncResp, obj.second[0].first, obj.first,
1067 *swId);
Ed Tanous002d39b2022-05-31 08:59:27 -07001068 }
1069 if (!found)
1070 {
Ed Tanous62598e32023-07-17 17:06:25 -07001071 BMCWEB_LOG_WARNING("Input swID {} not found!", *swId);
Ed Tanous002d39b2022-05-31 08:59:27 -07001072 messages::resourceMissingAtURI(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001073 asyncResp->res,
1074 boost::urls::format(
1075 "/redfish/v1/UpdateService/FirmwareInventory/{}",
1076 *swId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001077 return;
1078 }
1079 asyncResp->res.jsonValue["@odata.type"] =
1080 "#SoftwareInventory.v1_1_0.SoftwareInventory";
1081 asyncResp->res.jsonValue["Name"] = "Software Inventory";
1082 asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001083
Ed Tanous002d39b2022-05-31 08:59:27 -07001084 asyncResp->res.jsonValue["Updateable"] = false;
Willy Tueee00132022-06-14 14:53:17 -07001085 sw_util::getSwUpdatableStatus(asyncResp, swId);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001086 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001087 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001088}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001089
1090} // namespace redfish