blob: 357c04770bee43adb702a2e14bacd04e677aa77b [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{
66 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 Tanous8b242752023-06-27 17:17:13 -070074 BMCWEB_LOG_DEBUG << "error_code = " << ec;
75 BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
Ed Tanous002d39b2022-05-31 08:59:27 -070076 }
George Liu9ae226f2023-06-21 17:56:46 +080077 });
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{
Ed Tanousb9d36b42022-02-26 21:42:46 -080086 dbus::utility::DBusInteracesMap interfacesProperties;
Andrew Geissler86adcd62019-04-18 10:58:05 -050087
88 sdbusplus::message::object_path objPath;
89
90 m.read(objPath, interfacesProperties);
91
92 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 {
95 BMCWEB_LOG_DEBUG << "interface = " << interface.first;
96
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 Tanous8b242752023-06-27 17:17:13 -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 {
123 BMCWEB_LOG_ERROR << "Invalid Object Size "
124 << objInfo.size();
125 if (asyncResp)
126 {
127 messages::internalError(asyncResp->res);
128 }
129 cleanUp();
130 return;
131 }
132 // cancel timer only when
133 // xyz.openbmc_project.Software.Activation interface
134 // is added
135 fwAvailableTimer = nullptr;
136
137 activateImage(objPath.str, objInfo[0].first);
138 if (asyncResp)
139 {
140 std::shared_ptr<task::TaskData> task =
141 task::TaskData::createTask(
Ed Tanous8b242752023-06-27 17:17:13 -0700142 [](const boost::system::error_code& ec2,
Patrick Williams59d494e2022-07-22 19:26:55 -0500143 sdbusplus::message_t& msg,
Ed Tanous002d39b2022-05-31 08:59:27 -0700144 const std::shared_ptr<task::TaskData>&
145 taskData) {
Ed Tanous8b242752023-06-27 17:17:13 -0700146 if (ec2)
Ed Tanous002d39b2022-05-31 08:59:27 -0700147 {
148 return task::completed;
149 }
150
151 std::string iface;
152 dbus::utility::DBusPropertiesMap values;
153
154 std::string index = std::to_string(taskData->index);
155 msg.read(iface, values);
156
157 if (iface == "xyz.openbmc_project.Software.Activation")
158 {
Gayathri Leburu0fb5b502022-11-17 04:51:55 +0000159 const std::string* state = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -0700160 for (const auto& property : values)
161 {
162 if (property.first == "Activation")
163 {
Gayathri Leburu0fb5b502022-11-17 04:51:55 +0000164 state = std::get_if<std::string>(
165 &property.second);
166 if (state == nullptr)
James Feist32898ce2020-03-10 16:16:52 -0700167 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700168 taskData->messages.emplace_back(
169 messages::internalError());
James Feist32898ce2020-03-10 16:16:52 -0700170 return task::completed;
171 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700172 }
173 }
James Feist32898ce2020-03-10 16:16:52 -0700174
Ed Tanous002d39b2022-05-31 08:59:27 -0700175 if (state == nullptr)
176 {
177 return !task::completed;
178 }
James Feist32898ce2020-03-10 16:16:52 -0700179
Ed Tanous11ba3972022-07-11 09:50:41 -0700180 if (state->ends_with("Invalid") ||
181 state->ends_with("Failed"))
Ed Tanous002d39b2022-05-31 08:59:27 -0700182 {
183 taskData->state = "Exception";
184 taskData->status = "Warning";
185 taskData->messages.emplace_back(
186 messages::taskAborted(index));
187 return task::completed;
188 }
James Feiste5d50062020-05-11 17:29:00 -0700189
Ed Tanous11ba3972022-07-11 09:50:41 -0700190 if (state->ends_with("Staged"))
Ed Tanous002d39b2022-05-31 08:59:27 -0700191 {
192 taskData->state = "Stopping";
193 taskData->messages.emplace_back(
194 messages::taskPaused(index));
195
196 // its staged, set a long timer to
197 // allow them time to complete the
198 // update (probably cycle the
199 // system) if this expires then
200 // task will be cancelled
201 taskData->extendTimer(std::chrono::hours(5));
202 return !task::completed;
203 }
204
Ed Tanous11ba3972022-07-11 09:50:41 -0700205 if (state->ends_with("Active"))
Ed Tanous002d39b2022-05-31 08:59:27 -0700206 {
207 taskData->messages.emplace_back(
208 messages::taskCompletedOK(index));
209 taskData->state = "Completed";
210 return task::completed;
211 }
212 }
213 else if (
214 iface ==
215 "xyz.openbmc_project.Software.ActivationProgress")
216 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700217 const uint8_t* progress = nullptr;
218 for (const auto& property : values)
219 {
220 if (property.first == "Progress")
221 {
Gayathri Leburu0fb5b502022-11-17 04:51:55 +0000222 progress =
223 std::get_if<uint8_t>(&property.second);
224 if (progress == nullptr)
James Feist32898ce2020-03-10 16:16:52 -0700225 {
James Feist32898ce2020-03-10 16:16:52 -0700226 taskData->messages.emplace_back(
Ed Tanous002d39b2022-05-31 08:59:27 -0700227 messages::internalError());
228 return task::completed;
James Feist32898ce2020-03-10 16:16:52 -0700229 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700230 }
231 }
James Feist32898ce2020-03-10 16:16:52 -0700232
Ed Tanous002d39b2022-05-31 08:59:27 -0700233 if (progress == nullptr)
234 {
235 return !task::completed;
236 }
Gayathri Leburu0fb5b502022-11-17 04:51:55 +0000237 taskData->percentComplete = *progress;
Ed Tanous002d39b2022-05-31 08:59:27 -0700238 taskData->messages.emplace_back(
Gayathri Leburu0fb5b502022-11-17 04:51:55 +0000239 messages::taskProgressChanged(index,
240 *progress));
James Feist32898ce2020-03-10 16:16:52 -0700241
Ed Tanous002d39b2022-05-31 08:59:27 -0700242 // if we're getting status updates it's
243 // still alive, update timer
244 taskData->extendTimer(std::chrono::minutes(5));
245 }
246
247 // as firmware update often results in a
248 // reboot, the task may never "complete"
249 // unless it is an error
250
251 return !task::completed;
252 },
253 "type='signal',interface='org.freedesktop.DBus.Properties',"
254 "member='PropertiesChanged',path='" +
255 objPath.str + "'");
256 task->startTimer(std::chrono::minutes(5));
257 task->populateResp(asyncResp->res);
258 task->payload.emplace(std::move(payload));
259 }
260 fwUpdateInProgress = false;
George Liu2b731192023-01-11 16:27:13 +0800261 });
Patrick Williams62bafc02022-09-08 17:35:35 -0500262
263 break;
Andrew Geissler86adcd62019-04-18 10:58:05 -0500264 }
265 }
266}
267
Andrew Geissler0554c982019-04-23 14:40:12 -0500268// Note that asyncResp can be either a valid pointer or nullptr. If nullptr
269// then no asyncResp updates will occur
Ed Tanousb5a76932020-09-29 16:16:58 -0700270static void monitorForSoftwareAvailable(
zhanghch058d1b46d2021-04-01 11:18:24 +0800271 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
272 const crow::Request& req, const std::string& url,
Gunnar Mills5d138942022-09-07 10:26:21 -0500273 int timeoutTimeSeconds = 25)
Andrew Geissler86adcd62019-04-18 10:58:05 -0500274{
275 // Only allow one FW update at a time
Ed Tanouse05aec52022-01-25 10:28:56 -0800276 if (fwUpdateInProgress)
Andrew Geissler86adcd62019-04-18 10:58:05 -0500277 {
Andrew Geissler0554c982019-04-23 14:40:12 -0500278 if (asyncResp)
279 {
Andrew Geissler0554c982019-04-23 14:40:12 -0500280 messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
281 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500282 return;
283 }
284
Andrew Geissler0554c982019-04-23 14:40:12 -0500285 fwAvailableTimer =
Ed Tanous271584a2019-07-09 16:24:22 -0700286 std::make_unique<boost::asio::steady_timer>(*req.ioService);
Andrew Geissler86adcd62019-04-18 10:58:05 -0500287
Ed Tanous271584a2019-07-09 16:24:22 -0700288 fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds));
Andrew Geissler86adcd62019-04-18 10:58:05 -0500289
290 fwAvailableTimer->async_wait(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500291 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700292 cleanUp();
293 if (ec == boost::asio::error::operation_aborted)
294 {
295 // expected, we were canceled before the timer completed.
296 return;
297 }
298 BMCWEB_LOG_ERROR
299 << "Timed out waiting for firmware object being created";
300 BMCWEB_LOG_ERROR << "FW image may has already been uploaded to server";
301 if (ec)
302 {
303 BMCWEB_LOG_ERROR << "Async_wait failed" << ec;
304 return;
305 }
306 if (asyncResp)
307 {
308 redfish::messages::internalError(asyncResp->res);
309 }
310 });
Ed Tanousa3e65892021-09-16 14:13:20 -0700311 task::Payload payload(req);
Patrick Williams59d494e2022-07-22 19:26:55 -0500312 auto callback = [asyncResp, payload](sdbusplus::message_t& m) mutable {
Andrew Geissler86adcd62019-04-18 10:58:05 -0500313 BMCWEB_LOG_DEBUG << "Match fired";
Ed Tanousa3e65892021-09-16 14:13:20 -0700314 softwareInterfaceAdded(asyncResp, m, std::move(payload));
Andrew Geissler86adcd62019-04-18 10:58:05 -0500315 };
316
317 fwUpdateInProgress = true;
318
Patrick Williams59d494e2022-07-22 19:26:55 -0500319 fwUpdateMatcher = std::make_unique<sdbusplus::bus::match_t>(
Andrew Geissler86adcd62019-04-18 10:58:05 -0500320 *crow::connections::systemBus,
321 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
322 "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
323 callback);
James Feist4cde5d92020-06-11 10:39:55 -0700324
Patrick Williams59d494e2022-07-22 19:26:55 -0500325 fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match_t>(
James Feist4cde5d92020-06-11 10:39:55 -0700326 *crow::connections::systemBus,
Brian Mae1cc4822021-12-01 17:05:54 +0800327 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
328 "member='InterfacesAdded',"
329 "path='/xyz/openbmc_project/logging'",
Patrick Williams59d494e2022-07-22 19:26:55 -0500330 [asyncResp, url](sdbusplus::message_t& m) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700331 std::vector<std::pair<std::string, dbus::utility::DBusPropertiesMap>>
332 interfacesProperties;
333 sdbusplus::message::object_path objPath;
334 m.read(objPath, interfacesProperties);
335 BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
336 for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>&
337 interface : interfacesProperties)
338 {
339 if (interface.first == "xyz.openbmc_project.Logging.Entry")
James Feist4cde5d92020-06-11 10:39:55 -0700340 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700341 for (const std::pair<std::string,
342 dbus::utility::DbusVariantType>& value :
343 interface.second)
Brian Mae1cc4822021-12-01 17:05:54 +0800344 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700345 if (value.first != "Message")
Brian Mae1cc4822021-12-01 17:05:54 +0800346 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700347 continue;
348 }
349 const std::string* type =
350 std::get_if<std::string>(&value.second);
351 if (type == nullptr)
352 {
353 // if this was our message, timeout will cover it
354 return;
355 }
356 fwAvailableTimer = nullptr;
357 if (*type ==
358 "xyz.openbmc_project.Software.Image.Error.UnTarFailure")
359 {
360 redfish::messages::invalidUpload(asyncResp->res, url,
361 "Invalid archive");
362 }
363 else if (*type ==
364 "xyz.openbmc_project.Software.Image.Error."
365 "ManifestFileFailure")
366 {
367 redfish::messages::invalidUpload(asyncResp->res, url,
368 "Invalid manifest");
369 }
370 else if (
371 *type ==
372 "xyz.openbmc_project.Software.Image.Error.ImageFailure")
373 {
374 redfish::messages::invalidUpload(
375 asyncResp->res, url, "Invalid image format");
376 }
377 else if (
378 *type ==
379 "xyz.openbmc_project.Software.Version.Error.AlreadyExists")
380 {
381 redfish::messages::invalidUpload(
382 asyncResp->res, url,
383 "Image version already exists");
Gunnar Mills88b3dd12020-11-20 14:26:04 -0600384
Ed Tanous002d39b2022-05-31 08:59:27 -0700385 redfish::messages::resourceAlreadyExists(
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800386 asyncResp->res, "UpdateService", "Version",
Ed Tanous002d39b2022-05-31 08:59:27 -0700387 "uploaded version");
388 }
389 else if (
390 *type ==
391 "xyz.openbmc_project.Software.Image.Error.BusyFailure")
392 {
393 redfish::messages::resourceExhaustion(asyncResp->res,
394 url);
395 }
396 else
397 {
398 redfish::messages::internalError(asyncResp->res);
Brian Mae1cc4822021-12-01 17:05:54 +0800399 }
400 }
Gunnar Mills88b3dd12020-11-20 14:26:04 -0600401 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700402 }
James Feist4cde5d92020-06-11 10:39:55 -0700403 });
Andrew Geissler86adcd62019-04-18 10:58:05 -0500404}
Jennifer Lee729dae72018-04-24 15:59:34 -0700405
Andrew Geissler0554c982019-04-23 14:40:12 -0500406/**
407 * UpdateServiceActionsSimpleUpdate class supports handle POST method for
408 * SimpleUpdate action.
409 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700410inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app)
Andrew Geissler0554c982019-04-23 14:40:12 -0500411{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700412 BMCWEB_ROUTE(
413 app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
Ed Tanoused398212021-06-09 17:05:54 -0700414 .privileges(redfish::privileges::postUpdateService)
Ed Tanous002d39b2022-05-31 08:59:27 -0700415 .methods(boost::beast::http::verb::post)(
416 [&app](const crow::Request& req,
417 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000418 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700419 {
420 return;
421 }
Ed Tanous45ca1b82022-03-25 13:07:27 -0700422
Ed Tanous002d39b2022-05-31 08:59:27 -0700423 std::optional<std::string> transferProtocol;
424 std::string imageURI;
Andrew Geissler0554c982019-04-23 14:40:12 -0500425
Ed Tanous002d39b2022-05-31 08:59:27 -0700426 BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost";
Andrew Geissler0554c982019-04-23 14:40:12 -0500427
Ed Tanous002d39b2022-05-31 08:59:27 -0700428 // User can pass in both TransferProtocol and ImageURI parameters or
429 // they can pass in just the ImageURI with the transfer protocol
430 // embedded within it.
431 // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
432 // 2) ImageURI:tftp://1.1.1.1/myfile.bin
Andrew Geissler0554c982019-04-23 14:40:12 -0500433
Ed Tanous002d39b2022-05-31 08:59:27 -0700434 if (!json_util::readJsonAction(req, asyncResp->res, "TransferProtocol",
435 transferProtocol, "ImageURI", imageURI))
436 {
437 BMCWEB_LOG_DEBUG
438 << "Missing TransferProtocol or ImageURI parameter";
439 return;
440 }
441 if (!transferProtocol)
442 {
443 // Must be option 2
444 // Verify ImageURI has transfer protocol in it
445 size_t separator = imageURI.find(':');
Andrew Geissler0554c982019-04-23 14:40:12 -0500446 if ((separator == std::string::npos) ||
447 ((separator + 1) > imageURI.size()))
448 {
449 messages::actionParameterValueTypeError(
450 asyncResp->res, imageURI, "ImageURI",
451 "UpdateService.SimpleUpdate");
Ed Tanous002d39b2022-05-31 08:59:27 -0700452 BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: "
453 << imageURI;
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530454 return;
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530455 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700456 transferProtocol = imageURI.substr(0, separator);
457 // Ensure protocol is upper case for a common comparison path
458 // below
459 boost::to_upper(*transferProtocol);
460 BMCWEB_LOG_DEBUG << "Encoded transfer protocol "
461 << *transferProtocol;
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530462
Ed Tanous002d39b2022-05-31 08:59:27 -0700463 // Adjust imageURI to not have the protocol on it for parsing
464 // below
465 // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin
466 imageURI = imageURI.substr(separator + 3);
467 BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI;
468 }
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530469
Ed Tanous002d39b2022-05-31 08:59:27 -0700470 // OpenBMC currently only supports TFTP
471 if (*transferProtocol != "TFTP")
472 {
473 messages::actionParameterNotSupported(asyncResp->res,
474 "TransferProtocol",
475 "UpdateService.SimpleUpdate");
476 BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: "
477 << *transferProtocol;
478 return;
479 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700480
Ed Tanous002d39b2022-05-31 08:59:27 -0700481 // Format should be <IP or Hostname>/<file> for imageURI
482 size_t separator = imageURI.find('/');
483 if ((separator == std::string::npos) ||
484 ((separator + 1) > imageURI.size()))
485 {
486 messages::actionParameterValueTypeError(
487 asyncResp->res, imageURI, "ImageURI",
488 "UpdateService.SimpleUpdate");
489 BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI;
490 return;
491 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700492
Ed Tanous002d39b2022-05-31 08:59:27 -0700493 std::string tftpServer = imageURI.substr(0, separator);
494 std::string fwFile = imageURI.substr(separator + 1);
495 BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700496
Ed Tanous002d39b2022-05-31 08:59:27 -0700497 // Setup callback for when new software detected
498 // Give TFTP 10 minutes to complete
499 monitorForSoftwareAvailable(
500 asyncResp, req,
501 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate",
502 600);
503
504 // TFTP can take up to 10 minutes depending on image size and
505 // connection speed. Return to caller as soon as the TFTP operation
506 // has been started. The callback above will ensure the activate
507 // is started once the download has completed
508 redfish::messages::success(asyncResp->res);
509
510 // Call TFTP service
511 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800512 [](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700513 if (ec)
514 {
515 // messages::internalError(asyncResp->res);
516 cleanUp();
517 BMCWEB_LOG_DEBUG << "error_code = " << ec;
518 BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
519 }
520 else
521 {
522 BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success";
523 }
524 },
525 "xyz.openbmc_project.Software.Download",
526 "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP",
527 "DownloadViaTFTP", fwFile, tftpServer);
528
529 BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700530 });
531}
532
George Liu0ed80c82020-05-12 16:06:27 +0800533inline void uploadImageFile(crow::Response& res, std::string_view body)
534{
Ed Tanous2c6ffdb2023-06-28 11:28:38 -0700535 std::filesystem::path filepath("/tmp/images/" + bmcweb::getRandomUUID());
536
George Liu0ed80c82020-05-12 16:06:27 +0800537 BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
538 std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
539 std::ofstream::trunc);
540 // set the permission of the file to 640
Patrick Williams89492a12023-05-10 07:51:34 -0500541 std::filesystem::perms permission = std::filesystem::perms::owner_read |
542 std::filesystem::perms::group_read;
George Liu0ed80c82020-05-12 16:06:27 +0800543 std::filesystem::permissions(filepath, permission);
544 out << body;
545
546 if (out.bad())
547 {
548 messages::internalError(res);
549 cleanUp();
550 }
551}
552
553inline void setApplyTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
554 const std::string& applyTime)
555{
556 std::string applyTimeNewVal;
557 if (applyTime == "Immediate")
558 {
559 applyTimeNewVal =
560 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate";
561 }
562 else if (applyTime == "OnReset")
563 {
564 applyTimeNewVal =
565 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
566 }
567 else
568 {
569 BMCWEB_LOG_INFO
570 << "ApplyTime value is not in the list of acceptable values";
571 messages::propertyValueNotInList(asyncResp->res, applyTime,
572 "ApplyTime");
573 return;
574 }
575
576 // Set the requested image apply time value
George Liu9ae226f2023-06-21 17:56:46 +0800577 sdbusplus::asio::setProperty(
578 *crow::connections::systemBus, "xyz.openbmc_project.Settings",
George Liu0ed80c82020-05-12 16:06:27 +0800579 "/xyz/openbmc_project/software/apply_time",
George Liu0ed80c82020-05-12 16:06:27 +0800580 "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime",
George Liu9ae226f2023-06-21 17:56:46 +0800581 applyTimeNewVal, [asyncResp](const boost::system::error_code& ec) {
582 if (ec)
583 {
584 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
585 messages::internalError(asyncResp->res);
586 return;
587 }
588 messages::success(asyncResp->res);
589 });
George Liu0ed80c82020-05-12 16:06:27 +0800590}
591
592inline void
593 updateMultipartContext(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
594 const MultipartParser& parser)
595{
596 const std::string* uploadData = nullptr;
597 std::optional<std::string> applyTime = "OnReset";
598 bool targetFound = false;
599 for (const FormPart& formpart : parser.mime_fields)
600 {
601 boost::beast::http::fields::const_iterator it =
602 formpart.fields.find("Content-Disposition");
603 if (it == formpart.fields.end())
604 {
605 BMCWEB_LOG_ERROR << "Couldn't find Content-Disposition";
606 return;
607 }
608 BMCWEB_LOG_INFO << "Parsing value " << it->value();
609
610 // The construction parameters of param_list must start with `;`
611 size_t index = it->value().find(';');
612 if (index == std::string::npos)
613 {
614 continue;
615 }
616
Patrick Williams89492a12023-05-10 07:51:34 -0500617 for (const auto& param :
George Liu0ed80c82020-05-12 16:06:27 +0800618 boost::beast::http::param_list{it->value().substr(index)})
619 {
620 if (param.first != "name" || param.second.empty())
621 {
622 continue;
623 }
624
625 if (param.second == "UpdateParameters")
626 {
627 std::vector<std::string> targets;
628 nlohmann::json content =
629 nlohmann::json::parse(formpart.content);
630 if (!json_util::readJson(content, asyncResp->res, "Targets",
631 targets, "@Redfish.OperationApplyTime",
632 applyTime))
633 {
634 return;
635 }
636 if (targets.size() != 1)
637 {
638 messages::propertyValueFormatError(asyncResp->res,
639 "Targets", "");
640 return;
641 }
642 if (targets[0] != "/redfish/v1/Managers/bmc")
643 {
644 messages::propertyValueNotInList(asyncResp->res,
645 "Targets/0", targets[0]);
646 return;
647 }
648 targetFound = true;
649 }
650 else if (param.second == "UpdateFile")
651 {
652 uploadData = &(formpart.content);
653 }
654 }
655 }
656
657 if (uploadData == nullptr)
658 {
659 BMCWEB_LOG_ERROR << "Upload data is NULL";
660 messages::propertyMissing(asyncResp->res, "UpdateFile");
661 return;
662 }
663 if (!targetFound)
664 {
665 messages::propertyMissing(asyncResp->res, "targets");
666 return;
667 }
668
669 setApplyTime(asyncResp, *applyTime);
670
671 uploadImageFile(asyncResp->res, *uploadData);
672}
673
Ed Tanousc2051d12022-05-11 12:21:55 -0700674inline void
675 handleUpdateServicePost(App& app, const crow::Request& req,
676 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
677{
Carson Labrado3ba00072022-06-06 19:40:56 +0000678 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanousc2051d12022-05-11 12:21:55 -0700679 {
680 return;
681 }
Ninad Palsuleb33a4322023-06-09 09:19:18 -0500682 std::string_view contentType = req.getHeaderValue("Content-Type");
Ed Tanousc2051d12022-05-11 12:21:55 -0700683
Ninad Palsuleb33a4322023-06-09 09:19:18 -0500684 BMCWEB_LOG_DEBUG << "doPost: contentType=" << contentType;
Ed Tanousc2051d12022-05-11 12:21:55 -0700685
Ninad Palsuleb33a4322023-06-09 09:19:18 -0500686 // Make sure that content type is application/octet-stream or
687 // multipart/form-data
688 if (boost::iequals(contentType, "application/octet-stream"))
George Liu0ed80c82020-05-12 16:06:27 +0800689 {
Ninad Palsuleb33a4322023-06-09 09:19:18 -0500690 // Setup callback for when new software detected
691 monitorForSoftwareAvailable(asyncResp, req,
692 "/redfish/v1/UpdateService");
693
George Liu0ed80c82020-05-12 16:06:27 +0800694 uploadImageFile(asyncResp->res, req.body());
George Liu0ed80c82020-05-12 16:06:27 +0800695 }
Ninad Palsuleb33a4322023-06-09 09:19:18 -0500696 else if (contentType.starts_with("multipart/form-data"))
George Liu0ed80c82020-05-12 16:06:27 +0800697 {
Ninad Palsuleb33a4322023-06-09 09:19:18 -0500698 MultipartParser parser;
699
700 // Setup callback for when new software detected
701 monitorForSoftwareAvailable(asyncResp, req,
702 "/redfish/v1/UpdateService");
703
704 ParserError ec = parser.parse(req);
705 if (ec != ParserError::PARSER_SUCCESS)
706 {
707 // handle error
708 BMCWEB_LOG_ERROR << "MIME parse failed, ec : "
709 << static_cast<int>(ec);
710 messages::internalError(asyncResp->res);
711 return;
712 }
713 updateMultipartContext(asyncResp, parser);
George Liu0ed80c82020-05-12 16:06:27 +0800714 }
Ninad Palsuleb33a4322023-06-09 09:19:18 -0500715 else
716 {
717 BMCWEB_LOG_DEBUG << "Bad content type specified:" << contentType;
718 asyncResp->res.result(boost::beast::http::status::bad_request);
719 }
Ed Tanousc2051d12022-05-11 12:21:55 -0700720}
721
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700722inline void requestRoutesUpdateService(App& app)
723{
724 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
Ed Tanoused398212021-06-09 17:05:54 -0700725 .privileges(redfish::privileges::getUpdateService)
Ed Tanous002d39b2022-05-31 08:59:27 -0700726 .methods(boost::beast::http::verb::get)(
727 [&app](const crow::Request& req,
728 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000729 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700730 {
731 return;
732 }
733 asyncResp->res.jsonValue["@odata.type"] =
George Liu0ed80c82020-05-12 16:06:27 +0800734 "#UpdateService.v1_11_1.UpdateService";
Ed Tanous002d39b2022-05-31 08:59:27 -0700735 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
736 asyncResp->res.jsonValue["Id"] = "UpdateService";
737 asyncResp->res.jsonValue["Description"] = "Service for Software Update";
738 asyncResp->res.jsonValue["Name"] = "Update Service";
Ed Tanous4dc23f32022-05-11 11:32:19 -0700739
Ed Tanous002d39b2022-05-31 08:59:27 -0700740 asyncResp->res.jsonValue["HttpPushUri"] =
741 "/redfish/v1/UpdateService/update";
George Liu0ed80c82020-05-12 16:06:27 +0800742 asyncResp->res.jsonValue["MultipartHttpPushUri"] =
743 "/redfish/v1/UpdateService/update";
Ed Tanous4dc23f32022-05-11 11:32:19 -0700744
Ed Tanous002d39b2022-05-31 08:59:27 -0700745 // UpdateService cannot be disabled
746 asyncResp->res.jsonValue["ServiceEnabled"] = true;
747 asyncResp->res.jsonValue["FirmwareInventory"]["@odata.id"] =
748 "/redfish/v1/UpdateService/FirmwareInventory";
749 // Get the MaxImageSizeBytes
750 asyncResp->res.jsonValue["MaxImageSizeBytes"] =
751 bmcwebHttpReqBodyLimitMb * 1024 * 1024;
Tejas Patild61e5192021-06-04 15:49:35 +0530752
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700753#ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
Ed Tanous002d39b2022-05-31 08:59:27 -0700754 // Update Actions object.
755 nlohmann::json& updateSvcSimpleUpdate =
756 asyncResp->res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
757 updateSvcSimpleUpdate["target"] =
758 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
759 updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = {
760 "TFTP"};
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700761#endif
Ed Tanous002d39b2022-05-31 08:59:27 -0700762 // Get the current ApplyTime value
763 sdbusplus::asio::getProperty<std::string>(
764 *crow::connections::systemBus, "xyz.openbmc_project.Settings",
765 "/xyz/openbmc_project/software/apply_time",
766 "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800767 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700768 const std::string& applyTime) {
769 if (ec)
770 {
771 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
772 messages::internalError(asyncResp->res);
773 return;
774 }
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530775
Ed Tanous002d39b2022-05-31 08:59:27 -0700776 // Store the ApplyTime Value
777 if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
778 "RequestedApplyTimes.Immediate")
779 {
780 asyncResp->res.jsonValue["HttpPushUriOptions"]
781 ["HttpPushUriApplyTime"]["ApplyTime"] =
782 "Immediate";
783 }
784 else if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
785 "RequestedApplyTimes.OnReset")
786 {
787 asyncResp->res.jsonValue["HttpPushUriOptions"]
788 ["HttpPushUriApplyTime"]["ApplyTime"] =
789 "OnReset";
790 }
791 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700792 });
793 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
Ed Tanoused398212021-06-09 17:05:54 -0700794 .privileges(redfish::privileges::patchUpdateService)
Ed Tanous002d39b2022-05-31 08:59:27 -0700795 .methods(boost::beast::http::verb::patch)(
796 [&app](const crow::Request& req,
797 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000798 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700799 {
800 return;
801 }
802 BMCWEB_LOG_DEBUG << "doPatch...";
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530803
Ed Tanous002d39b2022-05-31 08:59:27 -0700804 std::optional<nlohmann::json> pushUriOptions;
805 if (!json_util::readJsonPatch(req, asyncResp->res, "HttpPushUriOptions",
806 pushUriOptions))
807 {
808 return;
809 }
810
811 if (pushUriOptions)
812 {
813 std::optional<nlohmann::json> pushUriApplyTime;
814 if (!json_util::readJson(*pushUriOptions, asyncResp->res,
815 "HttpPushUriApplyTime", pushUriApplyTime))
George Liu0fda0f12021-11-16 10:06:17 +0800816 {
817 return;
818 }
819
Ed Tanous002d39b2022-05-31 08:59:27 -0700820 if (pushUriApplyTime)
George Liu0fda0f12021-11-16 10:06:17 +0800821 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700822 std::optional<std::string> applyTime;
823 if (!json_util::readJson(*pushUriApplyTime, asyncResp->res,
824 "ApplyTime", applyTime))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700825 {
Ed Tanousc711bf82018-07-30 16:31:33 -0700826 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700827 }
Jennifer Lee6c4eb9d2018-05-22 10:58:31 -0700828
Ed Tanous002d39b2022-05-31 08:59:27 -0700829 if (applyTime)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700830 {
George Liu0ed80c82020-05-12 16:06:27 +0800831 setApplyTime(asyncResp, *applyTime);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700832 }
George Liu0fda0f12021-11-16 10:06:17 +0800833 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700834 }
George Liu0fda0f12021-11-16 10:06:17 +0800835 });
Ed Tanousc2051d12022-05-11 12:21:55 -0700836
Ed Tanous4dc23f32022-05-11 11:32:19 -0700837 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/update/")
838 .privileges(redfish::privileges::postUpdateService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700839 .methods(boost::beast::http::verb::post)(
Ed Tanousc2051d12022-05-11 12:21:55 -0700840 std::bind_front(handleUpdateServicePost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700841}
842
843inline void requestRoutesSoftwareInventoryCollection(App& app)
844{
845 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/")
Ed Tanoused398212021-06-09 17:05:54 -0700846 .privileges(redfish::privileges::getSoftwareInventoryCollection)
Ed Tanous14766872022-03-15 10:44:42 -0700847 .methods(boost::beast::http::verb::get)(
848 [&app](const crow::Request& req,
849 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000850 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700851 {
852 return;
853 }
854 asyncResp->res.jsonValue["@odata.type"] =
855 "#SoftwareInventoryCollection.SoftwareInventoryCollection";
856 asyncResp->res.jsonValue["@odata.id"] =
857 "/redfish/v1/UpdateService/FirmwareInventory";
858 asyncResp->res.jsonValue["Name"] = "Software Inventory Collection";
John Edward Broadbent08d81ad2022-05-17 20:00:23 -0700859 const std::array<const std::string_view, 1> iface = {
George Liue99073f2022-12-09 11:06:16 +0800860 "xyz.openbmc_project.Software.Version"};
Ed Tanous002d39b2022-05-31 08:59:27 -0700861
John Edward Broadbent08d81ad2022-05-17 20:00:23 -0700862 redfish::collection_util::getCollectionMembers(
863 asyncResp,
864 boost::urls::url("/redfish/v1/UpdateService/FirmwareInventory"),
865 iface, "/xyz/openbmc_project/software");
Ed Tanous002d39b2022-05-31 08:59:27 -0700866 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700867}
868/* Fill related item links (i.e. bmc, bios) in for inventory */
869inline static void
Ed Tanousac106bf2023-06-07 09:24:59 -0700870 getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700871 const std::string& purpose)
872{
Willy Tueee00132022-06-14 14:53:17 -0700873 if (purpose == sw_util::bmcPurpose)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700874 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700875 nlohmann::json& relatedItem = asyncResp->res.jsonValue["RelatedItem"];
Ed Tanous14766872022-03-15 10:44:42 -0700876 nlohmann::json::object_t item;
877 item["@odata.id"] = "/redfish/v1/Managers/bmc";
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500878 relatedItem.emplace_back(std::move(item));
Ed Tanousac106bf2023-06-07 09:24:59 -0700879 asyncResp->res.jsonValue["RelatedItem@odata.count"] =
880 relatedItem.size();
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700881 }
Willy Tueee00132022-06-14 14:53:17 -0700882 else if (purpose == sw_util::biosPurpose)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700883 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700884 nlohmann::json& relatedItem = asyncResp->res.jsonValue["RelatedItem"];
Ed Tanous14766872022-03-15 10:44:42 -0700885 nlohmann::json::object_t item;
886 item["@odata.id"] = "/redfish/v1/Systems/system/Bios";
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500887 relatedItem.emplace_back(std::move(item));
Ed Tanousac106bf2023-06-07 09:24:59 -0700888 asyncResp->res.jsonValue["RelatedItem@odata.count"] =
889 relatedItem.size();
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700890 }
891 else
892 {
893 BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
894 }
895}
896
Willy Tuaf246602022-06-14 15:51:53 -0700897inline void
898 getSoftwareVersion(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
899 const std::string& service, const std::string& path,
900 const std::string& swId)
901{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200902 sdbusplus::asio::getAllProperties(
903 *crow::connections::systemBus, service, path,
904 "xyz.openbmc_project.Software.Version",
Willy Tuaf246602022-06-14 15:51:53 -0700905 [asyncResp,
Ed Tanous8b242752023-06-27 17:17:13 -0700906 swId](const boost::system::error_code& ec,
Willy Tuaf246602022-06-14 15:51:53 -0700907 const dbus::utility::DBusPropertiesMap& propertiesList) {
Ed Tanous8b242752023-06-27 17:17:13 -0700908 if (ec)
Willy Tuaf246602022-06-14 15:51:53 -0700909 {
910 messages::internalError(asyncResp->res);
911 return;
912 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200913
Willy Tuaf246602022-06-14 15:51:53 -0700914 const std::string* swInvPurpose = nullptr;
915 const std::string* version = nullptr;
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200916
917 const bool success = sdbusplus::unpackPropertiesNoThrow(
918 dbus_utils::UnpackErrorPrinter(), propertiesList, "Purpose",
919 swInvPurpose, "Version", version);
920
921 if (!success)
Willy Tuaf246602022-06-14 15:51:53 -0700922 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200923 messages::internalError(asyncResp->res);
924 return;
Willy Tuaf246602022-06-14 15:51:53 -0700925 }
926
927 if (swInvPurpose == nullptr)
928 {
929 BMCWEB_LOG_DEBUG << "Can't find property \"Purpose\"!";
930 messages::internalError(asyncResp->res);
931 return;
932 }
933
934 BMCWEB_LOG_DEBUG << "swInvPurpose = " << *swInvPurpose;
935
936 if (version == nullptr)
937 {
938 BMCWEB_LOG_DEBUG << "Can't find property \"Version\"!";
939
940 messages::internalError(asyncResp->res);
941
942 return;
943 }
944 asyncResp->res.jsonValue["Version"] = *version;
945 asyncResp->res.jsonValue["Id"] = swId;
946
947 // swInvPurpose is of format:
948 // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
949 // Translate this to "ABC image"
950 size_t endDesc = swInvPurpose->rfind('.');
951 if (endDesc == std::string::npos)
952 {
953 messages::internalError(asyncResp->res);
954 return;
955 }
956 endDesc++;
957 if (endDesc >= swInvPurpose->size())
958 {
959 messages::internalError(asyncResp->res);
960 return;
961 }
962
963 std::string formatDesc = swInvPurpose->substr(endDesc);
964 asyncResp->res.jsonValue["Description"] = formatDesc + " image";
965 getRelatedItems(asyncResp, *swInvPurpose);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200966 });
Willy Tuaf246602022-06-14 15:51:53 -0700967}
968
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700969inline void requestRoutesSoftwareInventory(App& app)
970{
971 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700972 .privileges(redfish::privileges::getSoftwareInventory)
Ed Tanous002d39b2022-05-31 08:59:27 -0700973 .methods(boost::beast::http::verb::get)(
974 [&app](const crow::Request& req,
975 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
976 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000977 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700978 {
979 return;
980 }
981 std::shared_ptr<std::string> swId =
982 std::make_shared<std::string>(param);
983
Ed Tanousef4c65b2023-04-24 15:28:50 -0700984 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
985 "/redfish/v1/UpdateService/FirmwareInventory/{}", *swId);
Ed Tanous002d39b2022-05-31 08:59:27 -0700986
George Liue99073f2022-12-09 11:06:16 +0800987 constexpr std::array<std::string_view, 1> interfaces = {
988 "xyz.openbmc_project.Software.Version"};
989 dbus::utility::getSubTree(
990 "/", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -0700991 [asyncResp,
George Liue99073f2022-12-09 11:06:16 +0800992 swId](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700993 const dbus::utility::MapperGetSubTreeResponse& subtree) {
994 BMCWEB_LOG_DEBUG << "doGet callback...";
995 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700996 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700997 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -0700998 return;
999 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001000
Ed Tanous002d39b2022-05-31 08:59:27 -07001001 // Ensure we find our input swId, otherwise return an error
1002 bool found = false;
1003 for (const std::pair<std::string,
1004 std::vector<std::pair<
1005 std::string, std::vector<std::string>>>>&
1006 obj : subtree)
1007 {
Ed Tanous11ba3972022-07-11 09:50:41 -07001008 if (!obj.first.ends_with(*swId))
Ed Tanous002d39b2022-05-31 08:59:27 -07001009 {
1010 continue;
1011 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001012
Ed Tanous002d39b2022-05-31 08:59:27 -07001013 if (obj.second.empty())
1014 {
1015 continue;
1016 }
1017
1018 found = true;
Willy Tueee00132022-06-14 14:53:17 -07001019 sw_util::getSwStatus(asyncResp, swId, obj.second[0].first);
Willy Tuaf246602022-06-14 15:51:53 -07001020 getSoftwareVersion(asyncResp, obj.second[0].first, obj.first,
1021 *swId);
Ed Tanous002d39b2022-05-31 08:59:27 -07001022 }
1023 if (!found)
1024 {
Myung Baeb90d14f2023-05-31 14:40:39 -05001025 BMCWEB_LOG_WARNING << "Input swID " << *swId << " not found!";
Ed Tanous002d39b2022-05-31 08:59:27 -07001026 messages::resourceMissingAtURI(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001027 asyncResp->res,
1028 boost::urls::format(
1029 "/redfish/v1/UpdateService/FirmwareInventory/{}",
1030 *swId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001031 return;
1032 }
1033 asyncResp->res.jsonValue["@odata.type"] =
1034 "#SoftwareInventory.v1_1_0.SoftwareInventory";
1035 asyncResp->res.jsonValue["Name"] = "Software Inventory";
1036 asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001037
Ed Tanous002d39b2022-05-31 08:59:27 -07001038 asyncResp->res.jsonValue["Updateable"] = false;
Willy Tueee00132022-06-14 14:53:17 -07001039 sw_util::getSwUpdatableStatus(asyncResp, swId);
George Liue99073f2022-12-09 11:06:16 +08001040 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001041 });
1042}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001043
1044} // namespace redfish