blob: 650ba95622074e90c09e404650cbfa0dfe6e2c33 [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 Tanous3ccb3ad2023-01-13 17:40:03 -080023#include "query.hpp"
24#include "registries/privilege_registry.hpp"
Ed Tanousa8e884f2023-01-13 17:40:03 -080025#include "task.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080026#include "utils/dbus_utils.hpp"
27#include "utils/sw_utils.hpp"
28
Ed Tanousd093c992023-01-19 19:01:49 -080029#include <boost/algorithm/string/case_conv.hpp>
George Liue99073f2022-12-09 11:06:16 +080030#include <boost/system/error_code.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070031#include <sdbusplus/asio/property.hpp>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080032#include <sdbusplus/bus/match.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020033#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050034
George Liu2b731192023-01-11 16:27:13 +080035#include <array>
George Liu0ed80c82020-05-12 16:06:27 +080036#include <filesystem>
George Liu2b731192023-01-11 16:27:13 +080037#include <string_view>
38
Ed Tanous1abe55e2018-09-05 08:30:59 -070039namespace redfish
40{
Ed Tanous27826b52018-10-29 11:40:58 -070041
Andrew Geissler0e7de462019-03-04 19:11:54 -060042// Match signals added on software path
Ed Tanouscf9e4172022-12-21 09:30:16 -080043// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Patrick Williams59d494e2022-07-22 19:26:55 -050044static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateMatcher;
Ed Tanouscf9e4172022-12-21 09:30:16 -080045// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Patrick Williams59d494e2022-07-22 19:26:55 -050046static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateErrorMatcher;
Andrew Geissler0e7de462019-03-04 19:11:54 -060047// Only allow one update at a time
Ed Tanouscf9e4172022-12-21 09:30:16 -080048// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Andrew Geissler0e7de462019-03-04 19:11:54 -060049static bool fwUpdateInProgress = false;
Andrew Geissler86adcd62019-04-18 10:58:05 -050050// Timer for software available
Ed Tanouscf9e4172022-12-21 09:30:16 -080051// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Ed Tanous271584a2019-07-09 16:24:22 -070052static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer;
Andrew Geissler86adcd62019-04-18 10:58:05 -050053
John Edward Broadbent7e860f12021-04-08 15:57:16 -070054inline static void cleanUp()
Andrew Geissler86adcd62019-04-18 10:58:05 -050055{
56 fwUpdateInProgress = false;
57 fwUpdateMatcher = nullptr;
James Feist4cde5d92020-06-11 10:39:55 -070058 fwUpdateErrorMatcher = nullptr;
Andrew Geissler86adcd62019-04-18 10:58:05 -050059}
John Edward Broadbent7e860f12021-04-08 15:57:16 -070060inline static void activateImage(const std::string& objPath,
61 const std::string& service)
Andrew Geissler86adcd62019-04-18 10:58:05 -050062{
63 BMCWEB_LOG_DEBUG << "Activate image for " << objPath << " " << service;
64 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080065 [](const boost::system::error_code& errorCode) {
Ed Tanous002d39b2022-05-31 08:59:27 -070066 if (errorCode)
67 {
68 BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
69 BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message();
70 }
Andrew Geissler86adcd62019-04-18 10:58:05 -050071 },
72 service, objPath, "org.freedesktop.DBus.Properties", "Set",
73 "xyz.openbmc_project.Software.Activation", "RequestedActivation",
Ed Tanous168e20c2021-12-13 14:39:53 -080074 dbus::utility::DbusVariantType(
George Liu0fda0f12021-11-16 10:06:17 +080075 "xyz.openbmc_project.Software.Activation.RequestedActivations.Active"));
Andrew Geissler86adcd62019-04-18 10:58:05 -050076}
Andrew Geissler0554c982019-04-23 14:40:12 -050077
78// Note that asyncResp can be either a valid pointer or nullptr. If nullptr
79// then no asyncResp updates will occur
zhanghch058d1b46d2021-04-01 11:18:24 +080080static void
81 softwareInterfaceAdded(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Patrick Williams59d494e2022-07-22 19:26:55 -050082 sdbusplus::message_t& m, task::Payload&& payload)
Andrew Geissler86adcd62019-04-18 10:58:05 -050083{
Ed Tanousb9d36b42022-02-26 21:42:46 -080084 dbus::utility::DBusInteracesMap interfacesProperties;
Andrew Geissler86adcd62019-04-18 10:58:05 -050085
86 sdbusplus::message::object_path objPath;
87
88 m.read(objPath, interfacesProperties);
89
90 BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
Ed Tanouse3eb3d62022-12-21 11:56:07 -080091 for (const auto& interface : interfacesProperties)
Andrew Geissler86adcd62019-04-18 10:58:05 -050092 {
93 BMCWEB_LOG_DEBUG << "interface = " << interface.first;
94
95 if (interface.first == "xyz.openbmc_project.Software.Activation")
96 {
Andrew Geissler86adcd62019-04-18 10:58:05 -050097 // Retrieve service and activate
George Liu2b731192023-01-11 16:27:13 +080098 constexpr std::array<std::string_view, 1> interfaces = {
99 "xyz.openbmc_project.Software.Activation"};
100 dbus::utility::getDbusObject(
101 objPath.str, interfaces,
Ed Tanousa3e65892021-09-16 14:13:20 -0700102 [objPath, asyncResp, payload(std::move(payload))](
George Liu2b731192023-01-11 16:27:13 +0800103 const boost::system::error_code& errorCode,
Ed Tanousa3e65892021-09-16 14:13:20 -0700104 const std::vector<
105 std::pair<std::string, std::vector<std::string>>>&
106 objInfo) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700107 if (errorCode)
108 {
109 BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
110 BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message();
Andrew Geissler0554c982019-04-23 14:40:12 -0500111 if (asyncResp)
112 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700113 messages::internalError(asyncResp->res);
114 }
115 cleanUp();
116 return;
117 }
118 // Ensure we only got one service back
119 if (objInfo.size() != 1)
120 {
121 BMCWEB_LOG_ERROR << "Invalid Object Size "
122 << objInfo.size();
123 if (asyncResp)
124 {
125 messages::internalError(asyncResp->res);
126 }
127 cleanUp();
128 return;
129 }
130 // cancel timer only when
131 // xyz.openbmc_project.Software.Activation interface
132 // is added
133 fwAvailableTimer = nullptr;
134
135 activateImage(objPath.str, objInfo[0].first);
136 if (asyncResp)
137 {
138 std::shared_ptr<task::TaskData> task =
139 task::TaskData::createTask(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800140 [](const boost::system::error_code& ec,
Patrick Williams59d494e2022-07-22 19:26:55 -0500141 sdbusplus::message_t& msg,
Ed Tanous002d39b2022-05-31 08:59:27 -0700142 const std::shared_ptr<task::TaskData>&
143 taskData) {
144 if (ec)
145 {
146 return task::completed;
147 }
148
149 std::string iface;
150 dbus::utility::DBusPropertiesMap values;
151
152 std::string index = std::to_string(taskData->index);
153 msg.read(iface, values);
154
155 if (iface == "xyz.openbmc_project.Software.Activation")
156 {
Gayathri Leburu0fb5b502022-11-17 04:51:55 +0000157 const std::string* state = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -0700158 for (const auto& property : values)
159 {
160 if (property.first == "Activation")
161 {
Gayathri Leburu0fb5b502022-11-17 04:51:55 +0000162 state = std::get_if<std::string>(
163 &property.second);
164 if (state == nullptr)
James Feist32898ce2020-03-10 16:16:52 -0700165 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700166 taskData->messages.emplace_back(
167 messages::internalError());
James Feist32898ce2020-03-10 16:16:52 -0700168 return task::completed;
169 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700170 }
171 }
James Feist32898ce2020-03-10 16:16:52 -0700172
Ed Tanous002d39b2022-05-31 08:59:27 -0700173 if (state == nullptr)
174 {
175 return !task::completed;
176 }
James Feist32898ce2020-03-10 16:16:52 -0700177
Ed Tanous11ba3972022-07-11 09:50:41 -0700178 if (state->ends_with("Invalid") ||
179 state->ends_with("Failed"))
Ed Tanous002d39b2022-05-31 08:59:27 -0700180 {
181 taskData->state = "Exception";
182 taskData->status = "Warning";
183 taskData->messages.emplace_back(
184 messages::taskAborted(index));
185 return task::completed;
186 }
James Feiste5d50062020-05-11 17:29:00 -0700187
Ed Tanous11ba3972022-07-11 09:50:41 -0700188 if (state->ends_with("Staged"))
Ed Tanous002d39b2022-05-31 08:59:27 -0700189 {
190 taskData->state = "Stopping";
191 taskData->messages.emplace_back(
192 messages::taskPaused(index));
193
194 // its staged, set a long timer to
195 // allow them time to complete the
196 // update (probably cycle the
197 // system) if this expires then
198 // task will be cancelled
199 taskData->extendTimer(std::chrono::hours(5));
200 return !task::completed;
201 }
202
Ed Tanous11ba3972022-07-11 09:50:41 -0700203 if (state->ends_with("Active"))
Ed Tanous002d39b2022-05-31 08:59:27 -0700204 {
205 taskData->messages.emplace_back(
206 messages::taskCompletedOK(index));
207 taskData->state = "Completed";
208 return task::completed;
209 }
210 }
211 else if (
212 iface ==
213 "xyz.openbmc_project.Software.ActivationProgress")
214 {
215
216 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;
251 },
252 "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;
George Liu2b731192023-01-11 16:27:13 +0800260 });
Patrick Williams62bafc02022-09-08 17:35:35 -0500261
262 break;
Andrew Geissler86adcd62019-04-18 10:58:05 -0500263 }
264 }
265}
266
Andrew Geissler0554c982019-04-23 14:40:12 -0500267// Note that asyncResp can be either a valid pointer or nullptr. If nullptr
268// then no asyncResp updates will occur
Ed Tanousb5a76932020-09-29 16:16:58 -0700269static void monitorForSoftwareAvailable(
zhanghch058d1b46d2021-04-01 11:18:24 +0800270 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
271 const crow::Request& req, const std::string& url,
Gunnar Mills5d138942022-09-07 10:26:21 -0500272 int timeoutTimeSeconds = 25)
Andrew Geissler86adcd62019-04-18 10:58:05 -0500273{
274 // Only allow one FW update at a time
Ed Tanouse05aec52022-01-25 10:28:56 -0800275 if (fwUpdateInProgress)
Andrew Geissler86adcd62019-04-18 10:58:05 -0500276 {
Andrew Geissler0554c982019-04-23 14:40:12 -0500277 if (asyncResp)
278 {
Andrew Geissler0554c982019-04-23 14:40:12 -0500279 messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
280 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500281 return;
282 }
283
Andrew Geissler0554c982019-04-23 14:40:12 -0500284 fwAvailableTimer =
Ed Tanous271584a2019-07-09 16:24:22 -0700285 std::make_unique<boost::asio::steady_timer>(*req.ioService);
Andrew Geissler86adcd62019-04-18 10:58:05 -0500286
Ed Tanous271584a2019-07-09 16:24:22 -0700287 fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds));
Andrew Geissler86adcd62019-04-18 10:58:05 -0500288
289 fwAvailableTimer->async_wait(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500290 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700291 cleanUp();
292 if (ec == boost::asio::error::operation_aborted)
293 {
294 // expected, we were canceled before the timer completed.
295 return;
296 }
297 BMCWEB_LOG_ERROR
298 << "Timed out waiting for firmware object being created";
299 BMCWEB_LOG_ERROR << "FW image may has already been uploaded to server";
300 if (ec)
301 {
302 BMCWEB_LOG_ERROR << "Async_wait failed" << ec;
303 return;
304 }
305 if (asyncResp)
306 {
307 redfish::messages::internalError(asyncResp->res);
308 }
309 });
Ed Tanousa3e65892021-09-16 14:13:20 -0700310 task::Payload payload(req);
Patrick Williams59d494e2022-07-22 19:26:55 -0500311 auto callback = [asyncResp, payload](sdbusplus::message_t& m) mutable {
Andrew Geissler86adcd62019-04-18 10:58:05 -0500312 BMCWEB_LOG_DEBUG << "Match fired";
Ed Tanousa3e65892021-09-16 14:13:20 -0700313 softwareInterfaceAdded(asyncResp, m, std::move(payload));
Andrew Geissler86adcd62019-04-18 10:58:05 -0500314 };
315
316 fwUpdateInProgress = true;
317
Patrick Williams59d494e2022-07-22 19:26:55 -0500318 fwUpdateMatcher = std::make_unique<sdbusplus::bus::match_t>(
Andrew Geissler86adcd62019-04-18 10:58:05 -0500319 *crow::connections::systemBus,
320 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
321 "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
322 callback);
James Feist4cde5d92020-06-11 10:39:55 -0700323
Patrick Williams59d494e2022-07-22 19:26:55 -0500324 fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match_t>(
James Feist4cde5d92020-06-11 10:39:55 -0700325 *crow::connections::systemBus,
Brian Mae1cc4822021-12-01 17:05:54 +0800326 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
327 "member='InterfacesAdded',"
328 "path='/xyz/openbmc_project/logging'",
Patrick Williams59d494e2022-07-22 19:26:55 -0500329 [asyncResp, url](sdbusplus::message_t& m) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700330 std::vector<std::pair<std::string, dbus::utility::DBusPropertiesMap>>
331 interfacesProperties;
332 sdbusplus::message::object_path objPath;
333 m.read(objPath, interfacesProperties);
334 BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
335 for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>&
336 interface : interfacesProperties)
337 {
338 if (interface.first == "xyz.openbmc_project.Logging.Entry")
James Feist4cde5d92020-06-11 10:39:55 -0700339 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700340 for (const std::pair<std::string,
341 dbus::utility::DbusVariantType>& value :
342 interface.second)
Brian Mae1cc4822021-12-01 17:05:54 +0800343 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700344 if (value.first != "Message")
Brian Mae1cc4822021-12-01 17:05:54 +0800345 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700346 continue;
347 }
348 const std::string* type =
349 std::get_if<std::string>(&value.second);
350 if (type == nullptr)
351 {
352 // if this was our message, timeout will cover it
353 return;
354 }
355 fwAvailableTimer = nullptr;
356 if (*type ==
357 "xyz.openbmc_project.Software.Image.Error.UnTarFailure")
358 {
359 redfish::messages::invalidUpload(asyncResp->res, url,
360 "Invalid archive");
361 }
362 else if (*type ==
363 "xyz.openbmc_project.Software.Image.Error."
364 "ManifestFileFailure")
365 {
366 redfish::messages::invalidUpload(asyncResp->res, url,
367 "Invalid manifest");
368 }
369 else if (
370 *type ==
371 "xyz.openbmc_project.Software.Image.Error.ImageFailure")
372 {
373 redfish::messages::invalidUpload(
374 asyncResp->res, url, "Invalid image format");
375 }
376 else if (
377 *type ==
378 "xyz.openbmc_project.Software.Version.Error.AlreadyExists")
379 {
380 redfish::messages::invalidUpload(
381 asyncResp->res, url,
382 "Image version already exists");
Gunnar Mills88b3dd12020-11-20 14:26:04 -0600383
Ed Tanous002d39b2022-05-31 08:59:27 -0700384 redfish::messages::resourceAlreadyExists(
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800385 asyncResp->res, "UpdateService", "Version",
Ed Tanous002d39b2022-05-31 08:59:27 -0700386 "uploaded version");
387 }
388 else if (
389 *type ==
390 "xyz.openbmc_project.Software.Image.Error.BusyFailure")
391 {
392 redfish::messages::resourceExhaustion(asyncResp->res,
393 url);
394 }
395 else
396 {
397 redfish::messages::internalError(asyncResp->res);
Brian Mae1cc4822021-12-01 17:05:54 +0800398 }
399 }
Gunnar Mills88b3dd12020-11-20 14:26:04 -0600400 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700401 }
James Feist4cde5d92020-06-11 10:39:55 -0700402 });
Andrew Geissler86adcd62019-04-18 10:58:05 -0500403}
Jennifer Lee729dae72018-04-24 15:59:34 -0700404
Andrew Geissler0554c982019-04-23 14:40:12 -0500405/**
406 * UpdateServiceActionsSimpleUpdate class supports handle POST method for
407 * SimpleUpdate action.
408 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700409inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app)
Andrew Geissler0554c982019-04-23 14:40:12 -0500410{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700411 BMCWEB_ROUTE(
412 app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
Ed Tanoused398212021-06-09 17:05:54 -0700413 .privileges(redfish::privileges::postUpdateService)
Ed Tanous002d39b2022-05-31 08:59:27 -0700414 .methods(boost::beast::http::verb::post)(
415 [&app](const crow::Request& req,
416 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000417 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700418 {
419 return;
420 }
Ed Tanous45ca1b82022-03-25 13:07:27 -0700421
Ed Tanous002d39b2022-05-31 08:59:27 -0700422 std::optional<std::string> transferProtocol;
423 std::string imageURI;
Andrew Geissler0554c982019-04-23 14:40:12 -0500424
Ed Tanous002d39b2022-05-31 08:59:27 -0700425 BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost";
Andrew Geissler0554c982019-04-23 14:40:12 -0500426
Ed Tanous002d39b2022-05-31 08:59:27 -0700427 // User can pass in both TransferProtocol and ImageURI parameters or
428 // they can pass in just the ImageURI with the transfer protocol
429 // embedded within it.
430 // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
431 // 2) ImageURI:tftp://1.1.1.1/myfile.bin
Andrew Geissler0554c982019-04-23 14:40:12 -0500432
Ed Tanous002d39b2022-05-31 08:59:27 -0700433 if (!json_util::readJsonAction(req, asyncResp->res, "TransferProtocol",
434 transferProtocol, "ImageURI", imageURI))
435 {
436 BMCWEB_LOG_DEBUG
437 << "Missing TransferProtocol or ImageURI parameter";
438 return;
439 }
440 if (!transferProtocol)
441 {
442 // Must be option 2
443 // Verify ImageURI has transfer protocol in it
444 size_t separator = imageURI.find(':');
Andrew Geissler0554c982019-04-23 14:40:12 -0500445 if ((separator == std::string::npos) ||
446 ((separator + 1) > imageURI.size()))
447 {
448 messages::actionParameterValueTypeError(
449 asyncResp->res, imageURI, "ImageURI",
450 "UpdateService.SimpleUpdate");
Ed Tanous002d39b2022-05-31 08:59:27 -0700451 BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: "
452 << imageURI;
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530453 return;
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530454 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700455 transferProtocol = imageURI.substr(0, separator);
456 // Ensure protocol is upper case for a common comparison path
457 // below
458 boost::to_upper(*transferProtocol);
459 BMCWEB_LOG_DEBUG << "Encoded transfer protocol "
460 << *transferProtocol;
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530461
Ed Tanous002d39b2022-05-31 08:59:27 -0700462 // Adjust imageURI to not have the protocol on it for parsing
463 // below
464 // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin
465 imageURI = imageURI.substr(separator + 3);
466 BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI;
467 }
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530468
Ed Tanous002d39b2022-05-31 08:59:27 -0700469 // OpenBMC currently only supports TFTP
470 if (*transferProtocol != "TFTP")
471 {
472 messages::actionParameterNotSupported(asyncResp->res,
473 "TransferProtocol",
474 "UpdateService.SimpleUpdate");
475 BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: "
476 << *transferProtocol;
477 return;
478 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700479
Ed Tanous002d39b2022-05-31 08:59:27 -0700480 // Format should be <IP or Hostname>/<file> for imageURI
481 size_t separator = imageURI.find('/');
482 if ((separator == std::string::npos) ||
483 ((separator + 1) > imageURI.size()))
484 {
485 messages::actionParameterValueTypeError(
486 asyncResp->res, imageURI, "ImageURI",
487 "UpdateService.SimpleUpdate");
488 BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI;
489 return;
490 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700491
Ed Tanous002d39b2022-05-31 08:59:27 -0700492 std::string tftpServer = imageURI.substr(0, separator);
493 std::string fwFile = imageURI.substr(separator + 1);
494 BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700495
Ed Tanous002d39b2022-05-31 08:59:27 -0700496 // Setup callback for when new software detected
497 // Give TFTP 10 minutes to complete
498 monitorForSoftwareAvailable(
499 asyncResp, req,
500 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate",
501 600);
502
503 // TFTP can take up to 10 minutes depending on image size and
504 // connection speed. Return to caller as soon as the TFTP operation
505 // has been started. The callback above will ensure the activate
506 // is started once the download has completed
507 redfish::messages::success(asyncResp->res);
508
509 // Call TFTP service
510 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800511 [](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700512 if (ec)
513 {
514 // messages::internalError(asyncResp->res);
515 cleanUp();
516 BMCWEB_LOG_DEBUG << "error_code = " << ec;
517 BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
518 }
519 else
520 {
521 BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success";
522 }
523 },
524 "xyz.openbmc_project.Software.Download",
525 "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP",
526 "DownloadViaTFTP", fwFile, tftpServer);
527
528 BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700529 });
530}
531
George Liu0ed80c82020-05-12 16:06:27 +0800532inline void uploadImageFile(crow::Response& res, std::string_view body)
533{
534 std::filesystem::path filepath(
535 "/tmp/images/" +
536 boost::uuids::to_string(boost::uuids::random_generator()()));
537 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
541 std::filesystem::perms permission =
542 std::filesystem::perms::owner_read | std::filesystem::perms::group_read;
543 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
577 crow::connections::systemBus->async_method_call(
578 [asyncResp](const boost::system::error_code ec) {
579 if (ec)
580 {
581 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
582 messages::internalError(asyncResp->res);
583 return;
584 }
585 messages::success(asyncResp->res);
586 },
587 "xyz.openbmc_project.Settings",
588 "/xyz/openbmc_project/software/apply_time",
589 "org.freedesktop.DBus.Properties", "Set",
590 "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime",
591 dbus::utility::DbusVariantType{applyTimeNewVal});
592}
593
594inline void
595 updateMultipartContext(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
596 const MultipartParser& parser)
597{
598 const std::string* uploadData = nullptr;
599 std::optional<std::string> applyTime = "OnReset";
600 bool targetFound = false;
601 for (const FormPart& formpart : parser.mime_fields)
602 {
603 boost::beast::http::fields::const_iterator it =
604 formpart.fields.find("Content-Disposition");
605 if (it == formpart.fields.end())
606 {
607 BMCWEB_LOG_ERROR << "Couldn't find Content-Disposition";
608 return;
609 }
610 BMCWEB_LOG_INFO << "Parsing value " << it->value();
611
612 // The construction parameters of param_list must start with `;`
613 size_t index = it->value().find(';');
614 if (index == std::string::npos)
615 {
616 continue;
617 }
618
619 for (auto const& param :
620 boost::beast::http::param_list{it->value().substr(index)})
621 {
622 if (param.first != "name" || param.second.empty())
623 {
624 continue;
625 }
626
627 if (param.second == "UpdateParameters")
628 {
629 std::vector<std::string> targets;
630 nlohmann::json content =
631 nlohmann::json::parse(formpart.content);
632 if (!json_util::readJson(content, asyncResp->res, "Targets",
633 targets, "@Redfish.OperationApplyTime",
634 applyTime))
635 {
636 return;
637 }
638 if (targets.size() != 1)
639 {
640 messages::propertyValueFormatError(asyncResp->res,
641 "Targets", "");
642 return;
643 }
644 if (targets[0] != "/redfish/v1/Managers/bmc")
645 {
646 messages::propertyValueNotInList(asyncResp->res,
647 "Targets/0", targets[0]);
648 return;
649 }
650 targetFound = true;
651 }
652 else if (param.second == "UpdateFile")
653 {
654 uploadData = &(formpart.content);
655 }
656 }
657 }
658
659 if (uploadData == nullptr)
660 {
661 BMCWEB_LOG_ERROR << "Upload data is NULL";
662 messages::propertyMissing(asyncResp->res, "UpdateFile");
663 return;
664 }
665 if (!targetFound)
666 {
667 messages::propertyMissing(asyncResp->res, "targets");
668 return;
669 }
670
671 setApplyTime(asyncResp, *applyTime);
672
673 uploadImageFile(asyncResp->res, *uploadData);
674}
675
Ed Tanousc2051d12022-05-11 12:21:55 -0700676inline void
677 handleUpdateServicePost(App& app, const crow::Request& req,
678 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
679{
Carson Labrado3ba00072022-06-06 19:40:56 +0000680 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanousc2051d12022-05-11 12:21:55 -0700681 {
682 return;
683 }
684 BMCWEB_LOG_DEBUG << "doPost...";
685
686 // Setup callback for when new software detected
687 monitorForSoftwareAvailable(asyncResp, req, "/redfish/v1/UpdateService");
688
George Liu0ed80c82020-05-12 16:06:27 +0800689 MultipartParser parser;
690 ParserError ec = parser.parse(req);
691 if (ec == ParserError::ERROR_BOUNDARY_FORMAT)
692 {
693 // If the request didnt' contain boundary information, assume it was a
694 // POST binary payload.
695 uploadImageFile(asyncResp->res, req.body());
696 return;
697 }
698 if (ec != ParserError::PARSER_SUCCESS)
699 {
700 // handle error
701 BMCWEB_LOG_ERROR << "MIME parse failed, ec : " << static_cast<int>(ec);
702 messages::internalError(asyncResp->res);
703 return;
704 }
705 updateMultipartContext(asyncResp, parser);
Ed Tanousc2051d12022-05-11 12:21:55 -0700706}
707
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700708inline void requestRoutesUpdateService(App& app)
709{
710 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
Ed Tanoused398212021-06-09 17:05:54 -0700711 .privileges(redfish::privileges::getUpdateService)
Ed Tanous002d39b2022-05-31 08:59:27 -0700712 .methods(boost::beast::http::verb::get)(
713 [&app](const crow::Request& req,
714 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000715 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700716 {
717 return;
718 }
719 asyncResp->res.jsonValue["@odata.type"] =
George Liu0ed80c82020-05-12 16:06:27 +0800720 "#UpdateService.v1_11_1.UpdateService";
Ed Tanous002d39b2022-05-31 08:59:27 -0700721 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
722 asyncResp->res.jsonValue["Id"] = "UpdateService";
723 asyncResp->res.jsonValue["Description"] = "Service for Software Update";
724 asyncResp->res.jsonValue["Name"] = "Update Service";
Ed Tanous4dc23f32022-05-11 11:32:19 -0700725
Ed Tanous32ca38a2022-05-11 12:36:59 -0700726#ifdef BMCWEB_ENABLE_REDFISH_UPDATESERVICE_OLD_POST_URL
Ed Tanous002d39b2022-05-31 08:59:27 -0700727 // See note about later on in this file about why this is neccesary
728 // This is "Wrong" per the standard, but is done temporarily to
729 // avoid noise in failing tests as people transition to having this
730 // option disabled
731 asyncResp->res.addHeader(boost::beast::http::field::allow,
732 "GET, PATCH, HEAD");
Ed Tanous32ca38a2022-05-11 12:36:59 -0700733#endif
734
Ed Tanous002d39b2022-05-31 08:59:27 -0700735 asyncResp->res.jsonValue["HttpPushUri"] =
736 "/redfish/v1/UpdateService/update";
George Liu0ed80c82020-05-12 16:06:27 +0800737 asyncResp->res.jsonValue["MultipartHttpPushUri"] =
738 "/redfish/v1/UpdateService/update";
Ed Tanous4dc23f32022-05-11 11:32:19 -0700739
Ed Tanous002d39b2022-05-31 08:59:27 -0700740 // UpdateService cannot be disabled
741 asyncResp->res.jsonValue["ServiceEnabled"] = true;
742 asyncResp->res.jsonValue["FirmwareInventory"]["@odata.id"] =
743 "/redfish/v1/UpdateService/FirmwareInventory";
744 // Get the MaxImageSizeBytes
745 asyncResp->res.jsonValue["MaxImageSizeBytes"] =
746 bmcwebHttpReqBodyLimitMb * 1024 * 1024;
Tejas Patild61e5192021-06-04 15:49:35 +0530747
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700748#ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
Ed Tanous002d39b2022-05-31 08:59:27 -0700749 // Update Actions object.
750 nlohmann::json& updateSvcSimpleUpdate =
751 asyncResp->res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
752 updateSvcSimpleUpdate["target"] =
753 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
754 updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = {
755 "TFTP"};
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700756#endif
Ed Tanous002d39b2022-05-31 08:59:27 -0700757 // Get the current ApplyTime value
758 sdbusplus::asio::getProperty<std::string>(
759 *crow::connections::systemBus, "xyz.openbmc_project.Settings",
760 "/xyz/openbmc_project/software/apply_time",
761 "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800762 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700763 const std::string& applyTime) {
764 if (ec)
765 {
766 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
767 messages::internalError(asyncResp->res);
768 return;
769 }
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530770
Ed Tanous002d39b2022-05-31 08:59:27 -0700771 // Store the ApplyTime Value
772 if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
773 "RequestedApplyTimes.Immediate")
774 {
775 asyncResp->res.jsonValue["HttpPushUriOptions"]
776 ["HttpPushUriApplyTime"]["ApplyTime"] =
777 "Immediate";
778 }
779 else if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
780 "RequestedApplyTimes.OnReset")
781 {
782 asyncResp->res.jsonValue["HttpPushUriOptions"]
783 ["HttpPushUriApplyTime"]["ApplyTime"] =
784 "OnReset";
785 }
786 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700787 });
788 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
Ed Tanoused398212021-06-09 17:05:54 -0700789 .privileges(redfish::privileges::patchUpdateService)
Ed Tanous002d39b2022-05-31 08:59:27 -0700790 .methods(boost::beast::http::verb::patch)(
791 [&app](const crow::Request& req,
792 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000793 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700794 {
795 return;
796 }
797 BMCWEB_LOG_DEBUG << "doPatch...";
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530798
Ed Tanous002d39b2022-05-31 08:59:27 -0700799 std::optional<nlohmann::json> pushUriOptions;
800 if (!json_util::readJsonPatch(req, asyncResp->res, "HttpPushUriOptions",
801 pushUriOptions))
802 {
803 return;
804 }
805
806 if (pushUriOptions)
807 {
808 std::optional<nlohmann::json> pushUriApplyTime;
809 if (!json_util::readJson(*pushUriOptions, asyncResp->res,
810 "HttpPushUriApplyTime", pushUriApplyTime))
George Liu0fda0f12021-11-16 10:06:17 +0800811 {
812 return;
813 }
814
Ed Tanous002d39b2022-05-31 08:59:27 -0700815 if (pushUriApplyTime)
George Liu0fda0f12021-11-16 10:06:17 +0800816 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700817 std::optional<std::string> applyTime;
818 if (!json_util::readJson(*pushUriApplyTime, asyncResp->res,
819 "ApplyTime", applyTime))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700820 {
Ed Tanousc711bf82018-07-30 16:31:33 -0700821 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700822 }
Jennifer Lee6c4eb9d2018-05-22 10:58:31 -0700823
Ed Tanous002d39b2022-05-31 08:59:27 -0700824 if (applyTime)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700825 {
George Liu0ed80c82020-05-12 16:06:27 +0800826 setApplyTime(asyncResp, *applyTime);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700827 }
George Liu0fda0f12021-11-16 10:06:17 +0800828 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700829 }
George Liu0fda0f12021-11-16 10:06:17 +0800830 });
Ed Tanousc2051d12022-05-11 12:21:55 -0700831
Ed Tanous4dc23f32022-05-11 11:32:19 -0700832// The "old" behavior of the update service URI causes redfish-service validator
833// failures when the Allow header is supported, given that in the spec,
834// UpdateService does not allow POST. in openbmc, we unfortunately reused that
835// resource as our HttpPushUri as well. A number of services, including the
836// openbmc tests, and documentation have hardcoded that erroneous API, instead
837// of relying on HttpPushUri as the spec requires. This option will exist
838// temporarily to allow the old behavior until Q4 2022, at which time it will be
839// removed.
840#ifdef BMCWEB_ENABLE_REDFISH_UPDATESERVICE_OLD_POST_URL
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700841 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
Ed Tanoused398212021-06-09 17:05:54 -0700842 .privileges(redfish::privileges::postUpdateService)
Ed Tanous002d39b2022-05-31 08:59:27 -0700843 .methods(boost::beast::http::verb::post)(
844 [&app](const crow::Request& req,
845 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
846 asyncResp->res.addHeader(
847 boost::beast::http::field::warning,
848 "299 - \"POST to /redfish/v1/UpdateService is deprecated. Use "
849 "the value contained within HttpPushUri.\"");
850 handleUpdateServicePost(app, req, asyncResp);
Ed Tanous4dc23f32022-05-11 11:32:19 -0700851 });
852#endif
853 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/update/")
854 .privileges(redfish::privileges::postUpdateService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700855 .methods(boost::beast::http::verb::post)(
Ed Tanousc2051d12022-05-11 12:21:55 -0700856 std::bind_front(handleUpdateServicePost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700857}
858
859inline void requestRoutesSoftwareInventoryCollection(App& app)
860{
861 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/")
Ed Tanoused398212021-06-09 17:05:54 -0700862 .privileges(redfish::privileges::getSoftwareInventoryCollection)
Ed Tanous14766872022-03-15 10:44:42 -0700863 .methods(boost::beast::http::verb::get)(
864 [&app](const crow::Request& req,
865 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000866 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700867 {
868 return;
869 }
870 asyncResp->res.jsonValue["@odata.type"] =
871 "#SoftwareInventoryCollection.SoftwareInventoryCollection";
872 asyncResp->res.jsonValue["@odata.id"] =
873 "/redfish/v1/UpdateService/FirmwareInventory";
874 asyncResp->res.jsonValue["Name"] = "Software Inventory Collection";
875
George Liue99073f2022-12-09 11:06:16 +0800876 // Note that only firmware levels associated with a device
877 // are stored under /xyz/openbmc_project/software therefore
878 // to ensure only real FirmwareInventory items are returned,
879 // this full object path must be used here as input to
880 // mapper
881 constexpr std::array<std::string_view, 1> interfaces = {
882 "xyz.openbmc_project.Software.Version"};
883 dbus::utility::getSubTree(
884 "/xyz/openbmc_project/software", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -0700885 [asyncResp](
George Liue99073f2022-12-09 11:06:16 +0800886 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700887 const dbus::utility::MapperGetSubTreeResponse& subtree) {
888 if (ec)
889 {
890 messages::internalError(asyncResp->res);
891 return;
892 }
893 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
894 asyncResp->res.jsonValue["Members@odata.count"] = 0;
895
896 for (const auto& obj : subtree)
897 {
898 sdbusplus::message::object_path path(obj.first);
899 std::string swId = path.filename();
900 if (swId.empty())
Ed Tanous14766872022-03-15 10:44:42 -0700901 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700902 messages::internalError(asyncResp->res);
903 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
Ed Tanous14766872022-03-15 10:44:42 -0700904 return;
905 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700906
Ed Tanous002d39b2022-05-31 08:59:27 -0700907 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
908 nlohmann::json::object_t member;
Willy Tueddfc432022-09-26 16:46:38 +0000909 member["@odata.id"] = crow::utility::urlFromPieces(
910 "redfish", "v1", "UpdateService", "FirmwareInventory",
911 swId);
Ed Tanous002d39b2022-05-31 08:59:27 -0700912 members.push_back(std::move(member));
913 asyncResp->res.jsonValue["Members@odata.count"] =
914 members.size();
915 }
George Liue99073f2022-12-09 11:06:16 +0800916 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700917 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700918}
919/* Fill related item links (i.e. bmc, bios) in for inventory */
920inline static void
921 getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
922 const std::string& purpose)
923{
Willy Tueee00132022-06-14 14:53:17 -0700924 if (purpose == sw_util::bmcPurpose)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700925 {
926 nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
Ed Tanous14766872022-03-15 10:44:42 -0700927 nlohmann::json::object_t item;
928 item["@odata.id"] = "/redfish/v1/Managers/bmc";
929 relatedItem.push_back(std::move(item));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700930 aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
931 }
Willy Tueee00132022-06-14 14:53:17 -0700932 else if (purpose == sw_util::biosPurpose)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700933 {
934 nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
Ed Tanous14766872022-03-15 10:44:42 -0700935 nlohmann::json::object_t item;
936 item["@odata.id"] = "/redfish/v1/Systems/system/Bios";
937 relatedItem.push_back(std::move(item));
Jiaqing Zhao1a6e51a2022-01-19 19:20:24 +0800938 aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700939 }
940 else
941 {
942 BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
943 }
944}
945
Willy Tuaf246602022-06-14 15:51:53 -0700946inline void
947 getSoftwareVersion(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
948 const std::string& service, const std::string& path,
949 const std::string& swId)
950{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200951 sdbusplus::asio::getAllProperties(
952 *crow::connections::systemBus, service, path,
953 "xyz.openbmc_project.Software.Version",
Willy Tuaf246602022-06-14 15:51:53 -0700954 [asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800955 swId](const boost::system::error_code& errorCode,
Willy Tuaf246602022-06-14 15:51:53 -0700956 const dbus::utility::DBusPropertiesMap& propertiesList) {
957 if (errorCode)
958 {
959 messages::internalError(asyncResp->res);
960 return;
961 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200962
Willy Tuaf246602022-06-14 15:51:53 -0700963 const std::string* swInvPurpose = nullptr;
964 const std::string* version = nullptr;
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200965
966 const bool success = sdbusplus::unpackPropertiesNoThrow(
967 dbus_utils::UnpackErrorPrinter(), propertiesList, "Purpose",
968 swInvPurpose, "Version", version);
969
970 if (!success)
Willy Tuaf246602022-06-14 15:51:53 -0700971 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200972 messages::internalError(asyncResp->res);
973 return;
Willy Tuaf246602022-06-14 15:51:53 -0700974 }
975
976 if (swInvPurpose == nullptr)
977 {
978 BMCWEB_LOG_DEBUG << "Can't find property \"Purpose\"!";
979 messages::internalError(asyncResp->res);
980 return;
981 }
982
983 BMCWEB_LOG_DEBUG << "swInvPurpose = " << *swInvPurpose;
984
985 if (version == nullptr)
986 {
987 BMCWEB_LOG_DEBUG << "Can't find property \"Version\"!";
988
989 messages::internalError(asyncResp->res);
990
991 return;
992 }
993 asyncResp->res.jsonValue["Version"] = *version;
994 asyncResp->res.jsonValue["Id"] = swId;
995
996 // swInvPurpose is of format:
997 // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
998 // Translate this to "ABC image"
999 size_t endDesc = swInvPurpose->rfind('.');
1000 if (endDesc == std::string::npos)
1001 {
1002 messages::internalError(asyncResp->res);
1003 return;
1004 }
1005 endDesc++;
1006 if (endDesc >= swInvPurpose->size())
1007 {
1008 messages::internalError(asyncResp->res);
1009 return;
1010 }
1011
1012 std::string formatDesc = swInvPurpose->substr(endDesc);
1013 asyncResp->res.jsonValue["Description"] = formatDesc + " image";
1014 getRelatedItems(asyncResp, *swInvPurpose);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001015 });
Willy Tuaf246602022-06-14 15:51:53 -07001016}
1017
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001018inline void requestRoutesSoftwareInventory(App& app)
1019{
1020 BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001021 .privileges(redfish::privileges::getSoftwareInventory)
Ed Tanous002d39b2022-05-31 08:59:27 -07001022 .methods(boost::beast::http::verb::get)(
1023 [&app](const crow::Request& req,
1024 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1025 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001026 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001027 {
1028 return;
1029 }
1030 std::shared_ptr<std::string> swId =
1031 std::make_shared<std::string>(param);
1032
Willy Tueddfc432022-09-26 16:46:38 +00001033 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
1034 "redfish", "v1", "UpdateService", "FirmwareInventory", *swId);
Ed Tanous002d39b2022-05-31 08:59:27 -07001035
George Liue99073f2022-12-09 11:06:16 +08001036 constexpr std::array<std::string_view, 1> interfaces = {
1037 "xyz.openbmc_project.Software.Version"};
1038 dbus::utility::getSubTree(
1039 "/", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001040 [asyncResp,
George Liue99073f2022-12-09 11:06:16 +08001041 swId](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001042 const dbus::utility::MapperGetSubTreeResponse& subtree) {
1043 BMCWEB_LOG_DEBUG << "doGet callback...";
1044 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001045 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001046 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001047 return;
1048 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001049
Ed Tanous002d39b2022-05-31 08:59:27 -07001050 // Ensure we find our input swId, otherwise return an error
1051 bool found = false;
1052 for (const std::pair<std::string,
1053 std::vector<std::pair<
1054 std::string, std::vector<std::string>>>>&
1055 obj : subtree)
1056 {
Ed Tanous11ba3972022-07-11 09:50:41 -07001057 if (!obj.first.ends_with(*swId))
Ed Tanous002d39b2022-05-31 08:59:27 -07001058 {
1059 continue;
1060 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001061
Ed Tanous002d39b2022-05-31 08:59:27 -07001062 if (obj.second.empty())
1063 {
1064 continue;
1065 }
1066
1067 found = true;
Willy Tueee00132022-06-14 14:53:17 -07001068 sw_util::getSwStatus(asyncResp, swId, obj.second[0].first);
Willy Tuaf246602022-06-14 15:51:53 -07001069 getSoftwareVersion(asyncResp, obj.second[0].first, obj.first,
1070 *swId);
Ed Tanous002d39b2022-05-31 08:59:27 -07001071 }
1072 if (!found)
1073 {
1074 BMCWEB_LOG_ERROR << "Input swID " << *swId << " not found!";
1075 messages::resourceMissingAtURI(
1076 asyncResp->res, crow::utility::urlFromPieces(
1077 "redfish", "v1", "UpdateService",
1078 "FirmwareInventory", *swId));
1079 return;
1080 }
1081 asyncResp->res.jsonValue["@odata.type"] =
1082 "#SoftwareInventory.v1_1_0.SoftwareInventory";
1083 asyncResp->res.jsonValue["Name"] = "Software Inventory";
1084 asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001085
Ed Tanous002d39b2022-05-31 08:59:27 -07001086 asyncResp->res.jsonValue["Updateable"] = false;
Willy Tueee00132022-06-14 14:53:17 -07001087 sw_util::getSwUpdatableStatus(asyncResp, swId);
George Liue99073f2022-12-09 11:06:16 +08001088 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001089 });
1090}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001091
1092} // namespace redfish