xref: /openbmc/bmcweb/features/redfish/lib/update_service.hpp (revision e1cc482880d7b47dcf19609cedba7df80368ae3c)
1729dae72SJennifer Lee /*
2729dae72SJennifer Lee // Copyright (c) 2018 Intel Corporation
3729dae72SJennifer Lee //
4729dae72SJennifer Lee // Licensed under the Apache License, Version 2.0 (the "License");
5729dae72SJennifer Lee // you may not use this file except in compliance with the License.
6729dae72SJennifer Lee // You may obtain a copy of the License at
7729dae72SJennifer Lee //
8729dae72SJennifer Lee //      http://www.apache.org/licenses/LICENSE-2.0
9729dae72SJennifer Lee //
10729dae72SJennifer Lee // Unless required by applicable law or agreed to in writing, software
11729dae72SJennifer Lee // distributed under the License is distributed on an "AS IS" BASIS,
12729dae72SJennifer Lee // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13729dae72SJennifer Lee // See the License for the specific language governing permissions and
14729dae72SJennifer Lee // limitations under the License.
15729dae72SJennifer Lee */
16729dae72SJennifer Lee #pragma once
17729dae72SJennifer Lee 
18d61e5194STejas Patil #include "bmcweb_config.h"
19d61e5194STejas Patil 
207e860f15SJohn Edward Broadbent #include <app.hpp>
21729dae72SJennifer Lee #include <boost/container/flat_map.hpp>
22168e20c1SEd Tanous #include <dbus_utility.hpp>
23ed398213SEd Tanous #include <registries/privilege_registry.hpp>
241e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
2587d84729SAndrew Geissler #include <utils/fw_utils.hpp>
261214b7e7SGunnar Mills 
271abe55efSEd Tanous namespace redfish
281abe55efSEd Tanous {
2927826b5fSEd Tanous 
300e7de46fSAndrew Geissler // Match signals added on software path
31acb7cfb4SJennifer Lee static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher;
324cde5d90SJames Feist static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateErrorMatcher;
330e7de46fSAndrew Geissler // Only allow one update at a time
340e7de46fSAndrew Geissler static bool fwUpdateInProgress = false;
3586adcd6dSAndrew Geissler // Timer for software available
36271584abSEd Tanous static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer;
3786adcd6dSAndrew Geissler 
387e860f15SJohn Edward Broadbent inline static void cleanUp()
3986adcd6dSAndrew Geissler {
4086adcd6dSAndrew Geissler     fwUpdateInProgress = false;
4186adcd6dSAndrew Geissler     fwUpdateMatcher = nullptr;
424cde5d90SJames Feist     fwUpdateErrorMatcher = nullptr;
4386adcd6dSAndrew Geissler }
447e860f15SJohn Edward Broadbent inline static void activateImage(const std::string& objPath,
4586adcd6dSAndrew Geissler                                  const std::string& service)
4686adcd6dSAndrew Geissler {
4786adcd6dSAndrew Geissler     BMCWEB_LOG_DEBUG << "Activate image for " << objPath << " " << service;
4886adcd6dSAndrew Geissler     crow::connections::systemBus->async_method_call(
4981ce609eSEd Tanous         [](const boost::system::error_code errorCode) {
5081ce609eSEd Tanous             if (errorCode)
5186adcd6dSAndrew Geissler             {
5281ce609eSEd Tanous                 BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
5381ce609eSEd Tanous                 BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message();
5486adcd6dSAndrew Geissler             }
5586adcd6dSAndrew Geissler         },
5686adcd6dSAndrew Geissler         service, objPath, "org.freedesktop.DBus.Properties", "Set",
5786adcd6dSAndrew Geissler         "xyz.openbmc_project.Software.Activation", "RequestedActivation",
58168e20c1SEd Tanous         dbus::utility::DbusVariantType(
590fda0f12SGeorge Liu             "xyz.openbmc_project.Software.Activation.RequestedActivations.Active"));
6086adcd6dSAndrew Geissler }
610554c984SAndrew Geissler 
620554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr
630554c984SAndrew Geissler // then no asyncResp updates will occur
648d1b46d7Szhanghch05 static void
658d1b46d7Szhanghch05     softwareInterfaceAdded(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
66fe306728SJames Feist                            sdbusplus::message::message& m,
67a3e65892SEd Tanous                            task::Payload&& payload)
6886adcd6dSAndrew Geissler {
6986adcd6dSAndrew Geissler     std::vector<std::pair<
7086adcd6dSAndrew Geissler         std::string,
71168e20c1SEd Tanous         std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>>>
7286adcd6dSAndrew Geissler         interfacesProperties;
7386adcd6dSAndrew Geissler 
7486adcd6dSAndrew Geissler     sdbusplus::message::object_path objPath;
7586adcd6dSAndrew Geissler 
7686adcd6dSAndrew Geissler     m.read(objPath, interfacesProperties);
7786adcd6dSAndrew Geissler 
7886adcd6dSAndrew Geissler     BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
7986adcd6dSAndrew Geissler     for (auto& interface : interfacesProperties)
8086adcd6dSAndrew Geissler     {
8186adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "interface = " << interface.first;
8286adcd6dSAndrew Geissler 
8386adcd6dSAndrew Geissler         if (interface.first == "xyz.openbmc_project.Software.Activation")
8486adcd6dSAndrew Geissler         {
8586adcd6dSAndrew Geissler             // Retrieve service and activate
8686adcd6dSAndrew Geissler             crow::connections::systemBus->async_method_call(
87a3e65892SEd Tanous                 [objPath, asyncResp, payload(std::move(payload))](
88a3e65892SEd Tanous                     const boost::system::error_code errorCode,
89a3e65892SEd Tanous                     const std::vector<
90a3e65892SEd Tanous                         std::pair<std::string, std::vector<std::string>>>&
91a3e65892SEd Tanous                         objInfo) mutable {
9281ce609eSEd Tanous                     if (errorCode)
9386adcd6dSAndrew Geissler                     {
9481ce609eSEd Tanous                         BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
9586adcd6dSAndrew Geissler                         BMCWEB_LOG_DEBUG << "error msg = "
9681ce609eSEd Tanous                                          << errorCode.message();
970554c984SAndrew Geissler                         if (asyncResp)
980554c984SAndrew Geissler                         {
9986adcd6dSAndrew Geissler                             messages::internalError(asyncResp->res);
1000554c984SAndrew Geissler                         }
10186adcd6dSAndrew Geissler                         cleanUp();
10286adcd6dSAndrew Geissler                         return;
10386adcd6dSAndrew Geissler                     }
10486adcd6dSAndrew Geissler                     // Ensure we only got one service back
10586adcd6dSAndrew Geissler                     if (objInfo.size() != 1)
10686adcd6dSAndrew Geissler                     {
10786adcd6dSAndrew Geissler                         BMCWEB_LOG_ERROR << "Invalid Object Size "
10886adcd6dSAndrew Geissler                                          << objInfo.size();
1090554c984SAndrew Geissler                         if (asyncResp)
1100554c984SAndrew Geissler                         {
11186adcd6dSAndrew Geissler                             messages::internalError(asyncResp->res);
1120554c984SAndrew Geissler                         }
11386adcd6dSAndrew Geissler                         cleanUp();
11486adcd6dSAndrew Geissler                         return;
11586adcd6dSAndrew Geissler                     }
11686adcd6dSAndrew Geissler                     // cancel timer only when
11786adcd6dSAndrew Geissler                     // xyz.openbmc_project.Software.Activation interface
11886adcd6dSAndrew Geissler                     // is added
11986adcd6dSAndrew Geissler                     fwAvailableTimer = nullptr;
12086adcd6dSAndrew Geissler 
12186adcd6dSAndrew Geissler                     activateImage(objPath.str, objInfo[0].first);
1220554c984SAndrew Geissler                     if (asyncResp)
1230554c984SAndrew Geissler                     {
12432898ceaSJames Feist                         std::shared_ptr<task::TaskData> task =
12532898ceaSJames Feist                             task::TaskData::createTask(
12632898ceaSJames Feist                                 [](boost::system::error_code ec,
12732898ceaSJames Feist                                    sdbusplus::message::message& msg,
1281214b7e7SGunnar Mills                                    const std::shared_ptr<task::TaskData>&
1291214b7e7SGunnar Mills                                        taskData) {
13032898ceaSJames Feist                                     if (ec)
13132898ceaSJames Feist                                     {
13232898ceaSJames Feist                                         return task::completed;
13332898ceaSJames Feist                                     }
13432898ceaSJames Feist 
13532898ceaSJames Feist                                     std::string iface;
13632898ceaSJames Feist                                     boost::container::flat_map<
137fd9ab9e1SJames Feist                                         std::string,
138168e20c1SEd Tanous                                         dbus::utility::DbusVariantType>
13932898ceaSJames Feist                                         values;
140fd9ab9e1SJames Feist 
141fd9ab9e1SJames Feist                                     std::string index =
142fd9ab9e1SJames Feist                                         std::to_string(taskData->index);
14332898ceaSJames Feist                                     msg.read(iface, values);
144fd9ab9e1SJames Feist 
1450fda0f12SGeorge Liu                                     if (iface ==
1460fda0f12SGeorge Liu                                         "xyz.openbmc_project.Software.Activation")
147fd9ab9e1SJames Feist                                     {
14832898ceaSJames Feist                                         auto findActivation =
14932898ceaSJames Feist                                             values.find("Activation");
15032898ceaSJames Feist                                         if (findActivation == values.end())
15132898ceaSJames Feist                                         {
15232898ceaSJames Feist                                             return !task::completed;
15332898ceaSJames Feist                                         }
15432898ceaSJames Feist                                         std::string* state =
15532898ceaSJames Feist                                             std::get_if<std::string>(
15632898ceaSJames Feist                                                 &(findActivation->second));
15732898ceaSJames Feist 
15832898ceaSJames Feist                                         if (state == nullptr)
15932898ceaSJames Feist                                         {
16032898ceaSJames Feist                                             taskData->messages.emplace_back(
16132898ceaSJames Feist                                                 messages::internalError());
16232898ceaSJames Feist                                             return task::completed;
16332898ceaSJames Feist                                         }
16432898ceaSJames Feist 
165fd9ab9e1SJames Feist                                         if (boost::ends_with(*state,
166fd9ab9e1SJames Feist                                                              "Invalid") ||
16732898ceaSJames Feist                                             boost::ends_with(*state, "Failed"))
16832898ceaSJames Feist                                         {
16932898ceaSJames Feist                                             taskData->state = "Exception";
17032898ceaSJames Feist                                             taskData->status = "Warning";
17132898ceaSJames Feist                                             taskData->messages.emplace_back(
172e5d5006bSJames Feist                                                 messages::taskAborted(index));
17332898ceaSJames Feist                                             return task::completed;
17432898ceaSJames Feist                                         }
17532898ceaSJames Feist 
17632898ceaSJames Feist                                         if (boost::ends_with(*state, "Staged"))
17732898ceaSJames Feist                                         {
178fd9ab9e1SJames Feist                                             taskData->state = "Stopping";
179fd9ab9e1SJames Feist                                             taskData->messages.emplace_back(
180fd9ab9e1SJames Feist                                                 messages::taskPaused(index));
181fd9ab9e1SJames Feist 
182fd9ab9e1SJames Feist                                             // its staged, set a long timer to
183fd9ab9e1SJames Feist                                             // allow them time to complete the
184fd9ab9e1SJames Feist                                             // update (probably cycle the
185fd9ab9e1SJames Feist                                             // system) if this expires then
186fd9ab9e1SJames Feist                                             // task will be cancelled
187fd9ab9e1SJames Feist                                             taskData->extendTimer(
188fd9ab9e1SJames Feist                                                 std::chrono::hours(5));
18932898ceaSJames Feist                                             return !task::completed;
19032898ceaSJames Feist                                         }
19132898ceaSJames Feist 
19232898ceaSJames Feist                                         if (boost::ends_with(*state, "Active"))
19332898ceaSJames Feist                                         {
19432898ceaSJames Feist                                             taskData->messages.emplace_back(
195fd9ab9e1SJames Feist                                                 messages::taskCompletedOK(
196fd9ab9e1SJames Feist                                                     index));
19732898ceaSJames Feist                                             taskData->state = "Completed";
19832898ceaSJames Feist                                             return task::completed;
19932898ceaSJames Feist                                         }
200fd9ab9e1SJames Feist                                     }
2010fda0f12SGeorge Liu                                     else if (
2020fda0f12SGeorge Liu                                         iface ==
2030fda0f12SGeorge Liu                                         "xyz.openbmc_project.Software.ActivationProgress")
204fd9ab9e1SJames Feist                                     {
205fd9ab9e1SJames Feist                                         auto findProgress =
206fd9ab9e1SJames Feist                                             values.find("Progress");
207fd9ab9e1SJames Feist                                         if (findProgress == values.end())
208fd9ab9e1SJames Feist                                         {
209fd9ab9e1SJames Feist                                             return !task::completed;
210fd9ab9e1SJames Feist                                         }
211fd9ab9e1SJames Feist                                         uint8_t* progress =
212fd9ab9e1SJames Feist                                             std::get_if<uint8_t>(
213fd9ab9e1SJames Feist                                                 &(findProgress->second));
214fd9ab9e1SJames Feist 
215fd9ab9e1SJames Feist                                         if (progress == nullptr)
216fd9ab9e1SJames Feist                                         {
217fd9ab9e1SJames Feist                                             taskData->messages.emplace_back(
218fd9ab9e1SJames Feist                                                 messages::internalError());
219fd9ab9e1SJames Feist                                             return task::completed;
220fd9ab9e1SJames Feist                                         }
2216868ff50SGeorge Liu                                         taskData->percentComplete =
2226868ff50SGeorge Liu                                             static_cast<int>(*progress);
223fd9ab9e1SJames Feist                                         taskData->messages.emplace_back(
224fd9ab9e1SJames Feist                                             messages::taskProgressChanged(
225fd9ab9e1SJames Feist                                                 index, static_cast<size_t>(
226fd9ab9e1SJames Feist                                                            *progress)));
227fd9ab9e1SJames Feist 
228fd9ab9e1SJames Feist                                         // if we're getting status updates it's
229fd9ab9e1SJames Feist                                         // still alive, update timer
230fd9ab9e1SJames Feist                                         taskData->extendTimer(
231fd9ab9e1SJames Feist                                             std::chrono::minutes(5));
232fd9ab9e1SJames Feist                                     }
23332898ceaSJames Feist 
23432898ceaSJames Feist                                     // as firmware update often results in a
23532898ceaSJames Feist                                     // reboot, the task  may never "complete"
23632898ceaSJames Feist                                     // unless it is an error
23732898ceaSJames Feist 
23832898ceaSJames Feist                                     return !task::completed;
23932898ceaSJames Feist                                 },
2400fda0f12SGeorge Liu                                 "type='signal',interface='org.freedesktop.DBus.Properties',"
241fd9ab9e1SJames Feist                                 "member='PropertiesChanged',path='" +
24232898ceaSJames Feist                                     objPath.str + "'");
24332898ceaSJames Feist                         task->startTimer(std::chrono::minutes(5));
24432898ceaSJames Feist                         task->populateResp(asyncResp->res);
245a3e65892SEd Tanous                         task->payload.emplace(std::move(payload));
2460554c984SAndrew Geissler                     }
24786adcd6dSAndrew Geissler                     fwUpdateInProgress = false;
24886adcd6dSAndrew Geissler                 },
24986adcd6dSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper",
25086adcd6dSAndrew Geissler                 "/xyz/openbmc_project/object_mapper",
25186adcd6dSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str,
25286adcd6dSAndrew Geissler                 std::array<const char*, 1>{
25386adcd6dSAndrew Geissler                     "xyz.openbmc_project.Software.Activation"});
25486adcd6dSAndrew Geissler         }
25586adcd6dSAndrew Geissler     }
25686adcd6dSAndrew Geissler }
25786adcd6dSAndrew Geissler 
2580554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr
2590554c984SAndrew Geissler // then no asyncResp updates will occur
260b5a76932SEd Tanous static void monitorForSoftwareAvailable(
2618d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2628d1b46d7Szhanghch05     const crow::Request& req, const std::string& url,
2638d1b46d7Szhanghch05     int timeoutTimeSeconds = 10)
26486adcd6dSAndrew Geissler {
26586adcd6dSAndrew Geissler     // Only allow one FW update at a time
26686adcd6dSAndrew Geissler     if (fwUpdateInProgress != false)
26786adcd6dSAndrew Geissler     {
2680554c984SAndrew Geissler         if (asyncResp)
2690554c984SAndrew Geissler         {
27086adcd6dSAndrew Geissler             messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
2710554c984SAndrew Geissler         }
27286adcd6dSAndrew Geissler         return;
27386adcd6dSAndrew Geissler     }
27486adcd6dSAndrew Geissler 
2750554c984SAndrew Geissler     fwAvailableTimer =
276271584abSEd Tanous         std::make_unique<boost::asio::steady_timer>(*req.ioService);
27786adcd6dSAndrew Geissler 
278271584abSEd Tanous     fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds));
27986adcd6dSAndrew Geissler 
28086adcd6dSAndrew Geissler     fwAvailableTimer->async_wait(
28186adcd6dSAndrew Geissler         [asyncResp](const boost::system::error_code& ec) {
28286adcd6dSAndrew Geissler             cleanUp();
28386adcd6dSAndrew Geissler             if (ec == boost::asio::error::operation_aborted)
28486adcd6dSAndrew Geissler             {
28586adcd6dSAndrew Geissler                 // expected, we were canceled before the timer completed.
28686adcd6dSAndrew Geissler                 return;
28786adcd6dSAndrew Geissler             }
28886adcd6dSAndrew Geissler             BMCWEB_LOG_ERROR
28986adcd6dSAndrew Geissler                 << "Timed out waiting for firmware object being created";
29086adcd6dSAndrew Geissler             BMCWEB_LOG_ERROR
29186adcd6dSAndrew Geissler                 << "FW image may has already been uploaded to server";
29286adcd6dSAndrew Geissler             if (ec)
29386adcd6dSAndrew Geissler             {
29486adcd6dSAndrew Geissler                 BMCWEB_LOG_ERROR << "Async_wait failed" << ec;
29586adcd6dSAndrew Geissler                 return;
29686adcd6dSAndrew Geissler             }
2970554c984SAndrew Geissler             if (asyncResp)
2980554c984SAndrew Geissler             {
29986adcd6dSAndrew Geissler                 redfish::messages::internalError(asyncResp->res);
3000554c984SAndrew Geissler             }
30186adcd6dSAndrew Geissler         });
302a3e65892SEd Tanous     task::Payload payload(req);
303a3e65892SEd Tanous     auto callback = [asyncResp,
304a3e65892SEd Tanous                      payload](sdbusplus::message::message& m) mutable {
30586adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "Match fired";
306a3e65892SEd Tanous         softwareInterfaceAdded(asyncResp, m, std::move(payload));
30786adcd6dSAndrew Geissler     };
30886adcd6dSAndrew Geissler 
30986adcd6dSAndrew Geissler     fwUpdateInProgress = true;
31086adcd6dSAndrew Geissler 
31186adcd6dSAndrew Geissler     fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
31286adcd6dSAndrew Geissler         *crow::connections::systemBus,
31386adcd6dSAndrew Geissler         "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
31486adcd6dSAndrew Geissler         "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
31586adcd6dSAndrew Geissler         callback);
3164cde5d90SJames Feist 
3174cde5d90SJames Feist     fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match::match>(
3184cde5d90SJames Feist         *crow::connections::systemBus,
319*e1cc4828SBrian Ma         "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
320*e1cc4828SBrian Ma         "member='InterfacesAdded',"
321*e1cc4828SBrian Ma         "path='/xyz/openbmc_project/logging'",
3224cde5d90SJames Feist         [asyncResp, url](sdbusplus::message::message& m) {
323*e1cc4828SBrian Ma             std::vector<
324*e1cc4828SBrian Ma                 std::pair<std::string, dbus::utility::DBusPropertiesMap>>
325*e1cc4828SBrian Ma                 interfacesProperties;
326*e1cc4828SBrian Ma             sdbusplus::message::object_path objPath;
327*e1cc4828SBrian Ma             m.read(objPath, interfacesProperties);
328*e1cc4828SBrian Ma             BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
329*e1cc4828SBrian Ma             for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>&
330*e1cc4828SBrian Ma                      interface : interfacesProperties)
331*e1cc4828SBrian Ma             {
332*e1cc4828SBrian Ma                 if (interface.first == "xyz.openbmc_project.Logging.Entry")
333*e1cc4828SBrian Ma                 {
3344cde5d90SJames Feist                     BMCWEB_LOG_DEBUG << "Error Match fired";
335*e1cc4828SBrian Ma                     const dbus::utility::DBusPropertiesMap& values =
336*e1cc4828SBrian Ma                         interface.second;
3374cde5d90SJames Feist                     auto find = values.find("Message");
3384cde5d90SJames Feist                     if (find == values.end())
3394cde5d90SJames Feist                     {
3404cde5d90SJames Feist                         return;
3414cde5d90SJames Feist                     }
342*e1cc4828SBrian Ma                     const std::string* type =
343*e1cc4828SBrian Ma                         std::get_if<std::string>(&(find->second));
3444cde5d90SJames Feist                     if (type == nullptr)
3454cde5d90SJames Feist                     {
346*e1cc4828SBrian Ma                         // if this was our message, timeout will cover it
3474cde5d90SJames Feist                         return;
3484cde5d90SJames Feist                     }
349*e1cc4828SBrian Ma                     fwAvailableTimer = nullptr;
3504cde5d90SJames Feist                     if (*type ==
3514cde5d90SJames Feist                         "xyz.openbmc_project.Software.Image.Error.UnTarFailure")
3524cde5d90SJames Feist                     {
3534cde5d90SJames Feist                         redfish::messages::invalidUpload(asyncResp->res, url,
3544cde5d90SJames Feist                                                          "Invalid archive");
3554cde5d90SJames Feist                     }
356*e1cc4828SBrian Ma                     else if (*type ==
357*e1cc4828SBrian Ma                              "xyz.openbmc_project.Software.Image.Error."
358*e1cc4828SBrian Ma                              "ManifestFileFailure")
3594cde5d90SJames Feist                     {
3604cde5d90SJames Feist                         redfish::messages::invalidUpload(asyncResp->res, url,
3614cde5d90SJames Feist                                                          "Invalid manifest");
3624cde5d90SJames Feist                     }
363*e1cc4828SBrian Ma                     else if (
364*e1cc4828SBrian Ma                         *type ==
3654cde5d90SJames Feist                         "xyz.openbmc_project.Software.Image.Error.ImageFailure")
3664cde5d90SJames Feist                     {
367*e1cc4828SBrian Ma                         redfish::messages::invalidUpload(
368*e1cc4828SBrian Ma                             asyncResp->res, url, "Invalid image format");
3694cde5d90SJames Feist                     }
370*e1cc4828SBrian Ma                     else if (
371*e1cc4828SBrian Ma                         *type ==
3720fda0f12SGeorge Liu                         "xyz.openbmc_project.Software.Version.Error.AlreadyExists")
37388b3dd12SGunnar Mills                     {
37488b3dd12SGunnar Mills                         redfish::messages::invalidUpload(
375*e1cc4828SBrian Ma                             asyncResp->res, url,
376*e1cc4828SBrian Ma                             "Image version already exists");
37788b3dd12SGunnar Mills 
37888b3dd12SGunnar Mills                         redfish::messages::resourceAlreadyExists(
379*e1cc4828SBrian Ma                             asyncResp->res,
380*e1cc4828SBrian Ma                             "UpdateService.v1_5_0.UpdateService", "Version",
381*e1cc4828SBrian Ma                             "uploaded version");
38288b3dd12SGunnar Mills                     }
383*e1cc4828SBrian Ma                     else if (
384*e1cc4828SBrian Ma                         *type ==
3854cde5d90SJames Feist                         "xyz.openbmc_project.Software.Image.Error.BusyFailure")
3864cde5d90SJames Feist                     {
387*e1cc4828SBrian Ma                         redfish::messages::resourceExhaustion(asyncResp->res,
388*e1cc4828SBrian Ma                                                               url);
3894cde5d90SJames Feist                     }
3904cde5d90SJames Feist                     else
3914cde5d90SJames Feist                     {
3924cde5d90SJames Feist                         redfish::messages::internalError(asyncResp->res);
3934cde5d90SJames Feist                     }
394*e1cc4828SBrian Ma                 }
395*e1cc4828SBrian Ma             }
3964cde5d90SJames Feist         });
39786adcd6dSAndrew Geissler }
398729dae72SJennifer Lee 
3990554c984SAndrew Geissler /**
4000554c984SAndrew Geissler  * UpdateServiceActionsSimpleUpdate class supports handle POST method for
4010554c984SAndrew Geissler  * SimpleUpdate action.
4020554c984SAndrew Geissler  */
4037e860f15SJohn Edward Broadbent inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app)
4040554c984SAndrew Geissler {
4057e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
4067e860f15SJohn Edward Broadbent         app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
407ed398213SEd Tanous         .privileges(redfish::privileges::postUpdateService)
4087e860f15SJohn Edward Broadbent         .methods(
4097e860f15SJohn Edward Broadbent             boost::beast::http::verb::
4107e860f15SJohn Edward Broadbent                 post)([](const crow::Request& req,
4117e860f15SJohn Edward Broadbent                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
4120554c984SAndrew Geissler             std::optional<std::string> transferProtocol;
4130554c984SAndrew Geissler             std::string imageURI;
4140554c984SAndrew Geissler 
4150554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost";
4160554c984SAndrew Geissler 
4170554c984SAndrew Geissler             // User can pass in both TransferProtocol and ImageURI parameters or
4184e0453b1SGunnar Mills             // they can pass in just the ImageURI with the transfer protocol
4194e0453b1SGunnar Mills             // embedded within it.
4200554c984SAndrew Geissler             // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
4210554c984SAndrew Geissler             // 2) ImageURI:tftp://1.1.1.1/myfile.bin
4220554c984SAndrew Geissler 
4230554c984SAndrew Geissler             if (!json_util::readJson(req, asyncResp->res, "TransferProtocol",
4240554c984SAndrew Geissler                                      transferProtocol, "ImageURI", imageURI))
4250554c984SAndrew Geissler             {
4260554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG
4270554c984SAndrew Geissler                     << "Missing TransferProtocol or ImageURI parameter";
4280554c984SAndrew Geissler                 return;
4290554c984SAndrew Geissler             }
4300554c984SAndrew Geissler             if (!transferProtocol)
4310554c984SAndrew Geissler             {
4320554c984SAndrew Geissler                 // Must be option 2
4330554c984SAndrew Geissler                 // Verify ImageURI has transfer protocol in it
434f23b7296SEd Tanous                 size_t separator = imageURI.find(':');
4350554c984SAndrew Geissler                 if ((separator == std::string::npos) ||
4360554c984SAndrew Geissler                     ((separator + 1) > imageURI.size()))
4370554c984SAndrew Geissler                 {
4380554c984SAndrew Geissler                     messages::actionParameterValueTypeError(
4390554c984SAndrew Geissler                         asyncResp->res, imageURI, "ImageURI",
4400554c984SAndrew Geissler                         "UpdateService.SimpleUpdate");
4410554c984SAndrew Geissler                     BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: "
4420554c984SAndrew Geissler                                      << imageURI;
4430554c984SAndrew Geissler                     return;
4440554c984SAndrew Geissler                 }
4450554c984SAndrew Geissler                 transferProtocol = imageURI.substr(0, separator);
4467e860f15SJohn Edward Broadbent                 // Ensure protocol is upper case for a common comparison path
4477e860f15SJohn Edward Broadbent                 // below
4480554c984SAndrew Geissler                 boost::to_upper(*transferProtocol);
4490554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG << "Encoded transfer protocol "
4500554c984SAndrew Geissler                                  << *transferProtocol;
4510554c984SAndrew Geissler 
4520554c984SAndrew Geissler                 // Adjust imageURI to not have the protocol on it for parsing
4530554c984SAndrew Geissler                 // below
4540554c984SAndrew Geissler                 // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin
4550554c984SAndrew Geissler                 imageURI = imageURI.substr(separator + 3);
4560554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI;
4570554c984SAndrew Geissler             }
4580554c984SAndrew Geissler 
4590554c984SAndrew Geissler             // OpenBMC currently only supports TFTP
4600554c984SAndrew Geissler             if (*transferProtocol != "TFTP")
4610554c984SAndrew Geissler             {
4627e860f15SJohn Edward Broadbent                 messages::actionParameterNotSupported(
4637e860f15SJohn Edward Broadbent                     asyncResp->res, "TransferProtocol",
4640554c984SAndrew Geissler                     "UpdateService.SimpleUpdate");
4650554c984SAndrew Geissler                 BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: "
4660554c984SAndrew Geissler                                  << *transferProtocol;
4670554c984SAndrew Geissler                 return;
4680554c984SAndrew Geissler             }
4690554c984SAndrew Geissler 
4700554c984SAndrew Geissler             // Format should be <IP or Hostname>/<file> for imageURI
471f23b7296SEd Tanous             size_t separator = imageURI.find('/');
4720554c984SAndrew Geissler             if ((separator == std::string::npos) ||
4730554c984SAndrew Geissler                 ((separator + 1) > imageURI.size()))
4740554c984SAndrew Geissler             {
4750554c984SAndrew Geissler                 messages::actionParameterValueTypeError(
4760554c984SAndrew Geissler                     asyncResp->res, imageURI, "ImageURI",
4770554c984SAndrew Geissler                     "UpdateService.SimpleUpdate");
4780554c984SAndrew Geissler                 BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI;
4790554c984SAndrew Geissler                 return;
4800554c984SAndrew Geissler             }
4810554c984SAndrew Geissler 
4820554c984SAndrew Geissler             std::string tftpServer = imageURI.substr(0, separator);
4830554c984SAndrew Geissler             std::string fwFile = imageURI.substr(separator + 1);
4840554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile;
4850554c984SAndrew Geissler 
4860554c984SAndrew Geissler             // Setup callback for when new software detected
4872618d5e3SGunnar Mills             // Give TFTP 10 minutes to complete
4884cde5d90SJames Feist             monitorForSoftwareAvailable(
489d7a596bdSAlbert Zhang                 asyncResp, req,
4904cde5d90SJames Feist                 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate",
4912618d5e3SGunnar Mills                 600);
4920554c984SAndrew Geissler 
4932618d5e3SGunnar Mills             // TFTP can take up to 10 minutes depending on image size and
4940554c984SAndrew Geissler             // connection speed. Return to caller as soon as the TFTP operation
4950554c984SAndrew Geissler             // has been started. The callback above will ensure the activate
4960554c984SAndrew Geissler             // is started once the download has completed
4970554c984SAndrew Geissler             redfish::messages::success(asyncResp->res);
4980554c984SAndrew Geissler 
4990554c984SAndrew Geissler             // Call TFTP service
5000554c984SAndrew Geissler             crow::connections::systemBus->async_method_call(
5010554c984SAndrew Geissler                 [](const boost::system::error_code ec) {
5020554c984SAndrew Geissler                     if (ec)
5030554c984SAndrew Geissler                     {
5040554c984SAndrew Geissler                         // messages::internalError(asyncResp->res);
5050554c984SAndrew Geissler                         cleanUp();
5060554c984SAndrew Geissler                         BMCWEB_LOG_DEBUG << "error_code = " << ec;
5070554c984SAndrew Geissler                         BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
5080554c984SAndrew Geissler                     }
5090554c984SAndrew Geissler                     else
5100554c984SAndrew Geissler                     {
5110554c984SAndrew Geissler                         BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success";
5120554c984SAndrew Geissler                     }
5130554c984SAndrew Geissler                 },
5140554c984SAndrew Geissler                 "xyz.openbmc_project.Software.Download",
5157e860f15SJohn Edward Broadbent                 "/xyz/openbmc_project/software",
5167e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Common.TFTP", "DownloadViaTFTP", fwFile,
5177e860f15SJohn Edward Broadbent                 tftpServer);
5180554c984SAndrew Geissler 
5190554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost";
5207e860f15SJohn Edward Broadbent         });
521729dae72SJennifer Lee }
522729dae72SJennifer Lee 
5237e860f15SJohn Edward Broadbent inline void requestRoutesUpdateService(App& app)
5241abe55efSEd Tanous {
5257e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
526ed398213SEd Tanous         .privileges(redfish::privileges::getUpdateService)
5277e860f15SJohn Edward Broadbent         .methods(
5287e860f15SJohn Edward Broadbent             boost::beast::http::verb::
5297e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
5307e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
5318d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.type"] =
5320588a3b9SChicago Duan                 "#UpdateService.v1_5_0.UpdateService";
5338d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
5348d1b46d7Szhanghch05             asyncResp->res.jsonValue["Id"] = "UpdateService";
5357e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
5367e860f15SJohn Edward Broadbent                 "Service for Software Update";
5378d1b46d7Szhanghch05             asyncResp->res.jsonValue["Name"] = "Update Service";
5387e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["HttpPushUri"] =
5397e860f15SJohn Edward Broadbent                 "/redfish/v1/UpdateService";
5400f74e643SEd Tanous             // UpdateService cannot be disabled
5418d1b46d7Szhanghch05             asyncResp->res.jsonValue["ServiceEnabled"] = true;
5428d1b46d7Szhanghch05             asyncResp->res.jsonValue["FirmwareInventory"] = {
5430f74e643SEd Tanous                 {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}};
544d61e5194STejas Patil             // Get the MaxImageSizeBytes
545d61e5194STejas Patil             asyncResp->res.jsonValue["MaxImageSizeBytes"] =
546d61e5194STejas Patil                 bmcwebHttpReqBodyLimitMb * 1024 * 1024;
547d61e5194STejas Patil 
5480554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
5490554c984SAndrew Geissler             // Update Actions object.
5500554c984SAndrew Geissler             nlohmann::json& updateSvcSimpleUpdate =
5517e860f15SJohn Edward Broadbent                 asyncResp->res
5527e860f15SJohn Edward Broadbent                     .jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
5530554c984SAndrew Geissler             updateSvcSimpleUpdate["target"] =
5540554c984SAndrew Geissler                 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
5557e860f15SJohn Edward Broadbent             updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] =
5567e860f15SJohn Edward Broadbent                 {"TFTP"};
5570554c984SAndrew Geissler #endif
558274dfe62SJayashankar Padath             // Get the current ApplyTime value
5591e1e598dSJonathan Doman             sdbusplus::asio::getProperty<std::string>(
5601e1e598dSJonathan Doman                 *crow::connections::systemBus, "xyz.openbmc_project.Settings",
5611e1e598dSJonathan Doman                 "/xyz/openbmc_project/software/apply_time",
5621e1e598dSJonathan Doman                 "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime",
5638d1b46d7Szhanghch05                 [asyncResp](const boost::system::error_code ec,
5641e1e598dSJonathan Doman                             const std::string& applyTime) {
565274dfe62SJayashankar Padath                     if (ec)
566274dfe62SJayashankar Padath                     {
567274dfe62SJayashankar Padath                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
5688d1b46d7Szhanghch05                         messages::internalError(asyncResp->res);
569274dfe62SJayashankar Padath                         return;
570274dfe62SJayashankar Padath                     }
571274dfe62SJayashankar Padath 
572274dfe62SJayashankar Padath                     // Store the ApplyTime Value
5731e1e598dSJonathan Doman                     if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
5741e1e598dSJonathan Doman                                      "RequestedApplyTimes.Immediate")
575274dfe62SJayashankar Padath                     {
5768d1b46d7Szhanghch05                         asyncResp->res
5777e860f15SJohn Edward Broadbent                             .jsonValue["HttpPushUriOptions"]
5787e860f15SJohn Edward Broadbent                                       ["HttpPushUriApplyTime"]["ApplyTime"] =
5797e860f15SJohn Edward Broadbent                             "Immediate";
580274dfe62SJayashankar Padath                     }
5811e1e598dSJonathan Doman                     else if (applyTime ==
5821e1e598dSJonathan Doman                              "xyz.openbmc_project.Software.ApplyTime."
5831e1e598dSJonathan Doman                              "RequestedApplyTimes.OnReset")
584274dfe62SJayashankar Padath                     {
5858d1b46d7Szhanghch05                         asyncResp->res
5867e860f15SJohn Edward Broadbent                             .jsonValue["HttpPushUriOptions"]
5877e860f15SJohn Edward Broadbent                                       ["HttpPushUriApplyTime"]["ApplyTime"] =
5887e860f15SJohn Edward Broadbent                             "OnReset";
589274dfe62SJayashankar Padath                     }
5901e1e598dSJonathan Doman                 });
5917e860f15SJohn Edward Broadbent         });
5927e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
593ed398213SEd Tanous         .privileges(redfish::privileges::patchUpdateService)
5940fda0f12SGeorge Liu         .methods(
5950fda0f12SGeorge Liu             boost::beast::http::verb::
5960fda0f12SGeorge Liu                 patch)([](const crow::Request& req,
5977e860f15SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
598fa1a5a38SJayashankar Padath             BMCWEB_LOG_DEBUG << "doPatch...";
599fa1a5a38SJayashankar Padath 
600274dfe62SJayashankar Padath             std::optional<nlohmann::json> pushUriOptions;
6010fda0f12SGeorge Liu             if (!json_util::readJson(req, asyncResp->res, "HttpPushUriOptions",
6020fda0f12SGeorge Liu                                      pushUriOptions))
603fa1a5a38SJayashankar Padath             {
604fa1a5a38SJayashankar Padath                 return;
605fa1a5a38SJayashankar Padath             }
606fa1a5a38SJayashankar Padath 
607274dfe62SJayashankar Padath             if (pushUriOptions)
608274dfe62SJayashankar Padath             {
609274dfe62SJayashankar Padath                 std::optional<nlohmann::json> pushUriApplyTime;
6108d1b46d7Szhanghch05                 if (!json_util::readJson(*pushUriOptions, asyncResp->res,
6117e860f15SJohn Edward Broadbent                                          "HttpPushUriApplyTime",
6127e860f15SJohn Edward Broadbent                                          pushUriApplyTime))
613274dfe62SJayashankar Padath                 {
614274dfe62SJayashankar Padath                     return;
615274dfe62SJayashankar Padath                 }
616274dfe62SJayashankar Padath 
617274dfe62SJayashankar Padath                 if (pushUriApplyTime)
618274dfe62SJayashankar Padath                 {
619274dfe62SJayashankar Padath                     std::optional<std::string> applyTime;
6200fda0f12SGeorge Liu                     if (!json_util::readJson(*pushUriApplyTime, asyncResp->res,
6210fda0f12SGeorge Liu                                              "ApplyTime", applyTime))
622274dfe62SJayashankar Padath                     {
623274dfe62SJayashankar Padath                         return;
624274dfe62SJayashankar Padath                     }
625274dfe62SJayashankar Padath 
626274dfe62SJayashankar Padath                     if (applyTime)
627fa1a5a38SJayashankar Padath                     {
628fa1a5a38SJayashankar Padath                         std::string applyTimeNewVal;
629fa1a5a38SJayashankar Padath                         if (applyTime == "Immediate")
630fa1a5a38SJayashankar Padath                         {
631274dfe62SJayashankar Padath                             applyTimeNewVal =
6320fda0f12SGeorge Liu                                 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate";
633fa1a5a38SJayashankar Padath                         }
634274dfe62SJayashankar Padath                         else if (applyTime == "OnReset")
635274dfe62SJayashankar Padath                         {
636274dfe62SJayashankar Padath                             applyTimeNewVal =
6370fda0f12SGeorge Liu                                 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
638274dfe62SJayashankar Padath                         }
639fa1a5a38SJayashankar Padath                         else
640fa1a5a38SJayashankar Padath                         {
641274dfe62SJayashankar Padath                             BMCWEB_LOG_INFO
6420fda0f12SGeorge Liu                                 << "ApplyTime value is not in the list of acceptable values";
643274dfe62SJayashankar Padath                             messages::propertyValueNotInList(
644274dfe62SJayashankar Padath                                 asyncResp->res, *applyTime, "ApplyTime");
645274dfe62SJayashankar Padath                             return;
646fa1a5a38SJayashankar Padath                         }
647fa1a5a38SJayashankar Padath 
648fa1a5a38SJayashankar Padath                         // Set the requested image apply time value
649fa1a5a38SJayashankar Padath                         crow::connections::systemBus->async_method_call(
6500fda0f12SGeorge Liu                             [asyncResp](const boost::system::error_code ec) {
651fa1a5a38SJayashankar Padath                                 if (ec)
652fa1a5a38SJayashankar Padath                                 {
6537e860f15SJohn Edward Broadbent                                     BMCWEB_LOG_ERROR
6547e860f15SJohn Edward Broadbent                                         << "D-Bus responses error: " << ec;
655fa1a5a38SJayashankar Padath                                     messages::internalError(asyncResp->res);
656fa1a5a38SJayashankar Padath                                     return;
657fa1a5a38SJayashankar Padath                                 }
658fa1a5a38SJayashankar Padath                                 messages::success(asyncResp->res);
659fa1a5a38SJayashankar Padath                             },
660fa1a5a38SJayashankar Padath                             "xyz.openbmc_project.Settings",
661fa1a5a38SJayashankar Padath                             "/xyz/openbmc_project/software/apply_time",
662fa1a5a38SJayashankar Padath                             "org.freedesktop.DBus.Properties", "Set",
663274dfe62SJayashankar Padath                             "xyz.openbmc_project.Software.ApplyTime",
664274dfe62SJayashankar Padath                             "RequestedApplyTime",
665168e20c1SEd Tanous                             dbus::utility::DbusVariantType{applyTimeNewVal});
666fa1a5a38SJayashankar Padath                     }
667274dfe62SJayashankar Padath                 }
668fa1a5a38SJayashankar Padath             }
6697e860f15SJohn Edward Broadbent         });
6707e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
671ed398213SEd Tanous         .privileges(redfish::privileges::postUpdateService)
6727e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
6737e860f15SJohn Edward Broadbent             [](const crow::Request& req,
6747e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
675acb7cfb4SJennifer Lee                 BMCWEB_LOG_DEBUG << "doPost...";
676acb7cfb4SJennifer Lee 
67786adcd6dSAndrew Geissler                 // Setup callback for when new software detected
6784cde5d90SJames Feist                 monitorForSoftwareAvailable(asyncResp, req,
6794cde5d90SJames Feist                                             "/redfish/v1/UpdateService");
680acb7cfb4SJennifer Lee 
6817e860f15SJohn Edward Broadbent                 std::string filepath("/tmp/images/" +
6827e860f15SJohn Edward Broadbent                                      boost::uuids::to_string(
6837e860f15SJohn Edward Broadbent                                          boost::uuids::random_generator()()));
684acb7cfb4SJennifer Lee                 BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
6857e860f15SJohn Edward Broadbent                 std::ofstream out(filepath, std::ofstream::out |
6867e860f15SJohn Edward Broadbent                                                 std::ofstream::binary |
687acb7cfb4SJennifer Lee                                                 std::ofstream::trunc);
688acb7cfb4SJennifer Lee                 out << req.body;
689acb7cfb4SJennifer Lee                 out.close();
690acb7cfb4SJennifer Lee                 BMCWEB_LOG_DEBUG << "file upload complete!!";
6917e860f15SJohn Edward Broadbent             });
692729dae72SJennifer Lee }
693729dae72SJennifer Lee 
6947e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventoryCollection(App& app)
6951abe55efSEd Tanous {
6967e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/")
697ed398213SEd Tanous         .privileges(redfish::privileges::getSoftwareInventoryCollection)
6980fda0f12SGeorge Liu         .methods(
6990fda0f12SGeorge Liu             boost::beast::http::verb::
7000fda0f12SGeorge Liu                 get)([](const crow::Request&,
7017e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
7028d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.type"] =
7030f74e643SEd Tanous                 "#SoftwareInventoryCollection.SoftwareInventoryCollection";
7048d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] =
7050f74e643SEd Tanous                 "/redfish/v1/UpdateService/FirmwareInventory";
7060fda0f12SGeorge Liu             asyncResp->res.jsonValue["Name"] = "Software Inventory Collection";
707c711bf86SEd Tanous 
708c711bf86SEd Tanous             crow::connections::systemBus->async_method_call(
709c711bf86SEd Tanous                 [asyncResp](
710c711bf86SEd Tanous                     const boost::system::error_code ec,
7110fda0f12SGeorge Liu                     const std::vector<
7120fda0f12SGeorge Liu                         std::pair<std::string,
7130fda0f12SGeorge Liu                                   std::vector<std::pair<
7140fda0f12SGeorge Liu                                       std::string, std::vector<std::string>>>>>&
7151214b7e7SGunnar Mills                         subtree) {
7161abe55efSEd Tanous                     if (ec)
7171abe55efSEd Tanous                     {
718f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
7196c4eb9deSJennifer Lee                         return;
720729dae72SJennifer Lee                     }
7217e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"] =
7227e860f15SJohn Edward Broadbent                         nlohmann::json::array();
723c711bf86SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] = 0;
7246c4eb9deSJennifer Lee 
7251abe55efSEd Tanous                     for (auto& obj : subtree)
7261abe55efSEd Tanous                     {
7272dfd18efSEd Tanous                         sdbusplus::message::object_path path(obj.first);
7282dfd18efSEd Tanous                         std::string swId = path.filename();
7292dfd18efSEd Tanous                         if (swId.empty())
730f4b65ab1SJennifer Lee                         {
731f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
732f4b65ab1SJennifer Lee                             BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
733f4b65ab1SJennifer Lee                             return;
734f4b65ab1SJennifer Lee                         }
735f4b65ab1SJennifer Lee 
736c711bf86SEd Tanous                         nlohmann::json& members =
737c711bf86SEd Tanous                             asyncResp->res.jsonValue["Members"];
738c711bf86SEd Tanous                         members.push_back(
7390fda0f12SGeorge Liu                             {{"@odata.id",
7400fda0f12SGeorge Liu                               "/redfish/v1/UpdateService/FirmwareInventory/" +
741f4b65ab1SJennifer Lee                                   swId}});
742e0dd8057SAndrew Geissler                         asyncResp->res.jsonValue["Members@odata.count"] =
743c711bf86SEd Tanous                             members.size();
7446c4eb9deSJennifer Lee                     }
745c711bf86SEd Tanous                 },
7467e860f15SJohn Edward Broadbent                 // Note that only firmware levels associated with a device
7477e860f15SJohn Edward Broadbent                 // are stored under /xyz/openbmc_project/software therefore
7487e860f15SJohn Edward Broadbent                 // to ensure only real FirmwareInventory items are returned,
7497e860f15SJohn Edward Broadbent                 // this full object path must be used here as input to
7507e860f15SJohn Edward Broadbent                 // mapper
751c711bf86SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
752c711bf86SEd Tanous                 "/xyz/openbmc_project/object_mapper",
7532830a9cfSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
7542830a9cfSAndrew Geissler                 "/xyz/openbmc_project/software", static_cast<int32_t>(0),
7557e860f15SJohn Edward Broadbent                 std::array<const char*, 1>{
7567e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Software.Version"});
7577e860f15SJohn Edward Broadbent         });
758729dae72SJennifer Lee }
75987d84729SAndrew Geissler /* Fill related item links (i.e. bmc, bios) in for inventory */
7607e860f15SJohn Edward Broadbent inline static void
7617e860f15SJohn Edward Broadbent     getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
76287d84729SAndrew Geissler                     const std::string& purpose)
76387d84729SAndrew Geissler {
76487d84729SAndrew Geissler     if (purpose == fw_util::bmcPurpose)
76587d84729SAndrew Geissler     {
7665af690f5SAbhishek Patel         nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
7675af690f5SAbhishek Patel         relatedItem.push_back({{"@odata.id", "/redfish/v1/Managers/bmc"}});
7687e860f15SJohn Edward Broadbent         aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
76987d84729SAndrew Geissler     }
77087d84729SAndrew Geissler     else if (purpose == fw_util::biosPurpose)
77187d84729SAndrew Geissler     {
7725af690f5SAbhishek Patel         nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
7735af690f5SAbhishek Patel         relatedItem.push_back(
774f723d733SGunnar Mills             {{"@odata.id", "/redfish/v1/Systems/system/Bios"}});
7757e860f15SJohn Edward Broadbent         aResp->res.jsonValue["Members@odata.count"] = relatedItem.size();
77687d84729SAndrew Geissler     }
77787d84729SAndrew Geissler     else
77887d84729SAndrew Geissler     {
77987d84729SAndrew Geissler         BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
78087d84729SAndrew Geissler     }
78187d84729SAndrew Geissler }
78287d84729SAndrew Geissler 
7837e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventory(App& app)
7841abe55efSEd Tanous {
7857e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/")
786ed398213SEd Tanous         .privileges(redfish::privileges::getSoftwareInventory)
7877e860f15SJohn Edward Broadbent         .methods(
7887e860f15SJohn Edward Broadbent             boost::beast::http::verb::get)([](const crow::Request&,
7897e860f15SJohn Edward Broadbent                                               const std::shared_ptr<
7907e860f15SJohn Edward Broadbent                                                   bmcweb::AsyncResp>& asyncResp,
7917e860f15SJohn Edward Broadbent                                               const std::string& param) {
7923ae837c9SEd Tanous             std::shared_ptr<std::string> swId =
7937e860f15SJohn Edward Broadbent                 std::make_shared<std::string>(param);
794c711bf86SEd Tanous 
7958d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] =
7963ae837c9SEd Tanous                 "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
797c711bf86SEd Tanous 
798c711bf86SEd Tanous             crow::connections::systemBus->async_method_call(
7993ae837c9SEd Tanous                 [asyncResp, swId](
800c711bf86SEd Tanous                     const boost::system::error_code ec,
8017e860f15SJohn Edward Broadbent                     const std::vector<
8027e860f15SJohn Edward Broadbent                         std::pair<std::string,
8037e860f15SJohn Edward Broadbent                                   std::vector<std::pair<
8041214b7e7SGunnar Mills                                       std::string, std::vector<std::string>>>>>&
8051214b7e7SGunnar Mills                         subtree) {
80655c7b7a2SEd Tanous                     BMCWEB_LOG_DEBUG << "doGet callback...";
8071abe55efSEd Tanous                     if (ec)
8081abe55efSEd Tanous                     {
809f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
8106c4eb9deSJennifer Lee                         return;
8116c4eb9deSJennifer Lee                     }
8126c4eb9deSJennifer Lee 
8136913228dSAndrew Geissler                     // Ensure we find our input swId, otherwise return an error
8146913228dSAndrew Geissler                     bool found = false;
8151abe55efSEd Tanous                     for (const std::pair<
8161abe55efSEd Tanous                              std::string,
8177e860f15SJohn Edward Broadbent                              std::vector<std::pair<
8187e860f15SJohn Edward Broadbent                                  std::string, std::vector<std::string>>>>& obj :
8197e860f15SJohn Edward Broadbent                          subtree)
8201abe55efSEd Tanous                     {
8213ae837c9SEd Tanous                         if (boost::ends_with(obj.first, *swId) != true)
8221abe55efSEd Tanous                         {
823acb7cfb4SJennifer Lee                             continue;
824acb7cfb4SJennifer Lee                         }
825acb7cfb4SJennifer Lee 
826f4b65ab1SJennifer Lee                         if (obj.second.size() < 1)
8271abe55efSEd Tanous                         {
828acb7cfb4SJennifer Lee                             continue;
829acb7cfb4SJennifer Lee                         }
8306c4eb9deSJennifer Lee 
8316913228dSAndrew Geissler                         found = true;
8327e860f15SJohn Edward Broadbent                         fw_util::getFwStatus(asyncResp, swId,
8337e860f15SJohn Edward Broadbent                                              obj.second[0].first);
834e0dd8057SAndrew Geissler 
83555c7b7a2SEd Tanous                         crow::connections::systemBus->async_method_call(
836168e20c1SEd Tanous                             [asyncResp,
837168e20c1SEd Tanous                              swId](const boost::system::error_code errorCode,
8381abe55efSEd Tanous                                    const boost::container::flat_map<
839168e20c1SEd Tanous                                        std::string,
840168e20c1SEd Tanous                                        dbus::utility::DbusVariantType>&
841168e20c1SEd Tanous                                        propertiesList) {
84281ce609eSEd Tanous                                 if (errorCode)
8431abe55efSEd Tanous                                 {
844f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
8456c4eb9deSJennifer Lee                                     return;
8466c4eb9deSJennifer Lee                                 }
8471abe55efSEd Tanous                                 boost::container::flat_map<
848168e20c1SEd Tanous                                     std::string,
849168e20c1SEd Tanous                                     dbus::utility::DbusVariantType>::
850168e20c1SEd Tanous                                     const_iterator it =
851168e20c1SEd Tanous                                         propertiesList.find("Purpose");
8521abe55efSEd Tanous                                 if (it == propertiesList.end())
8531abe55efSEd Tanous                                 {
8541abe55efSEd Tanous                                     BMCWEB_LOG_DEBUG
8551abe55efSEd Tanous                                         << "Can't find property \"Purpose\"!";
856f12894f8SJason M. Bills                                     messages::propertyMissing(asyncResp->res,
857f12894f8SJason M. Bills                                                               "Purpose");
8586c4eb9deSJennifer Lee                                     return;
8596c4eb9deSJennifer Lee                                 }
8603ae837c9SEd Tanous                                 const std::string* swInvPurpose =
861abf2add6SEd Tanous                                     std::get_if<std::string>(&it->second);
8623ae837c9SEd Tanous                                 if (swInvPurpose == nullptr)
8631abe55efSEd Tanous                                 {
8640fda0f12SGeorge Liu                                     BMCWEB_LOG_DEBUG
8650fda0f12SGeorge Liu                                         << "wrong types for property\"Purpose\"!";
8667e860f15SJohn Edward Broadbent                                     messages::propertyValueTypeError(
8677e860f15SJohn Edward Broadbent                                         asyncResp->res, "", "Purpose");
868acb7cfb4SJennifer Lee                                     return;
869acb7cfb4SJennifer Lee                                 }
870c711bf86SEd Tanous 
8713ae837c9SEd Tanous                                 BMCWEB_LOG_DEBUG << "swInvPurpose = "
8723ae837c9SEd Tanous                                                  << *swInvPurpose;
873c711bf86SEd Tanous                                 it = propertiesList.find("Version");
8741abe55efSEd Tanous                                 if (it == propertiesList.end())
8751abe55efSEd Tanous                                 {
8761abe55efSEd Tanous                                     BMCWEB_LOG_DEBUG
8771abe55efSEd Tanous                                         << "Can't find property \"Version\"!";
878f12894f8SJason M. Bills                                     messages::propertyMissing(asyncResp->res,
879f12894f8SJason M. Bills                                                               "Version");
880c711bf86SEd Tanous                                     return;
881acb7cfb4SJennifer Lee                                 }
882acb7cfb4SJennifer Lee 
883f4b65ab1SJennifer Lee                                 BMCWEB_LOG_DEBUG << "Version found!";
884c711bf86SEd Tanous 
885f4b65ab1SJennifer Lee                                 const std::string* version =
886abf2add6SEd Tanous                                     std::get_if<std::string>(&it->second);
887f4b65ab1SJennifer Lee 
888f4b65ab1SJennifer Lee                                 if (version == nullptr)
8891abe55efSEd Tanous                                 {
8901abe55efSEd Tanous                                     BMCWEB_LOG_DEBUG
8911abe55efSEd Tanous                                         << "Can't find property \"Version\"!";
892f12894f8SJason M. Bills 
8937e860f15SJohn Edward Broadbent                                     messages::propertyValueTypeError(
8947e860f15SJohn Edward Broadbent                                         asyncResp->res, "", "Version");
8956c4eb9deSJennifer Lee                                     return;
8966c4eb9deSJennifer Lee                                 }
897c711bf86SEd Tanous                                 asyncResp->res.jsonValue["Version"] = *version;
8983ae837c9SEd Tanous                                 asyncResp->res.jsonValue["Id"] = *swId;
89954daabe7SAndrew Geissler 
90054daabe7SAndrew Geissler                                 // swInvPurpose is of format:
90154daabe7SAndrew Geissler                                 // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
902e2e96770SJames Feist                                 // Translate this to "ABC image"
903f23b7296SEd Tanous                                 size_t endDesc = swInvPurpose->rfind('.');
90454daabe7SAndrew Geissler                                 if (endDesc == std::string::npos)
90554daabe7SAndrew Geissler                                 {
90654daabe7SAndrew Geissler                                     messages::internalError(asyncResp->res);
90754daabe7SAndrew Geissler                                     return;
90854daabe7SAndrew Geissler                                 }
90954daabe7SAndrew Geissler                                 endDesc++;
91054daabe7SAndrew Geissler                                 if (endDesc >= swInvPurpose->size())
91154daabe7SAndrew Geissler                                 {
91254daabe7SAndrew Geissler                                     messages::internalError(asyncResp->res);
91354daabe7SAndrew Geissler                                     return;
91454daabe7SAndrew Geissler                                 }
91554daabe7SAndrew Geissler 
91654daabe7SAndrew Geissler                                 std::string formatDesc =
91754daabe7SAndrew Geissler                                     swInvPurpose->substr(endDesc);
91854daabe7SAndrew Geissler                                 asyncResp->res.jsonValue["Description"] =
919e2e96770SJames Feist                                     formatDesc + " image";
92087d84729SAndrew Geissler                                 getRelatedItems(asyncResp, *swInvPurpose);
9216c4eb9deSJennifer Lee                             },
922c711bf86SEd Tanous                             obj.second[0].first, obj.first,
923c711bf86SEd Tanous                             "org.freedesktop.DBus.Properties", "GetAll",
924c711bf86SEd Tanous                             "xyz.openbmc_project.Software.Version");
9256c4eb9deSJennifer Lee                     }
9266913228dSAndrew Geissler                     if (!found)
9276913228dSAndrew Geissler                     {
9287e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR
9297e860f15SJohn Edward Broadbent                             << "Input swID " + *swId + " not found!";
9306913228dSAndrew Geissler                         messages::resourceMissingAtURI(
9316913228dSAndrew Geissler                             asyncResp->res,
9327e860f15SJohn Edward Broadbent                             "/redfish/v1/UpdateService/FirmwareInventory/" +
9337e860f15SJohn Edward Broadbent                                 *swId);
9346913228dSAndrew Geissler                         return;
9356913228dSAndrew Geissler                     }
9364e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["@odata.type"] =
9374e68c45bSAyushi Smriti                         "#SoftwareInventory.v1_1_0.SoftwareInventory";
9384e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["Name"] = "Software Inventory";
9394e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
9403f8a743aSAppaRao Puli 
9413f8a743aSAppaRao Puli                     asyncResp->res.jsonValue["Updateable"] = false;
9423f8a743aSAppaRao Puli                     fw_util::getFwUpdateableStatus(asyncResp, swId);
943c711bf86SEd Tanous                 },
944c711bf86SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
945c711bf86SEd Tanous                 "/xyz/openbmc_project/object_mapper",
946271584abSEd Tanous                 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
947271584abSEd Tanous                 static_cast<int32_t>(0),
9487e860f15SJohn Edward Broadbent                 std::array<const char*, 1>{
9497e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Software.Version"});
9507e860f15SJohn Edward Broadbent         });
9516c4eb9deSJennifer Lee }
952729dae72SJennifer Lee 
953729dae72SJennifer Lee } // namespace redfish
954