xref: /openbmc/bmcweb/features/redfish/lib/update_service.hpp (revision 88b3dd12851cd7bdd4b5c065ba99f40feafb775e)
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                                         }
218fd9ab9e1SJames Feist                                         taskData->messages.emplace_back(
219fd9ab9e1SJames Feist                                             messages::taskProgressChanged(
220fd9ab9e1SJames Feist                                                 index, static_cast<size_t>(
221fd9ab9e1SJames Feist                                                            *progress)));
222fd9ab9e1SJames Feist 
223fd9ab9e1SJames Feist                                         // if we're getting status updates it's
224fd9ab9e1SJames Feist                                         // still alive, update timer
225fd9ab9e1SJames Feist                                         taskData->extendTimer(
226fd9ab9e1SJames Feist                                             std::chrono::minutes(5));
227fd9ab9e1SJames Feist                                     }
22832898ceaSJames Feist 
22932898ceaSJames Feist                                     // as firmware update often results in a
23032898ceaSJames Feist                                     // reboot, the task  may never "complete"
23132898ceaSJames Feist                                     // unless it is an error
23232898ceaSJames Feist 
23332898ceaSJames Feist                                     return !task::completed;
23432898ceaSJames Feist                                 },
23532898ceaSJames Feist                                 "type='signal',interface='org.freedesktop.DBus."
23632898ceaSJames Feist                                 "Properties',"
237fd9ab9e1SJames Feist                                 "member='PropertiesChanged',path='" +
23832898ceaSJames Feist                                     objPath.str + "'");
23932898ceaSJames Feist                         task->startTimer(std::chrono::minutes(5));
24032898ceaSJames Feist                         task->populateResp(asyncResp->res);
241fe306728SJames Feist                         task->payload.emplace(req);
2420554c984SAndrew Geissler                     }
24386adcd6dSAndrew Geissler                     fwUpdateInProgress = false;
24486adcd6dSAndrew Geissler                 },
24586adcd6dSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper",
24686adcd6dSAndrew Geissler                 "/xyz/openbmc_project/object_mapper",
24786adcd6dSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str,
24886adcd6dSAndrew Geissler                 std::array<const char*, 1>{
24986adcd6dSAndrew Geissler                     "xyz.openbmc_project.Software.Activation"});
25086adcd6dSAndrew Geissler         }
25186adcd6dSAndrew Geissler     }
25286adcd6dSAndrew Geissler }
25386adcd6dSAndrew Geissler 
2540554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr
2550554c984SAndrew Geissler // then no asyncResp updates will occur
256b5a76932SEd Tanous static void monitorForSoftwareAvailable(
257b5a76932SEd Tanous     const std::shared_ptr<AsyncResp>& asyncResp, const crow::Request& req,
258b5a76932SEd Tanous     const std::string& url, int timeoutTimeSeconds = 10)
25986adcd6dSAndrew Geissler {
26086adcd6dSAndrew Geissler     // Only allow one FW update at a time
26186adcd6dSAndrew Geissler     if (fwUpdateInProgress != false)
26286adcd6dSAndrew Geissler     {
2630554c984SAndrew Geissler         if (asyncResp)
2640554c984SAndrew Geissler         {
26586adcd6dSAndrew Geissler             messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
2660554c984SAndrew Geissler         }
26786adcd6dSAndrew Geissler         return;
26886adcd6dSAndrew Geissler     }
26986adcd6dSAndrew Geissler 
2700554c984SAndrew Geissler     fwAvailableTimer =
271271584abSEd Tanous         std::make_unique<boost::asio::steady_timer>(*req.ioService);
27286adcd6dSAndrew Geissler 
273271584abSEd Tanous     fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds));
27486adcd6dSAndrew Geissler 
27586adcd6dSAndrew Geissler     fwAvailableTimer->async_wait(
27686adcd6dSAndrew Geissler         [asyncResp](const boost::system::error_code& ec) {
27786adcd6dSAndrew Geissler             cleanUp();
27886adcd6dSAndrew Geissler             if (ec == boost::asio::error::operation_aborted)
27986adcd6dSAndrew Geissler             {
28086adcd6dSAndrew Geissler                 // expected, we were canceled before the timer completed.
28186adcd6dSAndrew Geissler                 return;
28286adcd6dSAndrew Geissler             }
28386adcd6dSAndrew Geissler             BMCWEB_LOG_ERROR
28486adcd6dSAndrew Geissler                 << "Timed out waiting for firmware object being created";
28586adcd6dSAndrew Geissler             BMCWEB_LOG_ERROR
28686adcd6dSAndrew Geissler                 << "FW image may has already been uploaded to server";
28786adcd6dSAndrew Geissler             if (ec)
28886adcd6dSAndrew Geissler             {
28986adcd6dSAndrew Geissler                 BMCWEB_LOG_ERROR << "Async_wait failed" << ec;
29086adcd6dSAndrew Geissler                 return;
29186adcd6dSAndrew Geissler             }
2920554c984SAndrew Geissler             if (asyncResp)
2930554c984SAndrew Geissler             {
29486adcd6dSAndrew Geissler                 redfish::messages::internalError(asyncResp->res);
2950554c984SAndrew Geissler             }
29686adcd6dSAndrew Geissler         });
29786adcd6dSAndrew Geissler 
298fe306728SJames Feist     auto callback = [asyncResp, req](sdbusplus::message::message& m) {
29986adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "Match fired";
300fe306728SJames Feist         softwareInterfaceAdded(asyncResp, m, req);
30186adcd6dSAndrew Geissler     };
30286adcd6dSAndrew Geissler 
30386adcd6dSAndrew Geissler     fwUpdateInProgress = true;
30486adcd6dSAndrew Geissler 
30586adcd6dSAndrew Geissler     fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
30686adcd6dSAndrew Geissler         *crow::connections::systemBus,
30786adcd6dSAndrew Geissler         "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
30886adcd6dSAndrew Geissler         "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
30986adcd6dSAndrew Geissler         callback);
3104cde5d90SJames Feist 
3114cde5d90SJames Feist     fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match::match>(
3124cde5d90SJames Feist         *crow::connections::systemBus,
3134cde5d90SJames Feist         "type='signal',member='PropertiesChanged',path_namespace='/xyz/"
3144cde5d90SJames Feist         "openbmc_project/logging/entry',"
3154cde5d90SJames Feist         "arg0='xyz.openbmc_project.Logging.Entry'",
3164cde5d90SJames Feist         [asyncResp, url](sdbusplus::message::message& m) {
3174cde5d90SJames Feist             BMCWEB_LOG_DEBUG << "Error Match fired";
3184cde5d90SJames Feist             boost::container::flat_map<std::string, std::variant<std::string>>
3194cde5d90SJames Feist                 values;
3204cde5d90SJames Feist             std::string objName;
3214cde5d90SJames Feist             m.read(objName, values);
3224cde5d90SJames Feist             auto find = values.find("Message");
3234cde5d90SJames Feist             if (find == values.end())
3244cde5d90SJames Feist             {
3254cde5d90SJames Feist                 return;
3264cde5d90SJames Feist             }
3274cde5d90SJames Feist             std::string* type = std::get_if<std::string>(&(find->second));
3284cde5d90SJames Feist             if (type == nullptr)
3294cde5d90SJames Feist             {
3304cde5d90SJames Feist                 return; // if this was our message, timeout will cover it
3314cde5d90SJames Feist             }
332*88b3dd12SGunnar Mills             if (!boost::starts_with(*type, "xyz.openbmc_project.Software"))
3334cde5d90SJames Feist             {
3344cde5d90SJames Feist                 return;
3354cde5d90SJames Feist             }
3364cde5d90SJames Feist             if (*type ==
3374cde5d90SJames Feist                 "xyz.openbmc_project.Software.Image.Error.UnTarFailure")
3384cde5d90SJames Feist             {
3394cde5d90SJames Feist                 redfish::messages::invalidUpload(asyncResp->res, url,
3404cde5d90SJames Feist                                                  "Invalid archive");
3414cde5d90SJames Feist             }
3424cde5d90SJames Feist             else if (*type == "xyz.openbmc_project.Software.Image.Error."
3434cde5d90SJames Feist                               "ManifestFileFailure")
3444cde5d90SJames Feist             {
3454cde5d90SJames Feist                 redfish::messages::invalidUpload(asyncResp->res, url,
3464cde5d90SJames Feist                                                  "Invalid manifest");
3474cde5d90SJames Feist             }
3484cde5d90SJames Feist             else if (*type ==
3494cde5d90SJames Feist                      "xyz.openbmc_project.Software.Image.Error.ImageFailure")
3504cde5d90SJames Feist             {
3514cde5d90SJames Feist                 redfish::messages::invalidUpload(asyncResp->res, url,
3524cde5d90SJames Feist                                                  "Invalid image format");
3534cde5d90SJames Feist             }
354*88b3dd12SGunnar Mills             else if (*type == "xyz.openbmc_project.Software.Version.Error."
355*88b3dd12SGunnar Mills                               "AlreadyExists")
356*88b3dd12SGunnar Mills             {
357*88b3dd12SGunnar Mills 
358*88b3dd12SGunnar Mills                 redfish::messages::invalidUpload(
359*88b3dd12SGunnar Mills                     asyncResp->res, url, "Image version already exists");
360*88b3dd12SGunnar Mills 
361*88b3dd12SGunnar Mills                 redfish::messages::resourceAlreadyExists(
362*88b3dd12SGunnar Mills                     asyncResp->res, "UpdateService.v1_4_0.UpdateService",
363*88b3dd12SGunnar Mills                     "Version", "uploaded version");
364*88b3dd12SGunnar Mills             }
3654cde5d90SJames Feist             else if (*type ==
3664cde5d90SJames Feist                      "xyz.openbmc_project.Software.Image.Error.BusyFailure")
3674cde5d90SJames Feist             {
3684cde5d90SJames Feist                 redfish::messages::resourceExhaustion(asyncResp->res, url);
3694cde5d90SJames Feist             }
3704cde5d90SJames Feist             else
3714cde5d90SJames Feist             {
3724cde5d90SJames Feist                 redfish::messages::internalError(asyncResp->res);
3734cde5d90SJames Feist             }
3744cde5d90SJames Feist             fwAvailableTimer = nullptr;
3754cde5d90SJames Feist         });
37686adcd6dSAndrew Geissler }
377729dae72SJennifer Lee 
3780554c984SAndrew Geissler /**
3790554c984SAndrew Geissler  * UpdateServiceActionsSimpleUpdate class supports handle POST method for
3800554c984SAndrew Geissler  * SimpleUpdate action.
3810554c984SAndrew Geissler  */
3820554c984SAndrew Geissler class UpdateServiceActionsSimpleUpdate : public Node
3830554c984SAndrew Geissler {
3840554c984SAndrew Geissler   public:
38552cc112dSEd Tanous     UpdateServiceActionsSimpleUpdate(App& app) :
3860554c984SAndrew Geissler         Node(app,
3870554c984SAndrew Geissler              "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
3880554c984SAndrew Geissler     {
3890554c984SAndrew Geissler         entityPrivileges = {
3900554c984SAndrew Geissler             {boost::beast::http::verb::get, {{"Login"}}},
3910554c984SAndrew Geissler             {boost::beast::http::verb::head, {{"Login"}}},
3920554c984SAndrew Geissler             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3930554c984SAndrew Geissler             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
3940554c984SAndrew Geissler             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
3950554c984SAndrew Geissler             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
3960554c984SAndrew Geissler     }
3970554c984SAndrew Geissler 
3980554c984SAndrew Geissler   private:
3990554c984SAndrew Geissler     void doPost(crow::Response& res, const crow::Request& req,
400cb13a392SEd Tanous                 const std::vector<std::string>&) override
4010554c984SAndrew Geissler     {
4020554c984SAndrew Geissler         std::optional<std::string> transferProtocol;
4030554c984SAndrew Geissler         std::string imageURI;
4040554c984SAndrew Geissler         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
4050554c984SAndrew Geissler 
4060554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost";
4070554c984SAndrew Geissler 
4080554c984SAndrew Geissler         // User can pass in both TransferProtocol and ImageURI parameters or
4094e0453b1SGunnar Mills         // they can pass in just the ImageURI with the transfer protocol
4104e0453b1SGunnar Mills         // embedded within it.
4110554c984SAndrew Geissler         // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
4120554c984SAndrew Geissler         // 2) ImageURI:tftp://1.1.1.1/myfile.bin
4130554c984SAndrew Geissler 
4140554c984SAndrew Geissler         if (!json_util::readJson(req, asyncResp->res, "TransferProtocol",
4150554c984SAndrew Geissler                                  transferProtocol, "ImageURI", imageURI))
4160554c984SAndrew Geissler         {
4170554c984SAndrew Geissler             BMCWEB_LOG_DEBUG
4180554c984SAndrew Geissler                 << "Missing TransferProtocol or ImageURI parameter";
4190554c984SAndrew Geissler             return;
4200554c984SAndrew Geissler         }
4210554c984SAndrew Geissler         if (!transferProtocol)
4220554c984SAndrew Geissler         {
4230554c984SAndrew Geissler             // Must be option 2
4240554c984SAndrew Geissler             // Verify ImageURI has transfer protocol in it
425f23b7296SEd Tanous             size_t separator = imageURI.find(':');
4260554c984SAndrew Geissler             if ((separator == std::string::npos) ||
4270554c984SAndrew Geissler                 ((separator + 1) > imageURI.size()))
4280554c984SAndrew Geissler             {
4290554c984SAndrew Geissler                 messages::actionParameterValueTypeError(
4300554c984SAndrew Geissler                     asyncResp->res, imageURI, "ImageURI",
4310554c984SAndrew Geissler                     "UpdateService.SimpleUpdate");
4320554c984SAndrew Geissler                 BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: "
4330554c984SAndrew Geissler                                  << imageURI;
4340554c984SAndrew Geissler                 return;
4350554c984SAndrew Geissler             }
4360554c984SAndrew Geissler             transferProtocol = imageURI.substr(0, separator);
4370554c984SAndrew Geissler             // Ensure protocol is upper case for a common comparison path below
4380554c984SAndrew Geissler             boost::to_upper(*transferProtocol);
4390554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Encoded transfer protocol "
4400554c984SAndrew Geissler                              << *transferProtocol;
4410554c984SAndrew Geissler 
4420554c984SAndrew Geissler             // Adjust imageURI to not have the protocol on it for parsing
4430554c984SAndrew Geissler             // below
4440554c984SAndrew Geissler             // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin
4450554c984SAndrew Geissler             imageURI = imageURI.substr(separator + 3);
4460554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI;
4470554c984SAndrew Geissler         }
4480554c984SAndrew Geissler 
4490554c984SAndrew Geissler         // OpenBMC currently only supports TFTP
4500554c984SAndrew Geissler         if (*transferProtocol != "TFTP")
4510554c984SAndrew Geissler         {
4520554c984SAndrew Geissler             messages::actionParameterNotSupported(asyncResp->res,
4530554c984SAndrew Geissler                                                   "TransferProtocol",
4540554c984SAndrew Geissler                                                   "UpdateService.SimpleUpdate");
4550554c984SAndrew Geissler             BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: "
4560554c984SAndrew Geissler                              << *transferProtocol;
4570554c984SAndrew Geissler             return;
4580554c984SAndrew Geissler         }
4590554c984SAndrew Geissler 
4600554c984SAndrew Geissler         // Format should be <IP or Hostname>/<file> for imageURI
461f23b7296SEd Tanous         size_t separator = imageURI.find('/');
4620554c984SAndrew Geissler         if ((separator == std::string::npos) ||
4630554c984SAndrew Geissler             ((separator + 1) > imageURI.size()))
4640554c984SAndrew Geissler         {
4650554c984SAndrew Geissler             messages::actionParameterValueTypeError(
4660554c984SAndrew Geissler                 asyncResp->res, imageURI, "ImageURI",
4670554c984SAndrew Geissler                 "UpdateService.SimpleUpdate");
4680554c984SAndrew Geissler             BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI;
4690554c984SAndrew Geissler             return;
4700554c984SAndrew Geissler         }
4710554c984SAndrew Geissler 
4720554c984SAndrew Geissler         std::string tftpServer = imageURI.substr(0, separator);
4730554c984SAndrew Geissler         std::string fwFile = imageURI.substr(separator + 1);
4740554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile;
4750554c984SAndrew Geissler 
4760554c984SAndrew Geissler         // Setup callback for when new software detected
4772618d5e3SGunnar Mills         // Give TFTP 10 minutes to complete
4784cde5d90SJames Feist         monitorForSoftwareAvailable(
4794cde5d90SJames Feist             nullptr, req,
4804cde5d90SJames Feist             "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate",
4812618d5e3SGunnar Mills             600);
4820554c984SAndrew Geissler 
4832618d5e3SGunnar Mills         // TFTP can take up to 10 minutes depending on image size and
4840554c984SAndrew Geissler         // connection speed. Return to caller as soon as the TFTP operation
4850554c984SAndrew Geissler         // has been started. The callback above will ensure the activate
4860554c984SAndrew Geissler         // is started once the download has completed
4870554c984SAndrew Geissler         redfish::messages::success(asyncResp->res);
4880554c984SAndrew Geissler 
4890554c984SAndrew Geissler         // Call TFTP service
4900554c984SAndrew Geissler         crow::connections::systemBus->async_method_call(
4910554c984SAndrew Geissler             [](const boost::system::error_code ec) {
4920554c984SAndrew Geissler                 if (ec)
4930554c984SAndrew Geissler                 {
4940554c984SAndrew Geissler                     // messages::internalError(asyncResp->res);
4950554c984SAndrew Geissler                     cleanUp();
4960554c984SAndrew Geissler                     BMCWEB_LOG_DEBUG << "error_code = " << ec;
4970554c984SAndrew Geissler                     BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
4980554c984SAndrew Geissler                 }
4990554c984SAndrew Geissler                 else
5000554c984SAndrew Geissler                 {
5010554c984SAndrew Geissler                     BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success";
5020554c984SAndrew Geissler                 }
5030554c984SAndrew Geissler             },
5040554c984SAndrew Geissler             "xyz.openbmc_project.Software.Download",
5050554c984SAndrew Geissler             "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP",
5060554c984SAndrew Geissler             "DownloadViaTFTP", fwFile, tftpServer);
5070554c984SAndrew Geissler 
5080554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost";
5090554c984SAndrew Geissler     }
5100554c984SAndrew Geissler };
5110554c984SAndrew Geissler 
5121abe55efSEd Tanous class UpdateService : public Node
5131abe55efSEd Tanous {
514729dae72SJennifer Lee   public:
51552cc112dSEd Tanous     UpdateService(App& app) : Node(app, "/redfish/v1/UpdateService/")
5161abe55efSEd Tanous     {
517729dae72SJennifer Lee         entityPrivileges = {
518729dae72SJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
519729dae72SJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
520729dae72SJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
521729dae72SJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
522729dae72SJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
523729dae72SJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
524729dae72SJennifer Lee     }
525729dae72SJennifer Lee 
526729dae72SJennifer Lee   private:
527cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
528cb13a392SEd Tanous                const std::vector<std::string>&) override
5291abe55efSEd Tanous     {
530274dfe62SJayashankar Padath         std::shared_ptr<AsyncResp> aResp = std::make_shared<AsyncResp>(res);
531274dfe62SJayashankar Padath         res.jsonValue["@odata.type"] = "#UpdateService.v1_4_0.UpdateService";
5320f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
5330f74e643SEd Tanous         res.jsonValue["Id"] = "UpdateService";
5340f74e643SEd Tanous         res.jsonValue["Description"] = "Service for Software Update";
5350f74e643SEd Tanous         res.jsonValue["Name"] = "Update Service";
5360f74e643SEd Tanous         res.jsonValue["HttpPushUri"] = "/redfish/v1/UpdateService";
5370f74e643SEd Tanous         // UpdateService cannot be disabled
5380f74e643SEd Tanous         res.jsonValue["ServiceEnabled"] = true;
5390f74e643SEd Tanous         res.jsonValue["FirmwareInventory"] = {
5400f74e643SEd Tanous             {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}};
5410554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
5420554c984SAndrew Geissler         // Update Actions object.
5430554c984SAndrew Geissler         nlohmann::json& updateSvcSimpleUpdate =
5440554c984SAndrew Geissler             res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
5450554c984SAndrew Geissler         updateSvcSimpleUpdate["target"] =
5460554c984SAndrew Geissler             "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
5470554c984SAndrew Geissler         updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = {
5480554c984SAndrew Geissler             "TFTP"};
5490554c984SAndrew Geissler #endif
550274dfe62SJayashankar Padath         // Get the current ApplyTime value
551274dfe62SJayashankar Padath         crow::connections::systemBus->async_method_call(
552274dfe62SJayashankar Padath             [aResp](const boost::system::error_code ec,
553274dfe62SJayashankar Padath                     const std::variant<std::string>& applyTime) {
554274dfe62SJayashankar Padath                 if (ec)
555274dfe62SJayashankar Padath                 {
556274dfe62SJayashankar Padath                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
557274dfe62SJayashankar Padath                     messages::internalError(aResp->res);
558274dfe62SJayashankar Padath                     return;
559274dfe62SJayashankar Padath                 }
560274dfe62SJayashankar Padath 
561274dfe62SJayashankar Padath                 const std::string* s = std::get_if<std::string>(&applyTime);
562274dfe62SJayashankar Padath                 if (s == nullptr)
563274dfe62SJayashankar Padath                 {
564274dfe62SJayashankar Padath                     return;
565274dfe62SJayashankar Padath                 }
566274dfe62SJayashankar Padath                 // Store the ApplyTime Value
567274dfe62SJayashankar Padath                 if (*s == "xyz.openbmc_project.Software.ApplyTime."
568274dfe62SJayashankar Padath                           "RequestedApplyTimes.Immediate")
569274dfe62SJayashankar Padath                 {
570274dfe62SJayashankar Padath                     aResp->res.jsonValue["HttpPushUriOptions"]
571274dfe62SJayashankar Padath                                         ["HttpPushUriApplyTime"]["ApplyTime"] =
572274dfe62SJayashankar Padath                         "Immediate";
573274dfe62SJayashankar Padath                 }
574274dfe62SJayashankar Padath                 else if (*s == "xyz.openbmc_project.Software.ApplyTime."
575274dfe62SJayashankar Padath                                "RequestedApplyTimes.OnReset")
576274dfe62SJayashankar Padath                 {
577274dfe62SJayashankar Padath                     aResp->res.jsonValue["HttpPushUriOptions"]
578274dfe62SJayashankar Padath                                         ["HttpPushUriApplyTime"]["ApplyTime"] =
579274dfe62SJayashankar Padath                         "OnReset";
580274dfe62SJayashankar Padath                 }
581274dfe62SJayashankar Padath             },
582274dfe62SJayashankar Padath             "xyz.openbmc_project.Settings",
583274dfe62SJayashankar Padath             "/xyz/openbmc_project/software/apply_time",
584274dfe62SJayashankar Padath             "org.freedesktop.DBus.Properties", "Get",
585274dfe62SJayashankar Padath             "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime");
586729dae72SJennifer Lee     }
5870e7de46fSAndrew Geissler 
588fa1a5a38SJayashankar Padath     void doPatch(crow::Response& res, const crow::Request& req,
589cb13a392SEd Tanous                  const std::vector<std::string>&) override
590fa1a5a38SJayashankar Padath     {
591fa1a5a38SJayashankar Padath         BMCWEB_LOG_DEBUG << "doPatch...";
592fa1a5a38SJayashankar Padath 
593fa1a5a38SJayashankar Padath         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
594fa1a5a38SJayashankar Padath 
595274dfe62SJayashankar Padath         std::optional<nlohmann::json> pushUriOptions;
596274dfe62SJayashankar Padath         if (!json_util::readJson(req, res, "HttpPushUriOptions",
597274dfe62SJayashankar Padath                                  pushUriOptions))
598fa1a5a38SJayashankar Padath         {
599fa1a5a38SJayashankar Padath             return;
600fa1a5a38SJayashankar Padath         }
601fa1a5a38SJayashankar Padath 
602274dfe62SJayashankar Padath         if (pushUriOptions)
603274dfe62SJayashankar Padath         {
604274dfe62SJayashankar Padath             std::optional<nlohmann::json> pushUriApplyTime;
605274dfe62SJayashankar Padath             if (!json_util::readJson(*pushUriOptions, res,
606274dfe62SJayashankar Padath                                      "HttpPushUriApplyTime", pushUriApplyTime))
607274dfe62SJayashankar Padath             {
608274dfe62SJayashankar Padath                 return;
609274dfe62SJayashankar Padath             }
610274dfe62SJayashankar Padath 
611274dfe62SJayashankar Padath             if (pushUriApplyTime)
612274dfe62SJayashankar Padath             {
613274dfe62SJayashankar Padath                 std::optional<std::string> applyTime;
614274dfe62SJayashankar Padath                 if (!json_util::readJson(*pushUriApplyTime, res, "ApplyTime",
615274dfe62SJayashankar Padath                                          applyTime))
616274dfe62SJayashankar Padath                 {
617274dfe62SJayashankar Padath                     return;
618274dfe62SJayashankar Padath                 }
619274dfe62SJayashankar Padath 
620274dfe62SJayashankar Padath                 if (applyTime)
621fa1a5a38SJayashankar Padath                 {
622fa1a5a38SJayashankar Padath                     std::string applyTimeNewVal;
623fa1a5a38SJayashankar Padath                     if (applyTime == "Immediate")
624fa1a5a38SJayashankar Padath                     {
625274dfe62SJayashankar Padath                         applyTimeNewVal =
626274dfe62SJayashankar Padath                             "xyz.openbmc_project.Software.ApplyTime."
627fa1a5a38SJayashankar Padath                             "RequestedApplyTimes.Immediate";
628fa1a5a38SJayashankar Padath                     }
629274dfe62SJayashankar Padath                     else if (applyTime == "OnReset")
630274dfe62SJayashankar Padath                     {
631274dfe62SJayashankar Padath                         applyTimeNewVal =
632274dfe62SJayashankar Padath                             "xyz.openbmc_project.Software.ApplyTime."
633274dfe62SJayashankar Padath                             "RequestedApplyTimes.OnReset";
634274dfe62SJayashankar Padath                     }
635fa1a5a38SJayashankar Padath                     else
636fa1a5a38SJayashankar Padath                     {
637274dfe62SJayashankar Padath                         BMCWEB_LOG_INFO
638274dfe62SJayashankar Padath                             << "ApplyTime value is not in the list of "
639274dfe62SJayashankar Padath                                "acceptable values";
640274dfe62SJayashankar Padath                         messages::propertyValueNotInList(
641274dfe62SJayashankar Padath                             asyncResp->res, *applyTime, "ApplyTime");
642274dfe62SJayashankar Padath                         return;
643fa1a5a38SJayashankar Padath                     }
644fa1a5a38SJayashankar Padath 
645fa1a5a38SJayashankar Padath                     // Set the requested image apply time value
646fa1a5a38SJayashankar Padath                     crow::connections::systemBus->async_method_call(
647fa1a5a38SJayashankar Padath                         [asyncResp](const boost::system::error_code ec) {
648fa1a5a38SJayashankar Padath                             if (ec)
649fa1a5a38SJayashankar Padath                             {
650274dfe62SJayashankar Padath                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
651274dfe62SJayashankar Padath                                                  << ec;
652fa1a5a38SJayashankar Padath                                 messages::internalError(asyncResp->res);
653fa1a5a38SJayashankar Padath                                 return;
654fa1a5a38SJayashankar Padath                             }
655fa1a5a38SJayashankar Padath                             messages::success(asyncResp->res);
656fa1a5a38SJayashankar Padath                         },
657fa1a5a38SJayashankar Padath                         "xyz.openbmc_project.Settings",
658fa1a5a38SJayashankar Padath                         "/xyz/openbmc_project/software/apply_time",
659fa1a5a38SJayashankar Padath                         "org.freedesktop.DBus.Properties", "Set",
660274dfe62SJayashankar Padath                         "xyz.openbmc_project.Software.ApplyTime",
661274dfe62SJayashankar Padath                         "RequestedApplyTime",
662fa1a5a38SJayashankar Padath                         std::variant<std::string>{applyTimeNewVal});
663fa1a5a38SJayashankar Padath                 }
664274dfe62SJayashankar Padath             }
665fa1a5a38SJayashankar Padath         }
666fa1a5a38SJayashankar Padath     }
667fa1a5a38SJayashankar Padath 
668acb7cfb4SJennifer Lee     void doPost(crow::Response& res, const crow::Request& req,
669cb13a392SEd Tanous                 const std::vector<std::string>&) override
6701abe55efSEd Tanous     {
671acb7cfb4SJennifer Lee         BMCWEB_LOG_DEBUG << "doPost...";
672acb7cfb4SJennifer Lee 
6730e7de46fSAndrew Geissler         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
674acb7cfb4SJennifer Lee 
67586adcd6dSAndrew Geissler         // Setup callback for when new software detected
6764cde5d90SJames Feist         monitorForSoftwareAvailable(asyncResp, req,
6774cde5d90SJames Feist                                     "/redfish/v1/UpdateService");
678acb7cfb4SJennifer Lee 
679acb7cfb4SJennifer Lee         std::string filepath(
680acb7cfb4SJennifer Lee             "/tmp/images/" +
681acb7cfb4SJennifer Lee             boost::uuids::to_string(boost::uuids::random_generator()()));
682acb7cfb4SJennifer Lee         BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
683acb7cfb4SJennifer Lee         std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
684acb7cfb4SJennifer Lee                                         std::ofstream::trunc);
685acb7cfb4SJennifer Lee         out << req.body;
686acb7cfb4SJennifer Lee         out.close();
687acb7cfb4SJennifer Lee         BMCWEB_LOG_DEBUG << "file upload complete!!";
688acb7cfb4SJennifer Lee     }
689729dae72SJennifer Lee };
690729dae72SJennifer Lee 
6911abe55efSEd Tanous class SoftwareInventoryCollection : public Node
6921abe55efSEd Tanous {
693729dae72SJennifer Lee   public:
69452cc112dSEd Tanous     SoftwareInventoryCollection(App& app) :
6951abe55efSEd Tanous         Node(app, "/redfish/v1/UpdateService/FirmwareInventory/")
6961abe55efSEd Tanous     {
697729dae72SJennifer Lee         entityPrivileges = {
698729dae72SJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
699729dae72SJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
700729dae72SJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
701729dae72SJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
702729dae72SJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
703729dae72SJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
704729dae72SJennifer Lee     }
705729dae72SJennifer Lee 
706729dae72SJennifer Lee   private:
707cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
708cb13a392SEd Tanous                const std::vector<std::string>&) override
7091abe55efSEd Tanous     {
710c711bf86SEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
7110f74e643SEd Tanous         res.jsonValue["@odata.type"] =
7120f74e643SEd Tanous             "#SoftwareInventoryCollection.SoftwareInventoryCollection";
7130f74e643SEd Tanous         res.jsonValue["@odata.id"] =
7140f74e643SEd Tanous             "/redfish/v1/UpdateService/FirmwareInventory";
7150f74e643SEd Tanous         res.jsonValue["Name"] = "Software Inventory Collection";
716c711bf86SEd Tanous 
717c711bf86SEd Tanous         crow::connections::systemBus->async_method_call(
718c711bf86SEd Tanous             [asyncResp](
719c711bf86SEd Tanous                 const boost::system::error_code ec,
7206c4eb9deSJennifer Lee                 const std::vector<std::pair<
7211abe55efSEd Tanous                     std::string, std::vector<std::pair<
7221214b7e7SGunnar Mills                                      std::string, std::vector<std::string>>>>>&
7231214b7e7SGunnar Mills                     subtree) {
7241abe55efSEd Tanous                 if (ec)
7251abe55efSEd Tanous                 {
726f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
7276c4eb9deSJennifer Lee                     return;
728729dae72SJennifer Lee                 }
729c711bf86SEd Tanous                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
730c711bf86SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
7316c4eb9deSJennifer Lee 
7321abe55efSEd Tanous                 for (auto& obj : subtree)
7331abe55efSEd Tanous                 {
734f4b65ab1SJennifer Lee                     // if can't parse fw id then return
73527826b5fSEd Tanous                     std::size_t idPos;
736f23b7296SEd Tanous                     if ((idPos = obj.first.rfind('/')) == std::string::npos)
737f4b65ab1SJennifer Lee                     {
738f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
739f4b65ab1SJennifer Lee                         BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
740f4b65ab1SJennifer Lee                         return;
741f4b65ab1SJennifer Lee                     }
742f4b65ab1SJennifer Lee                     std::string swId = obj.first.substr(idPos + 1);
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 */
784b5a76932SEd Tanous     static void getRelatedItems(const std::shared_ptr<AsyncResp>& aResp,
78587d84729SAndrew Geissler                                 const std::string& purpose)
78687d84729SAndrew Geissler     {
78787d84729SAndrew Geissler         if (purpose == fw_util::bmcPurpose)
78887d84729SAndrew Geissler         {
78987d84729SAndrew Geissler             nlohmann::json& members = aResp->res.jsonValue["RelatedItem"];
79087d84729SAndrew Geissler             members.push_back({{"@odata.id", "/redfish/v1/Managers/bmc"}});
79187d84729SAndrew Geissler             aResp->res.jsonValue["Members@odata.count"] = members.size();
79287d84729SAndrew Geissler         }
79387d84729SAndrew Geissler         else if (purpose == fw_util::biosPurpose)
79487d84729SAndrew Geissler         {
795f723d733SGunnar Mills             nlohmann::json& members = aResp->res.jsonValue["RelatedItem"];
796f723d733SGunnar Mills             members.push_back(
797f723d733SGunnar Mills                 {{"@odata.id", "/redfish/v1/Systems/system/Bios"}});
798f723d733SGunnar Mills             aResp->res.jsonValue["Members@odata.count"] = members.size();
79987d84729SAndrew Geissler         }
80087d84729SAndrew Geissler         else
80187d84729SAndrew Geissler         {
80287d84729SAndrew Geissler             BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
80387d84729SAndrew Geissler         }
80487d84729SAndrew Geissler     }
80587d84729SAndrew Geissler 
806cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
8071abe55efSEd Tanous                const std::vector<std::string>& params) override
8081abe55efSEd Tanous     {
809c711bf86SEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
8106c4eb9deSJennifer Lee 
8111abe55efSEd Tanous         if (params.size() != 1)
8121abe55efSEd Tanous         {
813f12894f8SJason M. Bills             messages::internalError(res);
814729dae72SJennifer Lee             res.end();
815729dae72SJennifer Lee             return;
816729dae72SJennifer Lee         }
817729dae72SJennifer Lee 
8183ae837c9SEd Tanous         std::shared_ptr<std::string> swId =
819c711bf86SEd Tanous             std::make_shared<std::string>(params[0]);
820c711bf86SEd Tanous 
82155c7b7a2SEd Tanous         res.jsonValue["@odata.id"] =
8223ae837c9SEd Tanous             "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
823c711bf86SEd Tanous 
824c711bf86SEd Tanous         crow::connections::systemBus->async_method_call(
8253ae837c9SEd Tanous             [asyncResp, swId](
826c711bf86SEd Tanous                 const boost::system::error_code ec,
8276c4eb9deSJennifer Lee                 const std::vector<std::pair<
8281abe55efSEd Tanous                     std::string, std::vector<std::pair<
8291214b7e7SGunnar Mills                                      std::string, std::vector<std::string>>>>>&
8301214b7e7SGunnar Mills                     subtree) {
83155c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "doGet callback...";
8321abe55efSEd Tanous                 if (ec)
8331abe55efSEd Tanous                 {
834f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
8356c4eb9deSJennifer Lee                     return;
8366c4eb9deSJennifer Lee                 }
8376c4eb9deSJennifer Lee 
8386913228dSAndrew Geissler                 // Ensure we find our input swId, otherwise return an error
8396913228dSAndrew Geissler                 bool found = false;
8401abe55efSEd Tanous                 for (const std::pair<
8411abe55efSEd Tanous                          std::string,
8421abe55efSEd Tanous                          std::vector<
8431214b7e7SGunnar Mills                              std::pair<std::string, std::vector<std::string>>>>&
8441214b7e7SGunnar Mills                          obj : subtree)
8451abe55efSEd Tanous                 {
8463ae837c9SEd Tanous                     if (boost::ends_with(obj.first, *swId) != true)
8471abe55efSEd Tanous                     {
848acb7cfb4SJennifer Lee                         continue;
849acb7cfb4SJennifer Lee                     }
850acb7cfb4SJennifer Lee 
851f4b65ab1SJennifer Lee                     if (obj.second.size() < 1)
8521abe55efSEd Tanous                     {
853acb7cfb4SJennifer Lee                         continue;
854acb7cfb4SJennifer Lee                     }
8556c4eb9deSJennifer Lee 
8566913228dSAndrew Geissler                     found = true;
857e0dd8057SAndrew Geissler                     fw_util::getFwStatus(asyncResp, swId, obj.second[0].first);
858e0dd8057SAndrew Geissler 
85955c7b7a2SEd Tanous                     crow::connections::systemBus->async_method_call(
8601abe55efSEd Tanous                         [asyncResp,
86181ce609eSEd Tanous                          swId](const boost::system::error_code errorCode,
8621abe55efSEd Tanous                                const boost::container::flat_map<
8631abe55efSEd Tanous                                    std::string, VariantType>& propertiesList) {
86481ce609eSEd Tanous                             if (errorCode)
8651abe55efSEd Tanous                             {
866f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
8676c4eb9deSJennifer Lee                                 return;
8686c4eb9deSJennifer Lee                             }
8691abe55efSEd Tanous                             boost::container::flat_map<
8701abe55efSEd Tanous                                 std::string, VariantType>::const_iterator it =
8716c4eb9deSJennifer Lee                                 propertiesList.find("Purpose");
8721abe55efSEd Tanous                             if (it == propertiesList.end())
8731abe55efSEd Tanous                             {
8741abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
8751abe55efSEd Tanous                                     << "Can't find property \"Purpose\"!";
876f12894f8SJason M. Bills                                 messages::propertyMissing(asyncResp->res,
877f12894f8SJason M. Bills                                                           "Purpose");
8786c4eb9deSJennifer Lee                                 return;
8796c4eb9deSJennifer Lee                             }
8803ae837c9SEd Tanous                             const std::string* swInvPurpose =
881abf2add6SEd Tanous                                 std::get_if<std::string>(&it->second);
8823ae837c9SEd Tanous                             if (swInvPurpose == nullptr)
8831abe55efSEd Tanous                             {
8841abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
8851abe55efSEd Tanous                                     << "wrong types for property\"Purpose\"!";
886f12894f8SJason M. Bills                                 messages::propertyValueTypeError(asyncResp->res,
887f12894f8SJason M. Bills                                                                  "", "Purpose");
888acb7cfb4SJennifer Lee                                 return;
889acb7cfb4SJennifer Lee                             }
890c711bf86SEd Tanous 
8913ae837c9SEd Tanous                             BMCWEB_LOG_DEBUG << "swInvPurpose = "
8923ae837c9SEd Tanous                                              << *swInvPurpose;
893c711bf86SEd Tanous                             it = propertiesList.find("Version");
8941abe55efSEd Tanous                             if (it == propertiesList.end())
8951abe55efSEd Tanous                             {
8961abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
8971abe55efSEd Tanous                                     << "Can't find property \"Version\"!";
898f12894f8SJason M. Bills                                 messages::propertyMissing(asyncResp->res,
899f12894f8SJason M. Bills                                                           "Version");
900c711bf86SEd Tanous                                 return;
901acb7cfb4SJennifer Lee                             }
902acb7cfb4SJennifer Lee 
903f4b65ab1SJennifer Lee                             BMCWEB_LOG_DEBUG << "Version found!";
904c711bf86SEd Tanous 
905f4b65ab1SJennifer Lee                             const std::string* version =
906abf2add6SEd Tanous                                 std::get_if<std::string>(&it->second);
907f4b65ab1SJennifer Lee 
908f4b65ab1SJennifer Lee                             if (version == nullptr)
9091abe55efSEd Tanous                             {
9101abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
9111abe55efSEd Tanous                                     << "Can't find property \"Version\"!";
912f12894f8SJason M. Bills 
913f12894f8SJason M. Bills                                 messages::propertyValueTypeError(asyncResp->res,
914f12894f8SJason M. Bills                                                                  "", "Version");
9156c4eb9deSJennifer Lee                                 return;
9166c4eb9deSJennifer Lee                             }
917c711bf86SEd Tanous                             asyncResp->res.jsonValue["Version"] = *version;
9183ae837c9SEd Tanous                             asyncResp->res.jsonValue["Id"] = *swId;
91954daabe7SAndrew Geissler 
92054daabe7SAndrew Geissler                             // swInvPurpose is of format:
92154daabe7SAndrew Geissler                             // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
922e2e96770SJames Feist                             // Translate this to "ABC image"
923f23b7296SEd Tanous                             size_t endDesc = swInvPurpose->rfind('.');
92454daabe7SAndrew Geissler                             if (endDesc == std::string::npos)
92554daabe7SAndrew Geissler                             {
92654daabe7SAndrew Geissler                                 messages::internalError(asyncResp->res);
92754daabe7SAndrew Geissler                                 return;
92854daabe7SAndrew Geissler                             }
92954daabe7SAndrew Geissler                             endDesc++;
93054daabe7SAndrew Geissler                             if (endDesc >= swInvPurpose->size())
93154daabe7SAndrew Geissler                             {
93254daabe7SAndrew Geissler                                 messages::internalError(asyncResp->res);
93354daabe7SAndrew Geissler                                 return;
93454daabe7SAndrew Geissler                             }
93554daabe7SAndrew Geissler 
93654daabe7SAndrew Geissler                             std::string formatDesc =
93754daabe7SAndrew Geissler                                 swInvPurpose->substr(endDesc);
93854daabe7SAndrew Geissler                             asyncResp->res.jsonValue["Description"] =
939e2e96770SJames Feist                                 formatDesc + " image";
94087d84729SAndrew Geissler                             getRelatedItems(asyncResp, *swInvPurpose);
9416c4eb9deSJennifer Lee                         },
942c711bf86SEd Tanous                         obj.second[0].first, obj.first,
943c711bf86SEd Tanous                         "org.freedesktop.DBus.Properties", "GetAll",
944c711bf86SEd Tanous                         "xyz.openbmc_project.Software.Version");
9456c4eb9deSJennifer Lee                 }
9466913228dSAndrew Geissler                 if (!found)
9476913228dSAndrew Geissler                 {
9486913228dSAndrew Geissler                     BMCWEB_LOG_ERROR << "Input swID " + *swId + " not found!";
9496913228dSAndrew Geissler                     messages::resourceMissingAtURI(
9506913228dSAndrew Geissler                         asyncResp->res,
9516913228dSAndrew Geissler                         "/redfish/v1/UpdateService/FirmwareInventory/" + *swId);
9526913228dSAndrew Geissler                     return;
9536913228dSAndrew Geissler                 }
9544e68c45bSAyushi Smriti                 asyncResp->res.jsonValue["@odata.type"] =
9554e68c45bSAyushi Smriti                     "#SoftwareInventory.v1_1_0.SoftwareInventory";
9564e68c45bSAyushi Smriti                 asyncResp->res.jsonValue["Name"] = "Software Inventory";
9574e68c45bSAyushi Smriti                 asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
9583f8a743aSAppaRao Puli 
9593f8a743aSAppaRao Puli                 asyncResp->res.jsonValue["Updateable"] = false;
9603f8a743aSAppaRao Puli                 fw_util::getFwUpdateableStatus(asyncResp, swId);
961c711bf86SEd Tanous             },
962c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper",
963c711bf86SEd Tanous             "/xyz/openbmc_project/object_mapper",
964271584abSEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
965271584abSEd Tanous             static_cast<int32_t>(0),
9661214b7e7SGunnar Mills             std::array<const char*, 1>{"xyz.openbmc_project.Software.Version"});
9676c4eb9deSJennifer Lee     }
968729dae72SJennifer Lee };
969729dae72SJennifer Lee 
970729dae72SJennifer Lee } // namespace redfish
971