xref: /openbmc/bmcweb/features/redfish/lib/update_service.hpp (revision d7a596bd7e449cdbfb92e961cc7049f80775e61f)
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
63b5a76932SEd Tanous static void softwareInterfaceAdded(const std::shared_ptr<AsyncResp>& asyncResp,
64fe306728SJames Feist                                    sdbusplus::message::message& m,
65fe306728SJames Feist                                    const crow::Request& req)
6686adcd6dSAndrew Geissler {
6786adcd6dSAndrew Geissler     std::vector<std::pair<
6886adcd6dSAndrew Geissler         std::string,
6986adcd6dSAndrew Geissler         std::vector<std::pair<std::string, std::variant<std::string>>>>>
7086adcd6dSAndrew Geissler         interfacesProperties;
7186adcd6dSAndrew Geissler 
7286adcd6dSAndrew Geissler     sdbusplus::message::object_path objPath;
7386adcd6dSAndrew Geissler 
7486adcd6dSAndrew Geissler     m.read(objPath, interfacesProperties);
7586adcd6dSAndrew Geissler 
7686adcd6dSAndrew Geissler     BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
7786adcd6dSAndrew Geissler     for (auto& interface : interfacesProperties)
7886adcd6dSAndrew Geissler     {
7986adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "interface = " << interface.first;
8086adcd6dSAndrew Geissler 
8186adcd6dSAndrew Geissler         if (interface.first == "xyz.openbmc_project.Software.Activation")
8286adcd6dSAndrew Geissler         {
8386adcd6dSAndrew Geissler             // Retrieve service and activate
8486adcd6dSAndrew Geissler             crow::connections::systemBus->async_method_call(
85fe306728SJames Feist                 [objPath, asyncResp,
8681ce609eSEd Tanous                  req](const boost::system::error_code errorCode,
8786adcd6dSAndrew Geissler                       const std::vector<std::pair<
8886adcd6dSAndrew Geissler                           std::string, std::vector<std::string>>>& objInfo) {
8981ce609eSEd Tanous                     if (errorCode)
9086adcd6dSAndrew Geissler                     {
9181ce609eSEd Tanous                         BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
9286adcd6dSAndrew Geissler                         BMCWEB_LOG_DEBUG << "error msg = "
9381ce609eSEd Tanous                                          << errorCode.message();
940554c984SAndrew Geissler                         if (asyncResp)
950554c984SAndrew Geissler                         {
9686adcd6dSAndrew Geissler                             messages::internalError(asyncResp->res);
970554c984SAndrew Geissler                         }
9886adcd6dSAndrew Geissler                         cleanUp();
9986adcd6dSAndrew Geissler                         return;
10086adcd6dSAndrew Geissler                     }
10186adcd6dSAndrew Geissler                     // Ensure we only got one service back
10286adcd6dSAndrew Geissler                     if (objInfo.size() != 1)
10386adcd6dSAndrew Geissler                     {
10486adcd6dSAndrew Geissler                         BMCWEB_LOG_ERROR << "Invalid Object Size "
10586adcd6dSAndrew Geissler                                          << objInfo.size();
1060554c984SAndrew Geissler                         if (asyncResp)
1070554c984SAndrew Geissler                         {
10886adcd6dSAndrew Geissler                             messages::internalError(asyncResp->res);
1090554c984SAndrew Geissler                         }
11086adcd6dSAndrew Geissler                         cleanUp();
11186adcd6dSAndrew Geissler                         return;
11286adcd6dSAndrew Geissler                     }
11386adcd6dSAndrew Geissler                     // cancel timer only when
11486adcd6dSAndrew Geissler                     // xyz.openbmc_project.Software.Activation interface
11586adcd6dSAndrew Geissler                     // is added
11686adcd6dSAndrew Geissler                     fwAvailableTimer = nullptr;
11786adcd6dSAndrew Geissler 
11886adcd6dSAndrew Geissler                     activateImage(objPath.str, objInfo[0].first);
1190554c984SAndrew Geissler                     if (asyncResp)
1200554c984SAndrew Geissler                     {
12132898ceaSJames Feist                         std::shared_ptr<task::TaskData> task =
12232898ceaSJames Feist                             task::TaskData::createTask(
12332898ceaSJames Feist                                 [](boost::system::error_code ec,
12432898ceaSJames Feist                                    sdbusplus::message::message& msg,
1251214b7e7SGunnar Mills                                    const std::shared_ptr<task::TaskData>&
1261214b7e7SGunnar Mills                                        taskData) {
12732898ceaSJames Feist                                     if (ec)
12832898ceaSJames Feist                                     {
12932898ceaSJames Feist                                         return task::completed;
13032898ceaSJames Feist                                     }
13132898ceaSJames Feist 
13232898ceaSJames Feist                                     std::string iface;
13332898ceaSJames Feist                                     boost::container::flat_map<
134fd9ab9e1SJames Feist                                         std::string,
135fd9ab9e1SJames Feist                                         std::variant<std::string, uint8_t>>
13632898ceaSJames Feist                                         values;
137fd9ab9e1SJames Feist 
138fd9ab9e1SJames Feist                                     std::string index =
139fd9ab9e1SJames Feist                                         std::to_string(taskData->index);
14032898ceaSJames Feist                                     msg.read(iface, values);
141fd9ab9e1SJames Feist 
142fd9ab9e1SJames Feist                                     if (iface == "xyz.openbmc_project.Software."
143fd9ab9e1SJames Feist                                                  "Activation")
144fd9ab9e1SJames Feist                                     {
14532898ceaSJames Feist                                         auto findActivation =
14632898ceaSJames Feist                                             values.find("Activation");
14732898ceaSJames Feist                                         if (findActivation == values.end())
14832898ceaSJames Feist                                         {
14932898ceaSJames Feist                                             return !task::completed;
15032898ceaSJames Feist                                         }
15132898ceaSJames Feist                                         std::string* state =
15232898ceaSJames Feist                                             std::get_if<std::string>(
15332898ceaSJames Feist                                                 &(findActivation->second));
15432898ceaSJames Feist 
15532898ceaSJames Feist                                         if (state == nullptr)
15632898ceaSJames Feist                                         {
15732898ceaSJames Feist                                             taskData->messages.emplace_back(
15832898ceaSJames Feist                                                 messages::internalError());
15932898ceaSJames Feist                                             return task::completed;
16032898ceaSJames Feist                                         }
16132898ceaSJames Feist 
162fd9ab9e1SJames Feist                                         if (boost::ends_with(*state,
163fd9ab9e1SJames Feist                                                              "Invalid") ||
16432898ceaSJames Feist                                             boost::ends_with(*state, "Failed"))
16532898ceaSJames Feist                                         {
16632898ceaSJames Feist                                             taskData->state = "Exception";
16732898ceaSJames Feist                                             taskData->status = "Warning";
16832898ceaSJames Feist                                             taskData->messages.emplace_back(
169e5d5006bSJames Feist                                                 messages::taskAborted(index));
17032898ceaSJames Feist                                             return task::completed;
17132898ceaSJames Feist                                         }
17232898ceaSJames Feist 
17332898ceaSJames Feist                                         if (boost::ends_with(*state, "Staged"))
17432898ceaSJames Feist                                         {
175fd9ab9e1SJames Feist                                             taskData->state = "Stopping";
176fd9ab9e1SJames Feist                                             taskData->messages.emplace_back(
177fd9ab9e1SJames Feist                                                 messages::taskPaused(index));
178fd9ab9e1SJames Feist 
179fd9ab9e1SJames Feist                                             // its staged, set a long timer to
180fd9ab9e1SJames Feist                                             // allow them time to complete the
181fd9ab9e1SJames Feist                                             // update (probably cycle the
182fd9ab9e1SJames Feist                                             // system) if this expires then
183fd9ab9e1SJames Feist                                             // task will be cancelled
184fd9ab9e1SJames Feist                                             taskData->extendTimer(
185fd9ab9e1SJames Feist                                                 std::chrono::hours(5));
18632898ceaSJames Feist                                             return !task::completed;
18732898ceaSJames Feist                                         }
18832898ceaSJames Feist 
18932898ceaSJames Feist                                         if (boost::ends_with(*state, "Active"))
19032898ceaSJames Feist                                         {
19132898ceaSJames Feist                                             taskData->messages.emplace_back(
192fd9ab9e1SJames Feist                                                 messages::taskCompletedOK(
193fd9ab9e1SJames Feist                                                     index));
19432898ceaSJames Feist                                             taskData->state = "Completed";
19532898ceaSJames Feist                                             return task::completed;
19632898ceaSJames Feist                                         }
197fd9ab9e1SJames Feist                                     }
198fd9ab9e1SJames Feist                                     else if (iface ==
199fd9ab9e1SJames Feist                                              "xyz.openbmc_project.Software."
200fd9ab9e1SJames Feist                                              "ActivationProgress")
201fd9ab9e1SJames Feist                                     {
202fd9ab9e1SJames Feist                                         auto findProgress =
203fd9ab9e1SJames Feist                                             values.find("Progress");
204fd9ab9e1SJames Feist                                         if (findProgress == values.end())
205fd9ab9e1SJames Feist                                         {
206fd9ab9e1SJames Feist                                             return !task::completed;
207fd9ab9e1SJames Feist                                         }
208fd9ab9e1SJames Feist                                         uint8_t* progress =
209fd9ab9e1SJames Feist                                             std::get_if<uint8_t>(
210fd9ab9e1SJames Feist                                                 &(findProgress->second));
211fd9ab9e1SJames Feist 
212fd9ab9e1SJames Feist                                         if (progress == nullptr)
213fd9ab9e1SJames Feist                                         {
214fd9ab9e1SJames Feist                                             taskData->messages.emplace_back(
215fd9ab9e1SJames Feist                                                 messages::internalError());
216fd9ab9e1SJames Feist                                             return task::completed;
217fd9ab9e1SJames Feist                                         }
2186868ff50SGeorge Liu                                         taskData->percentComplete =
2196868ff50SGeorge Liu                                             static_cast<int>(*progress);
220fd9ab9e1SJames Feist                                         taskData->messages.emplace_back(
221fd9ab9e1SJames Feist                                             messages::taskProgressChanged(
222fd9ab9e1SJames Feist                                                 index, static_cast<size_t>(
223fd9ab9e1SJames Feist                                                            *progress)));
224fd9ab9e1SJames Feist 
225fd9ab9e1SJames Feist                                         // if we're getting status updates it's
226fd9ab9e1SJames Feist                                         // still alive, update timer
227fd9ab9e1SJames Feist                                         taskData->extendTimer(
228fd9ab9e1SJames Feist                                             std::chrono::minutes(5));
229fd9ab9e1SJames Feist                                     }
23032898ceaSJames Feist 
23132898ceaSJames Feist                                     // as firmware update often results in a
23232898ceaSJames Feist                                     // reboot, the task  may never "complete"
23332898ceaSJames Feist                                     // unless it is an error
23432898ceaSJames Feist 
23532898ceaSJames Feist                                     return !task::completed;
23632898ceaSJames Feist                                 },
23732898ceaSJames Feist                                 "type='signal',interface='org.freedesktop.DBus."
23832898ceaSJames Feist                                 "Properties',"
239fd9ab9e1SJames Feist                                 "member='PropertiesChanged',path='" +
24032898ceaSJames Feist                                     objPath.str + "'");
24132898ceaSJames Feist                         task->startTimer(std::chrono::minutes(5));
24232898ceaSJames Feist                         task->populateResp(asyncResp->res);
243fe306728SJames Feist                         task->payload.emplace(req);
2440554c984SAndrew Geissler                     }
24586adcd6dSAndrew Geissler                     fwUpdateInProgress = false;
24686adcd6dSAndrew Geissler                 },
24786adcd6dSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper",
24886adcd6dSAndrew Geissler                 "/xyz/openbmc_project/object_mapper",
24986adcd6dSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str,
25086adcd6dSAndrew Geissler                 std::array<const char*, 1>{
25186adcd6dSAndrew Geissler                     "xyz.openbmc_project.Software.Activation"});
25286adcd6dSAndrew Geissler         }
25386adcd6dSAndrew Geissler     }
25486adcd6dSAndrew Geissler }
25586adcd6dSAndrew Geissler 
2560554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr
2570554c984SAndrew Geissler // then no asyncResp updates will occur
258b5a76932SEd Tanous static void monitorForSoftwareAvailable(
259b5a76932SEd Tanous     const std::shared_ptr<AsyncResp>& asyncResp, const crow::Request& req,
260b5a76932SEd Tanous     const std::string& url, int timeoutTimeSeconds = 10)
26186adcd6dSAndrew Geissler {
26286adcd6dSAndrew Geissler     // Only allow one FW update at a time
26386adcd6dSAndrew Geissler     if (fwUpdateInProgress != false)
26486adcd6dSAndrew Geissler     {
2650554c984SAndrew Geissler         if (asyncResp)
2660554c984SAndrew Geissler         {
26786adcd6dSAndrew Geissler             messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
2680554c984SAndrew Geissler         }
26986adcd6dSAndrew Geissler         return;
27086adcd6dSAndrew Geissler     }
27186adcd6dSAndrew Geissler 
2720554c984SAndrew Geissler     fwAvailableTimer =
273271584abSEd Tanous         std::make_unique<boost::asio::steady_timer>(*req.ioService);
27486adcd6dSAndrew Geissler 
275271584abSEd Tanous     fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds));
27686adcd6dSAndrew Geissler 
27786adcd6dSAndrew Geissler     fwAvailableTimer->async_wait(
27886adcd6dSAndrew Geissler         [asyncResp](const boost::system::error_code& ec) {
27986adcd6dSAndrew Geissler             cleanUp();
28086adcd6dSAndrew Geissler             if (ec == boost::asio::error::operation_aborted)
28186adcd6dSAndrew Geissler             {
28286adcd6dSAndrew Geissler                 // expected, we were canceled before the timer completed.
28386adcd6dSAndrew Geissler                 return;
28486adcd6dSAndrew Geissler             }
28586adcd6dSAndrew Geissler             BMCWEB_LOG_ERROR
28686adcd6dSAndrew Geissler                 << "Timed out waiting for firmware object being created";
28786adcd6dSAndrew Geissler             BMCWEB_LOG_ERROR
28886adcd6dSAndrew Geissler                 << "FW image may has already been uploaded to server";
28986adcd6dSAndrew Geissler             if (ec)
29086adcd6dSAndrew Geissler             {
29186adcd6dSAndrew Geissler                 BMCWEB_LOG_ERROR << "Async_wait failed" << ec;
29286adcd6dSAndrew Geissler                 return;
29386adcd6dSAndrew Geissler             }
2940554c984SAndrew Geissler             if (asyncResp)
2950554c984SAndrew Geissler             {
29686adcd6dSAndrew Geissler                 redfish::messages::internalError(asyncResp->res);
2970554c984SAndrew Geissler             }
29886adcd6dSAndrew Geissler         });
29986adcd6dSAndrew Geissler 
300fe306728SJames Feist     auto callback = [asyncResp, req](sdbusplus::message::message& m) {
30186adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "Match fired";
302fe306728SJames Feist         softwareInterfaceAdded(asyncResp, m, req);
30386adcd6dSAndrew Geissler     };
30486adcd6dSAndrew Geissler 
30586adcd6dSAndrew Geissler     fwUpdateInProgress = true;
30686adcd6dSAndrew Geissler 
30786adcd6dSAndrew Geissler     fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
30886adcd6dSAndrew Geissler         *crow::connections::systemBus,
30986adcd6dSAndrew Geissler         "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
31086adcd6dSAndrew Geissler         "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
31186adcd6dSAndrew Geissler         callback);
3124cde5d90SJames Feist 
3134cde5d90SJames Feist     fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match::match>(
3144cde5d90SJames Feist         *crow::connections::systemBus,
3154cde5d90SJames Feist         "type='signal',member='PropertiesChanged',path_namespace='/xyz/"
3164cde5d90SJames Feist         "openbmc_project/logging/entry',"
3174cde5d90SJames Feist         "arg0='xyz.openbmc_project.Logging.Entry'",
3184cde5d90SJames Feist         [asyncResp, url](sdbusplus::message::message& m) {
3194cde5d90SJames Feist             BMCWEB_LOG_DEBUG << "Error Match fired";
3204cde5d90SJames Feist             boost::container::flat_map<std::string, std::variant<std::string>>
3214cde5d90SJames Feist                 values;
3224cde5d90SJames Feist             std::string objName;
3234cde5d90SJames Feist             m.read(objName, values);
3244cde5d90SJames Feist             auto find = values.find("Message");
3254cde5d90SJames Feist             if (find == values.end())
3264cde5d90SJames Feist             {
3274cde5d90SJames Feist                 return;
3284cde5d90SJames Feist             }
3294cde5d90SJames Feist             std::string* type = std::get_if<std::string>(&(find->second));
3304cde5d90SJames Feist             if (type == nullptr)
3314cde5d90SJames Feist             {
3324cde5d90SJames Feist                 return; // if this was our message, timeout will cover it
3334cde5d90SJames Feist             }
33488b3dd12SGunnar Mills             if (!boost::starts_with(*type, "xyz.openbmc_project.Software"))
3354cde5d90SJames Feist             {
3364cde5d90SJames Feist                 return;
3374cde5d90SJames Feist             }
3384cde5d90SJames Feist             if (*type ==
3394cde5d90SJames Feist                 "xyz.openbmc_project.Software.Image.Error.UnTarFailure")
3404cde5d90SJames Feist             {
3414cde5d90SJames Feist                 redfish::messages::invalidUpload(asyncResp->res, url,
3424cde5d90SJames Feist                                                  "Invalid archive");
3434cde5d90SJames Feist             }
3444cde5d90SJames Feist             else if (*type == "xyz.openbmc_project.Software.Image.Error."
3454cde5d90SJames Feist                               "ManifestFileFailure")
3464cde5d90SJames Feist             {
3474cde5d90SJames Feist                 redfish::messages::invalidUpload(asyncResp->res, url,
3484cde5d90SJames Feist                                                  "Invalid manifest");
3494cde5d90SJames Feist             }
3504cde5d90SJames Feist             else if (*type ==
3514cde5d90SJames Feist                      "xyz.openbmc_project.Software.Image.Error.ImageFailure")
3524cde5d90SJames Feist             {
3534cde5d90SJames Feist                 redfish::messages::invalidUpload(asyncResp->res, url,
3544cde5d90SJames Feist                                                  "Invalid image format");
3554cde5d90SJames Feist             }
35688b3dd12SGunnar Mills             else if (*type == "xyz.openbmc_project.Software.Version.Error."
35788b3dd12SGunnar Mills                               "AlreadyExists")
35888b3dd12SGunnar Mills             {
35988b3dd12SGunnar Mills 
36088b3dd12SGunnar Mills                 redfish::messages::invalidUpload(
36188b3dd12SGunnar Mills                     asyncResp->res, url, "Image version already exists");
36288b3dd12SGunnar Mills 
36388b3dd12SGunnar Mills                 redfish::messages::resourceAlreadyExists(
36488b3dd12SGunnar Mills                     asyncResp->res, "UpdateService.v1_4_0.UpdateService",
36588b3dd12SGunnar Mills                     "Version", "uploaded version");
36688b3dd12SGunnar Mills             }
3674cde5d90SJames Feist             else if (*type ==
3684cde5d90SJames Feist                      "xyz.openbmc_project.Software.Image.Error.BusyFailure")
3694cde5d90SJames Feist             {
3704cde5d90SJames Feist                 redfish::messages::resourceExhaustion(asyncResp->res, url);
3714cde5d90SJames Feist             }
3724cde5d90SJames Feist             else
3734cde5d90SJames Feist             {
3744cde5d90SJames Feist                 redfish::messages::internalError(asyncResp->res);
3754cde5d90SJames Feist             }
3764cde5d90SJames Feist             fwAvailableTimer = nullptr;
3774cde5d90SJames Feist         });
37886adcd6dSAndrew Geissler }
379729dae72SJennifer Lee 
3800554c984SAndrew Geissler /**
3810554c984SAndrew Geissler  * UpdateServiceActionsSimpleUpdate class supports handle POST method for
3820554c984SAndrew Geissler  * SimpleUpdate action.
3830554c984SAndrew Geissler  */
3840554c984SAndrew Geissler class UpdateServiceActionsSimpleUpdate : public Node
3850554c984SAndrew Geissler {
3860554c984SAndrew Geissler   public:
38752cc112dSEd Tanous     UpdateServiceActionsSimpleUpdate(App& app) :
3880554c984SAndrew Geissler         Node(app,
3890554c984SAndrew Geissler              "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
3900554c984SAndrew Geissler     {
3910554c984SAndrew Geissler         entityPrivileges = {
3920554c984SAndrew Geissler             {boost::beast::http::verb::get, {{"Login"}}},
3930554c984SAndrew Geissler             {boost::beast::http::verb::head, {{"Login"}}},
3940554c984SAndrew Geissler             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3950554c984SAndrew Geissler             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
3960554c984SAndrew Geissler             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
3970554c984SAndrew Geissler             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
3980554c984SAndrew Geissler     }
3990554c984SAndrew Geissler 
4000554c984SAndrew Geissler   private:
4010554c984SAndrew Geissler     void doPost(crow::Response& res, const crow::Request& req,
402cb13a392SEd Tanous                 const std::vector<std::string>&) override
4030554c984SAndrew Geissler     {
4040554c984SAndrew Geissler         std::optional<std::string> transferProtocol;
4050554c984SAndrew Geissler         std::string imageURI;
4060554c984SAndrew Geissler         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
4070554c984SAndrew Geissler 
4080554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost";
4090554c984SAndrew Geissler 
4100554c984SAndrew Geissler         // User can pass in both TransferProtocol and ImageURI parameters or
4114e0453b1SGunnar Mills         // they can pass in just the ImageURI with the transfer protocol
4124e0453b1SGunnar Mills         // embedded within it.
4130554c984SAndrew Geissler         // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
4140554c984SAndrew Geissler         // 2) ImageURI:tftp://1.1.1.1/myfile.bin
4150554c984SAndrew Geissler 
4160554c984SAndrew Geissler         if (!json_util::readJson(req, asyncResp->res, "TransferProtocol",
4170554c984SAndrew Geissler                                  transferProtocol, "ImageURI", imageURI))
4180554c984SAndrew Geissler         {
4190554c984SAndrew Geissler             BMCWEB_LOG_DEBUG
4200554c984SAndrew Geissler                 << "Missing TransferProtocol or ImageURI parameter";
4210554c984SAndrew Geissler             return;
4220554c984SAndrew Geissler         }
4230554c984SAndrew Geissler         if (!transferProtocol)
4240554c984SAndrew Geissler         {
4250554c984SAndrew Geissler             // Must be option 2
4260554c984SAndrew Geissler             // Verify ImageURI has transfer protocol in it
427f23b7296SEd Tanous             size_t separator = imageURI.find(':');
4280554c984SAndrew Geissler             if ((separator == std::string::npos) ||
4290554c984SAndrew Geissler                 ((separator + 1) > imageURI.size()))
4300554c984SAndrew Geissler             {
4310554c984SAndrew Geissler                 messages::actionParameterValueTypeError(
4320554c984SAndrew Geissler                     asyncResp->res, imageURI, "ImageURI",
4330554c984SAndrew Geissler                     "UpdateService.SimpleUpdate");
4340554c984SAndrew Geissler                 BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: "
4350554c984SAndrew Geissler                                  << imageURI;
4360554c984SAndrew Geissler                 return;
4370554c984SAndrew Geissler             }
4380554c984SAndrew Geissler             transferProtocol = imageURI.substr(0, separator);
4390554c984SAndrew Geissler             // Ensure protocol is upper case for a common comparison path below
4400554c984SAndrew Geissler             boost::to_upper(*transferProtocol);
4410554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Encoded transfer protocol "
4420554c984SAndrew Geissler                              << *transferProtocol;
4430554c984SAndrew Geissler 
4440554c984SAndrew Geissler             // Adjust imageURI to not have the protocol on it for parsing
4450554c984SAndrew Geissler             // below
4460554c984SAndrew Geissler             // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin
4470554c984SAndrew Geissler             imageURI = imageURI.substr(separator + 3);
4480554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI;
4490554c984SAndrew Geissler         }
4500554c984SAndrew Geissler 
4510554c984SAndrew Geissler         // OpenBMC currently only supports TFTP
4520554c984SAndrew Geissler         if (*transferProtocol != "TFTP")
4530554c984SAndrew Geissler         {
4540554c984SAndrew Geissler             messages::actionParameterNotSupported(asyncResp->res,
4550554c984SAndrew Geissler                                                   "TransferProtocol",
4560554c984SAndrew Geissler                                                   "UpdateService.SimpleUpdate");
4570554c984SAndrew Geissler             BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: "
4580554c984SAndrew Geissler                              << *transferProtocol;
4590554c984SAndrew Geissler             return;
4600554c984SAndrew Geissler         }
4610554c984SAndrew Geissler 
4620554c984SAndrew Geissler         // Format should be <IP or Hostname>/<file> for imageURI
463f23b7296SEd Tanous         size_t separator = imageURI.find('/');
4640554c984SAndrew Geissler         if ((separator == std::string::npos) ||
4650554c984SAndrew Geissler             ((separator + 1) > imageURI.size()))
4660554c984SAndrew Geissler         {
4670554c984SAndrew Geissler             messages::actionParameterValueTypeError(
4680554c984SAndrew Geissler                 asyncResp->res, imageURI, "ImageURI",
4690554c984SAndrew Geissler                 "UpdateService.SimpleUpdate");
4700554c984SAndrew Geissler             BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI;
4710554c984SAndrew Geissler             return;
4720554c984SAndrew Geissler         }
4730554c984SAndrew Geissler 
4740554c984SAndrew Geissler         std::string tftpServer = imageURI.substr(0, separator);
4750554c984SAndrew Geissler         std::string fwFile = imageURI.substr(separator + 1);
4760554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile;
4770554c984SAndrew Geissler 
4780554c984SAndrew Geissler         // Setup callback for when new software detected
4792618d5e3SGunnar Mills         // Give TFTP 10 minutes to complete
4804cde5d90SJames Feist         monitorForSoftwareAvailable(
481*d7a596bdSAlbert Zhang             asyncResp, req,
4824cde5d90SJames Feist             "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate",
4832618d5e3SGunnar Mills             600);
4840554c984SAndrew Geissler 
4852618d5e3SGunnar Mills         // TFTP can take up to 10 minutes depending on image size and
4860554c984SAndrew Geissler         // connection speed. Return to caller as soon as the TFTP operation
4870554c984SAndrew Geissler         // has been started. The callback above will ensure the activate
4880554c984SAndrew Geissler         // is started once the download has completed
4890554c984SAndrew Geissler         redfish::messages::success(asyncResp->res);
4900554c984SAndrew Geissler 
4910554c984SAndrew Geissler         // Call TFTP service
4920554c984SAndrew Geissler         crow::connections::systemBus->async_method_call(
4930554c984SAndrew Geissler             [](const boost::system::error_code ec) {
4940554c984SAndrew Geissler                 if (ec)
4950554c984SAndrew Geissler                 {
4960554c984SAndrew Geissler                     // messages::internalError(asyncResp->res);
4970554c984SAndrew Geissler                     cleanUp();
4980554c984SAndrew Geissler                     BMCWEB_LOG_DEBUG << "error_code = " << ec;
4990554c984SAndrew Geissler                     BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
5000554c984SAndrew Geissler                 }
5010554c984SAndrew Geissler                 else
5020554c984SAndrew Geissler                 {
5030554c984SAndrew Geissler                     BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success";
5040554c984SAndrew Geissler                 }
5050554c984SAndrew Geissler             },
5060554c984SAndrew Geissler             "xyz.openbmc_project.Software.Download",
5070554c984SAndrew Geissler             "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP",
5080554c984SAndrew Geissler             "DownloadViaTFTP", fwFile, tftpServer);
5090554c984SAndrew Geissler 
5100554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost";
5110554c984SAndrew Geissler     }
5120554c984SAndrew Geissler };
5130554c984SAndrew Geissler 
5141abe55efSEd Tanous class UpdateService : public Node
5151abe55efSEd Tanous {
516729dae72SJennifer Lee   public:
51752cc112dSEd Tanous     UpdateService(App& app) : Node(app, "/redfish/v1/UpdateService/")
5181abe55efSEd Tanous     {
519729dae72SJennifer Lee         entityPrivileges = {
520729dae72SJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
521729dae72SJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
522729dae72SJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
523729dae72SJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
524729dae72SJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
525729dae72SJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
526729dae72SJennifer Lee     }
527729dae72SJennifer Lee 
528729dae72SJennifer Lee   private:
529cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
530cb13a392SEd Tanous                const std::vector<std::string>&) override
5311abe55efSEd Tanous     {
532274dfe62SJayashankar Padath         std::shared_ptr<AsyncResp> aResp = std::make_shared<AsyncResp>(res);
533274dfe62SJayashankar Padath         res.jsonValue["@odata.type"] = "#UpdateService.v1_4_0.UpdateService";
5340f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
5350f74e643SEd Tanous         res.jsonValue["Id"] = "UpdateService";
5360f74e643SEd Tanous         res.jsonValue["Description"] = "Service for Software Update";
5370f74e643SEd Tanous         res.jsonValue["Name"] = "Update Service";
5380f74e643SEd Tanous         res.jsonValue["HttpPushUri"] = "/redfish/v1/UpdateService";
5390f74e643SEd Tanous         // UpdateService cannot be disabled
5400f74e643SEd Tanous         res.jsonValue["ServiceEnabled"] = true;
5410f74e643SEd Tanous         res.jsonValue["FirmwareInventory"] = {
5420f74e643SEd Tanous             {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}};
5430554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
5440554c984SAndrew Geissler         // Update Actions object.
5450554c984SAndrew Geissler         nlohmann::json& updateSvcSimpleUpdate =
5460554c984SAndrew Geissler             res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
5470554c984SAndrew Geissler         updateSvcSimpleUpdate["target"] =
5480554c984SAndrew Geissler             "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
5490554c984SAndrew Geissler         updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = {
5500554c984SAndrew Geissler             "TFTP"};
5510554c984SAndrew Geissler #endif
552274dfe62SJayashankar Padath         // Get the current ApplyTime value
553274dfe62SJayashankar Padath         crow::connections::systemBus->async_method_call(
554274dfe62SJayashankar Padath             [aResp](const boost::system::error_code ec,
555274dfe62SJayashankar Padath                     const std::variant<std::string>& applyTime) {
556274dfe62SJayashankar Padath                 if (ec)
557274dfe62SJayashankar Padath                 {
558274dfe62SJayashankar Padath                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
559274dfe62SJayashankar Padath                     messages::internalError(aResp->res);
560274dfe62SJayashankar Padath                     return;
561274dfe62SJayashankar Padath                 }
562274dfe62SJayashankar Padath 
563274dfe62SJayashankar Padath                 const std::string* s = std::get_if<std::string>(&applyTime);
564274dfe62SJayashankar Padath                 if (s == nullptr)
565274dfe62SJayashankar Padath                 {
566274dfe62SJayashankar Padath                     return;
567274dfe62SJayashankar Padath                 }
568274dfe62SJayashankar Padath                 // Store the ApplyTime Value
569274dfe62SJayashankar Padath                 if (*s == "xyz.openbmc_project.Software.ApplyTime."
570274dfe62SJayashankar Padath                           "RequestedApplyTimes.Immediate")
571274dfe62SJayashankar Padath                 {
572274dfe62SJayashankar Padath                     aResp->res.jsonValue["HttpPushUriOptions"]
573274dfe62SJayashankar Padath                                         ["HttpPushUriApplyTime"]["ApplyTime"] =
574274dfe62SJayashankar Padath                         "Immediate";
575274dfe62SJayashankar Padath                 }
576274dfe62SJayashankar Padath                 else if (*s == "xyz.openbmc_project.Software.ApplyTime."
577274dfe62SJayashankar Padath                                "RequestedApplyTimes.OnReset")
578274dfe62SJayashankar Padath                 {
579274dfe62SJayashankar Padath                     aResp->res.jsonValue["HttpPushUriOptions"]
580274dfe62SJayashankar Padath                                         ["HttpPushUriApplyTime"]["ApplyTime"] =
581274dfe62SJayashankar Padath                         "OnReset";
582274dfe62SJayashankar Padath                 }
583274dfe62SJayashankar Padath             },
584274dfe62SJayashankar Padath             "xyz.openbmc_project.Settings",
585274dfe62SJayashankar Padath             "/xyz/openbmc_project/software/apply_time",
586274dfe62SJayashankar Padath             "org.freedesktop.DBus.Properties", "Get",
587274dfe62SJayashankar Padath             "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime");
588729dae72SJennifer Lee     }
5890e7de46fSAndrew Geissler 
590fa1a5a38SJayashankar Padath     void doPatch(crow::Response& res, const crow::Request& req,
591cb13a392SEd Tanous                  const std::vector<std::string>&) override
592fa1a5a38SJayashankar Padath     {
593fa1a5a38SJayashankar Padath         BMCWEB_LOG_DEBUG << "doPatch...";
594fa1a5a38SJayashankar Padath 
595fa1a5a38SJayashankar Padath         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
596fa1a5a38SJayashankar Padath 
597274dfe62SJayashankar Padath         std::optional<nlohmann::json> pushUriOptions;
598274dfe62SJayashankar Padath         if (!json_util::readJson(req, res, "HttpPushUriOptions",
599274dfe62SJayashankar Padath                                  pushUriOptions))
600fa1a5a38SJayashankar Padath         {
601fa1a5a38SJayashankar Padath             return;
602fa1a5a38SJayashankar Padath         }
603fa1a5a38SJayashankar Padath 
604274dfe62SJayashankar Padath         if (pushUriOptions)
605274dfe62SJayashankar Padath         {
606274dfe62SJayashankar Padath             std::optional<nlohmann::json> pushUriApplyTime;
607274dfe62SJayashankar Padath             if (!json_util::readJson(*pushUriOptions, res,
608274dfe62SJayashankar Padath                                      "HttpPushUriApplyTime", pushUriApplyTime))
609274dfe62SJayashankar Padath             {
610274dfe62SJayashankar Padath                 return;
611274dfe62SJayashankar Padath             }
612274dfe62SJayashankar Padath 
613274dfe62SJayashankar Padath             if (pushUriApplyTime)
614274dfe62SJayashankar Padath             {
615274dfe62SJayashankar Padath                 std::optional<std::string> applyTime;
616274dfe62SJayashankar Padath                 if (!json_util::readJson(*pushUriApplyTime, res, "ApplyTime",
617274dfe62SJayashankar Padath                                          applyTime))
618274dfe62SJayashankar Padath                 {
619274dfe62SJayashankar Padath                     return;
620274dfe62SJayashankar Padath                 }
621274dfe62SJayashankar Padath 
622274dfe62SJayashankar Padath                 if (applyTime)
623fa1a5a38SJayashankar Padath                 {
624fa1a5a38SJayashankar Padath                     std::string applyTimeNewVal;
625fa1a5a38SJayashankar Padath                     if (applyTime == "Immediate")
626fa1a5a38SJayashankar Padath                     {
627274dfe62SJayashankar Padath                         applyTimeNewVal =
628274dfe62SJayashankar Padath                             "xyz.openbmc_project.Software.ApplyTime."
629fa1a5a38SJayashankar Padath                             "RequestedApplyTimes.Immediate";
630fa1a5a38SJayashankar Padath                     }
631274dfe62SJayashankar Padath                     else if (applyTime == "OnReset")
632274dfe62SJayashankar Padath                     {
633274dfe62SJayashankar Padath                         applyTimeNewVal =
634274dfe62SJayashankar Padath                             "xyz.openbmc_project.Software.ApplyTime."
635274dfe62SJayashankar Padath                             "RequestedApplyTimes.OnReset";
636274dfe62SJayashankar Padath                     }
637fa1a5a38SJayashankar Padath                     else
638fa1a5a38SJayashankar Padath                     {
639274dfe62SJayashankar Padath                         BMCWEB_LOG_INFO
640274dfe62SJayashankar Padath                             << "ApplyTime value is not in the list of "
641274dfe62SJayashankar Padath                                "acceptable values";
642274dfe62SJayashankar Padath                         messages::propertyValueNotInList(
643274dfe62SJayashankar Padath                             asyncResp->res, *applyTime, "ApplyTime");
644274dfe62SJayashankar Padath                         return;
645fa1a5a38SJayashankar Padath                     }
646fa1a5a38SJayashankar Padath 
647fa1a5a38SJayashankar Padath                     // Set the requested image apply time value
648fa1a5a38SJayashankar Padath                     crow::connections::systemBus->async_method_call(
649fa1a5a38SJayashankar Padath                         [asyncResp](const boost::system::error_code ec) {
650fa1a5a38SJayashankar Padath                             if (ec)
651fa1a5a38SJayashankar Padath                             {
652274dfe62SJayashankar Padath                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
653274dfe62SJayashankar Padath                                                  << ec;
654fa1a5a38SJayashankar Padath                                 messages::internalError(asyncResp->res);
655fa1a5a38SJayashankar Padath                                 return;
656fa1a5a38SJayashankar Padath                             }
657fa1a5a38SJayashankar Padath                             messages::success(asyncResp->res);
658fa1a5a38SJayashankar Padath                         },
659fa1a5a38SJayashankar Padath                         "xyz.openbmc_project.Settings",
660fa1a5a38SJayashankar Padath                         "/xyz/openbmc_project/software/apply_time",
661fa1a5a38SJayashankar Padath                         "org.freedesktop.DBus.Properties", "Set",
662274dfe62SJayashankar Padath                         "xyz.openbmc_project.Software.ApplyTime",
663274dfe62SJayashankar Padath                         "RequestedApplyTime",
664fa1a5a38SJayashankar Padath                         std::variant<std::string>{applyTimeNewVal});
665fa1a5a38SJayashankar Padath                 }
666274dfe62SJayashankar Padath             }
667fa1a5a38SJayashankar Padath         }
668fa1a5a38SJayashankar Padath     }
669fa1a5a38SJayashankar Padath 
670acb7cfb4SJennifer Lee     void doPost(crow::Response& res, const crow::Request& req,
671cb13a392SEd Tanous                 const std::vector<std::string>&) override
6721abe55efSEd Tanous     {
673acb7cfb4SJennifer Lee         BMCWEB_LOG_DEBUG << "doPost...";
674acb7cfb4SJennifer Lee 
6750e7de46fSAndrew Geissler         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
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:
709cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
710cb13a392SEd Tanous                const std::vector<std::string>&) override
7111abe55efSEd Tanous     {
712c711bf86SEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
7130f74e643SEd Tanous         res.jsonValue["@odata.type"] =
7140f74e643SEd Tanous             "#SoftwareInventoryCollection.SoftwareInventoryCollection";
7150f74e643SEd Tanous         res.jsonValue["@odata.id"] =
7160f74e643SEd Tanous             "/redfish/v1/UpdateService/FirmwareInventory";
7170f74e643SEd Tanous         res.jsonValue["Name"] = "Software Inventory Collection";
718c711bf86SEd Tanous 
719c711bf86SEd Tanous         crow::connections::systemBus->async_method_call(
720c711bf86SEd Tanous             [asyncResp](
721c711bf86SEd Tanous                 const boost::system::error_code ec,
7226c4eb9deSJennifer Lee                 const std::vector<std::pair<
7231abe55efSEd Tanous                     std::string, std::vector<std::pair<
7241214b7e7SGunnar Mills                                      std::string, std::vector<std::string>>>>>&
7251214b7e7SGunnar Mills                     subtree) {
7261abe55efSEd Tanous                 if (ec)
7271abe55efSEd Tanous                 {
728f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
7296c4eb9deSJennifer Lee                     return;
730729dae72SJennifer Lee                 }
731c711bf86SEd Tanous                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
732c711bf86SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
7336c4eb9deSJennifer Lee 
7341abe55efSEd Tanous                 for (auto& obj : subtree)
7351abe55efSEd Tanous                 {
736f4b65ab1SJennifer Lee                     // if can't parse fw id then return
73727826b5fSEd Tanous                     std::size_t idPos;
738f23b7296SEd Tanous                     if ((idPos = obj.first.rfind('/')) == std::string::npos)
739f4b65ab1SJennifer Lee                     {
740f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
741f4b65ab1SJennifer Lee                         BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
742f4b65ab1SJennifer Lee                         return;
743f4b65ab1SJennifer Lee                     }
744f4b65ab1SJennifer Lee                     std::string swId = obj.first.substr(idPos + 1);
745f4b65ab1SJennifer Lee 
746c711bf86SEd Tanous                     nlohmann::json& members =
747c711bf86SEd Tanous                         asyncResp->res.jsonValue["Members"];
748c711bf86SEd Tanous                     members.push_back(
749f4b65ab1SJennifer Lee                         {{"@odata.id", "/redfish/v1/UpdateService/"
7501abe55efSEd Tanous                                        "FirmwareInventory/" +
751f4b65ab1SJennifer Lee                                            swId}});
752e0dd8057SAndrew Geissler                     asyncResp->res.jsonValue["Members@odata.count"] =
753c711bf86SEd Tanous                         members.size();
7546c4eb9deSJennifer Lee                 }
755c711bf86SEd Tanous             },
7562830a9cfSAndrew Geissler             // Note that only firmware levels associated with a device are
7572830a9cfSAndrew Geissler             // stored under /xyz/openbmc_project/software therefore to ensure
7582830a9cfSAndrew Geissler             // only real FirmwareInventory items are returned, this full object
7592830a9cfSAndrew Geissler             // path must be used here as input to mapper
760c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper",
761c711bf86SEd Tanous             "/xyz/openbmc_project/object_mapper",
7622830a9cfSAndrew Geissler             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
7632830a9cfSAndrew Geissler             "/xyz/openbmc_project/software", static_cast<int32_t>(0),
7641214b7e7SGunnar Mills             std::array<const char*, 1>{"xyz.openbmc_project.Software.Version"});
765729dae72SJennifer Lee     }
766729dae72SJennifer Lee };
767c711bf86SEd Tanous 
7681abe55efSEd Tanous class SoftwareInventory : public Node
7691abe55efSEd Tanous {
770729dae72SJennifer Lee   public:
77152cc112dSEd Tanous     SoftwareInventory(App& app) :
7721abe55efSEd Tanous         Node(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/",
7731abe55efSEd Tanous              std::string())
7741abe55efSEd Tanous     {
775729dae72SJennifer Lee         entityPrivileges = {
776729dae72SJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
777729dae72SJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
778729dae72SJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
779729dae72SJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
780729dae72SJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
781729dae72SJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
782729dae72SJennifer Lee     }
783729dae72SJennifer Lee 
784729dae72SJennifer Lee   private:
78587d84729SAndrew Geissler     /* Fill related item links (i.e. bmc, bios) in for inventory */
786b5a76932SEd Tanous     static void getRelatedItems(const std::shared_ptr<AsyncResp>& aResp,
78787d84729SAndrew Geissler                                 const std::string& purpose)
78887d84729SAndrew Geissler     {
78987d84729SAndrew Geissler         if (purpose == fw_util::bmcPurpose)
79087d84729SAndrew Geissler         {
79187d84729SAndrew Geissler             nlohmann::json& members = aResp->res.jsonValue["RelatedItem"];
79287d84729SAndrew Geissler             members.push_back({{"@odata.id", "/redfish/v1/Managers/bmc"}});
79387d84729SAndrew Geissler             aResp->res.jsonValue["Members@odata.count"] = members.size();
79487d84729SAndrew Geissler         }
79587d84729SAndrew Geissler         else if (purpose == fw_util::biosPurpose)
79687d84729SAndrew Geissler         {
797f723d733SGunnar Mills             nlohmann::json& members = aResp->res.jsonValue["RelatedItem"];
798f723d733SGunnar Mills             members.push_back(
799f723d733SGunnar Mills                 {{"@odata.id", "/redfish/v1/Systems/system/Bios"}});
800f723d733SGunnar Mills             aResp->res.jsonValue["Members@odata.count"] = members.size();
80187d84729SAndrew Geissler         }
80287d84729SAndrew Geissler         else
80387d84729SAndrew Geissler         {
80487d84729SAndrew Geissler             BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
80587d84729SAndrew Geissler         }
80687d84729SAndrew Geissler     }
80787d84729SAndrew Geissler 
808cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
8091abe55efSEd Tanous                const std::vector<std::string>& params) override
8101abe55efSEd Tanous     {
811c711bf86SEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
8126c4eb9deSJennifer Lee 
8131abe55efSEd Tanous         if (params.size() != 1)
8141abe55efSEd Tanous         {
815f12894f8SJason M. Bills             messages::internalError(res);
816729dae72SJennifer Lee             res.end();
817729dae72SJennifer Lee             return;
818729dae72SJennifer Lee         }
819729dae72SJennifer Lee 
8203ae837c9SEd Tanous         std::shared_ptr<std::string> swId =
821c711bf86SEd Tanous             std::make_shared<std::string>(params[0]);
822c711bf86SEd Tanous 
82355c7b7a2SEd Tanous         res.jsonValue["@odata.id"] =
8243ae837c9SEd Tanous             "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
825c711bf86SEd Tanous 
826c711bf86SEd Tanous         crow::connections::systemBus->async_method_call(
8273ae837c9SEd Tanous             [asyncResp, swId](
828c711bf86SEd Tanous                 const boost::system::error_code ec,
8296c4eb9deSJennifer Lee                 const std::vector<std::pair<
8301abe55efSEd Tanous                     std::string, std::vector<std::pair<
8311214b7e7SGunnar Mills                                      std::string, std::vector<std::string>>>>>&
8321214b7e7SGunnar Mills                     subtree) {
83355c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "doGet callback...";
8341abe55efSEd Tanous                 if (ec)
8351abe55efSEd Tanous                 {
836f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
8376c4eb9deSJennifer Lee                     return;
8386c4eb9deSJennifer Lee                 }
8396c4eb9deSJennifer Lee 
8406913228dSAndrew Geissler                 // Ensure we find our input swId, otherwise return an error
8416913228dSAndrew Geissler                 bool found = false;
8421abe55efSEd Tanous                 for (const std::pair<
8431abe55efSEd Tanous                          std::string,
8441abe55efSEd Tanous                          std::vector<
8451214b7e7SGunnar Mills                              std::pair<std::string, std::vector<std::string>>>>&
8461214b7e7SGunnar Mills                          obj : subtree)
8471abe55efSEd Tanous                 {
8483ae837c9SEd Tanous                     if (boost::ends_with(obj.first, *swId) != true)
8491abe55efSEd Tanous                     {
850acb7cfb4SJennifer Lee                         continue;
851acb7cfb4SJennifer Lee                     }
852acb7cfb4SJennifer Lee 
853f4b65ab1SJennifer Lee                     if (obj.second.size() < 1)
8541abe55efSEd Tanous                     {
855acb7cfb4SJennifer Lee                         continue;
856acb7cfb4SJennifer Lee                     }
8576c4eb9deSJennifer Lee 
8586913228dSAndrew Geissler                     found = true;
859e0dd8057SAndrew Geissler                     fw_util::getFwStatus(asyncResp, swId, obj.second[0].first);
860e0dd8057SAndrew Geissler 
86155c7b7a2SEd Tanous                     crow::connections::systemBus->async_method_call(
8621abe55efSEd Tanous                         [asyncResp,
86381ce609eSEd Tanous                          swId](const boost::system::error_code errorCode,
8641abe55efSEd Tanous                                const boost::container::flat_map<
8651abe55efSEd Tanous                                    std::string, VariantType>& propertiesList) {
86681ce609eSEd Tanous                             if (errorCode)
8671abe55efSEd Tanous                             {
868f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
8696c4eb9deSJennifer Lee                                 return;
8706c4eb9deSJennifer Lee                             }
8711abe55efSEd Tanous                             boost::container::flat_map<
8721abe55efSEd Tanous                                 std::string, VariantType>::const_iterator it =
8736c4eb9deSJennifer Lee                                 propertiesList.find("Purpose");
8741abe55efSEd Tanous                             if (it == propertiesList.end())
8751abe55efSEd Tanous                             {
8761abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
8771abe55efSEd Tanous                                     << "Can't find property \"Purpose\"!";
878f12894f8SJason M. Bills                                 messages::propertyMissing(asyncResp->res,
879f12894f8SJason M. Bills                                                           "Purpose");
8806c4eb9deSJennifer Lee                                 return;
8816c4eb9deSJennifer Lee                             }
8823ae837c9SEd Tanous                             const std::string* swInvPurpose =
883abf2add6SEd Tanous                                 std::get_if<std::string>(&it->second);
8843ae837c9SEd Tanous                             if (swInvPurpose == nullptr)
8851abe55efSEd Tanous                             {
8861abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
8871abe55efSEd Tanous                                     << "wrong types for property\"Purpose\"!";
888f12894f8SJason M. Bills                                 messages::propertyValueTypeError(asyncResp->res,
889f12894f8SJason M. Bills                                                                  "", "Purpose");
890acb7cfb4SJennifer Lee                                 return;
891acb7cfb4SJennifer Lee                             }
892c711bf86SEd Tanous 
8933ae837c9SEd Tanous                             BMCWEB_LOG_DEBUG << "swInvPurpose = "
8943ae837c9SEd Tanous                                              << *swInvPurpose;
895c711bf86SEd Tanous                             it = propertiesList.find("Version");
8961abe55efSEd Tanous                             if (it == propertiesList.end())
8971abe55efSEd Tanous                             {
8981abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
8991abe55efSEd Tanous                                     << "Can't find property \"Version\"!";
900f12894f8SJason M. Bills                                 messages::propertyMissing(asyncResp->res,
901f12894f8SJason M. Bills                                                           "Version");
902c711bf86SEd Tanous                                 return;
903acb7cfb4SJennifer Lee                             }
904acb7cfb4SJennifer Lee 
905f4b65ab1SJennifer Lee                             BMCWEB_LOG_DEBUG << "Version found!";
906c711bf86SEd Tanous 
907f4b65ab1SJennifer Lee                             const std::string* version =
908abf2add6SEd Tanous                                 std::get_if<std::string>(&it->second);
909f4b65ab1SJennifer Lee 
910f4b65ab1SJennifer Lee                             if (version == nullptr)
9111abe55efSEd Tanous                             {
9121abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
9131abe55efSEd Tanous                                     << "Can't find property \"Version\"!";
914f12894f8SJason M. Bills 
915f12894f8SJason M. Bills                                 messages::propertyValueTypeError(asyncResp->res,
916f12894f8SJason M. Bills                                                                  "", "Version");
9176c4eb9deSJennifer Lee                                 return;
9186c4eb9deSJennifer Lee                             }
919c711bf86SEd Tanous                             asyncResp->res.jsonValue["Version"] = *version;
9203ae837c9SEd Tanous                             asyncResp->res.jsonValue["Id"] = *swId;
92154daabe7SAndrew Geissler 
92254daabe7SAndrew Geissler                             // swInvPurpose is of format:
92354daabe7SAndrew Geissler                             // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
924e2e96770SJames Feist                             // Translate this to "ABC image"
925f23b7296SEd Tanous                             size_t endDesc = swInvPurpose->rfind('.');
92654daabe7SAndrew Geissler                             if (endDesc == std::string::npos)
92754daabe7SAndrew Geissler                             {
92854daabe7SAndrew Geissler                                 messages::internalError(asyncResp->res);
92954daabe7SAndrew Geissler                                 return;
93054daabe7SAndrew Geissler                             }
93154daabe7SAndrew Geissler                             endDesc++;
93254daabe7SAndrew Geissler                             if (endDesc >= swInvPurpose->size())
93354daabe7SAndrew Geissler                             {
93454daabe7SAndrew Geissler                                 messages::internalError(asyncResp->res);
93554daabe7SAndrew Geissler                                 return;
93654daabe7SAndrew Geissler                             }
93754daabe7SAndrew Geissler 
93854daabe7SAndrew Geissler                             std::string formatDesc =
93954daabe7SAndrew Geissler                                 swInvPurpose->substr(endDesc);
94054daabe7SAndrew Geissler                             asyncResp->res.jsonValue["Description"] =
941e2e96770SJames Feist                                 formatDesc + " image";
94287d84729SAndrew Geissler                             getRelatedItems(asyncResp, *swInvPurpose);
9436c4eb9deSJennifer Lee                         },
944c711bf86SEd Tanous                         obj.second[0].first, obj.first,
945c711bf86SEd Tanous                         "org.freedesktop.DBus.Properties", "GetAll",
946c711bf86SEd Tanous                         "xyz.openbmc_project.Software.Version");
9476c4eb9deSJennifer Lee                 }
9486913228dSAndrew Geissler                 if (!found)
9496913228dSAndrew Geissler                 {
9506913228dSAndrew Geissler                     BMCWEB_LOG_ERROR << "Input swID " + *swId + " not found!";
9516913228dSAndrew Geissler                     messages::resourceMissingAtURI(
9526913228dSAndrew Geissler                         asyncResp->res,
9536913228dSAndrew Geissler                         "/redfish/v1/UpdateService/FirmwareInventory/" + *swId);
9546913228dSAndrew Geissler                     return;
9556913228dSAndrew Geissler                 }
9564e68c45bSAyushi Smriti                 asyncResp->res.jsonValue["@odata.type"] =
9574e68c45bSAyushi Smriti                     "#SoftwareInventory.v1_1_0.SoftwareInventory";
9584e68c45bSAyushi Smriti                 asyncResp->res.jsonValue["Name"] = "Software Inventory";
9594e68c45bSAyushi Smriti                 asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
9603f8a743aSAppaRao Puli 
9613f8a743aSAppaRao Puli                 asyncResp->res.jsonValue["Updateable"] = false;
9623f8a743aSAppaRao Puli                 fw_util::getFwUpdateableStatus(asyncResp, swId);
963c711bf86SEd Tanous             },
964c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper",
965c711bf86SEd Tanous             "/xyz/openbmc_project/object_mapper",
966271584abSEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
967271584abSEd Tanous             static_cast<int32_t>(0),
9681214b7e7SGunnar Mills             std::array<const char*, 1>{"xyz.openbmc_project.Software.Version"});
9696c4eb9deSJennifer Lee     }
970729dae72SJennifer Lee };
971729dae72SJennifer Lee 
972729dae72SJennifer Lee } // namespace redfish
973