xref: /openbmc/bmcweb/features/redfish/lib/update_service.hpp (revision 168e20c1306e3e689907ba43e14ea679e2328611)
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>
22*168e20c1SEd Tanous #include <dbus_utility.hpp>
23ed398213SEd Tanous #include <registries/privilege_registry.hpp>
2487d84729SAndrew Geissler #include <utils/fw_utils.hpp>
251214b7e7SGunnar Mills 
261abe55efSEd Tanous namespace redfish
271abe55efSEd Tanous {
2827826b5fSEd Tanous 
290e7de46fSAndrew Geissler // Match signals added on software path
30acb7cfb4SJennifer Lee static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher;
314cde5d90SJames Feist static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateErrorMatcher;
320e7de46fSAndrew Geissler // Only allow one update at a time
330e7de46fSAndrew Geissler static bool fwUpdateInProgress = false;
3486adcd6dSAndrew Geissler // Timer for software available
35271584abSEd Tanous static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer;
3686adcd6dSAndrew Geissler 
377e860f15SJohn Edward Broadbent inline static void cleanUp()
3886adcd6dSAndrew Geissler {
3986adcd6dSAndrew Geissler     fwUpdateInProgress = false;
4086adcd6dSAndrew Geissler     fwUpdateMatcher = nullptr;
414cde5d90SJames Feist     fwUpdateErrorMatcher = nullptr;
4286adcd6dSAndrew Geissler }
437e860f15SJohn Edward Broadbent inline static void activateImage(const std::string& objPath,
4486adcd6dSAndrew Geissler                                  const std::string& service)
4586adcd6dSAndrew Geissler {
4686adcd6dSAndrew Geissler     BMCWEB_LOG_DEBUG << "Activate image for " << objPath << " " << service;
4786adcd6dSAndrew Geissler     crow::connections::systemBus->async_method_call(
4881ce609eSEd Tanous         [](const boost::system::error_code errorCode) {
4981ce609eSEd Tanous             if (errorCode)
5086adcd6dSAndrew Geissler             {
5181ce609eSEd Tanous                 BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
5281ce609eSEd Tanous                 BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message();
5386adcd6dSAndrew Geissler             }
5486adcd6dSAndrew Geissler         },
5586adcd6dSAndrew Geissler         service, objPath, "org.freedesktop.DBus.Properties", "Set",
5686adcd6dSAndrew Geissler         "xyz.openbmc_project.Software.Activation", "RequestedActivation",
57*168e20c1SEd Tanous         dbus::utility::DbusVariantType(
580fda0f12SGeorge Liu             "xyz.openbmc_project.Software.Activation.RequestedActivations.Active"));
5986adcd6dSAndrew Geissler }
600554c984SAndrew Geissler 
610554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr
620554c984SAndrew Geissler // then no asyncResp updates will occur
638d1b46d7Szhanghch05 static void
648d1b46d7Szhanghch05     softwareInterfaceAdded(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
65fe306728SJames Feist                            sdbusplus::message::message& m,
66a3e65892SEd Tanous                            task::Payload&& payload)
6786adcd6dSAndrew Geissler {
6886adcd6dSAndrew Geissler     std::vector<std::pair<
6986adcd6dSAndrew Geissler         std::string,
70*168e20c1SEd Tanous         std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>>>
7186adcd6dSAndrew Geissler         interfacesProperties;
7286adcd6dSAndrew Geissler 
7386adcd6dSAndrew Geissler     sdbusplus::message::object_path objPath;
7486adcd6dSAndrew Geissler 
7586adcd6dSAndrew Geissler     m.read(objPath, interfacesProperties);
7686adcd6dSAndrew Geissler 
7786adcd6dSAndrew Geissler     BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
7886adcd6dSAndrew Geissler     for (auto& interface : interfacesProperties)
7986adcd6dSAndrew Geissler     {
8086adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "interface = " << interface.first;
8186adcd6dSAndrew Geissler 
8286adcd6dSAndrew Geissler         if (interface.first == "xyz.openbmc_project.Software.Activation")
8386adcd6dSAndrew Geissler         {
8486adcd6dSAndrew Geissler             // Retrieve service and activate
8586adcd6dSAndrew Geissler             crow::connections::systemBus->async_method_call(
86a3e65892SEd Tanous                 [objPath, asyncResp, payload(std::move(payload))](
87a3e65892SEd Tanous                     const boost::system::error_code errorCode,
88a3e65892SEd Tanous                     const std::vector<
89a3e65892SEd Tanous                         std::pair<std::string, std::vector<std::string>>>&
90a3e65892SEd Tanous                         objInfo) mutable {
9181ce609eSEd Tanous                     if (errorCode)
9286adcd6dSAndrew Geissler                     {
9381ce609eSEd Tanous                         BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
9486adcd6dSAndrew Geissler                         BMCWEB_LOG_DEBUG << "error msg = "
9581ce609eSEd Tanous                                          << errorCode.message();
960554c984SAndrew Geissler                         if (asyncResp)
970554c984SAndrew Geissler                         {
9886adcd6dSAndrew Geissler                             messages::internalError(asyncResp->res);
990554c984SAndrew Geissler                         }
10086adcd6dSAndrew Geissler                         cleanUp();
10186adcd6dSAndrew Geissler                         return;
10286adcd6dSAndrew Geissler                     }
10386adcd6dSAndrew Geissler                     // Ensure we only got one service back
10486adcd6dSAndrew Geissler                     if (objInfo.size() != 1)
10586adcd6dSAndrew Geissler                     {
10686adcd6dSAndrew Geissler                         BMCWEB_LOG_ERROR << "Invalid Object Size "
10786adcd6dSAndrew Geissler                                          << objInfo.size();
1080554c984SAndrew Geissler                         if (asyncResp)
1090554c984SAndrew Geissler                         {
11086adcd6dSAndrew Geissler                             messages::internalError(asyncResp->res);
1110554c984SAndrew Geissler                         }
11286adcd6dSAndrew Geissler                         cleanUp();
11386adcd6dSAndrew Geissler                         return;
11486adcd6dSAndrew Geissler                     }
11586adcd6dSAndrew Geissler                     // cancel timer only when
11686adcd6dSAndrew Geissler                     // xyz.openbmc_project.Software.Activation interface
11786adcd6dSAndrew Geissler                     // is added
11886adcd6dSAndrew Geissler                     fwAvailableTimer = nullptr;
11986adcd6dSAndrew Geissler 
12086adcd6dSAndrew Geissler                     activateImage(objPath.str, objInfo[0].first);
1210554c984SAndrew Geissler                     if (asyncResp)
1220554c984SAndrew Geissler                     {
12332898ceaSJames Feist                         std::shared_ptr<task::TaskData> task =
12432898ceaSJames Feist                             task::TaskData::createTask(
12532898ceaSJames Feist                                 [](boost::system::error_code ec,
12632898ceaSJames Feist                                    sdbusplus::message::message& msg,
1271214b7e7SGunnar Mills                                    const std::shared_ptr<task::TaskData>&
1281214b7e7SGunnar Mills                                        taskData) {
12932898ceaSJames Feist                                     if (ec)
13032898ceaSJames Feist                                     {
13132898ceaSJames Feist                                         return task::completed;
13232898ceaSJames Feist                                     }
13332898ceaSJames Feist 
13432898ceaSJames Feist                                     std::string iface;
13532898ceaSJames Feist                                     boost::container::flat_map<
136fd9ab9e1SJames Feist                                         std::string,
137*168e20c1SEd Tanous                                         dbus::utility::DbusVariantType>
13832898ceaSJames Feist                                         values;
139fd9ab9e1SJames Feist 
140fd9ab9e1SJames Feist                                     std::string index =
141fd9ab9e1SJames Feist                                         std::to_string(taskData->index);
14232898ceaSJames Feist                                     msg.read(iface, values);
143fd9ab9e1SJames Feist 
1440fda0f12SGeorge Liu                                     if (iface ==
1450fda0f12SGeorge Liu                                         "xyz.openbmc_project.Software.Activation")
146fd9ab9e1SJames Feist                                     {
14732898ceaSJames Feist                                         auto findActivation =
14832898ceaSJames Feist                                             values.find("Activation");
14932898ceaSJames Feist                                         if (findActivation == values.end())
15032898ceaSJames Feist                                         {
15132898ceaSJames Feist                                             return !task::completed;
15232898ceaSJames Feist                                         }
15332898ceaSJames Feist                                         std::string* state =
15432898ceaSJames Feist                                             std::get_if<std::string>(
15532898ceaSJames Feist                                                 &(findActivation->second));
15632898ceaSJames Feist 
15732898ceaSJames Feist                                         if (state == nullptr)
15832898ceaSJames Feist                                         {
15932898ceaSJames Feist                                             taskData->messages.emplace_back(
16032898ceaSJames Feist                                                 messages::internalError());
16132898ceaSJames Feist                                             return task::completed;
16232898ceaSJames Feist                                         }
16332898ceaSJames Feist 
164fd9ab9e1SJames Feist                                         if (boost::ends_with(*state,
165fd9ab9e1SJames Feist                                                              "Invalid") ||
16632898ceaSJames Feist                                             boost::ends_with(*state, "Failed"))
16732898ceaSJames Feist                                         {
16832898ceaSJames Feist                                             taskData->state = "Exception";
16932898ceaSJames Feist                                             taskData->status = "Warning";
17032898ceaSJames Feist                                             taskData->messages.emplace_back(
171e5d5006bSJames Feist                                                 messages::taskAborted(index));
17232898ceaSJames Feist                                             return task::completed;
17332898ceaSJames Feist                                         }
17432898ceaSJames Feist 
17532898ceaSJames Feist                                         if (boost::ends_with(*state, "Staged"))
17632898ceaSJames Feist                                         {
177fd9ab9e1SJames Feist                                             taskData->state = "Stopping";
178fd9ab9e1SJames Feist                                             taskData->messages.emplace_back(
179fd9ab9e1SJames Feist                                                 messages::taskPaused(index));
180fd9ab9e1SJames Feist 
181fd9ab9e1SJames Feist                                             // its staged, set a long timer to
182fd9ab9e1SJames Feist                                             // allow them time to complete the
183fd9ab9e1SJames Feist                                             // update (probably cycle the
184fd9ab9e1SJames Feist                                             // system) if this expires then
185fd9ab9e1SJames Feist                                             // task will be cancelled
186fd9ab9e1SJames Feist                                             taskData->extendTimer(
187fd9ab9e1SJames Feist                                                 std::chrono::hours(5));
18832898ceaSJames Feist                                             return !task::completed;
18932898ceaSJames Feist                                         }
19032898ceaSJames Feist 
19132898ceaSJames Feist                                         if (boost::ends_with(*state, "Active"))
19232898ceaSJames Feist                                         {
19332898ceaSJames Feist                                             taskData->messages.emplace_back(
194fd9ab9e1SJames Feist                                                 messages::taskCompletedOK(
195fd9ab9e1SJames Feist                                                     index));
19632898ceaSJames Feist                                             taskData->state = "Completed";
19732898ceaSJames Feist                                             return task::completed;
19832898ceaSJames Feist                                         }
199fd9ab9e1SJames Feist                                     }
2000fda0f12SGeorge Liu                                     else if (
2010fda0f12SGeorge Liu                                         iface ==
2020fda0f12SGeorge Liu                                         "xyz.openbmc_project.Software.ActivationProgress")
203fd9ab9e1SJames Feist                                     {
204fd9ab9e1SJames Feist                                         auto findProgress =
205fd9ab9e1SJames Feist                                             values.find("Progress");
206fd9ab9e1SJames Feist                                         if (findProgress == values.end())
207fd9ab9e1SJames Feist                                         {
208fd9ab9e1SJames Feist                                             return !task::completed;
209fd9ab9e1SJames Feist                                         }
210fd9ab9e1SJames Feist                                         uint8_t* progress =
211fd9ab9e1SJames Feist                                             std::get_if<uint8_t>(
212fd9ab9e1SJames Feist                                                 &(findProgress->second));
213fd9ab9e1SJames Feist 
214fd9ab9e1SJames Feist                                         if (progress == nullptr)
215fd9ab9e1SJames Feist                                         {
216fd9ab9e1SJames Feist                                             taskData->messages.emplace_back(
217fd9ab9e1SJames Feist                                                 messages::internalError());
218fd9ab9e1SJames Feist                                             return task::completed;
219fd9ab9e1SJames Feist                                         }
2206868ff50SGeorge Liu                                         taskData->percentComplete =
2216868ff50SGeorge Liu                                             static_cast<int>(*progress);
222fd9ab9e1SJames Feist                                         taskData->messages.emplace_back(
223fd9ab9e1SJames Feist                                             messages::taskProgressChanged(
224fd9ab9e1SJames Feist                                                 index, static_cast<size_t>(
225fd9ab9e1SJames Feist                                                            *progress)));
226fd9ab9e1SJames Feist 
227fd9ab9e1SJames Feist                                         // if we're getting status updates it's
228fd9ab9e1SJames Feist                                         // still alive, update timer
229fd9ab9e1SJames Feist                                         taskData->extendTimer(
230fd9ab9e1SJames Feist                                             std::chrono::minutes(5));
231fd9ab9e1SJames Feist                                     }
23232898ceaSJames Feist 
23332898ceaSJames Feist                                     // as firmware update often results in a
23432898ceaSJames Feist                                     // reboot, the task  may never "complete"
23532898ceaSJames Feist                                     // unless it is an error
23632898ceaSJames Feist 
23732898ceaSJames Feist                                     return !task::completed;
23832898ceaSJames Feist                                 },
2390fda0f12SGeorge Liu                                 "type='signal',interface='org.freedesktop.DBus.Properties',"
240fd9ab9e1SJames Feist                                 "member='PropertiesChanged',path='" +
24132898ceaSJames Feist                                     objPath.str + "'");
24232898ceaSJames Feist                         task->startTimer(std::chrono::minutes(5));
24332898ceaSJames Feist                         task->populateResp(asyncResp->res);
244a3e65892SEd Tanous                         task->payload.emplace(std::move(payload));
2450554c984SAndrew Geissler                     }
24686adcd6dSAndrew Geissler                     fwUpdateInProgress = false;
24786adcd6dSAndrew Geissler                 },
24886adcd6dSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper",
24986adcd6dSAndrew Geissler                 "/xyz/openbmc_project/object_mapper",
25086adcd6dSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str,
25186adcd6dSAndrew Geissler                 std::array<const char*, 1>{
25286adcd6dSAndrew Geissler                     "xyz.openbmc_project.Software.Activation"});
25386adcd6dSAndrew Geissler         }
25486adcd6dSAndrew Geissler     }
25586adcd6dSAndrew Geissler }
25686adcd6dSAndrew Geissler 
2570554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr
2580554c984SAndrew Geissler // then no asyncResp updates will occur
259b5a76932SEd Tanous static void monitorForSoftwareAvailable(
2608d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2618d1b46d7Szhanghch05     const crow::Request& req, const std::string& url,
2628d1b46d7Szhanghch05     int timeoutTimeSeconds = 10)
26386adcd6dSAndrew Geissler {
26486adcd6dSAndrew Geissler     // Only allow one FW update at a time
26586adcd6dSAndrew Geissler     if (fwUpdateInProgress != false)
26686adcd6dSAndrew Geissler     {
2670554c984SAndrew Geissler         if (asyncResp)
2680554c984SAndrew Geissler         {
26986adcd6dSAndrew Geissler             messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
2700554c984SAndrew Geissler         }
27186adcd6dSAndrew Geissler         return;
27286adcd6dSAndrew Geissler     }
27386adcd6dSAndrew Geissler 
2740554c984SAndrew Geissler     fwAvailableTimer =
275271584abSEd Tanous         std::make_unique<boost::asio::steady_timer>(*req.ioService);
27686adcd6dSAndrew Geissler 
277271584abSEd Tanous     fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds));
27886adcd6dSAndrew Geissler 
27986adcd6dSAndrew Geissler     fwAvailableTimer->async_wait(
28086adcd6dSAndrew Geissler         [asyncResp](const boost::system::error_code& ec) {
28186adcd6dSAndrew Geissler             cleanUp();
28286adcd6dSAndrew Geissler             if (ec == boost::asio::error::operation_aborted)
28386adcd6dSAndrew Geissler             {
28486adcd6dSAndrew Geissler                 // expected, we were canceled before the timer completed.
28586adcd6dSAndrew Geissler                 return;
28686adcd6dSAndrew Geissler             }
28786adcd6dSAndrew Geissler             BMCWEB_LOG_ERROR
28886adcd6dSAndrew Geissler                 << "Timed out waiting for firmware object being created";
28986adcd6dSAndrew Geissler             BMCWEB_LOG_ERROR
29086adcd6dSAndrew Geissler                 << "FW image may has already been uploaded to server";
29186adcd6dSAndrew Geissler             if (ec)
29286adcd6dSAndrew Geissler             {
29386adcd6dSAndrew Geissler                 BMCWEB_LOG_ERROR << "Async_wait failed" << ec;
29486adcd6dSAndrew Geissler                 return;
29586adcd6dSAndrew Geissler             }
2960554c984SAndrew Geissler             if (asyncResp)
2970554c984SAndrew Geissler             {
29886adcd6dSAndrew Geissler                 redfish::messages::internalError(asyncResp->res);
2990554c984SAndrew Geissler             }
30086adcd6dSAndrew Geissler         });
301a3e65892SEd Tanous     task::Payload payload(req);
302a3e65892SEd Tanous     auto callback = [asyncResp,
303a3e65892SEd Tanous                      payload](sdbusplus::message::message& m) mutable {
30486adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "Match fired";
305a3e65892SEd Tanous         softwareInterfaceAdded(asyncResp, m, std::move(payload));
30686adcd6dSAndrew Geissler     };
30786adcd6dSAndrew Geissler 
30886adcd6dSAndrew Geissler     fwUpdateInProgress = true;
30986adcd6dSAndrew Geissler 
31086adcd6dSAndrew Geissler     fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
31186adcd6dSAndrew Geissler         *crow::connections::systemBus,
31286adcd6dSAndrew Geissler         "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
31386adcd6dSAndrew Geissler         "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
31486adcd6dSAndrew Geissler         callback);
3154cde5d90SJames Feist 
3164cde5d90SJames Feist     fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match::match>(
3174cde5d90SJames Feist         *crow::connections::systemBus,
3184cde5d90SJames Feist         "type='signal',member='PropertiesChanged',path_namespace='/xyz/"
3194cde5d90SJames Feist         "openbmc_project/logging/entry',"
3204cde5d90SJames Feist         "arg0='xyz.openbmc_project.Logging.Entry'",
3214cde5d90SJames Feist         [asyncResp, url](sdbusplus::message::message& m) {
3224cde5d90SJames Feist             BMCWEB_LOG_DEBUG << "Error Match fired";
323*168e20c1SEd Tanous             boost::container::flat_map<std::string,
324*168e20c1SEd Tanous                                        dbus::utility::DbusVariantType>
3254cde5d90SJames Feist                 values;
3264cde5d90SJames Feist             std::string objName;
3274cde5d90SJames Feist             m.read(objName, values);
3284cde5d90SJames Feist             auto find = values.find("Message");
3294cde5d90SJames Feist             if (find == values.end())
3304cde5d90SJames Feist             {
3314cde5d90SJames Feist                 return;
3324cde5d90SJames Feist             }
3334cde5d90SJames Feist             std::string* type = std::get_if<std::string>(&(find->second));
3344cde5d90SJames Feist             if (type == nullptr)
3354cde5d90SJames Feist             {
3364cde5d90SJames Feist                 return; // if this was our message, timeout will cover it
3374cde5d90SJames Feist             }
33888b3dd12SGunnar Mills             if (!boost::starts_with(*type, "xyz.openbmc_project.Software"))
3394cde5d90SJames Feist             {
3404cde5d90SJames Feist                 return;
3414cde5d90SJames Feist             }
3424cde5d90SJames Feist             if (*type ==
3434cde5d90SJames Feist                 "xyz.openbmc_project.Software.Image.Error.UnTarFailure")
3444cde5d90SJames Feist             {
3454cde5d90SJames Feist                 redfish::messages::invalidUpload(asyncResp->res, url,
3464cde5d90SJames Feist                                                  "Invalid archive");
3474cde5d90SJames Feist             }
3480fda0f12SGeorge Liu             else if (
3490fda0f12SGeorge Liu                 *type ==
3500fda0f12SGeorge Liu                 "xyz.openbmc_project.Software.Image.Error.ManifestFileFailure")
3514cde5d90SJames Feist             {
3524cde5d90SJames Feist                 redfish::messages::invalidUpload(asyncResp->res, url,
3534cde5d90SJames Feist                                                  "Invalid manifest");
3544cde5d90SJames Feist             }
3554cde5d90SJames Feist             else if (*type ==
3564cde5d90SJames Feist                      "xyz.openbmc_project.Software.Image.Error.ImageFailure")
3574cde5d90SJames Feist             {
3584cde5d90SJames Feist                 redfish::messages::invalidUpload(asyncResp->res, url,
3594cde5d90SJames Feist                                                  "Invalid image format");
3604cde5d90SJames Feist             }
3610fda0f12SGeorge Liu             else if (*type ==
3620fda0f12SGeorge Liu                      "xyz.openbmc_project.Software.Version.Error.AlreadyExists")
36388b3dd12SGunnar Mills             {
36488b3dd12SGunnar Mills 
36588b3dd12SGunnar Mills                 redfish::messages::invalidUpload(
36688b3dd12SGunnar Mills                     asyncResp->res, url, "Image version already exists");
36788b3dd12SGunnar Mills 
36888b3dd12SGunnar Mills                 redfish::messages::resourceAlreadyExists(
3690588a3b9SChicago Duan                     asyncResp->res, "UpdateService.v1_5_0.UpdateService",
37088b3dd12SGunnar Mills                     "Version", "uploaded version");
37188b3dd12SGunnar Mills             }
3724cde5d90SJames Feist             else if (*type ==
3734cde5d90SJames Feist                      "xyz.openbmc_project.Software.Image.Error.BusyFailure")
3744cde5d90SJames Feist             {
3754cde5d90SJames Feist                 redfish::messages::resourceExhaustion(asyncResp->res, url);
3764cde5d90SJames Feist             }
3774cde5d90SJames Feist             else
3784cde5d90SJames Feist             {
3794cde5d90SJames Feist                 redfish::messages::internalError(asyncResp->res);
3804cde5d90SJames Feist             }
3814cde5d90SJames Feist             fwAvailableTimer = nullptr;
3824cde5d90SJames Feist         });
38386adcd6dSAndrew Geissler }
384729dae72SJennifer Lee 
3850554c984SAndrew Geissler /**
3860554c984SAndrew Geissler  * UpdateServiceActionsSimpleUpdate class supports handle POST method for
3870554c984SAndrew Geissler  * SimpleUpdate action.
3880554c984SAndrew Geissler  */
3897e860f15SJohn Edward Broadbent inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app)
3900554c984SAndrew Geissler {
3917e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
3927e860f15SJohn Edward Broadbent         app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
393ed398213SEd Tanous         .privileges(redfish::privileges::postUpdateService)
3947e860f15SJohn Edward Broadbent         .methods(
3957e860f15SJohn Edward Broadbent             boost::beast::http::verb::
3967e860f15SJohn Edward Broadbent                 post)([](const crow::Request& req,
3977e860f15SJohn Edward Broadbent                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3980554c984SAndrew Geissler             std::optional<std::string> transferProtocol;
3990554c984SAndrew Geissler             std::string imageURI;
4000554c984SAndrew Geissler 
4010554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost";
4020554c984SAndrew Geissler 
4030554c984SAndrew Geissler             // User can pass in both TransferProtocol and ImageURI parameters or
4044e0453b1SGunnar Mills             // they can pass in just the ImageURI with the transfer protocol
4054e0453b1SGunnar Mills             // embedded within it.
4060554c984SAndrew Geissler             // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
4070554c984SAndrew Geissler             // 2) ImageURI:tftp://1.1.1.1/myfile.bin
4080554c984SAndrew Geissler 
4090554c984SAndrew Geissler             if (!json_util::readJson(req, asyncResp->res, "TransferProtocol",
4100554c984SAndrew Geissler                                      transferProtocol, "ImageURI", imageURI))
4110554c984SAndrew Geissler             {
4120554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG
4130554c984SAndrew Geissler                     << "Missing TransferProtocol or ImageURI parameter";
4140554c984SAndrew Geissler                 return;
4150554c984SAndrew Geissler             }
4160554c984SAndrew Geissler             if (!transferProtocol)
4170554c984SAndrew Geissler             {
4180554c984SAndrew Geissler                 // Must be option 2
4190554c984SAndrew Geissler                 // Verify ImageURI has transfer protocol in it
420f23b7296SEd Tanous                 size_t separator = imageURI.find(':');
4210554c984SAndrew Geissler                 if ((separator == std::string::npos) ||
4220554c984SAndrew Geissler                     ((separator + 1) > imageURI.size()))
4230554c984SAndrew Geissler                 {
4240554c984SAndrew Geissler                     messages::actionParameterValueTypeError(
4250554c984SAndrew Geissler                         asyncResp->res, imageURI, "ImageURI",
4260554c984SAndrew Geissler                         "UpdateService.SimpleUpdate");
4270554c984SAndrew Geissler                     BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: "
4280554c984SAndrew Geissler                                      << imageURI;
4290554c984SAndrew Geissler                     return;
4300554c984SAndrew Geissler                 }
4310554c984SAndrew Geissler                 transferProtocol = imageURI.substr(0, separator);
4327e860f15SJohn Edward Broadbent                 // Ensure protocol is upper case for a common comparison path
4337e860f15SJohn Edward Broadbent                 // below
4340554c984SAndrew Geissler                 boost::to_upper(*transferProtocol);
4350554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG << "Encoded transfer protocol "
4360554c984SAndrew Geissler                                  << *transferProtocol;
4370554c984SAndrew Geissler 
4380554c984SAndrew Geissler                 // Adjust imageURI to not have the protocol on it for parsing
4390554c984SAndrew Geissler                 // below
4400554c984SAndrew Geissler                 // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin
4410554c984SAndrew Geissler                 imageURI = imageURI.substr(separator + 3);
4420554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI;
4430554c984SAndrew Geissler             }
4440554c984SAndrew Geissler 
4450554c984SAndrew Geissler             // OpenBMC currently only supports TFTP
4460554c984SAndrew Geissler             if (*transferProtocol != "TFTP")
4470554c984SAndrew Geissler             {
4487e860f15SJohn Edward Broadbent                 messages::actionParameterNotSupported(
4497e860f15SJohn Edward Broadbent                     asyncResp->res, "TransferProtocol",
4500554c984SAndrew Geissler                     "UpdateService.SimpleUpdate");
4510554c984SAndrew Geissler                 BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: "
4520554c984SAndrew Geissler                                  << *transferProtocol;
4530554c984SAndrew Geissler                 return;
4540554c984SAndrew Geissler             }
4550554c984SAndrew Geissler 
4560554c984SAndrew Geissler             // Format should be <IP or Hostname>/<file> for imageURI
457f23b7296SEd Tanous             size_t separator = imageURI.find('/');
4580554c984SAndrew Geissler             if ((separator == std::string::npos) ||
4590554c984SAndrew Geissler                 ((separator + 1) > imageURI.size()))
4600554c984SAndrew Geissler             {
4610554c984SAndrew Geissler                 messages::actionParameterValueTypeError(
4620554c984SAndrew Geissler                     asyncResp->res, imageURI, "ImageURI",
4630554c984SAndrew Geissler                     "UpdateService.SimpleUpdate");
4640554c984SAndrew Geissler                 BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI;
4650554c984SAndrew Geissler                 return;
4660554c984SAndrew Geissler             }
4670554c984SAndrew Geissler 
4680554c984SAndrew Geissler             std::string tftpServer = imageURI.substr(0, separator);
4690554c984SAndrew Geissler             std::string fwFile = imageURI.substr(separator + 1);
4700554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile;
4710554c984SAndrew Geissler 
4720554c984SAndrew Geissler             // Setup callback for when new software detected
4732618d5e3SGunnar Mills             // Give TFTP 10 minutes to complete
4744cde5d90SJames Feist             monitorForSoftwareAvailable(
475d7a596bdSAlbert Zhang                 asyncResp, req,
4764cde5d90SJames Feist                 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate",
4772618d5e3SGunnar Mills                 600);
4780554c984SAndrew Geissler 
4792618d5e3SGunnar Mills             // TFTP can take up to 10 minutes depending on image size and
4800554c984SAndrew Geissler             // connection speed. Return to caller as soon as the TFTP operation
4810554c984SAndrew Geissler             // has been started. The callback above will ensure the activate
4820554c984SAndrew Geissler             // is started once the download has completed
4830554c984SAndrew Geissler             redfish::messages::success(asyncResp->res);
4840554c984SAndrew Geissler 
4850554c984SAndrew Geissler             // Call TFTP service
4860554c984SAndrew Geissler             crow::connections::systemBus->async_method_call(
4870554c984SAndrew Geissler                 [](const boost::system::error_code ec) {
4880554c984SAndrew Geissler                     if (ec)
4890554c984SAndrew Geissler                     {
4900554c984SAndrew Geissler                         // messages::internalError(asyncResp->res);
4910554c984SAndrew Geissler                         cleanUp();
4920554c984SAndrew Geissler                         BMCWEB_LOG_DEBUG << "error_code = " << ec;
4930554c984SAndrew Geissler                         BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
4940554c984SAndrew Geissler                     }
4950554c984SAndrew Geissler                     else
4960554c984SAndrew Geissler                     {
4970554c984SAndrew Geissler                         BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success";
4980554c984SAndrew Geissler                     }
4990554c984SAndrew Geissler                 },
5000554c984SAndrew Geissler                 "xyz.openbmc_project.Software.Download",
5017e860f15SJohn Edward Broadbent                 "/xyz/openbmc_project/software",
5027e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Common.TFTP", "DownloadViaTFTP", fwFile,
5037e860f15SJohn Edward Broadbent                 tftpServer);
5040554c984SAndrew Geissler 
5050554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost";
5067e860f15SJohn Edward Broadbent         });
507729dae72SJennifer Lee }
508729dae72SJennifer Lee 
5097e860f15SJohn Edward Broadbent inline void requestRoutesUpdateService(App& app)
5101abe55efSEd Tanous {
5117e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
512ed398213SEd Tanous         .privileges(redfish::privileges::getUpdateService)
5137e860f15SJohn Edward Broadbent         .methods(
5147e860f15SJohn Edward Broadbent             boost::beast::http::verb::
5157e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
5167e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
5178d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.type"] =
5180588a3b9SChicago Duan                 "#UpdateService.v1_5_0.UpdateService";
5198d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
5208d1b46d7Szhanghch05             asyncResp->res.jsonValue["Id"] = "UpdateService";
5217e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
5227e860f15SJohn Edward Broadbent                 "Service for Software Update";
5238d1b46d7Szhanghch05             asyncResp->res.jsonValue["Name"] = "Update Service";
5247e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["HttpPushUri"] =
5257e860f15SJohn Edward Broadbent                 "/redfish/v1/UpdateService";
5260f74e643SEd Tanous             // UpdateService cannot be disabled
5278d1b46d7Szhanghch05             asyncResp->res.jsonValue["ServiceEnabled"] = true;
5288d1b46d7Szhanghch05             asyncResp->res.jsonValue["FirmwareInventory"] = {
5290f74e643SEd Tanous                 {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}};
530d61e5194STejas Patil             // Get the MaxImageSizeBytes
531d61e5194STejas Patil             asyncResp->res.jsonValue["MaxImageSizeBytes"] =
532d61e5194STejas Patil                 bmcwebHttpReqBodyLimitMb * 1024 * 1024;
533d61e5194STejas Patil 
5340554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
5350554c984SAndrew Geissler             // Update Actions object.
5360554c984SAndrew Geissler             nlohmann::json& updateSvcSimpleUpdate =
5377e860f15SJohn Edward Broadbent                 asyncResp->res
5387e860f15SJohn Edward Broadbent                     .jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
5390554c984SAndrew Geissler             updateSvcSimpleUpdate["target"] =
5400554c984SAndrew Geissler                 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
5417e860f15SJohn Edward Broadbent             updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] =
5427e860f15SJohn Edward Broadbent                 {"TFTP"};
5430554c984SAndrew Geissler #endif
544274dfe62SJayashankar Padath             // Get the current ApplyTime value
545274dfe62SJayashankar Padath             crow::connections::systemBus->async_method_call(
5468d1b46d7Szhanghch05                 [asyncResp](const boost::system::error_code ec,
547*168e20c1SEd Tanous                             const dbus::utility::DbusVariantType& applyTime) {
548274dfe62SJayashankar Padath                     if (ec)
549274dfe62SJayashankar Padath                     {
550274dfe62SJayashankar Padath                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
5518d1b46d7Szhanghch05                         messages::internalError(asyncResp->res);
552274dfe62SJayashankar Padath                         return;
553274dfe62SJayashankar Padath                     }
554274dfe62SJayashankar Padath 
555274dfe62SJayashankar Padath                     const std::string* s = std::get_if<std::string>(&applyTime);
556274dfe62SJayashankar Padath                     if (s == nullptr)
557274dfe62SJayashankar Padath                     {
558274dfe62SJayashankar Padath                         return;
559274dfe62SJayashankar Padath                     }
560274dfe62SJayashankar Padath                     // Store the ApplyTime Value
5610fda0f12SGeorge Liu                     if (*s ==
5620fda0f12SGeorge Liu                         "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate")
563274dfe62SJayashankar Padath                     {
5648d1b46d7Szhanghch05                         asyncResp->res
5657e860f15SJohn Edward Broadbent                             .jsonValue["HttpPushUriOptions"]
5667e860f15SJohn Edward Broadbent                                       ["HttpPushUriApplyTime"]["ApplyTime"] =
5677e860f15SJohn Edward Broadbent                             "Immediate";
568274dfe62SJayashankar Padath                     }
5690fda0f12SGeorge Liu                     else if (
5700fda0f12SGeorge Liu                         *s ==
5710fda0f12SGeorge Liu                         "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset")
572274dfe62SJayashankar Padath                     {
5738d1b46d7Szhanghch05                         asyncResp->res
5747e860f15SJohn Edward Broadbent                             .jsonValue["HttpPushUriOptions"]
5757e860f15SJohn Edward Broadbent                                       ["HttpPushUriApplyTime"]["ApplyTime"] =
5767e860f15SJohn Edward Broadbent                             "OnReset";
577274dfe62SJayashankar Padath                     }
578274dfe62SJayashankar Padath                 },
579274dfe62SJayashankar Padath                 "xyz.openbmc_project.Settings",
580274dfe62SJayashankar Padath                 "/xyz/openbmc_project/software/apply_time",
581274dfe62SJayashankar Padath                 "org.freedesktop.DBus.Properties", "Get",
582274dfe62SJayashankar Padath                 "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime");
5837e860f15SJohn Edward Broadbent         });
5847e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
585ed398213SEd Tanous         .privileges(redfish::privileges::patchUpdateService)
5860fda0f12SGeorge Liu         .methods(
5870fda0f12SGeorge Liu             boost::beast::http::verb::
5880fda0f12SGeorge Liu                 patch)([](const crow::Request& req,
5897e860f15SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
590fa1a5a38SJayashankar Padath             BMCWEB_LOG_DEBUG << "doPatch...";
591fa1a5a38SJayashankar Padath 
592274dfe62SJayashankar Padath             std::optional<nlohmann::json> pushUriOptions;
5930fda0f12SGeorge Liu             if (!json_util::readJson(req, asyncResp->res, "HttpPushUriOptions",
5940fda0f12SGeorge Liu                                      pushUriOptions))
595fa1a5a38SJayashankar Padath             {
596fa1a5a38SJayashankar Padath                 return;
597fa1a5a38SJayashankar Padath             }
598fa1a5a38SJayashankar Padath 
599274dfe62SJayashankar Padath             if (pushUriOptions)
600274dfe62SJayashankar Padath             {
601274dfe62SJayashankar Padath                 std::optional<nlohmann::json> pushUriApplyTime;
6028d1b46d7Szhanghch05                 if (!json_util::readJson(*pushUriOptions, asyncResp->res,
6037e860f15SJohn Edward Broadbent                                          "HttpPushUriApplyTime",
6047e860f15SJohn Edward Broadbent                                          pushUriApplyTime))
605274dfe62SJayashankar Padath                 {
606274dfe62SJayashankar Padath                     return;
607274dfe62SJayashankar Padath                 }
608274dfe62SJayashankar Padath 
609274dfe62SJayashankar Padath                 if (pushUriApplyTime)
610274dfe62SJayashankar Padath                 {
611274dfe62SJayashankar Padath                     std::optional<std::string> applyTime;
6120fda0f12SGeorge Liu                     if (!json_util::readJson(*pushUriApplyTime, asyncResp->res,
6130fda0f12SGeorge Liu                                              "ApplyTime", applyTime))
614274dfe62SJayashankar Padath                     {
615274dfe62SJayashankar Padath                         return;
616274dfe62SJayashankar Padath                     }
617274dfe62SJayashankar Padath 
618274dfe62SJayashankar Padath                     if (applyTime)
619fa1a5a38SJayashankar Padath                     {
620fa1a5a38SJayashankar Padath                         std::string applyTimeNewVal;
621fa1a5a38SJayashankar Padath                         if (applyTime == "Immediate")
622fa1a5a38SJayashankar Padath                         {
623274dfe62SJayashankar Padath                             applyTimeNewVal =
6240fda0f12SGeorge Liu                                 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate";
625fa1a5a38SJayashankar Padath                         }
626274dfe62SJayashankar Padath                         else if (applyTime == "OnReset")
627274dfe62SJayashankar Padath                         {
628274dfe62SJayashankar Padath                             applyTimeNewVal =
6290fda0f12SGeorge Liu                                 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
630274dfe62SJayashankar Padath                         }
631fa1a5a38SJayashankar Padath                         else
632fa1a5a38SJayashankar Padath                         {
633274dfe62SJayashankar Padath                             BMCWEB_LOG_INFO
6340fda0f12SGeorge Liu                                 << "ApplyTime value is not in the list of acceptable values";
635274dfe62SJayashankar Padath                             messages::propertyValueNotInList(
636274dfe62SJayashankar Padath                                 asyncResp->res, *applyTime, "ApplyTime");
637274dfe62SJayashankar Padath                             return;
638fa1a5a38SJayashankar Padath                         }
639fa1a5a38SJayashankar Padath 
640fa1a5a38SJayashankar Padath                         // Set the requested image apply time value
641fa1a5a38SJayashankar Padath                         crow::connections::systemBus->async_method_call(
6420fda0f12SGeorge Liu                             [asyncResp](const boost::system::error_code ec) {
643fa1a5a38SJayashankar Padath                                 if (ec)
644fa1a5a38SJayashankar Padath                                 {
6457e860f15SJohn Edward Broadbent                                     BMCWEB_LOG_ERROR
6467e860f15SJohn Edward Broadbent                                         << "D-Bus responses error: " << ec;
647fa1a5a38SJayashankar Padath                                     messages::internalError(asyncResp->res);
648fa1a5a38SJayashankar Padath                                     return;
649fa1a5a38SJayashankar Padath                                 }
650fa1a5a38SJayashankar Padath                                 messages::success(asyncResp->res);
651fa1a5a38SJayashankar Padath                             },
652fa1a5a38SJayashankar Padath                             "xyz.openbmc_project.Settings",
653fa1a5a38SJayashankar Padath                             "/xyz/openbmc_project/software/apply_time",
654fa1a5a38SJayashankar Padath                             "org.freedesktop.DBus.Properties", "Set",
655274dfe62SJayashankar Padath                             "xyz.openbmc_project.Software.ApplyTime",
656274dfe62SJayashankar Padath                             "RequestedApplyTime",
657*168e20c1SEd Tanous                             dbus::utility::DbusVariantType{applyTimeNewVal});
658fa1a5a38SJayashankar Padath                     }
659274dfe62SJayashankar Padath                 }
660fa1a5a38SJayashankar Padath             }
6617e860f15SJohn Edward Broadbent         });
6627e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
663ed398213SEd Tanous         .privileges(redfish::privileges::postUpdateService)
6647e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
6657e860f15SJohn Edward Broadbent             [](const crow::Request& req,
6667e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
667acb7cfb4SJennifer Lee                 BMCWEB_LOG_DEBUG << "doPost...";
668acb7cfb4SJennifer Lee 
66986adcd6dSAndrew Geissler                 // Setup callback for when new software detected
6704cde5d90SJames Feist                 monitorForSoftwareAvailable(asyncResp, req,
6714cde5d90SJames Feist                                             "/redfish/v1/UpdateService");
672acb7cfb4SJennifer Lee 
6737e860f15SJohn Edward Broadbent                 std::string filepath("/tmp/images/" +
6747e860f15SJohn Edward Broadbent                                      boost::uuids::to_string(
6757e860f15SJohn Edward Broadbent                                          boost::uuids::random_generator()()));
676acb7cfb4SJennifer Lee                 BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
6777e860f15SJohn Edward Broadbent                 std::ofstream out(filepath, std::ofstream::out |
6787e860f15SJohn Edward Broadbent                                                 std::ofstream::binary |
679acb7cfb4SJennifer Lee                                                 std::ofstream::trunc);
680acb7cfb4SJennifer Lee                 out << req.body;
681acb7cfb4SJennifer Lee                 out.close();
682acb7cfb4SJennifer Lee                 BMCWEB_LOG_DEBUG << "file upload complete!!";
6837e860f15SJohn Edward Broadbent             });
684729dae72SJennifer Lee }
685729dae72SJennifer Lee 
6867e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventoryCollection(App& app)
6871abe55efSEd Tanous {
6887e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/")
689ed398213SEd Tanous         .privileges(redfish::privileges::getSoftwareInventoryCollection)
6900fda0f12SGeorge Liu         .methods(
6910fda0f12SGeorge Liu             boost::beast::http::verb::
6920fda0f12SGeorge Liu                 get)([](const crow::Request&,
6937e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
6948d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.type"] =
6950f74e643SEd Tanous                 "#SoftwareInventoryCollection.SoftwareInventoryCollection";
6968d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] =
6970f74e643SEd Tanous                 "/redfish/v1/UpdateService/FirmwareInventory";
6980fda0f12SGeorge Liu             asyncResp->res.jsonValue["Name"] = "Software Inventory Collection";
699c711bf86SEd Tanous 
700c711bf86SEd Tanous             crow::connections::systemBus->async_method_call(
701c711bf86SEd Tanous                 [asyncResp](
702c711bf86SEd Tanous                     const boost::system::error_code ec,
7030fda0f12SGeorge Liu                     const std::vector<
7040fda0f12SGeorge Liu                         std::pair<std::string,
7050fda0f12SGeorge Liu                                   std::vector<std::pair<
7060fda0f12SGeorge Liu                                       std::string, std::vector<std::string>>>>>&
7071214b7e7SGunnar Mills                         subtree) {
7081abe55efSEd Tanous                     if (ec)
7091abe55efSEd Tanous                     {
710f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
7116c4eb9deSJennifer Lee                         return;
712729dae72SJennifer Lee                     }
7137e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"] =
7147e860f15SJohn Edward Broadbent                         nlohmann::json::array();
715c711bf86SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] = 0;
7166c4eb9deSJennifer Lee 
7171abe55efSEd Tanous                     for (auto& obj : subtree)
7181abe55efSEd Tanous                     {
7192dfd18efSEd Tanous                         sdbusplus::message::object_path path(obj.first);
7202dfd18efSEd Tanous                         std::string swId = path.filename();
7212dfd18efSEd Tanous                         if (swId.empty())
722f4b65ab1SJennifer Lee                         {
723f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
724f4b65ab1SJennifer Lee                             BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
725f4b65ab1SJennifer Lee                             return;
726f4b65ab1SJennifer Lee                         }
727f4b65ab1SJennifer Lee 
728c711bf86SEd Tanous                         nlohmann::json& members =
729c711bf86SEd Tanous                             asyncResp->res.jsonValue["Members"];
730c711bf86SEd Tanous                         members.push_back(
7310fda0f12SGeorge Liu                             {{"@odata.id",
7320fda0f12SGeorge Liu                               "/redfish/v1/UpdateService/FirmwareInventory/" +
733f4b65ab1SJennifer Lee                                   swId}});
734e0dd8057SAndrew Geissler                         asyncResp->res.jsonValue["Members@odata.count"] =
735c711bf86SEd Tanous                             members.size();
7366c4eb9deSJennifer Lee                     }
737c711bf86SEd Tanous                 },
7387e860f15SJohn Edward Broadbent                 // Note that only firmware levels associated with a device
7397e860f15SJohn Edward Broadbent                 // are stored under /xyz/openbmc_project/software therefore
7407e860f15SJohn Edward Broadbent                 // to ensure only real FirmwareInventory items are returned,
7417e860f15SJohn Edward Broadbent                 // this full object path must be used here as input to
7427e860f15SJohn Edward Broadbent                 // mapper
743c711bf86SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
744c711bf86SEd Tanous                 "/xyz/openbmc_project/object_mapper",
7452830a9cfSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
7462830a9cfSAndrew Geissler                 "/xyz/openbmc_project/software", static_cast<int32_t>(0),
7477e860f15SJohn Edward Broadbent                 std::array<const char*, 1>{
7487e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Software.Version"});
7497e860f15SJohn Edward Broadbent         });
750729dae72SJennifer Lee }
75187d84729SAndrew Geissler /* Fill related item links (i.e. bmc, bios) in for inventory */
7527e860f15SJohn Edward Broadbent inline static void
7537e860f15SJohn Edward Broadbent     getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
75487d84729SAndrew Geissler                     const std::string& purpose)
75587d84729SAndrew Geissler {
75687d84729SAndrew Geissler     if (purpose == fw_util::bmcPurpose)
75787d84729SAndrew Geissler     {
7585af690f5SAbhishek Patel         nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
7595af690f5SAbhishek Patel         relatedItem.push_back({{"@odata.id", "/redfish/v1/Managers/bmc"}});
7607e860f15SJohn Edward Broadbent         aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
76187d84729SAndrew Geissler     }
76287d84729SAndrew Geissler     else if (purpose == fw_util::biosPurpose)
76387d84729SAndrew Geissler     {
7645af690f5SAbhishek Patel         nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
7655af690f5SAbhishek Patel         relatedItem.push_back(
766f723d733SGunnar Mills             {{"@odata.id", "/redfish/v1/Systems/system/Bios"}});
7677e860f15SJohn Edward Broadbent         aResp->res.jsonValue["Members@odata.count"] = relatedItem.size();
76887d84729SAndrew Geissler     }
76987d84729SAndrew Geissler     else
77087d84729SAndrew Geissler     {
77187d84729SAndrew Geissler         BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
77287d84729SAndrew Geissler     }
77387d84729SAndrew Geissler }
77487d84729SAndrew Geissler 
7757e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventory(App& app)
7761abe55efSEd Tanous {
7777e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/")
778ed398213SEd Tanous         .privileges(redfish::privileges::getSoftwareInventory)
7797e860f15SJohn Edward Broadbent         .methods(
7807e860f15SJohn Edward Broadbent             boost::beast::http::verb::get)([](const crow::Request&,
7817e860f15SJohn Edward Broadbent                                               const std::shared_ptr<
7827e860f15SJohn Edward Broadbent                                                   bmcweb::AsyncResp>& asyncResp,
7837e860f15SJohn Edward Broadbent                                               const std::string& param) {
7843ae837c9SEd Tanous             std::shared_ptr<std::string> swId =
7857e860f15SJohn Edward Broadbent                 std::make_shared<std::string>(param);
786c711bf86SEd Tanous 
7878d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] =
7883ae837c9SEd Tanous                 "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
789c711bf86SEd Tanous 
790c711bf86SEd Tanous             crow::connections::systemBus->async_method_call(
7913ae837c9SEd Tanous                 [asyncResp, swId](
792c711bf86SEd Tanous                     const boost::system::error_code ec,
7937e860f15SJohn Edward Broadbent                     const std::vector<
7947e860f15SJohn Edward Broadbent                         std::pair<std::string,
7957e860f15SJohn Edward Broadbent                                   std::vector<std::pair<
7961214b7e7SGunnar Mills                                       std::string, std::vector<std::string>>>>>&
7971214b7e7SGunnar Mills                         subtree) {
79855c7b7a2SEd Tanous                     BMCWEB_LOG_DEBUG << "doGet callback...";
7991abe55efSEd Tanous                     if (ec)
8001abe55efSEd Tanous                     {
801f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
8026c4eb9deSJennifer Lee                         return;
8036c4eb9deSJennifer Lee                     }
8046c4eb9deSJennifer Lee 
8056913228dSAndrew Geissler                     // Ensure we find our input swId, otherwise return an error
8066913228dSAndrew Geissler                     bool found = false;
8071abe55efSEd Tanous                     for (const std::pair<
8081abe55efSEd Tanous                              std::string,
8097e860f15SJohn Edward Broadbent                              std::vector<std::pair<
8107e860f15SJohn Edward Broadbent                                  std::string, std::vector<std::string>>>>& obj :
8117e860f15SJohn Edward Broadbent                          subtree)
8121abe55efSEd Tanous                     {
8133ae837c9SEd Tanous                         if (boost::ends_with(obj.first, *swId) != true)
8141abe55efSEd Tanous                         {
815acb7cfb4SJennifer Lee                             continue;
816acb7cfb4SJennifer Lee                         }
817acb7cfb4SJennifer Lee 
818f4b65ab1SJennifer Lee                         if (obj.second.size() < 1)
8191abe55efSEd Tanous                         {
820acb7cfb4SJennifer Lee                             continue;
821acb7cfb4SJennifer Lee                         }
8226c4eb9deSJennifer Lee 
8236913228dSAndrew Geissler                         found = true;
8247e860f15SJohn Edward Broadbent                         fw_util::getFwStatus(asyncResp, swId,
8257e860f15SJohn Edward Broadbent                                              obj.second[0].first);
826e0dd8057SAndrew Geissler 
82755c7b7a2SEd Tanous                         crow::connections::systemBus->async_method_call(
828*168e20c1SEd Tanous                             [asyncResp,
829*168e20c1SEd Tanous                              swId](const boost::system::error_code errorCode,
8301abe55efSEd Tanous                                    const boost::container::flat_map<
831*168e20c1SEd Tanous                                        std::string,
832*168e20c1SEd Tanous                                        dbus::utility::DbusVariantType>&
833*168e20c1SEd Tanous                                        propertiesList) {
83481ce609eSEd Tanous                                 if (errorCode)
8351abe55efSEd Tanous                                 {
836f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
8376c4eb9deSJennifer Lee                                     return;
8386c4eb9deSJennifer Lee                                 }
8391abe55efSEd Tanous                                 boost::container::flat_map<
840*168e20c1SEd Tanous                                     std::string,
841*168e20c1SEd Tanous                                     dbus::utility::DbusVariantType>::
842*168e20c1SEd Tanous                                     const_iterator it =
843*168e20c1SEd Tanous                                         propertiesList.find("Purpose");
8441abe55efSEd Tanous                                 if (it == propertiesList.end())
8451abe55efSEd Tanous                                 {
8461abe55efSEd Tanous                                     BMCWEB_LOG_DEBUG
8471abe55efSEd Tanous                                         << "Can't find property \"Purpose\"!";
848f12894f8SJason M. Bills                                     messages::propertyMissing(asyncResp->res,
849f12894f8SJason M. Bills                                                               "Purpose");
8506c4eb9deSJennifer Lee                                     return;
8516c4eb9deSJennifer Lee                                 }
8523ae837c9SEd Tanous                                 const std::string* swInvPurpose =
853abf2add6SEd Tanous                                     std::get_if<std::string>(&it->second);
8543ae837c9SEd Tanous                                 if (swInvPurpose == nullptr)
8551abe55efSEd Tanous                                 {
8560fda0f12SGeorge Liu                                     BMCWEB_LOG_DEBUG
8570fda0f12SGeorge Liu                                         << "wrong types for property\"Purpose\"!";
8587e860f15SJohn Edward Broadbent                                     messages::propertyValueTypeError(
8597e860f15SJohn Edward Broadbent                                         asyncResp->res, "", "Purpose");
860acb7cfb4SJennifer Lee                                     return;
861acb7cfb4SJennifer Lee                                 }
862c711bf86SEd Tanous 
8633ae837c9SEd Tanous                                 BMCWEB_LOG_DEBUG << "swInvPurpose = "
8643ae837c9SEd Tanous                                                  << *swInvPurpose;
865c711bf86SEd Tanous                                 it = propertiesList.find("Version");
8661abe55efSEd Tanous                                 if (it == propertiesList.end())
8671abe55efSEd Tanous                                 {
8681abe55efSEd Tanous                                     BMCWEB_LOG_DEBUG
8691abe55efSEd Tanous                                         << "Can't find property \"Version\"!";
870f12894f8SJason M. Bills                                     messages::propertyMissing(asyncResp->res,
871f12894f8SJason M. Bills                                                               "Version");
872c711bf86SEd Tanous                                     return;
873acb7cfb4SJennifer Lee                                 }
874acb7cfb4SJennifer Lee 
875f4b65ab1SJennifer Lee                                 BMCWEB_LOG_DEBUG << "Version found!";
876c711bf86SEd Tanous 
877f4b65ab1SJennifer Lee                                 const std::string* version =
878abf2add6SEd Tanous                                     std::get_if<std::string>(&it->second);
879f4b65ab1SJennifer Lee 
880f4b65ab1SJennifer Lee                                 if (version == nullptr)
8811abe55efSEd Tanous                                 {
8821abe55efSEd Tanous                                     BMCWEB_LOG_DEBUG
8831abe55efSEd Tanous                                         << "Can't find property \"Version\"!";
884f12894f8SJason M. Bills 
8857e860f15SJohn Edward Broadbent                                     messages::propertyValueTypeError(
8867e860f15SJohn Edward Broadbent                                         asyncResp->res, "", "Version");
8876c4eb9deSJennifer Lee                                     return;
8886c4eb9deSJennifer Lee                                 }
889c711bf86SEd Tanous                                 asyncResp->res.jsonValue["Version"] = *version;
8903ae837c9SEd Tanous                                 asyncResp->res.jsonValue["Id"] = *swId;
89154daabe7SAndrew Geissler 
89254daabe7SAndrew Geissler                                 // swInvPurpose is of format:
89354daabe7SAndrew Geissler                                 // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
894e2e96770SJames Feist                                 // Translate this to "ABC image"
895f23b7296SEd Tanous                                 size_t endDesc = swInvPurpose->rfind('.');
89654daabe7SAndrew Geissler                                 if (endDesc == std::string::npos)
89754daabe7SAndrew Geissler                                 {
89854daabe7SAndrew Geissler                                     messages::internalError(asyncResp->res);
89954daabe7SAndrew Geissler                                     return;
90054daabe7SAndrew Geissler                                 }
90154daabe7SAndrew Geissler                                 endDesc++;
90254daabe7SAndrew Geissler                                 if (endDesc >= swInvPurpose->size())
90354daabe7SAndrew Geissler                                 {
90454daabe7SAndrew Geissler                                     messages::internalError(asyncResp->res);
90554daabe7SAndrew Geissler                                     return;
90654daabe7SAndrew Geissler                                 }
90754daabe7SAndrew Geissler 
90854daabe7SAndrew Geissler                                 std::string formatDesc =
90954daabe7SAndrew Geissler                                     swInvPurpose->substr(endDesc);
91054daabe7SAndrew Geissler                                 asyncResp->res.jsonValue["Description"] =
911e2e96770SJames Feist                                     formatDesc + " image";
91287d84729SAndrew Geissler                                 getRelatedItems(asyncResp, *swInvPurpose);
9136c4eb9deSJennifer Lee                             },
914c711bf86SEd Tanous                             obj.second[0].first, obj.first,
915c711bf86SEd Tanous                             "org.freedesktop.DBus.Properties", "GetAll",
916c711bf86SEd Tanous                             "xyz.openbmc_project.Software.Version");
9176c4eb9deSJennifer Lee                     }
9186913228dSAndrew Geissler                     if (!found)
9196913228dSAndrew Geissler                     {
9207e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR
9217e860f15SJohn Edward Broadbent                             << "Input swID " + *swId + " not found!";
9226913228dSAndrew Geissler                         messages::resourceMissingAtURI(
9236913228dSAndrew Geissler                             asyncResp->res,
9247e860f15SJohn Edward Broadbent                             "/redfish/v1/UpdateService/FirmwareInventory/" +
9257e860f15SJohn Edward Broadbent                                 *swId);
9266913228dSAndrew Geissler                         return;
9276913228dSAndrew Geissler                     }
9284e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["@odata.type"] =
9294e68c45bSAyushi Smriti                         "#SoftwareInventory.v1_1_0.SoftwareInventory";
9304e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["Name"] = "Software Inventory";
9314e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
9323f8a743aSAppaRao Puli 
9333f8a743aSAppaRao Puli                     asyncResp->res.jsonValue["Updateable"] = false;
9343f8a743aSAppaRao Puli                     fw_util::getFwUpdateableStatus(asyncResp, swId);
935c711bf86SEd Tanous                 },
936c711bf86SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
937c711bf86SEd Tanous                 "/xyz/openbmc_project/object_mapper",
938271584abSEd Tanous                 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
939271584abSEd Tanous                 static_cast<int32_t>(0),
9407e860f15SJohn Edward Broadbent                 std::array<const char*, 1>{
9417e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Software.Version"});
9427e860f15SJohn Edward Broadbent         });
9436c4eb9deSJennifer Lee }
944729dae72SJennifer Lee 
945729dae72SJennifer Lee } // namespace redfish
946