xref: /openbmc/bmcweb/features/redfish/lib/update_service.hpp (revision 5af690f5a027159e2fd92bbe78d5e1cf98146bf6)
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 
18729dae72SJennifer Lee #include "node.hpp"
191abe55efSEd Tanous 
20729dae72SJennifer Lee #include <boost/container/flat_map.hpp>
2187d84729SAndrew Geissler #include <utils/fw_utils.hpp>
221214b7e7SGunnar Mills 
23abf2add6SEd Tanous #include <variant>
24729dae72SJennifer Lee 
251abe55efSEd Tanous namespace redfish
261abe55efSEd Tanous {
2727826b5fSEd Tanous 
280e7de46fSAndrew Geissler // Match signals added on software path
29acb7cfb4SJennifer Lee static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher;
304cde5d90SJames Feist static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateErrorMatcher;
310e7de46fSAndrew Geissler // Only allow one update at a time
320e7de46fSAndrew Geissler static bool fwUpdateInProgress = false;
3386adcd6dSAndrew Geissler // Timer for software available
34271584abSEd Tanous static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer;
3586adcd6dSAndrew Geissler 
3686adcd6dSAndrew Geissler static void cleanUp()
3786adcd6dSAndrew Geissler {
3886adcd6dSAndrew Geissler     fwUpdateInProgress = false;
3986adcd6dSAndrew Geissler     fwUpdateMatcher = nullptr;
404cde5d90SJames Feist     fwUpdateErrorMatcher = nullptr;
4186adcd6dSAndrew Geissler }
4286adcd6dSAndrew Geissler static void activateImage(const std::string& objPath,
4386adcd6dSAndrew Geissler                           const std::string& service)
4486adcd6dSAndrew Geissler {
4586adcd6dSAndrew Geissler     BMCWEB_LOG_DEBUG << "Activate image for " << objPath << " " << service;
4686adcd6dSAndrew Geissler     crow::connections::systemBus->async_method_call(
4781ce609eSEd Tanous         [](const boost::system::error_code errorCode) {
4881ce609eSEd Tanous             if (errorCode)
4986adcd6dSAndrew Geissler             {
5081ce609eSEd Tanous                 BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
5181ce609eSEd Tanous                 BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message();
5286adcd6dSAndrew Geissler             }
5386adcd6dSAndrew Geissler         },
5486adcd6dSAndrew Geissler         service, objPath, "org.freedesktop.DBus.Properties", "Set",
5586adcd6dSAndrew Geissler         "xyz.openbmc_project.Software.Activation", "RequestedActivation",
5686adcd6dSAndrew Geissler         std::variant<std::string>(
5786adcd6dSAndrew Geissler             "xyz.openbmc_project.Software.Activation.RequestedActivations."
5886adcd6dSAndrew Geissler             "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,
66fe306728SJames Feist                            const crow::Request& req)
6786adcd6dSAndrew Geissler {
6886adcd6dSAndrew Geissler     std::vector<std::pair<
6986adcd6dSAndrew Geissler         std::string,
7086adcd6dSAndrew Geissler         std::vector<std::pair<std::string, std::variant<std::string>>>>>
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(
86fe306728SJames Feist                 [objPath, asyncResp,
8781ce609eSEd Tanous                  req](const boost::system::error_code errorCode,
8886adcd6dSAndrew Geissler                       const std::vector<std::pair<
8986adcd6dSAndrew Geissler                           std::string, std::vector<std::string>>>& objInfo) {
9081ce609eSEd Tanous                     if (errorCode)
9186adcd6dSAndrew Geissler                     {
9281ce609eSEd Tanous                         BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
9386adcd6dSAndrew Geissler                         BMCWEB_LOG_DEBUG << "error msg = "
9481ce609eSEd Tanous                                          << errorCode.message();
950554c984SAndrew Geissler                         if (asyncResp)
960554c984SAndrew Geissler                         {
9786adcd6dSAndrew Geissler                             messages::internalError(asyncResp->res);
980554c984SAndrew Geissler                         }
9986adcd6dSAndrew Geissler                         cleanUp();
10086adcd6dSAndrew Geissler                         return;
10186adcd6dSAndrew Geissler                     }
10286adcd6dSAndrew Geissler                     // Ensure we only got one service back
10386adcd6dSAndrew Geissler                     if (objInfo.size() != 1)
10486adcd6dSAndrew Geissler                     {
10586adcd6dSAndrew Geissler                         BMCWEB_LOG_ERROR << "Invalid Object Size "
10686adcd6dSAndrew Geissler                                          << objInfo.size();
1070554c984SAndrew Geissler                         if (asyncResp)
1080554c984SAndrew Geissler                         {
10986adcd6dSAndrew Geissler                             messages::internalError(asyncResp->res);
1100554c984SAndrew Geissler                         }
11186adcd6dSAndrew Geissler                         cleanUp();
11286adcd6dSAndrew Geissler                         return;
11386adcd6dSAndrew Geissler                     }
11486adcd6dSAndrew Geissler                     // cancel timer only when
11586adcd6dSAndrew Geissler                     // xyz.openbmc_project.Software.Activation interface
11686adcd6dSAndrew Geissler                     // is added
11786adcd6dSAndrew Geissler                     fwAvailableTimer = nullptr;
11886adcd6dSAndrew Geissler 
11986adcd6dSAndrew Geissler                     activateImage(objPath.str, objInfo[0].first);
1200554c984SAndrew Geissler                     if (asyncResp)
1210554c984SAndrew Geissler                     {
12232898ceaSJames Feist                         std::shared_ptr<task::TaskData> task =
12332898ceaSJames Feist                             task::TaskData::createTask(
12432898ceaSJames Feist                                 [](boost::system::error_code ec,
12532898ceaSJames Feist                                    sdbusplus::message::message& msg,
1261214b7e7SGunnar Mills                                    const std::shared_ptr<task::TaskData>&
1271214b7e7SGunnar Mills                                        taskData) {
12832898ceaSJames Feist                                     if (ec)
12932898ceaSJames Feist                                     {
13032898ceaSJames Feist                                         return task::completed;
13132898ceaSJames Feist                                     }
13232898ceaSJames Feist 
13332898ceaSJames Feist                                     std::string iface;
13432898ceaSJames Feist                                     boost::container::flat_map<
135fd9ab9e1SJames Feist                                         std::string,
136fd9ab9e1SJames Feist                                         std::variant<std::string, uint8_t>>
13732898ceaSJames Feist                                         values;
138fd9ab9e1SJames Feist 
139fd9ab9e1SJames Feist                                     std::string index =
140fd9ab9e1SJames Feist                                         std::to_string(taskData->index);
14132898ceaSJames Feist                                     msg.read(iface, values);
142fd9ab9e1SJames Feist 
143fd9ab9e1SJames Feist                                     if (iface == "xyz.openbmc_project.Software."
144fd9ab9e1SJames Feist                                                  "Activation")
145fd9ab9e1SJames Feist                                     {
14632898ceaSJames Feist                                         auto findActivation =
14732898ceaSJames Feist                                             values.find("Activation");
14832898ceaSJames Feist                                         if (findActivation == values.end())
14932898ceaSJames Feist                                         {
15032898ceaSJames Feist                                             return !task::completed;
15132898ceaSJames Feist                                         }
15232898ceaSJames Feist                                         std::string* state =
15332898ceaSJames Feist                                             std::get_if<std::string>(
15432898ceaSJames Feist                                                 &(findActivation->second));
15532898ceaSJames Feist 
15632898ceaSJames Feist                                         if (state == nullptr)
15732898ceaSJames Feist                                         {
15832898ceaSJames Feist                                             taskData->messages.emplace_back(
15932898ceaSJames Feist                                                 messages::internalError());
16032898ceaSJames Feist                                             return task::completed;
16132898ceaSJames Feist                                         }
16232898ceaSJames Feist 
163fd9ab9e1SJames Feist                                         if (boost::ends_with(*state,
164fd9ab9e1SJames Feist                                                              "Invalid") ||
16532898ceaSJames Feist                                             boost::ends_with(*state, "Failed"))
16632898ceaSJames Feist                                         {
16732898ceaSJames Feist                                             taskData->state = "Exception";
16832898ceaSJames Feist                                             taskData->status = "Warning";
16932898ceaSJames Feist                                             taskData->messages.emplace_back(
170e5d5006bSJames Feist                                                 messages::taskAborted(index));
17132898ceaSJames Feist                                             return task::completed;
17232898ceaSJames Feist                                         }
17332898ceaSJames Feist 
17432898ceaSJames Feist                                         if (boost::ends_with(*state, "Staged"))
17532898ceaSJames Feist                                         {
176fd9ab9e1SJames Feist                                             taskData->state = "Stopping";
177fd9ab9e1SJames Feist                                             taskData->messages.emplace_back(
178fd9ab9e1SJames Feist                                                 messages::taskPaused(index));
179fd9ab9e1SJames Feist 
180fd9ab9e1SJames Feist                                             // its staged, set a long timer to
181fd9ab9e1SJames Feist                                             // allow them time to complete the
182fd9ab9e1SJames Feist                                             // update (probably cycle the
183fd9ab9e1SJames Feist                                             // system) if this expires then
184fd9ab9e1SJames Feist                                             // task will be cancelled
185fd9ab9e1SJames Feist                                             taskData->extendTimer(
186fd9ab9e1SJames Feist                                                 std::chrono::hours(5));
18732898ceaSJames Feist                                             return !task::completed;
18832898ceaSJames Feist                                         }
18932898ceaSJames Feist 
19032898ceaSJames Feist                                         if (boost::ends_with(*state, "Active"))
19132898ceaSJames Feist                                         {
19232898ceaSJames Feist                                             taskData->messages.emplace_back(
193fd9ab9e1SJames Feist                                                 messages::taskCompletedOK(
194fd9ab9e1SJames Feist                                                     index));
19532898ceaSJames Feist                                             taskData->state = "Completed";
19632898ceaSJames Feist                                             return task::completed;
19732898ceaSJames Feist                                         }
198fd9ab9e1SJames Feist                                     }
199fd9ab9e1SJames Feist                                     else if (iface ==
200fd9ab9e1SJames Feist                                              "xyz.openbmc_project.Software."
201fd9ab9e1SJames Feist                                              "ActivationProgress")
202fd9ab9e1SJames Feist                                     {
203fd9ab9e1SJames Feist                                         auto findProgress =
204fd9ab9e1SJames Feist                                             values.find("Progress");
205fd9ab9e1SJames Feist                                         if (findProgress == values.end())
206fd9ab9e1SJames Feist                                         {
207fd9ab9e1SJames Feist                                             return !task::completed;
208fd9ab9e1SJames Feist                                         }
209fd9ab9e1SJames Feist                                         uint8_t* progress =
210fd9ab9e1SJames Feist                                             std::get_if<uint8_t>(
211fd9ab9e1SJames Feist                                                 &(findProgress->second));
212fd9ab9e1SJames Feist 
213fd9ab9e1SJames Feist                                         if (progress == nullptr)
214fd9ab9e1SJames Feist                                         {
215fd9ab9e1SJames Feist                                             taskData->messages.emplace_back(
216fd9ab9e1SJames Feist                                                 messages::internalError());
217fd9ab9e1SJames Feist                                             return task::completed;
218fd9ab9e1SJames Feist                                         }
2196868ff50SGeorge Liu                                         taskData->percentComplete =
2206868ff50SGeorge Liu                                             static_cast<int>(*progress);
221fd9ab9e1SJames Feist                                         taskData->messages.emplace_back(
222fd9ab9e1SJames Feist                                             messages::taskProgressChanged(
223fd9ab9e1SJames Feist                                                 index, static_cast<size_t>(
224fd9ab9e1SJames Feist                                                            *progress)));
225fd9ab9e1SJames Feist 
226fd9ab9e1SJames Feist                                         // if we're getting status updates it's
227fd9ab9e1SJames Feist                                         // still alive, update timer
228fd9ab9e1SJames Feist                                         taskData->extendTimer(
229fd9ab9e1SJames Feist                                             std::chrono::minutes(5));
230fd9ab9e1SJames Feist                                     }
23132898ceaSJames Feist 
23232898ceaSJames Feist                                     // as firmware update often results in a
23332898ceaSJames Feist                                     // reboot, the task  may never "complete"
23432898ceaSJames Feist                                     // unless it is an error
23532898ceaSJames Feist 
23632898ceaSJames Feist                                     return !task::completed;
23732898ceaSJames Feist                                 },
23832898ceaSJames Feist                                 "type='signal',interface='org.freedesktop.DBus."
23932898ceaSJames Feist                                 "Properties',"
240fd9ab9e1SJames Feist                                 "member='PropertiesChanged',path='" +
24132898ceaSJames Feist                                     objPath.str + "'");
24232898ceaSJames Feist                         task->startTimer(std::chrono::minutes(5));
24332898ceaSJames Feist                         task->populateResp(asyncResp->res);
244fe306728SJames Feist                         task->payload.emplace(req);
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         });
30186adcd6dSAndrew Geissler 
302fe306728SJames Feist     auto callback = [asyncResp, req](sdbusplus::message::message& m) {
30386adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "Match fired";
304fe306728SJames Feist         softwareInterfaceAdded(asyncResp, m, req);
30586adcd6dSAndrew Geissler     };
30686adcd6dSAndrew Geissler 
30786adcd6dSAndrew Geissler     fwUpdateInProgress = true;
30886adcd6dSAndrew Geissler 
30986adcd6dSAndrew Geissler     fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
31086adcd6dSAndrew Geissler         *crow::connections::systemBus,
31186adcd6dSAndrew Geissler         "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
31286adcd6dSAndrew Geissler         "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
31386adcd6dSAndrew Geissler         callback);
3144cde5d90SJames Feist 
3154cde5d90SJames Feist     fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match::match>(
3164cde5d90SJames Feist         *crow::connections::systemBus,
3174cde5d90SJames Feist         "type='signal',member='PropertiesChanged',path_namespace='/xyz/"
3184cde5d90SJames Feist         "openbmc_project/logging/entry',"
3194cde5d90SJames Feist         "arg0='xyz.openbmc_project.Logging.Entry'",
3204cde5d90SJames Feist         [asyncResp, url](sdbusplus::message::message& m) {
3214cde5d90SJames Feist             BMCWEB_LOG_DEBUG << "Error Match fired";
3224cde5d90SJames Feist             boost::container::flat_map<std::string, std::variant<std::string>>
3234cde5d90SJames Feist                 values;
3244cde5d90SJames Feist             std::string objName;
3254cde5d90SJames Feist             m.read(objName, values);
3264cde5d90SJames Feist             auto find = values.find("Message");
3274cde5d90SJames Feist             if (find == values.end())
3284cde5d90SJames Feist             {
3294cde5d90SJames Feist                 return;
3304cde5d90SJames Feist             }
3314cde5d90SJames Feist             std::string* type = std::get_if<std::string>(&(find->second));
3324cde5d90SJames Feist             if (type == nullptr)
3334cde5d90SJames Feist             {
3344cde5d90SJames Feist                 return; // if this was our message, timeout will cover it
3354cde5d90SJames Feist             }
33688b3dd12SGunnar Mills             if (!boost::starts_with(*type, "xyz.openbmc_project.Software"))
3374cde5d90SJames Feist             {
3384cde5d90SJames Feist                 return;
3394cde5d90SJames Feist             }
3404cde5d90SJames Feist             if (*type ==
3414cde5d90SJames Feist                 "xyz.openbmc_project.Software.Image.Error.UnTarFailure")
3424cde5d90SJames Feist             {
3434cde5d90SJames Feist                 redfish::messages::invalidUpload(asyncResp->res, url,
3444cde5d90SJames Feist                                                  "Invalid archive");
3454cde5d90SJames Feist             }
3464cde5d90SJames Feist             else if (*type == "xyz.openbmc_project.Software.Image.Error."
3474cde5d90SJames Feist                               "ManifestFileFailure")
3484cde5d90SJames Feist             {
3494cde5d90SJames Feist                 redfish::messages::invalidUpload(asyncResp->res, url,
3504cde5d90SJames Feist                                                  "Invalid manifest");
3514cde5d90SJames Feist             }
3524cde5d90SJames Feist             else if (*type ==
3534cde5d90SJames Feist                      "xyz.openbmc_project.Software.Image.Error.ImageFailure")
3544cde5d90SJames Feist             {
3554cde5d90SJames Feist                 redfish::messages::invalidUpload(asyncResp->res, url,
3564cde5d90SJames Feist                                                  "Invalid image format");
3574cde5d90SJames Feist             }
35888b3dd12SGunnar Mills             else if (*type == "xyz.openbmc_project.Software.Version.Error."
35988b3dd12SGunnar Mills                               "AlreadyExists")
36088b3dd12SGunnar Mills             {
36188b3dd12SGunnar Mills 
36288b3dd12SGunnar Mills                 redfish::messages::invalidUpload(
36388b3dd12SGunnar Mills                     asyncResp->res, url, "Image version already exists");
36488b3dd12SGunnar Mills 
36588b3dd12SGunnar Mills                 redfish::messages::resourceAlreadyExists(
36688b3dd12SGunnar Mills                     asyncResp->res, "UpdateService.v1_4_0.UpdateService",
36788b3dd12SGunnar Mills                     "Version", "uploaded version");
36888b3dd12SGunnar Mills             }
3694cde5d90SJames Feist             else if (*type ==
3704cde5d90SJames Feist                      "xyz.openbmc_project.Software.Image.Error.BusyFailure")
3714cde5d90SJames Feist             {
3724cde5d90SJames Feist                 redfish::messages::resourceExhaustion(asyncResp->res, url);
3734cde5d90SJames Feist             }
3744cde5d90SJames Feist             else
3754cde5d90SJames Feist             {
3764cde5d90SJames Feist                 redfish::messages::internalError(asyncResp->res);
3774cde5d90SJames Feist             }
3784cde5d90SJames Feist             fwAvailableTimer = nullptr;
3794cde5d90SJames Feist         });
38086adcd6dSAndrew Geissler }
381729dae72SJennifer Lee 
3820554c984SAndrew Geissler /**
3830554c984SAndrew Geissler  * UpdateServiceActionsSimpleUpdate class supports handle POST method for
3840554c984SAndrew Geissler  * SimpleUpdate action.
3850554c984SAndrew Geissler  */
3860554c984SAndrew Geissler class UpdateServiceActionsSimpleUpdate : public Node
3870554c984SAndrew Geissler {
3880554c984SAndrew Geissler   public:
38952cc112dSEd Tanous     UpdateServiceActionsSimpleUpdate(App& app) :
3900554c984SAndrew Geissler         Node(app,
3910554c984SAndrew Geissler              "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
3920554c984SAndrew Geissler     {
3930554c984SAndrew Geissler         entityPrivileges = {
3940554c984SAndrew Geissler             {boost::beast::http::verb::get, {{"Login"}}},
3950554c984SAndrew Geissler             {boost::beast::http::verb::head, {{"Login"}}},
3960554c984SAndrew Geissler             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3970554c984SAndrew Geissler             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
3980554c984SAndrew Geissler             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
3990554c984SAndrew Geissler             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
4000554c984SAndrew Geissler     }
4010554c984SAndrew Geissler 
4020554c984SAndrew Geissler   private:
4038d1b46d7Szhanghch05     void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4048d1b46d7Szhanghch05                 const crow::Request& req,
405cb13a392SEd Tanous                 const std::vector<std::string>&) override
4060554c984SAndrew Geissler     {
4070554c984SAndrew Geissler         std::optional<std::string> transferProtocol;
4080554c984SAndrew Geissler         std::string imageURI;
4090554c984SAndrew Geissler 
4100554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost";
4110554c984SAndrew Geissler 
4120554c984SAndrew Geissler         // User can pass in both TransferProtocol and ImageURI parameters or
4134e0453b1SGunnar Mills         // they can pass in just the ImageURI with the transfer protocol
4144e0453b1SGunnar Mills         // embedded within it.
4150554c984SAndrew Geissler         // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
4160554c984SAndrew Geissler         // 2) ImageURI:tftp://1.1.1.1/myfile.bin
4170554c984SAndrew Geissler 
4180554c984SAndrew Geissler         if (!json_util::readJson(req, asyncResp->res, "TransferProtocol",
4190554c984SAndrew Geissler                                  transferProtocol, "ImageURI", imageURI))
4200554c984SAndrew Geissler         {
4210554c984SAndrew Geissler             BMCWEB_LOG_DEBUG
4220554c984SAndrew Geissler                 << "Missing TransferProtocol or ImageURI parameter";
4230554c984SAndrew Geissler             return;
4240554c984SAndrew Geissler         }
4250554c984SAndrew Geissler         if (!transferProtocol)
4260554c984SAndrew Geissler         {
4270554c984SAndrew Geissler             // Must be option 2
4280554c984SAndrew Geissler             // Verify ImageURI has transfer protocol in it
429f23b7296SEd Tanous             size_t separator = imageURI.find(':');
4300554c984SAndrew Geissler             if ((separator == std::string::npos) ||
4310554c984SAndrew Geissler                 ((separator + 1) > imageURI.size()))
4320554c984SAndrew Geissler             {
4330554c984SAndrew Geissler                 messages::actionParameterValueTypeError(
4340554c984SAndrew Geissler                     asyncResp->res, imageURI, "ImageURI",
4350554c984SAndrew Geissler                     "UpdateService.SimpleUpdate");
4360554c984SAndrew Geissler                 BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: "
4370554c984SAndrew Geissler                                  << imageURI;
4380554c984SAndrew Geissler                 return;
4390554c984SAndrew Geissler             }
4400554c984SAndrew Geissler             transferProtocol = imageURI.substr(0, separator);
4410554c984SAndrew Geissler             // Ensure protocol is upper case for a common comparison path below
4420554c984SAndrew Geissler             boost::to_upper(*transferProtocol);
4430554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Encoded transfer protocol "
4440554c984SAndrew Geissler                              << *transferProtocol;
4450554c984SAndrew Geissler 
4460554c984SAndrew Geissler             // Adjust imageURI to not have the protocol on it for parsing
4470554c984SAndrew Geissler             // below
4480554c984SAndrew Geissler             // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin
4490554c984SAndrew Geissler             imageURI = imageURI.substr(separator + 3);
4500554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI;
4510554c984SAndrew Geissler         }
4520554c984SAndrew Geissler 
4530554c984SAndrew Geissler         // OpenBMC currently only supports TFTP
4540554c984SAndrew Geissler         if (*transferProtocol != "TFTP")
4550554c984SAndrew Geissler         {
4560554c984SAndrew Geissler             messages::actionParameterNotSupported(asyncResp->res,
4570554c984SAndrew Geissler                                                   "TransferProtocol",
4580554c984SAndrew Geissler                                                   "UpdateService.SimpleUpdate");
4590554c984SAndrew Geissler             BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: "
4600554c984SAndrew Geissler                              << *transferProtocol;
4610554c984SAndrew Geissler             return;
4620554c984SAndrew Geissler         }
4630554c984SAndrew Geissler 
4640554c984SAndrew Geissler         // Format should be <IP or Hostname>/<file> for imageURI
465f23b7296SEd Tanous         size_t separator = imageURI.find('/');
4660554c984SAndrew Geissler         if ((separator == std::string::npos) ||
4670554c984SAndrew Geissler             ((separator + 1) > imageURI.size()))
4680554c984SAndrew Geissler         {
4690554c984SAndrew Geissler             messages::actionParameterValueTypeError(
4700554c984SAndrew Geissler                 asyncResp->res, imageURI, "ImageURI",
4710554c984SAndrew Geissler                 "UpdateService.SimpleUpdate");
4720554c984SAndrew Geissler             BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI;
4730554c984SAndrew Geissler             return;
4740554c984SAndrew Geissler         }
4750554c984SAndrew Geissler 
4760554c984SAndrew Geissler         std::string tftpServer = imageURI.substr(0, separator);
4770554c984SAndrew Geissler         std::string fwFile = imageURI.substr(separator + 1);
4780554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile;
4790554c984SAndrew Geissler 
4800554c984SAndrew Geissler         // Setup callback for when new software detected
4812618d5e3SGunnar Mills         // Give TFTP 10 minutes to complete
4824cde5d90SJames Feist         monitorForSoftwareAvailable(
483d7a596bdSAlbert Zhang             asyncResp, req,
4844cde5d90SJames Feist             "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate",
4852618d5e3SGunnar Mills             600);
4860554c984SAndrew Geissler 
4872618d5e3SGunnar Mills         // TFTP can take up to 10 minutes depending on image size and
4880554c984SAndrew Geissler         // connection speed. Return to caller as soon as the TFTP operation
4890554c984SAndrew Geissler         // has been started. The callback above will ensure the activate
4900554c984SAndrew Geissler         // is started once the download has completed
4910554c984SAndrew Geissler         redfish::messages::success(asyncResp->res);
4920554c984SAndrew Geissler 
4930554c984SAndrew Geissler         // Call TFTP service
4940554c984SAndrew Geissler         crow::connections::systemBus->async_method_call(
4950554c984SAndrew Geissler             [](const boost::system::error_code ec) {
4960554c984SAndrew Geissler                 if (ec)
4970554c984SAndrew Geissler                 {
4980554c984SAndrew Geissler                     // messages::internalError(asyncResp->res);
4990554c984SAndrew Geissler                     cleanUp();
5000554c984SAndrew Geissler                     BMCWEB_LOG_DEBUG << "error_code = " << ec;
5010554c984SAndrew Geissler                     BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
5020554c984SAndrew Geissler                 }
5030554c984SAndrew Geissler                 else
5040554c984SAndrew Geissler                 {
5050554c984SAndrew Geissler                     BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success";
5060554c984SAndrew Geissler                 }
5070554c984SAndrew Geissler             },
5080554c984SAndrew Geissler             "xyz.openbmc_project.Software.Download",
5090554c984SAndrew Geissler             "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP",
5100554c984SAndrew Geissler             "DownloadViaTFTP", fwFile, tftpServer);
5110554c984SAndrew Geissler 
5120554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost";
5130554c984SAndrew Geissler     }
5140554c984SAndrew Geissler };
5150554c984SAndrew Geissler 
5161abe55efSEd Tanous class UpdateService : public Node
5171abe55efSEd Tanous {
518729dae72SJennifer Lee   public:
51952cc112dSEd Tanous     UpdateService(App& app) : Node(app, "/redfish/v1/UpdateService/")
5201abe55efSEd Tanous     {
521729dae72SJennifer Lee         entityPrivileges = {
522729dae72SJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
523729dae72SJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
524729dae72SJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
525729dae72SJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
526729dae72SJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
527729dae72SJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
528729dae72SJennifer Lee     }
529729dae72SJennifer Lee 
530729dae72SJennifer Lee   private:
5318d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5328d1b46d7Szhanghch05                const crow::Request&, const std::vector<std::string>&) override
5331abe55efSEd Tanous     {
5348d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
5358d1b46d7Szhanghch05             "#UpdateService.v1_4_0.UpdateService";
5368d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
5378d1b46d7Szhanghch05         asyncResp->res.jsonValue["Id"] = "UpdateService";
5388d1b46d7Szhanghch05         asyncResp->res.jsonValue["Description"] = "Service for Software Update";
5398d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Update Service";
5408d1b46d7Szhanghch05         asyncResp->res.jsonValue["HttpPushUri"] = "/redfish/v1/UpdateService";
5410f74e643SEd Tanous         // UpdateService cannot be disabled
5428d1b46d7Szhanghch05         asyncResp->res.jsonValue["ServiceEnabled"] = true;
5438d1b46d7Szhanghch05         asyncResp->res.jsonValue["FirmwareInventory"] = {
5440f74e643SEd Tanous             {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}};
5450554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
5460554c984SAndrew Geissler         // Update Actions object.
5470554c984SAndrew Geissler         nlohmann::json& updateSvcSimpleUpdate =
5488d1b46d7Szhanghch05             asyncResp->res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
5490554c984SAndrew Geissler         updateSvcSimpleUpdate["target"] =
5500554c984SAndrew Geissler             "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
5510554c984SAndrew Geissler         updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = {
5520554c984SAndrew Geissler             "TFTP"};
5530554c984SAndrew Geissler #endif
554274dfe62SJayashankar Padath         // Get the current ApplyTime value
555274dfe62SJayashankar Padath         crow::connections::systemBus->async_method_call(
5568d1b46d7Szhanghch05             [asyncResp](const boost::system::error_code ec,
557274dfe62SJayashankar Padath                         const std::variant<std::string>& applyTime) {
558274dfe62SJayashankar Padath                 if (ec)
559274dfe62SJayashankar Padath                 {
560274dfe62SJayashankar Padath                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
5618d1b46d7Szhanghch05                     messages::internalError(asyncResp->res);
562274dfe62SJayashankar Padath                     return;
563274dfe62SJayashankar Padath                 }
564274dfe62SJayashankar Padath 
565274dfe62SJayashankar Padath                 const std::string* s = std::get_if<std::string>(&applyTime);
566274dfe62SJayashankar Padath                 if (s == nullptr)
567274dfe62SJayashankar Padath                 {
568274dfe62SJayashankar Padath                     return;
569274dfe62SJayashankar Padath                 }
570274dfe62SJayashankar Padath                 // Store the ApplyTime Value
571274dfe62SJayashankar Padath                 if (*s == "xyz.openbmc_project.Software.ApplyTime."
572274dfe62SJayashankar Padath                           "RequestedApplyTimes.Immediate")
573274dfe62SJayashankar Padath                 {
5748d1b46d7Szhanghch05                     asyncResp->res
5758d1b46d7Szhanghch05                         .jsonValue["HttpPushUriOptions"]["HttpPushUriApplyTime"]
5768d1b46d7Szhanghch05                                   ["ApplyTime"] = "Immediate";
577274dfe62SJayashankar Padath                 }
578274dfe62SJayashankar Padath                 else if (*s == "xyz.openbmc_project.Software.ApplyTime."
579274dfe62SJayashankar Padath                                "RequestedApplyTimes.OnReset")
580274dfe62SJayashankar Padath                 {
5818d1b46d7Szhanghch05                     asyncResp->res
5828d1b46d7Szhanghch05                         .jsonValue["HttpPushUriOptions"]["HttpPushUriApplyTime"]
5838d1b46d7Szhanghch05                                   ["ApplyTime"] = "OnReset";
584274dfe62SJayashankar Padath                 }
585274dfe62SJayashankar Padath             },
586274dfe62SJayashankar Padath             "xyz.openbmc_project.Settings",
587274dfe62SJayashankar Padath             "/xyz/openbmc_project/software/apply_time",
588274dfe62SJayashankar Padath             "org.freedesktop.DBus.Properties", "Get",
589274dfe62SJayashankar Padath             "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime");
590729dae72SJennifer Lee     }
5910e7de46fSAndrew Geissler 
5928d1b46d7Szhanghch05     void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5938d1b46d7Szhanghch05                  const crow::Request& req,
594cb13a392SEd Tanous                  const std::vector<std::string>&) override
595fa1a5a38SJayashankar Padath     {
596fa1a5a38SJayashankar Padath         BMCWEB_LOG_DEBUG << "doPatch...";
597fa1a5a38SJayashankar Padath 
598274dfe62SJayashankar Padath         std::optional<nlohmann::json> pushUriOptions;
5998d1b46d7Szhanghch05         if (!json_util::readJson(req, asyncResp->res, "HttpPushUriOptions",
600274dfe62SJayashankar Padath                                  pushUriOptions))
601fa1a5a38SJayashankar Padath         {
602fa1a5a38SJayashankar Padath             return;
603fa1a5a38SJayashankar Padath         }
604fa1a5a38SJayashankar Padath 
605274dfe62SJayashankar Padath         if (pushUriOptions)
606274dfe62SJayashankar Padath         {
607274dfe62SJayashankar Padath             std::optional<nlohmann::json> pushUriApplyTime;
6088d1b46d7Szhanghch05             if (!json_util::readJson(*pushUriOptions, asyncResp->res,
609274dfe62SJayashankar Padath                                      "HttpPushUriApplyTime", pushUriApplyTime))
610274dfe62SJayashankar Padath             {
611274dfe62SJayashankar Padath                 return;
612274dfe62SJayashankar Padath             }
613274dfe62SJayashankar Padath 
614274dfe62SJayashankar Padath             if (pushUriApplyTime)
615274dfe62SJayashankar Padath             {
616274dfe62SJayashankar Padath                 std::optional<std::string> applyTime;
6178d1b46d7Szhanghch05                 if (!json_util::readJson(*pushUriApplyTime, asyncResp->res,
6188d1b46d7Szhanghch05                                          "ApplyTime", applyTime))
619274dfe62SJayashankar Padath                 {
620274dfe62SJayashankar Padath                     return;
621274dfe62SJayashankar Padath                 }
622274dfe62SJayashankar Padath 
623274dfe62SJayashankar Padath                 if (applyTime)
624fa1a5a38SJayashankar Padath                 {
625fa1a5a38SJayashankar Padath                     std::string applyTimeNewVal;
626fa1a5a38SJayashankar Padath                     if (applyTime == "Immediate")
627fa1a5a38SJayashankar Padath                     {
628274dfe62SJayashankar Padath                         applyTimeNewVal =
629274dfe62SJayashankar Padath                             "xyz.openbmc_project.Software.ApplyTime."
630fa1a5a38SJayashankar Padath                             "RequestedApplyTimes.Immediate";
631fa1a5a38SJayashankar Padath                     }
632274dfe62SJayashankar Padath                     else if (applyTime == "OnReset")
633274dfe62SJayashankar Padath                     {
634274dfe62SJayashankar Padath                         applyTimeNewVal =
635274dfe62SJayashankar Padath                             "xyz.openbmc_project.Software.ApplyTime."
636274dfe62SJayashankar Padath                             "RequestedApplyTimes.OnReset";
637274dfe62SJayashankar Padath                     }
638fa1a5a38SJayashankar Padath                     else
639fa1a5a38SJayashankar Padath                     {
640274dfe62SJayashankar Padath                         BMCWEB_LOG_INFO
641274dfe62SJayashankar Padath                             << "ApplyTime value is not in the list of "
642274dfe62SJayashankar Padath                                "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(
650fa1a5a38SJayashankar Padath                         [asyncResp](const boost::system::error_code ec) {
651fa1a5a38SJayashankar Padath                             if (ec)
652fa1a5a38SJayashankar Padath                             {
653274dfe62SJayashankar Padath                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
654274dfe62SJayashankar Padath                                                  << 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",
665fa1a5a38SJayashankar Padath                         std::variant<std::string>{applyTimeNewVal});
666fa1a5a38SJayashankar Padath                 }
667274dfe62SJayashankar Padath             }
668fa1a5a38SJayashankar Padath         }
669fa1a5a38SJayashankar Padath     }
670fa1a5a38SJayashankar Padath 
6718d1b46d7Szhanghch05     void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6728d1b46d7Szhanghch05                 const crow::Request& req,
673cb13a392SEd Tanous                 const std::vector<std::string>&) override
6741abe55efSEd Tanous     {
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 
681acb7cfb4SJennifer Lee         std::string filepath(
682acb7cfb4SJennifer Lee             "/tmp/images/" +
683acb7cfb4SJennifer Lee             boost::uuids::to_string(boost::uuids::random_generator()()));
684acb7cfb4SJennifer Lee         BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
685acb7cfb4SJennifer Lee         std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
686acb7cfb4SJennifer Lee                                         std::ofstream::trunc);
687acb7cfb4SJennifer Lee         out << req.body;
688acb7cfb4SJennifer Lee         out.close();
689acb7cfb4SJennifer Lee         BMCWEB_LOG_DEBUG << "file upload complete!!";
690acb7cfb4SJennifer Lee     }
691729dae72SJennifer Lee };
692729dae72SJennifer Lee 
6931abe55efSEd Tanous class SoftwareInventoryCollection : public Node
6941abe55efSEd Tanous {
695729dae72SJennifer Lee   public:
69652cc112dSEd Tanous     SoftwareInventoryCollection(App& app) :
6971abe55efSEd Tanous         Node(app, "/redfish/v1/UpdateService/FirmwareInventory/")
6981abe55efSEd Tanous     {
699729dae72SJennifer Lee         entityPrivileges = {
700729dae72SJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
701729dae72SJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
702729dae72SJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
703729dae72SJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
704729dae72SJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
705729dae72SJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
706729dae72SJennifer Lee     }
707729dae72SJennifer Lee 
708729dae72SJennifer Lee   private:
7098d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7108d1b46d7Szhanghch05                const crow::Request&, const std::vector<std::string>&) override
7111abe55efSEd Tanous     {
7128d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
7130f74e643SEd Tanous             "#SoftwareInventoryCollection.SoftwareInventoryCollection";
7148d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
7150f74e643SEd Tanous             "/redfish/v1/UpdateService/FirmwareInventory";
7168d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Software Inventory Collection";
717c711bf86SEd Tanous 
718c711bf86SEd Tanous         crow::connections::systemBus->async_method_call(
719c711bf86SEd Tanous             [asyncResp](
720c711bf86SEd Tanous                 const boost::system::error_code ec,
7216c4eb9deSJennifer Lee                 const std::vector<std::pair<
7221abe55efSEd Tanous                     std::string, std::vector<std::pair<
7231214b7e7SGunnar Mills                                      std::string, std::vector<std::string>>>>>&
7241214b7e7SGunnar Mills                     subtree) {
7251abe55efSEd Tanous                 if (ec)
7261abe55efSEd Tanous                 {
727f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
7286c4eb9deSJennifer Lee                     return;
729729dae72SJennifer Lee                 }
730c711bf86SEd Tanous                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
731c711bf86SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
7326c4eb9deSJennifer Lee 
7331abe55efSEd Tanous                 for (auto& obj : subtree)
7341abe55efSEd Tanous                 {
7352dfd18efSEd Tanous                     sdbusplus::message::object_path path(obj.first);
7362dfd18efSEd Tanous                     std::string swId = path.filename();
7372dfd18efSEd Tanous                     if (swId.empty())
738f4b65ab1SJennifer Lee                     {
739f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
740f4b65ab1SJennifer Lee                         BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
741f4b65ab1SJennifer Lee                         return;
742f4b65ab1SJennifer Lee                     }
743f4b65ab1SJennifer Lee 
744c711bf86SEd Tanous                     nlohmann::json& members =
745c711bf86SEd Tanous                         asyncResp->res.jsonValue["Members"];
746c711bf86SEd Tanous                     members.push_back(
747f4b65ab1SJennifer Lee                         {{"@odata.id", "/redfish/v1/UpdateService/"
7481abe55efSEd Tanous                                        "FirmwareInventory/" +
749f4b65ab1SJennifer Lee                                            swId}});
750e0dd8057SAndrew Geissler                     asyncResp->res.jsonValue["Members@odata.count"] =
751c711bf86SEd Tanous                         members.size();
7526c4eb9deSJennifer Lee                 }
753c711bf86SEd Tanous             },
7542830a9cfSAndrew Geissler             // Note that only firmware levels associated with a device are
7552830a9cfSAndrew Geissler             // stored under /xyz/openbmc_project/software therefore to ensure
7562830a9cfSAndrew Geissler             // only real FirmwareInventory items are returned, this full object
7572830a9cfSAndrew Geissler             // path must be used here as input to mapper
758c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper",
759c711bf86SEd Tanous             "/xyz/openbmc_project/object_mapper",
7602830a9cfSAndrew Geissler             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
7612830a9cfSAndrew Geissler             "/xyz/openbmc_project/software", static_cast<int32_t>(0),
7621214b7e7SGunnar Mills             std::array<const char*, 1>{"xyz.openbmc_project.Software.Version"});
763729dae72SJennifer Lee     }
764729dae72SJennifer Lee };
765c711bf86SEd Tanous 
7661abe55efSEd Tanous class SoftwareInventory : public Node
7671abe55efSEd Tanous {
768729dae72SJennifer Lee   public:
76952cc112dSEd Tanous     SoftwareInventory(App& app) :
7701abe55efSEd Tanous         Node(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/",
7711abe55efSEd Tanous              std::string())
7721abe55efSEd Tanous     {
773729dae72SJennifer Lee         entityPrivileges = {
774729dae72SJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
775729dae72SJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
776729dae72SJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
777729dae72SJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
778729dae72SJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
779729dae72SJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
780729dae72SJennifer Lee     }
781729dae72SJennifer Lee 
782729dae72SJennifer Lee   private:
78387d84729SAndrew Geissler     /* Fill related item links (i.e. bmc, bios) in for inventory */
7848d1b46d7Szhanghch05     static void getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
78587d84729SAndrew Geissler                                 const std::string& purpose)
78687d84729SAndrew Geissler     {
78787d84729SAndrew Geissler         if (purpose == fw_util::bmcPurpose)
78887d84729SAndrew Geissler         {
789*5af690f5SAbhishek Patel             nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
790*5af690f5SAbhishek Patel             relatedItem.push_back({{"@odata.id", "/redfish/v1/Managers/bmc"}});
791*5af690f5SAbhishek Patel             aResp->res.jsonValue["RelatedItem@odata.count"] =
792*5af690f5SAbhishek Patel                 relatedItem.size();
79387d84729SAndrew Geissler         }
79487d84729SAndrew Geissler         else if (purpose == fw_util::biosPurpose)
79587d84729SAndrew Geissler         {
796*5af690f5SAbhishek Patel             nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
797*5af690f5SAbhishek Patel             relatedItem.push_back(
798f723d733SGunnar Mills                 {{"@odata.id", "/redfish/v1/Systems/system/Bios"}});
799*5af690f5SAbhishek Patel             aResp->res.jsonValue["RelatedItem@odata.count"] =
800*5af690f5SAbhishek Patel                 relatedItem.size();
80187d84729SAndrew Geissler         }
80287d84729SAndrew Geissler         else
80387d84729SAndrew Geissler         {
80487d84729SAndrew Geissler             BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
80587d84729SAndrew Geissler         }
80687d84729SAndrew Geissler     }
80787d84729SAndrew Geissler 
8088d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8098d1b46d7Szhanghch05                const crow::Request&,
8101abe55efSEd Tanous                const std::vector<std::string>& params) override
8111abe55efSEd Tanous     {
8121abe55efSEd Tanous         if (params.size() != 1)
8131abe55efSEd Tanous         {
8148d1b46d7Szhanghch05             messages::internalError(asyncResp->res);
8158d1b46d7Szhanghch05 
816729dae72SJennifer Lee             return;
817729dae72SJennifer Lee         }
818729dae72SJennifer Lee 
8193ae837c9SEd Tanous         std::shared_ptr<std::string> swId =
820c711bf86SEd Tanous             std::make_shared<std::string>(params[0]);
821c711bf86SEd Tanous 
8228d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
8233ae837c9SEd Tanous             "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
824c711bf86SEd Tanous 
825c711bf86SEd Tanous         crow::connections::systemBus->async_method_call(
8263ae837c9SEd Tanous             [asyncResp, swId](
827c711bf86SEd Tanous                 const boost::system::error_code ec,
8286c4eb9deSJennifer Lee                 const std::vector<std::pair<
8291abe55efSEd Tanous                     std::string, std::vector<std::pair<
8301214b7e7SGunnar Mills                                      std::string, std::vector<std::string>>>>>&
8311214b7e7SGunnar Mills                     subtree) {
83255c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "doGet callback...";
8331abe55efSEd Tanous                 if (ec)
8341abe55efSEd Tanous                 {
835f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
8366c4eb9deSJennifer Lee                     return;
8376c4eb9deSJennifer Lee                 }
8386c4eb9deSJennifer Lee 
8396913228dSAndrew Geissler                 // Ensure we find our input swId, otherwise return an error
8406913228dSAndrew Geissler                 bool found = false;
8411abe55efSEd Tanous                 for (const std::pair<
8421abe55efSEd Tanous                          std::string,
8431abe55efSEd Tanous                          std::vector<
8441214b7e7SGunnar Mills                              std::pair<std::string, std::vector<std::string>>>>&
8451214b7e7SGunnar Mills                          obj : subtree)
8461abe55efSEd Tanous                 {
8473ae837c9SEd Tanous                     if (boost::ends_with(obj.first, *swId) != true)
8481abe55efSEd Tanous                     {
849acb7cfb4SJennifer Lee                         continue;
850acb7cfb4SJennifer Lee                     }
851acb7cfb4SJennifer Lee 
852f4b65ab1SJennifer Lee                     if (obj.second.size() < 1)
8531abe55efSEd Tanous                     {
854acb7cfb4SJennifer Lee                         continue;
855acb7cfb4SJennifer Lee                     }
8566c4eb9deSJennifer Lee 
8576913228dSAndrew Geissler                     found = true;
858e0dd8057SAndrew Geissler                     fw_util::getFwStatus(asyncResp, swId, obj.second[0].first);
859e0dd8057SAndrew Geissler 
86055c7b7a2SEd Tanous                     crow::connections::systemBus->async_method_call(
8611abe55efSEd Tanous                         [asyncResp,
86281ce609eSEd Tanous                          swId](const boost::system::error_code errorCode,
8631abe55efSEd Tanous                                const boost::container::flat_map<
8641abe55efSEd Tanous                                    std::string, VariantType>& propertiesList) {
86581ce609eSEd Tanous                             if (errorCode)
8661abe55efSEd Tanous                             {
867f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
8686c4eb9deSJennifer Lee                                 return;
8696c4eb9deSJennifer Lee                             }
8701abe55efSEd Tanous                             boost::container::flat_map<
8711abe55efSEd Tanous                                 std::string, VariantType>::const_iterator it =
8726c4eb9deSJennifer Lee                                 propertiesList.find("Purpose");
8731abe55efSEd Tanous                             if (it == propertiesList.end())
8741abe55efSEd Tanous                             {
8751abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
8761abe55efSEd Tanous                                     << "Can't find property \"Purpose\"!";
877f12894f8SJason M. Bills                                 messages::propertyMissing(asyncResp->res,
878f12894f8SJason M. Bills                                                           "Purpose");
8796c4eb9deSJennifer Lee                                 return;
8806c4eb9deSJennifer Lee                             }
8813ae837c9SEd Tanous                             const std::string* swInvPurpose =
882abf2add6SEd Tanous                                 std::get_if<std::string>(&it->second);
8833ae837c9SEd Tanous                             if (swInvPurpose == nullptr)
8841abe55efSEd Tanous                             {
8851abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
8861abe55efSEd Tanous                                     << "wrong types for property\"Purpose\"!";
887f12894f8SJason M. Bills                                 messages::propertyValueTypeError(asyncResp->res,
888f12894f8SJason M. Bills                                                                  "", "Purpose");
889acb7cfb4SJennifer Lee                                 return;
890acb7cfb4SJennifer Lee                             }
891c711bf86SEd Tanous 
8923ae837c9SEd Tanous                             BMCWEB_LOG_DEBUG << "swInvPurpose = "
8933ae837c9SEd Tanous                                              << *swInvPurpose;
894c711bf86SEd Tanous                             it = propertiesList.find("Version");
8951abe55efSEd Tanous                             if (it == propertiesList.end())
8961abe55efSEd Tanous                             {
8971abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
8981abe55efSEd Tanous                                     << "Can't find property \"Version\"!";
899f12894f8SJason M. Bills                                 messages::propertyMissing(asyncResp->res,
900f12894f8SJason M. Bills                                                           "Version");
901c711bf86SEd Tanous                                 return;
902acb7cfb4SJennifer Lee                             }
903acb7cfb4SJennifer Lee 
904f4b65ab1SJennifer Lee                             BMCWEB_LOG_DEBUG << "Version found!";
905c711bf86SEd Tanous 
906f4b65ab1SJennifer Lee                             const std::string* version =
907abf2add6SEd Tanous                                 std::get_if<std::string>(&it->second);
908f4b65ab1SJennifer Lee 
909f4b65ab1SJennifer Lee                             if (version == nullptr)
9101abe55efSEd Tanous                             {
9111abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
9121abe55efSEd Tanous                                     << "Can't find property \"Version\"!";
913f12894f8SJason M. Bills 
914f12894f8SJason M. Bills                                 messages::propertyValueTypeError(asyncResp->res,
915f12894f8SJason M. Bills                                                                  "", "Version");
9166c4eb9deSJennifer Lee                                 return;
9176c4eb9deSJennifer Lee                             }
918c711bf86SEd Tanous                             asyncResp->res.jsonValue["Version"] = *version;
9193ae837c9SEd Tanous                             asyncResp->res.jsonValue["Id"] = *swId;
92054daabe7SAndrew Geissler 
92154daabe7SAndrew Geissler                             // swInvPurpose is of format:
92254daabe7SAndrew Geissler                             // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
923e2e96770SJames Feist                             // Translate this to "ABC image"
924f23b7296SEd Tanous                             size_t endDesc = swInvPurpose->rfind('.');
92554daabe7SAndrew Geissler                             if (endDesc == std::string::npos)
92654daabe7SAndrew Geissler                             {
92754daabe7SAndrew Geissler                                 messages::internalError(asyncResp->res);
92854daabe7SAndrew Geissler                                 return;
92954daabe7SAndrew Geissler                             }
93054daabe7SAndrew Geissler                             endDesc++;
93154daabe7SAndrew Geissler                             if (endDesc >= swInvPurpose->size())
93254daabe7SAndrew Geissler                             {
93354daabe7SAndrew Geissler                                 messages::internalError(asyncResp->res);
93454daabe7SAndrew Geissler                                 return;
93554daabe7SAndrew Geissler                             }
93654daabe7SAndrew Geissler 
93754daabe7SAndrew Geissler                             std::string formatDesc =
93854daabe7SAndrew Geissler                                 swInvPurpose->substr(endDesc);
93954daabe7SAndrew Geissler                             asyncResp->res.jsonValue["Description"] =
940e2e96770SJames Feist                                 formatDesc + " image";
94187d84729SAndrew Geissler                             getRelatedItems(asyncResp, *swInvPurpose);
9426c4eb9deSJennifer Lee                         },
943c711bf86SEd Tanous                         obj.second[0].first, obj.first,
944c711bf86SEd Tanous                         "org.freedesktop.DBus.Properties", "GetAll",
945c711bf86SEd Tanous                         "xyz.openbmc_project.Software.Version");
9466c4eb9deSJennifer Lee                 }
9476913228dSAndrew Geissler                 if (!found)
9486913228dSAndrew Geissler                 {
9496913228dSAndrew Geissler                     BMCWEB_LOG_ERROR << "Input swID " + *swId + " not found!";
9506913228dSAndrew Geissler                     messages::resourceMissingAtURI(
9516913228dSAndrew Geissler                         asyncResp->res,
9526913228dSAndrew Geissler                         "/redfish/v1/UpdateService/FirmwareInventory/" + *swId);
9536913228dSAndrew Geissler                     return;
9546913228dSAndrew Geissler                 }
9554e68c45bSAyushi Smriti                 asyncResp->res.jsonValue["@odata.type"] =
9564e68c45bSAyushi Smriti                     "#SoftwareInventory.v1_1_0.SoftwareInventory";
9574e68c45bSAyushi Smriti                 asyncResp->res.jsonValue["Name"] = "Software Inventory";
9584e68c45bSAyushi Smriti                 asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
9593f8a743aSAppaRao Puli 
9603f8a743aSAppaRao Puli                 asyncResp->res.jsonValue["Updateable"] = false;
9613f8a743aSAppaRao Puli                 fw_util::getFwUpdateableStatus(asyncResp, swId);
962c711bf86SEd Tanous             },
963c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper",
964c711bf86SEd Tanous             "/xyz/openbmc_project/object_mapper",
965271584abSEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
966271584abSEd Tanous             static_cast<int32_t>(0),
9671214b7e7SGunnar Mills             std::array<const char*, 1>{"xyz.openbmc_project.Software.Version"});
9686c4eb9deSJennifer Lee     }
969729dae72SJennifer Lee };
970729dae72SJennifer Lee 
971729dae72SJennifer Lee } // namespace redfish
972