xref: /openbmc/bmcweb/features/redfish/lib/update_service.hpp (revision 59d494ee9608850f17bd7f4644838c26daab2669)
1729dae72SJennifer Lee /*
2729dae72SJennifer Lee // Copyright (c) 2018 Intel Corporation
3729dae72SJennifer Lee //
4729dae72SJennifer Lee // Licensed under the Apache License, Version 2.0 (the "License");
5729dae72SJennifer Lee // you may not use this file except in compliance with the License.
6729dae72SJennifer Lee // You may obtain a copy of the License at
7729dae72SJennifer Lee //
8729dae72SJennifer Lee //      http://www.apache.org/licenses/LICENSE-2.0
9729dae72SJennifer Lee //
10729dae72SJennifer Lee // Unless required by applicable law or agreed to in writing, software
11729dae72SJennifer Lee // distributed under the License is distributed on an "AS IS" BASIS,
12729dae72SJennifer Lee // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13729dae72SJennifer Lee // See the License for the specific language governing permissions and
14729dae72SJennifer Lee // limitations under the License.
15729dae72SJennifer Lee */
16729dae72SJennifer Lee #pragma once
17729dae72SJennifer Lee 
18d61e5194STejas Patil #include "bmcweb_config.h"
19d61e5194STejas Patil 
207e860f15SJohn Edward Broadbent #include <app.hpp>
21168e20c1SEd Tanous #include <dbus_utility.hpp>
2245ca1b86SEd Tanous #include <query.hpp>
23ed398213SEd Tanous #include <registries/privilege_registry.hpp>
241e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
25eee0013eSWilly Tu #include <utils/sw_utils.hpp>
261214b7e7SGunnar Mills 
271abe55efSEd Tanous namespace redfish
281abe55efSEd Tanous {
2927826b5fSEd Tanous 
300e7de46fSAndrew Geissler // Match signals added on software path
31*59d494eeSPatrick Williams static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateMatcher;
32*59d494eeSPatrick Williams static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateErrorMatcher;
330e7de46fSAndrew Geissler // Only allow one update at a time
340e7de46fSAndrew Geissler static bool fwUpdateInProgress = false;
3586adcd6dSAndrew Geissler // Timer for software available
36271584abSEd Tanous static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer;
3786adcd6dSAndrew Geissler 
387e860f15SJohn Edward Broadbent inline static void cleanUp()
3986adcd6dSAndrew Geissler {
4086adcd6dSAndrew Geissler     fwUpdateInProgress = false;
4186adcd6dSAndrew Geissler     fwUpdateMatcher = nullptr;
424cde5d90SJames Feist     fwUpdateErrorMatcher = nullptr;
4386adcd6dSAndrew Geissler }
447e860f15SJohn Edward Broadbent inline static void activateImage(const std::string& objPath,
4586adcd6dSAndrew Geissler                                  const std::string& service)
4686adcd6dSAndrew Geissler {
4786adcd6dSAndrew Geissler     BMCWEB_LOG_DEBUG << "Activate image for " << objPath << " " << service;
4886adcd6dSAndrew Geissler     crow::connections::systemBus->async_method_call(
4981ce609eSEd Tanous         [](const boost::system::error_code errorCode) {
5081ce609eSEd Tanous         if (errorCode)
5186adcd6dSAndrew Geissler         {
5281ce609eSEd Tanous             BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
5381ce609eSEd Tanous             BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message();
5486adcd6dSAndrew Geissler         }
5586adcd6dSAndrew Geissler         },
5686adcd6dSAndrew Geissler         service, objPath, "org.freedesktop.DBus.Properties", "Set",
5786adcd6dSAndrew Geissler         "xyz.openbmc_project.Software.Activation", "RequestedActivation",
58168e20c1SEd Tanous         dbus::utility::DbusVariantType(
590fda0f12SGeorge Liu             "xyz.openbmc_project.Software.Activation.RequestedActivations.Active"));
6086adcd6dSAndrew Geissler }
610554c984SAndrew Geissler 
620554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr
630554c984SAndrew Geissler // then no asyncResp updates will occur
648d1b46d7Szhanghch05 static void
658d1b46d7Szhanghch05     softwareInterfaceAdded(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
66*59d494eeSPatrick Williams                            sdbusplus::message_t& m, task::Payload&& payload)
6786adcd6dSAndrew Geissler {
68b9d36b47SEd Tanous     dbus::utility::DBusInteracesMap interfacesProperties;
6986adcd6dSAndrew Geissler 
7086adcd6dSAndrew Geissler     sdbusplus::message::object_path objPath;
7186adcd6dSAndrew Geissler 
7286adcd6dSAndrew Geissler     m.read(objPath, interfacesProperties);
7386adcd6dSAndrew Geissler 
7486adcd6dSAndrew Geissler     BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
7586adcd6dSAndrew Geissler     for (auto& interface : interfacesProperties)
7686adcd6dSAndrew Geissler     {
7786adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "interface = " << interface.first;
7886adcd6dSAndrew Geissler 
7986adcd6dSAndrew Geissler         if (interface.first == "xyz.openbmc_project.Software.Activation")
8086adcd6dSAndrew Geissler         {
8186adcd6dSAndrew Geissler             // Retrieve service and activate
8286adcd6dSAndrew Geissler             crow::connections::systemBus->async_method_call(
83a3e65892SEd Tanous                 [objPath, asyncResp, payload(std::move(payload))](
84a3e65892SEd Tanous                     const boost::system::error_code errorCode,
85a3e65892SEd Tanous                     const std::vector<
86a3e65892SEd Tanous                         std::pair<std::string, std::vector<std::string>>>&
87a3e65892SEd Tanous                         objInfo) mutable {
8881ce609eSEd Tanous                 if (errorCode)
8986adcd6dSAndrew Geissler                 {
9081ce609eSEd Tanous                     BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
91002d39b4SEd Tanous                     BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message();
920554c984SAndrew Geissler                     if (asyncResp)
930554c984SAndrew Geissler                     {
9486adcd6dSAndrew Geissler                         messages::internalError(asyncResp->res);
950554c984SAndrew Geissler                     }
9686adcd6dSAndrew Geissler                     cleanUp();
9786adcd6dSAndrew Geissler                     return;
9886adcd6dSAndrew Geissler                 }
9986adcd6dSAndrew Geissler                 // Ensure we only got one service back
10086adcd6dSAndrew Geissler                 if (objInfo.size() != 1)
10186adcd6dSAndrew Geissler                 {
10286adcd6dSAndrew Geissler                     BMCWEB_LOG_ERROR << "Invalid Object Size "
10386adcd6dSAndrew Geissler                                      << objInfo.size();
1040554c984SAndrew Geissler                     if (asyncResp)
1050554c984SAndrew Geissler                     {
10686adcd6dSAndrew Geissler                         messages::internalError(asyncResp->res);
1070554c984SAndrew Geissler                     }
10886adcd6dSAndrew Geissler                     cleanUp();
10986adcd6dSAndrew Geissler                     return;
11086adcd6dSAndrew Geissler                 }
11186adcd6dSAndrew Geissler                 // cancel timer only when
11286adcd6dSAndrew Geissler                 // xyz.openbmc_project.Software.Activation interface
11386adcd6dSAndrew Geissler                 // is added
11486adcd6dSAndrew Geissler                 fwAvailableTimer = nullptr;
11586adcd6dSAndrew Geissler 
11686adcd6dSAndrew Geissler                 activateImage(objPath.str, objInfo[0].first);
1170554c984SAndrew Geissler                 if (asyncResp)
1180554c984SAndrew Geissler                 {
11932898ceaSJames Feist                     std::shared_ptr<task::TaskData> task =
12032898ceaSJames Feist                         task::TaskData::createTask(
12132898ceaSJames Feist                             [](boost::system::error_code ec,
122*59d494eeSPatrick Williams                                sdbusplus::message_t& msg,
1231214b7e7SGunnar Mills                                const std::shared_ptr<task::TaskData>&
1241214b7e7SGunnar Mills                                    taskData) {
12532898ceaSJames Feist                         if (ec)
12632898ceaSJames Feist                         {
12732898ceaSJames Feist                             return task::completed;
12832898ceaSJames Feist                         }
12932898ceaSJames Feist 
13032898ceaSJames Feist                         std::string iface;
131b9d36b47SEd Tanous                         dbus::utility::DBusPropertiesMap values;
132fd9ab9e1SJames Feist 
133002d39b4SEd Tanous                         std::string index = std::to_string(taskData->index);
13432898ceaSJames Feist                         msg.read(iface, values);
135fd9ab9e1SJames Feist 
136002d39b4SEd Tanous                         if (iface == "xyz.openbmc_project.Software.Activation")
137fd9ab9e1SJames Feist                         {
138b9d36b47SEd Tanous                             std::string* state = nullptr;
139b9d36b47SEd Tanous                             for (const auto& property : values)
14032898ceaSJames Feist                             {
141b9d36b47SEd Tanous                                 if (property.first == "Activation")
142b9d36b47SEd Tanous                                 {
1438a592810SEd Tanous                                     const std::string* activationState =
14432898ceaSJames Feist                                         std::get_if<std::string>(
145b9d36b47SEd Tanous                                             &property.second);
1468a592810SEd Tanous                                     if (activationState == nullptr)
147b9d36b47SEd Tanous                                     {
148002d39b4SEd Tanous                                         taskData->messages.emplace_back(
149002d39b4SEd Tanous                                             messages::internalError());
150b9d36b47SEd Tanous                                         return task::completed;
151b9d36b47SEd Tanous                                     }
152b9d36b47SEd Tanous                                 }
153b9d36b47SEd Tanous                             }
15432898ceaSJames Feist 
15532898ceaSJames Feist                             if (state == nullptr)
15632898ceaSJames Feist                             {
157b9d36b47SEd Tanous                                 return !task::completed;
15832898ceaSJames Feist                             }
15932898ceaSJames Feist 
16011ba3979SEd Tanous                             if (state->ends_with("Invalid") ||
16111ba3979SEd Tanous                                 state->ends_with("Failed"))
16232898ceaSJames Feist                             {
16332898ceaSJames Feist                                 taskData->state = "Exception";
16432898ceaSJames Feist                                 taskData->status = "Warning";
16532898ceaSJames Feist                                 taskData->messages.emplace_back(
166e5d5006bSJames Feist                                     messages::taskAborted(index));
16732898ceaSJames Feist                                 return task::completed;
16832898ceaSJames Feist                             }
16932898ceaSJames Feist 
17011ba3979SEd Tanous                             if (state->ends_with("Staged"))
17132898ceaSJames Feist                             {
172fd9ab9e1SJames Feist                                 taskData->state = "Stopping";
173fd9ab9e1SJames Feist                                 taskData->messages.emplace_back(
174fd9ab9e1SJames Feist                                     messages::taskPaused(index));
175fd9ab9e1SJames Feist 
176fd9ab9e1SJames Feist                                 // its staged, set a long timer to
177fd9ab9e1SJames Feist                                 // allow them time to complete the
178fd9ab9e1SJames Feist                                 // update (probably cycle the
179fd9ab9e1SJames Feist                                 // system) if this expires then
180fd9ab9e1SJames Feist                                 // task will be cancelled
181002d39b4SEd Tanous                                 taskData->extendTimer(std::chrono::hours(5));
18232898ceaSJames Feist                                 return !task::completed;
18332898ceaSJames Feist                             }
18432898ceaSJames Feist 
18511ba3979SEd Tanous                             if (state->ends_with("Active"))
18632898ceaSJames Feist                             {
18732898ceaSJames Feist                                 taskData->messages.emplace_back(
188002d39b4SEd Tanous                                     messages::taskCompletedOK(index));
18932898ceaSJames Feist                                 taskData->state = "Completed";
19032898ceaSJames Feist                                 return task::completed;
19132898ceaSJames Feist                             }
192fd9ab9e1SJames Feist                         }
1930fda0f12SGeorge Liu                         else if (
1940fda0f12SGeorge Liu                             iface ==
1950fda0f12SGeorge Liu                             "xyz.openbmc_project.Software.ActivationProgress")
196fd9ab9e1SJames Feist                         {
197b9d36b47SEd Tanous 
198b9d36b47SEd Tanous                             const uint8_t* progress = nullptr;
199b9d36b47SEd Tanous                             for (const auto& property : values)
200fd9ab9e1SJames Feist                             {
201b9d36b47SEd Tanous                                 if (property.first == "Progress")
202b9d36b47SEd Tanous                                 {
2038a592810SEd Tanous                                     const std::string* progressStr =
204b9d36b47SEd Tanous                                         std::get_if<std::string>(
205b9d36b47SEd Tanous                                             &property.second);
2068a592810SEd Tanous                                     if (progressStr == nullptr)
207b9d36b47SEd Tanous                                     {
208002d39b4SEd Tanous                                         taskData->messages.emplace_back(
209002d39b4SEd Tanous                                             messages::internalError());
210b9d36b47SEd Tanous                                         return task::completed;
211fd9ab9e1SJames Feist                                     }
212b9d36b47SEd Tanous                                 }
213b9d36b47SEd Tanous                             }
214fd9ab9e1SJames Feist 
215fd9ab9e1SJames Feist                             if (progress == nullptr)
216fd9ab9e1SJames Feist                             {
217b9d36b47SEd Tanous                                 return !task::completed;
218fd9ab9e1SJames Feist                             }
2196868ff50SGeorge Liu                             taskData->percentComplete =
2206868ff50SGeorge Liu                                 static_cast<int>(*progress);
221fd9ab9e1SJames Feist                             taskData->messages.emplace_back(
222fd9ab9e1SJames Feist                                 messages::taskProgressChanged(
223002d39b4SEd Tanous                                     index, static_cast<size_t>(*progress)));
224fd9ab9e1SJames Feist 
225fd9ab9e1SJames Feist                             // if we're getting status updates it's
226fd9ab9e1SJames Feist                             // still alive, update timer
227002d39b4SEd Tanous                             taskData->extendTimer(std::chrono::minutes(5));
228fd9ab9e1SJames Feist                         }
22932898ceaSJames Feist 
23032898ceaSJames Feist                         // as firmware update often results in a
23132898ceaSJames Feist                         // reboot, the task  may never "complete"
23232898ceaSJames Feist                         // unless it is an error
23332898ceaSJames Feist 
23432898ceaSJames Feist                         return !task::completed;
23532898ceaSJames Feist                             },
2360fda0f12SGeorge Liu                             "type='signal',interface='org.freedesktop.DBus.Properties',"
237fd9ab9e1SJames Feist                             "member='PropertiesChanged',path='" +
23832898ceaSJames Feist                                 objPath.str + "'");
23932898ceaSJames Feist                     task->startTimer(std::chrono::minutes(5));
24032898ceaSJames Feist                     task->populateResp(asyncResp->res);
241a3e65892SEd Tanous                     task->payload.emplace(std::move(payload));
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(
2578d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2588d1b46d7Szhanghch05     const crow::Request& req, const std::string& url,
2598d1b46d7Szhanghch05     int timeoutTimeSeconds = 10)
26086adcd6dSAndrew Geissler {
26186adcd6dSAndrew Geissler     // Only allow one FW update at a time
262e05aec50SEd Tanous     if (fwUpdateInProgress)
26386adcd6dSAndrew Geissler     {
2640554c984SAndrew Geissler         if (asyncResp)
2650554c984SAndrew Geissler         {
26686adcd6dSAndrew Geissler             messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
2670554c984SAndrew Geissler         }
26886adcd6dSAndrew Geissler         return;
26986adcd6dSAndrew Geissler     }
27086adcd6dSAndrew Geissler 
2710554c984SAndrew Geissler     fwAvailableTimer =
272271584abSEd Tanous         std::make_unique<boost::asio::steady_timer>(*req.ioService);
27386adcd6dSAndrew Geissler 
274271584abSEd Tanous     fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds));
27586adcd6dSAndrew Geissler 
27686adcd6dSAndrew Geissler     fwAvailableTimer->async_wait(
27786adcd6dSAndrew Geissler         [asyncResp](const boost::system::error_code& ec) {
27886adcd6dSAndrew Geissler         cleanUp();
27986adcd6dSAndrew Geissler         if (ec == boost::asio::error::operation_aborted)
28086adcd6dSAndrew Geissler         {
28186adcd6dSAndrew Geissler             // expected, we were canceled before the timer completed.
28286adcd6dSAndrew Geissler             return;
28386adcd6dSAndrew Geissler         }
28486adcd6dSAndrew Geissler         BMCWEB_LOG_ERROR
28586adcd6dSAndrew Geissler             << "Timed out waiting for firmware object being created";
286002d39b4SEd Tanous         BMCWEB_LOG_ERROR << "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     });
297a3e65892SEd Tanous     task::Payload payload(req);
298*59d494eeSPatrick Williams     auto callback = [asyncResp, payload](sdbusplus::message_t& m) mutable {
29986adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "Match fired";
300a3e65892SEd Tanous         softwareInterfaceAdded(asyncResp, m, std::move(payload));
30186adcd6dSAndrew Geissler     };
30286adcd6dSAndrew Geissler 
30386adcd6dSAndrew Geissler     fwUpdateInProgress = true;
30486adcd6dSAndrew Geissler 
305*59d494eeSPatrick Williams     fwUpdateMatcher = std::make_unique<sdbusplus::bus::match_t>(
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 
311*59d494eeSPatrick Williams     fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match_t>(
3124cde5d90SJames Feist         *crow::connections::systemBus,
313e1cc4828SBrian Ma         "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
314e1cc4828SBrian Ma         "member='InterfacesAdded',"
315e1cc4828SBrian Ma         "path='/xyz/openbmc_project/logging'",
316*59d494eeSPatrick Williams         [asyncResp, url](sdbusplus::message_t& m) {
317002d39b4SEd Tanous         std::vector<std::pair<std::string, dbus::utility::DBusPropertiesMap>>
318e1cc4828SBrian Ma             interfacesProperties;
319e1cc4828SBrian Ma         sdbusplus::message::object_path objPath;
320e1cc4828SBrian Ma         m.read(objPath, interfacesProperties);
321e1cc4828SBrian Ma         BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
322e1cc4828SBrian Ma         for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>&
323e1cc4828SBrian Ma                  interface : interfacesProperties)
324e1cc4828SBrian Ma         {
325e1cc4828SBrian Ma             if (interface.first == "xyz.openbmc_project.Logging.Entry")
326e1cc4828SBrian Ma             {
327711ac7a9SEd Tanous                 for (const std::pair<std::string,
328002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& value :
329002d39b4SEd Tanous                      interface.second)
3304cde5d90SJames Feist                 {
331711ac7a9SEd Tanous                     if (value.first != "Message")
332711ac7a9SEd Tanous                     {
333711ac7a9SEd Tanous                         continue;
3344cde5d90SJames Feist                     }
335e1cc4828SBrian Ma                     const std::string* type =
336711ac7a9SEd Tanous                         std::get_if<std::string>(&value.second);
3374cde5d90SJames Feist                     if (type == nullptr)
3384cde5d90SJames Feist                     {
339e1cc4828SBrian Ma                         // if this was our message, timeout will cover it
3404cde5d90SJames Feist                         return;
3414cde5d90SJames Feist                     }
342e1cc4828SBrian Ma                     fwAvailableTimer = nullptr;
3434cde5d90SJames Feist                     if (*type ==
3444cde5d90SJames Feist                         "xyz.openbmc_project.Software.Image.Error.UnTarFailure")
3454cde5d90SJames Feist                     {
346002d39b4SEd Tanous                         redfish::messages::invalidUpload(asyncResp->res, url,
347002d39b4SEd Tanous                                                          "Invalid archive");
3484cde5d90SJames Feist                     }
349e1cc4828SBrian Ma                     else if (*type ==
350e1cc4828SBrian Ma                              "xyz.openbmc_project.Software.Image.Error."
351e1cc4828SBrian Ma                              "ManifestFileFailure")
3524cde5d90SJames Feist                     {
353002d39b4SEd Tanous                         redfish::messages::invalidUpload(asyncResp->res, url,
354002d39b4SEd Tanous                                                          "Invalid manifest");
3554cde5d90SJames Feist                     }
356e1cc4828SBrian Ma                     else if (
357e1cc4828SBrian Ma                         *type ==
3584cde5d90SJames Feist                         "xyz.openbmc_project.Software.Image.Error.ImageFailure")
3594cde5d90SJames Feist                     {
360e1cc4828SBrian Ma                         redfish::messages::invalidUpload(
361e1cc4828SBrian Ma                             asyncResp->res, url, "Invalid image format");
3624cde5d90SJames Feist                     }
363e1cc4828SBrian Ma                     else if (
364e1cc4828SBrian Ma                         *type ==
3650fda0f12SGeorge Liu                         "xyz.openbmc_project.Software.Version.Error.AlreadyExists")
36688b3dd12SGunnar Mills                     {
36788b3dd12SGunnar Mills                         redfish::messages::invalidUpload(
368e1cc4828SBrian Ma                             asyncResp->res, url,
369e1cc4828SBrian Ma                             "Image version already exists");
37088b3dd12SGunnar Mills 
37188b3dd12SGunnar Mills                         redfish::messages::resourceAlreadyExists(
372e1cc4828SBrian Ma                             asyncResp->res,
373e1cc4828SBrian Ma                             "UpdateService.v1_5_0.UpdateService", "Version",
374e1cc4828SBrian Ma                             "uploaded version");
37588b3dd12SGunnar Mills                     }
376e1cc4828SBrian Ma                     else if (
377e1cc4828SBrian Ma                         *type ==
3784cde5d90SJames Feist                         "xyz.openbmc_project.Software.Image.Error.BusyFailure")
3794cde5d90SJames Feist                     {
380002d39b4SEd Tanous                         redfish::messages::resourceExhaustion(asyncResp->res,
381002d39b4SEd Tanous                                                               url);
3824cde5d90SJames Feist                     }
3834cde5d90SJames Feist                     else
3844cde5d90SJames Feist                     {
3854cde5d90SJames Feist                         redfish::messages::internalError(asyncResp->res);
3864cde5d90SJames Feist                     }
387e1cc4828SBrian Ma                 }
388e1cc4828SBrian Ma             }
389711ac7a9SEd Tanous         }
3904cde5d90SJames Feist         });
39186adcd6dSAndrew Geissler }
392729dae72SJennifer Lee 
3930554c984SAndrew Geissler /**
3940554c984SAndrew Geissler  * UpdateServiceActionsSimpleUpdate class supports handle POST method for
3950554c984SAndrew Geissler  * SimpleUpdate action.
3960554c984SAndrew Geissler  */
3977e860f15SJohn Edward Broadbent inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app)
3980554c984SAndrew Geissler {
3997e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
4007e860f15SJohn Edward Broadbent         app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
401ed398213SEd Tanous         .privileges(redfish::privileges::postUpdateService)
402002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
403002d39b4SEd Tanous             [&app](const crow::Request& req,
4047e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
4053ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
40645ca1b86SEd Tanous         {
40745ca1b86SEd Tanous             return;
40845ca1b86SEd Tanous         }
40945ca1b86SEd Tanous 
4100554c984SAndrew Geissler         std::optional<std::string> transferProtocol;
4110554c984SAndrew Geissler         std::string imageURI;
4120554c984SAndrew Geissler 
4130554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost";
4140554c984SAndrew Geissler 
4150554c984SAndrew Geissler         // User can pass in both TransferProtocol and ImageURI parameters or
4164e0453b1SGunnar Mills         // they can pass in just the ImageURI with the transfer protocol
4174e0453b1SGunnar Mills         // embedded within it.
4180554c984SAndrew Geissler         // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
4190554c984SAndrew Geissler         // 2) ImageURI:tftp://1.1.1.1/myfile.bin
4200554c984SAndrew Geissler 
421002d39b4SEd Tanous         if (!json_util::readJsonAction(req, asyncResp->res, "TransferProtocol",
422002d39b4SEd Tanous                                        transferProtocol, "ImageURI", imageURI))
4230554c984SAndrew Geissler         {
4240554c984SAndrew Geissler             BMCWEB_LOG_DEBUG
4250554c984SAndrew Geissler                 << "Missing TransferProtocol or ImageURI parameter";
4260554c984SAndrew Geissler             return;
4270554c984SAndrew Geissler         }
4280554c984SAndrew Geissler         if (!transferProtocol)
4290554c984SAndrew Geissler         {
4300554c984SAndrew Geissler             // Must be option 2
4310554c984SAndrew Geissler             // Verify ImageURI has transfer protocol in it
432f23b7296SEd Tanous             size_t separator = imageURI.find(':');
4330554c984SAndrew Geissler             if ((separator == std::string::npos) ||
4340554c984SAndrew Geissler                 ((separator + 1) > imageURI.size()))
4350554c984SAndrew Geissler             {
4360554c984SAndrew Geissler                 messages::actionParameterValueTypeError(
4370554c984SAndrew Geissler                     asyncResp->res, imageURI, "ImageURI",
4380554c984SAndrew Geissler                     "UpdateService.SimpleUpdate");
4390554c984SAndrew Geissler                 BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: "
4400554c984SAndrew Geissler                                  << imageURI;
4410554c984SAndrew Geissler                 return;
4420554c984SAndrew Geissler             }
4430554c984SAndrew Geissler             transferProtocol = imageURI.substr(0, separator);
4447e860f15SJohn Edward Broadbent             // Ensure protocol is upper case for a common comparison path
4457e860f15SJohn Edward Broadbent             // below
4460554c984SAndrew Geissler             boost::to_upper(*transferProtocol);
4470554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Encoded transfer protocol "
4480554c984SAndrew Geissler                              << *transferProtocol;
4490554c984SAndrew Geissler 
4500554c984SAndrew Geissler             // Adjust imageURI to not have the protocol on it for parsing
4510554c984SAndrew Geissler             // below
4520554c984SAndrew Geissler             // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin
4530554c984SAndrew Geissler             imageURI = imageURI.substr(separator + 3);
4540554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI;
4550554c984SAndrew Geissler         }
4560554c984SAndrew Geissler 
4570554c984SAndrew Geissler         // OpenBMC currently only supports TFTP
4580554c984SAndrew Geissler         if (*transferProtocol != "TFTP")
4590554c984SAndrew Geissler         {
460002d39b4SEd Tanous             messages::actionParameterNotSupported(asyncResp->res,
461002d39b4SEd Tanous                                                   "TransferProtocol",
4620554c984SAndrew Geissler                                                   "UpdateService.SimpleUpdate");
4630554c984SAndrew Geissler             BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: "
4640554c984SAndrew Geissler                              << *transferProtocol;
4650554c984SAndrew Geissler             return;
4660554c984SAndrew Geissler         }
4670554c984SAndrew Geissler 
4680554c984SAndrew Geissler         // Format should be <IP or Hostname>/<file> for imageURI
469f23b7296SEd Tanous         size_t separator = imageURI.find('/');
4700554c984SAndrew Geissler         if ((separator == std::string::npos) ||
4710554c984SAndrew Geissler             ((separator + 1) > imageURI.size()))
4720554c984SAndrew Geissler         {
4730554c984SAndrew Geissler             messages::actionParameterValueTypeError(
4740554c984SAndrew Geissler                 asyncResp->res, imageURI, "ImageURI",
4750554c984SAndrew Geissler                 "UpdateService.SimpleUpdate");
4760554c984SAndrew Geissler             BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI;
4770554c984SAndrew Geissler             return;
4780554c984SAndrew Geissler         }
4790554c984SAndrew Geissler 
4800554c984SAndrew Geissler         std::string tftpServer = imageURI.substr(0, separator);
4810554c984SAndrew Geissler         std::string fwFile = imageURI.substr(separator + 1);
4820554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile;
4830554c984SAndrew Geissler 
4840554c984SAndrew Geissler         // Setup callback for when new software detected
4852618d5e3SGunnar Mills         // Give TFTP 10 minutes to complete
4864cde5d90SJames Feist         monitorForSoftwareAvailable(
487d7a596bdSAlbert Zhang             asyncResp, req,
4884cde5d90SJames Feist             "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate",
4892618d5e3SGunnar Mills             600);
4900554c984SAndrew Geissler 
4912618d5e3SGunnar Mills         // TFTP can take up to 10 minutes depending on image size and
4920554c984SAndrew Geissler         // connection speed. Return to caller as soon as the TFTP operation
4930554c984SAndrew Geissler         // has been started. The callback above will ensure the activate
4940554c984SAndrew Geissler         // is started once the download has completed
4950554c984SAndrew Geissler         redfish::messages::success(asyncResp->res);
4960554c984SAndrew Geissler 
4970554c984SAndrew Geissler         // Call TFTP service
4980554c984SAndrew Geissler         crow::connections::systemBus->async_method_call(
4990554c984SAndrew Geissler             [](const boost::system::error_code ec) {
5000554c984SAndrew Geissler             if (ec)
5010554c984SAndrew Geissler             {
5020554c984SAndrew Geissler                 // messages::internalError(asyncResp->res);
5030554c984SAndrew Geissler                 cleanUp();
5040554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG << "error_code = " << ec;
5050554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
5060554c984SAndrew Geissler             }
5070554c984SAndrew Geissler             else
5080554c984SAndrew Geissler             {
5090554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success";
5100554c984SAndrew Geissler             }
5110554c984SAndrew Geissler             },
5120554c984SAndrew Geissler             "xyz.openbmc_project.Software.Download",
513002d39b4SEd Tanous             "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP",
514002d39b4SEd Tanous             "DownloadViaTFTP", fwFile, tftpServer);
5150554c984SAndrew Geissler 
5160554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost";
5177e860f15SJohn Edward Broadbent         });
518729dae72SJennifer Lee }
519729dae72SJennifer Lee 
520c2051d11SEd Tanous inline void
521c2051d11SEd Tanous     handleUpdateServicePost(App& app, const crow::Request& req,
522c2051d11SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
523c2051d11SEd Tanous {
5243ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
525c2051d11SEd Tanous     {
526c2051d11SEd Tanous         return;
527c2051d11SEd Tanous     }
528c2051d11SEd Tanous     BMCWEB_LOG_DEBUG << "doPost...";
529c2051d11SEd Tanous 
530c2051d11SEd Tanous     // Setup callback for when new software detected
531c2051d11SEd Tanous     monitorForSoftwareAvailable(asyncResp, req, "/redfish/v1/UpdateService");
532c2051d11SEd Tanous 
533c2051d11SEd Tanous     std::string filepath(
534c2051d11SEd Tanous         "/tmp/images/" +
535c2051d11SEd Tanous         boost::uuids::to_string(boost::uuids::random_generator()()));
536c2051d11SEd Tanous     BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
537c2051d11SEd Tanous     std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
538c2051d11SEd Tanous                                     std::ofstream::trunc);
539c2051d11SEd Tanous     out << req.body;
540c2051d11SEd Tanous     out.close();
541c2051d11SEd Tanous     BMCWEB_LOG_DEBUG << "file upload complete!!";
542c2051d11SEd Tanous }
543c2051d11SEd Tanous 
5447e860f15SJohn Edward Broadbent inline void requestRoutesUpdateService(App& app)
5451abe55efSEd Tanous {
5467e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
547ed398213SEd Tanous         .privileges(redfish::privileges::getUpdateService)
548002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
549002d39b4SEd Tanous             [&app](const crow::Request& req,
550002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
5513ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
55245ca1b86SEd Tanous         {
55345ca1b86SEd Tanous             return;
55445ca1b86SEd Tanous         }
5558d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
5560588a3b9SChicago Duan             "#UpdateService.v1_5_0.UpdateService";
5578d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
5588d1b46d7Szhanghch05         asyncResp->res.jsonValue["Id"] = "UpdateService";
559002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "Service for Software Update";
5608d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Update Service";
5614dc23f3fSEd Tanous 
56232ca38afSEd Tanous #ifdef BMCWEB_ENABLE_REDFISH_UPDATESERVICE_OLD_POST_URL
56332ca38afSEd Tanous         // See note about later on in this file about why this is neccesary
56432ca38afSEd Tanous         // This is "Wrong" per the standard, but is done temporarily to
56532ca38afSEd Tanous         // avoid noise in failing tests as people transition to having this
56632ca38afSEd Tanous         // option disabled
56732ca38afSEd Tanous         asyncResp->res.addHeader(boost::beast::http::field::allow,
56832ca38afSEd Tanous                                  "GET, PATCH, HEAD");
56932ca38afSEd Tanous #endif
57032ca38afSEd Tanous 
5717e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["HttpPushUri"] =
5724dc23f3fSEd Tanous             "/redfish/v1/UpdateService/update";
5734dc23f3fSEd Tanous 
5740f74e643SEd Tanous         // UpdateService cannot be disabled
5758d1b46d7Szhanghch05         asyncResp->res.jsonValue["ServiceEnabled"] = true;
5761476687dSEd Tanous         asyncResp->res.jsonValue["FirmwareInventory"]["@odata.id"] =
5771476687dSEd Tanous             "/redfish/v1/UpdateService/FirmwareInventory";
578d61e5194STejas Patil         // Get the MaxImageSizeBytes
579d61e5194STejas Patil         asyncResp->res.jsonValue["MaxImageSizeBytes"] =
580d61e5194STejas Patil             bmcwebHttpReqBodyLimitMb * 1024 * 1024;
581d61e5194STejas Patil 
5820554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
5830554c984SAndrew Geissler         // Update Actions object.
5840554c984SAndrew Geissler         nlohmann::json& updateSvcSimpleUpdate =
585002d39b4SEd Tanous             asyncResp->res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
5860554c984SAndrew Geissler         updateSvcSimpleUpdate["target"] =
5870554c984SAndrew Geissler             "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
588002d39b4SEd Tanous         updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = {
589002d39b4SEd Tanous             "TFTP"};
5900554c984SAndrew Geissler #endif
591274dfe62SJayashankar Padath         // Get the current ApplyTime value
5921e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
5931e1e598dSJonathan Doman             *crow::connections::systemBus, "xyz.openbmc_project.Settings",
5941e1e598dSJonathan Doman             "/xyz/openbmc_project/software/apply_time",
5951e1e598dSJonathan Doman             "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime",
5968d1b46d7Szhanghch05             [asyncResp](const boost::system::error_code ec,
5971e1e598dSJonathan Doman                         const std::string& applyTime) {
598274dfe62SJayashankar Padath             if (ec)
599274dfe62SJayashankar Padath             {
600274dfe62SJayashankar Padath                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
6018d1b46d7Szhanghch05                 messages::internalError(asyncResp->res);
602274dfe62SJayashankar Padath                 return;
603274dfe62SJayashankar Padath             }
604274dfe62SJayashankar Padath 
605274dfe62SJayashankar Padath             // Store the ApplyTime Value
6061e1e598dSJonathan Doman             if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
6071e1e598dSJonathan Doman                              "RequestedApplyTimes.Immediate")
608274dfe62SJayashankar Padath             {
609002d39b4SEd Tanous                 asyncResp->res.jsonValue["HttpPushUriOptions"]
6107e860f15SJohn Edward Broadbent                                         ["HttpPushUriApplyTime"]["ApplyTime"] =
6117e860f15SJohn Edward Broadbent                     "Immediate";
612274dfe62SJayashankar Padath             }
613002d39b4SEd Tanous             else if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
6141e1e598dSJonathan Doman                                   "RequestedApplyTimes.OnReset")
615274dfe62SJayashankar Padath             {
616002d39b4SEd Tanous                 asyncResp->res.jsonValue["HttpPushUriOptions"]
6177e860f15SJohn Edward Broadbent                                         ["HttpPushUriApplyTime"]["ApplyTime"] =
6187e860f15SJohn Edward Broadbent                     "OnReset";
619274dfe62SJayashankar Padath             }
6201e1e598dSJonathan Doman             });
6217e860f15SJohn Edward Broadbent         });
6227e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
623ed398213SEd Tanous         .privileges(redfish::privileges::patchUpdateService)
624002d39b4SEd Tanous         .methods(boost::beast::http::verb::patch)(
625002d39b4SEd Tanous             [&app](const crow::Request& req,
626002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
6273ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
62845ca1b86SEd Tanous         {
62945ca1b86SEd Tanous             return;
63045ca1b86SEd Tanous         }
631fa1a5a38SJayashankar Padath         BMCWEB_LOG_DEBUG << "doPatch...";
632fa1a5a38SJayashankar Padath 
633274dfe62SJayashankar Padath         std::optional<nlohmann::json> pushUriOptions;
634002d39b4SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "HttpPushUriOptions",
635002d39b4SEd Tanous                                       pushUriOptions))
636fa1a5a38SJayashankar Padath         {
637fa1a5a38SJayashankar Padath             return;
638fa1a5a38SJayashankar Padath         }
639fa1a5a38SJayashankar Padath 
640274dfe62SJayashankar Padath         if (pushUriOptions)
641274dfe62SJayashankar Padath         {
642274dfe62SJayashankar Padath             std::optional<nlohmann::json> pushUriApplyTime;
6438d1b46d7Szhanghch05             if (!json_util::readJson(*pushUriOptions, asyncResp->res,
644002d39b4SEd Tanous                                      "HttpPushUriApplyTime", pushUriApplyTime))
645274dfe62SJayashankar Padath             {
646274dfe62SJayashankar Padath                 return;
647274dfe62SJayashankar Padath             }
648274dfe62SJayashankar Padath 
649274dfe62SJayashankar Padath             if (pushUriApplyTime)
650274dfe62SJayashankar Padath             {
651274dfe62SJayashankar Padath                 std::optional<std::string> applyTime;
6520fda0f12SGeorge Liu                 if (!json_util::readJson(*pushUriApplyTime, asyncResp->res,
6530fda0f12SGeorge Liu                                          "ApplyTime", applyTime))
654274dfe62SJayashankar Padath                 {
655274dfe62SJayashankar Padath                     return;
656274dfe62SJayashankar Padath                 }
657274dfe62SJayashankar Padath 
658274dfe62SJayashankar Padath                 if (applyTime)
659fa1a5a38SJayashankar Padath                 {
660fa1a5a38SJayashankar Padath                     std::string applyTimeNewVal;
661fa1a5a38SJayashankar Padath                     if (applyTime == "Immediate")
662fa1a5a38SJayashankar Padath                     {
663274dfe62SJayashankar Padath                         applyTimeNewVal =
6640fda0f12SGeorge Liu                             "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate";
665fa1a5a38SJayashankar Padath                     }
666274dfe62SJayashankar Padath                     else if (applyTime == "OnReset")
667274dfe62SJayashankar Padath                     {
668274dfe62SJayashankar Padath                         applyTimeNewVal =
6690fda0f12SGeorge Liu                             "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
670274dfe62SJayashankar Padath                     }
671fa1a5a38SJayashankar Padath                     else
672fa1a5a38SJayashankar Padath                     {
673274dfe62SJayashankar Padath                         BMCWEB_LOG_INFO
6740fda0f12SGeorge Liu                             << "ApplyTime value is not in the list of acceptable values";
675274dfe62SJayashankar Padath                         messages::propertyValueNotInList(
676274dfe62SJayashankar Padath                             asyncResp->res, *applyTime, "ApplyTime");
677274dfe62SJayashankar Padath                         return;
678fa1a5a38SJayashankar Padath                     }
679fa1a5a38SJayashankar Padath 
680fa1a5a38SJayashankar Padath                     // Set the requested image apply time value
681fa1a5a38SJayashankar Padath                     crow::connections::systemBus->async_method_call(
6820fda0f12SGeorge Liu                         [asyncResp](const boost::system::error_code ec) {
683fa1a5a38SJayashankar Padath                         if (ec)
684fa1a5a38SJayashankar Padath                         {
685002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
686fa1a5a38SJayashankar Padath                             messages::internalError(asyncResp->res);
687fa1a5a38SJayashankar Padath                             return;
688fa1a5a38SJayashankar Padath                         }
689fa1a5a38SJayashankar Padath                         messages::success(asyncResp->res);
690fa1a5a38SJayashankar Padath                         },
691fa1a5a38SJayashankar Padath                         "xyz.openbmc_project.Settings",
692fa1a5a38SJayashankar Padath                         "/xyz/openbmc_project/software/apply_time",
693fa1a5a38SJayashankar Padath                         "org.freedesktop.DBus.Properties", "Set",
694274dfe62SJayashankar Padath                         "xyz.openbmc_project.Software.ApplyTime",
695274dfe62SJayashankar Padath                         "RequestedApplyTime",
696168e20c1SEd Tanous                         dbus::utility::DbusVariantType{applyTimeNewVal});
697fa1a5a38SJayashankar Padath                 }
698274dfe62SJayashankar Padath             }
699fa1a5a38SJayashankar Padath         }
7007e860f15SJohn Edward Broadbent         });
701c2051d11SEd Tanous 
7024dc23f3fSEd Tanous // The "old" behavior of the update service URI causes redfish-service validator
7034dc23f3fSEd Tanous // failures when the Allow header is supported, given that in the spec,
7044dc23f3fSEd Tanous // UpdateService does not allow POST.  in openbmc, we unfortunately reused that
7054dc23f3fSEd Tanous // resource as our HttpPushUri as well.  A number of services, including the
7064dc23f3fSEd Tanous // openbmc tests, and documentation have hardcoded that erroneous API, instead
7074dc23f3fSEd Tanous // of relying on HttpPushUri as the spec requires.  This option will exist
7084dc23f3fSEd Tanous // temporarily to allow the old behavior until Q4 2022, at which time it will be
7094dc23f3fSEd Tanous // removed.
7104dc23f3fSEd Tanous #ifdef BMCWEB_ENABLE_REDFISH_UPDATESERVICE_OLD_POST_URL
7117e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
712ed398213SEd Tanous         .privileges(redfish::privileges::postUpdateService)
713002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
714002d39b4SEd Tanous             [&app](const crow::Request& req,
7154dc23f3fSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
7164dc23f3fSEd Tanous         asyncResp->res.addHeader(
7174dc23f3fSEd Tanous             boost::beast::http::field::warning,
7184dc23f3fSEd Tanous             "299 - \"POST to /redfish/v1/UpdateService is deprecated. Use "
7194dc23f3fSEd Tanous             "the value contained within HttpPushUri.\"");
7204dc23f3fSEd Tanous         handleUpdateServicePost(app, req, asyncResp);
7214dc23f3fSEd Tanous         });
7224dc23f3fSEd Tanous #endif
7234dc23f3fSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/update/")
7244dc23f3fSEd Tanous         .privileges(redfish::privileges::postUpdateService)
7257e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
726c2051d11SEd Tanous             std::bind_front(handleUpdateServicePost, std::ref(app)));
727729dae72SJennifer Lee }
728729dae72SJennifer Lee 
7297e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventoryCollection(App& app)
7301abe55efSEd Tanous {
7317e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/")
732ed398213SEd Tanous         .privileges(redfish::privileges::getSoftwareInventoryCollection)
7331476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
7341476687dSEd Tanous             [&app](const crow::Request& req,
7351476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
7363ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
73745ca1b86SEd Tanous         {
73845ca1b86SEd Tanous             return;
73945ca1b86SEd Tanous         }
7408d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
7410f74e643SEd Tanous             "#SoftwareInventoryCollection.SoftwareInventoryCollection";
7428d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
7430f74e643SEd Tanous             "/redfish/v1/UpdateService/FirmwareInventory";
744002d39b4SEd Tanous         asyncResp->res.jsonValue["Name"] = "Software Inventory Collection";
745c711bf86SEd Tanous 
746c711bf86SEd Tanous         crow::connections::systemBus->async_method_call(
747002d39b4SEd Tanous             [asyncResp](
748002d39b4SEd Tanous                 const boost::system::error_code ec,
749002d39b4SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
7501abe55efSEd Tanous             if (ec)
7511abe55efSEd Tanous             {
752f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
7536c4eb9deSJennifer Lee                 return;
754729dae72SJennifer Lee             }
755002d39b4SEd Tanous             asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
756c711bf86SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = 0;
7576c4eb9deSJennifer Lee 
7589eb808c1SEd Tanous             for (const auto& obj : subtree)
7591abe55efSEd Tanous             {
7602dfd18efSEd Tanous                 sdbusplus::message::object_path path(obj.first);
7612dfd18efSEd Tanous                 std::string swId = path.filename();
7622dfd18efSEd Tanous                 if (swId.empty())
763f4b65ab1SJennifer Lee                 {
764f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
765f4b65ab1SJennifer Lee                     BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
766f4b65ab1SJennifer Lee                     return;
767f4b65ab1SJennifer Lee                 }
768f4b65ab1SJennifer Lee 
769002d39b4SEd Tanous                 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
7701476687dSEd Tanous                 nlohmann::json::object_t member;
7711476687dSEd Tanous                 member["@odata.id"] =
772002d39b4SEd Tanous                     "/redfish/v1/UpdateService/FirmwareInventory/" + swId;
7731476687dSEd Tanous                 members.push_back(std::move(member));
774e0dd8057SAndrew Geissler                 asyncResp->res.jsonValue["Members@odata.count"] =
775c711bf86SEd Tanous                     members.size();
7766c4eb9deSJennifer Lee             }
777c711bf86SEd Tanous             },
7787e860f15SJohn Edward Broadbent             // Note that only firmware levels associated with a device
7797e860f15SJohn Edward Broadbent             // are stored under /xyz/openbmc_project/software therefore
7807e860f15SJohn Edward Broadbent             // to ensure only real FirmwareInventory items are returned,
7817e860f15SJohn Edward Broadbent             // this full object path must be used here as input to
7827e860f15SJohn Edward Broadbent             // mapper
783c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper",
784c711bf86SEd Tanous             "/xyz/openbmc_project/object_mapper",
7852830a9cfSAndrew Geissler             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
7862830a9cfSAndrew Geissler             "/xyz/openbmc_project/software", static_cast<int32_t>(0),
787002d39b4SEd Tanous             std::array<const char*, 1>{"xyz.openbmc_project.Software.Version"});
7887e860f15SJohn Edward Broadbent         });
789729dae72SJennifer Lee }
79087d84729SAndrew Geissler /* Fill related item links (i.e. bmc, bios) in for inventory */
7917e860f15SJohn Edward Broadbent inline static void
7927e860f15SJohn Edward Broadbent     getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
79387d84729SAndrew Geissler                     const std::string& purpose)
79487d84729SAndrew Geissler {
795eee0013eSWilly Tu     if (purpose == sw_util::bmcPurpose)
79687d84729SAndrew Geissler     {
7975af690f5SAbhishek Patel         nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
7981476687dSEd Tanous         nlohmann::json::object_t item;
7991476687dSEd Tanous         item["@odata.id"] = "/redfish/v1/Managers/bmc";
8001476687dSEd Tanous         relatedItem.push_back(std::move(item));
8017e860f15SJohn Edward Broadbent         aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
80287d84729SAndrew Geissler     }
803eee0013eSWilly Tu     else if (purpose == sw_util::biosPurpose)
80487d84729SAndrew Geissler     {
8055af690f5SAbhishek Patel         nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
8061476687dSEd Tanous         nlohmann::json::object_t item;
8071476687dSEd Tanous         item["@odata.id"] = "/redfish/v1/Systems/system/Bios";
8081476687dSEd Tanous         relatedItem.push_back(std::move(item));
8091a6e51aeSJiaqing Zhao         aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
81087d84729SAndrew Geissler     }
81187d84729SAndrew Geissler     else
81287d84729SAndrew Geissler     {
81387d84729SAndrew Geissler         BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
81487d84729SAndrew Geissler     }
81587d84729SAndrew Geissler }
81687d84729SAndrew Geissler 
817af24660dSWilly Tu inline void
818af24660dSWilly Tu     getSoftwareVersion(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
819af24660dSWilly Tu                        const std::string& service, const std::string& path,
820af24660dSWilly Tu                        const std::string& swId)
821af24660dSWilly Tu {
822af24660dSWilly Tu     crow::connections::systemBus->async_method_call(
823af24660dSWilly Tu         [asyncResp,
824af24660dSWilly Tu          swId](const boost::system::error_code errorCode,
825af24660dSWilly Tu                const dbus::utility::DBusPropertiesMap& propertiesList) {
826af24660dSWilly Tu         if (errorCode)
827af24660dSWilly Tu         {
828af24660dSWilly Tu             messages::internalError(asyncResp->res);
829af24660dSWilly Tu             return;
830af24660dSWilly Tu         }
831af24660dSWilly Tu         const std::string* swInvPurpose = nullptr;
832af24660dSWilly Tu         const std::string* version = nullptr;
833af24660dSWilly Tu         for (const auto& property : propertiesList)
834af24660dSWilly Tu         {
835af24660dSWilly Tu             if (property.first == "Purpose")
836af24660dSWilly Tu             {
837af24660dSWilly Tu                 swInvPurpose = std::get_if<std::string>(&property.second);
838af24660dSWilly Tu             }
839af24660dSWilly Tu             else if (property.first == "Version")
840af24660dSWilly Tu             {
841af24660dSWilly Tu                 version = std::get_if<std::string>(&property.second);
842af24660dSWilly Tu             }
843af24660dSWilly Tu         }
844af24660dSWilly Tu 
845af24660dSWilly Tu         if (swInvPurpose == nullptr)
846af24660dSWilly Tu         {
847af24660dSWilly Tu             BMCWEB_LOG_DEBUG << "Can't find property \"Purpose\"!";
848af24660dSWilly Tu             messages::internalError(asyncResp->res);
849af24660dSWilly Tu             return;
850af24660dSWilly Tu         }
851af24660dSWilly Tu 
852af24660dSWilly Tu         BMCWEB_LOG_DEBUG << "swInvPurpose = " << *swInvPurpose;
853af24660dSWilly Tu 
854af24660dSWilly Tu         if (version == nullptr)
855af24660dSWilly Tu         {
856af24660dSWilly Tu             BMCWEB_LOG_DEBUG << "Can't find property \"Version\"!";
857af24660dSWilly Tu 
858af24660dSWilly Tu             messages::internalError(asyncResp->res);
859af24660dSWilly Tu 
860af24660dSWilly Tu             return;
861af24660dSWilly Tu         }
862af24660dSWilly Tu         asyncResp->res.jsonValue["Version"] = *version;
863af24660dSWilly Tu         asyncResp->res.jsonValue["Id"] = swId;
864af24660dSWilly Tu 
865af24660dSWilly Tu         // swInvPurpose is of format:
866af24660dSWilly Tu         // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
867af24660dSWilly Tu         // Translate this to "ABC image"
868af24660dSWilly Tu         size_t endDesc = swInvPurpose->rfind('.');
869af24660dSWilly Tu         if (endDesc == std::string::npos)
870af24660dSWilly Tu         {
871af24660dSWilly Tu             messages::internalError(asyncResp->res);
872af24660dSWilly Tu             return;
873af24660dSWilly Tu         }
874af24660dSWilly Tu         endDesc++;
875af24660dSWilly Tu         if (endDesc >= swInvPurpose->size())
876af24660dSWilly Tu         {
877af24660dSWilly Tu             messages::internalError(asyncResp->res);
878af24660dSWilly Tu             return;
879af24660dSWilly Tu         }
880af24660dSWilly Tu 
881af24660dSWilly Tu         std::string formatDesc = swInvPurpose->substr(endDesc);
882af24660dSWilly Tu         asyncResp->res.jsonValue["Description"] = formatDesc + " image";
883af24660dSWilly Tu         getRelatedItems(asyncResp, *swInvPurpose);
884af24660dSWilly Tu         },
885af24660dSWilly Tu         service, path, "org.freedesktop.DBus.Properties", "GetAll",
886af24660dSWilly Tu         "xyz.openbmc_project.Software.Version");
887af24660dSWilly Tu }
888af24660dSWilly Tu 
8897e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventory(App& app)
8901abe55efSEd Tanous {
8917e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/")
892ed398213SEd Tanous         .privileges(redfish::privileges::getSoftwareInventory)
893002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
894002d39b4SEd Tanous             [&app](const crow::Request& req,
89545ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8967e860f15SJohn Edward Broadbent                    const std::string& param) {
8973ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
89845ca1b86SEd Tanous         {
89945ca1b86SEd Tanous             return;
90045ca1b86SEd Tanous         }
9013ae837c9SEd Tanous         std::shared_ptr<std::string> swId =
9027e860f15SJohn Edward Broadbent             std::make_shared<std::string>(param);
903c711bf86SEd Tanous 
9048d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
9053ae837c9SEd Tanous             "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
906c711bf86SEd Tanous 
907c711bf86SEd Tanous         crow::connections::systemBus->async_method_call(
908b9d36b47SEd Tanous             [asyncResp,
909b9d36b47SEd Tanous              swId](const boost::system::error_code ec,
910b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
91155c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "doGet callback...";
9121abe55efSEd Tanous             if (ec)
9131abe55efSEd Tanous             {
914f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
9156c4eb9deSJennifer Lee                 return;
9166c4eb9deSJennifer Lee             }
9176c4eb9deSJennifer Lee 
9186913228dSAndrew Geissler             // Ensure we find our input swId, otherwise return an error
9196913228dSAndrew Geissler             bool found = false;
920002d39b4SEd Tanous             for (const std::pair<std::string,
9217e860f15SJohn Edward Broadbent                                  std::vector<std::pair<
922002d39b4SEd Tanous                                      std::string, std::vector<std::string>>>>&
923002d39b4SEd Tanous                      obj : subtree)
9241abe55efSEd Tanous             {
92511ba3979SEd Tanous                 if (!obj.first.ends_with(*swId))
9261abe55efSEd Tanous                 {
927acb7cfb4SJennifer Lee                     continue;
928acb7cfb4SJennifer Lee                 }
929acb7cfb4SJennifer Lee 
93026f6976fSEd Tanous                 if (obj.second.empty())
9311abe55efSEd Tanous                 {
932acb7cfb4SJennifer Lee                     continue;
933acb7cfb4SJennifer Lee                 }
9346c4eb9deSJennifer Lee 
9356913228dSAndrew Geissler                 found = true;
936eee0013eSWilly Tu                 sw_util::getSwStatus(asyncResp, swId, obj.second[0].first);
937af24660dSWilly Tu                 getSoftwareVersion(asyncResp, obj.second[0].first, obj.first,
938af24660dSWilly Tu                                    *swId);
9396c4eb9deSJennifer Lee             }
9406913228dSAndrew Geissler             if (!found)
9416913228dSAndrew Geissler             {
942002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Input swID " << *swId << " not found!";
9436913228dSAndrew Geissler                 messages::resourceMissingAtURI(
944002d39b4SEd Tanous                     asyncResp->res, crow::utility::urlFromPieces(
945ace85d60SEd Tanous                                         "redfish", "v1", "UpdateService",
946ace85d60SEd Tanous                                         "FirmwareInventory", *swId));
9476913228dSAndrew Geissler                 return;
9486913228dSAndrew Geissler             }
9494e68c45bSAyushi Smriti             asyncResp->res.jsonValue["@odata.type"] =
9504e68c45bSAyushi Smriti                 "#SoftwareInventory.v1_1_0.SoftwareInventory";
9514e68c45bSAyushi Smriti             asyncResp->res.jsonValue["Name"] = "Software Inventory";
9524e68c45bSAyushi Smriti             asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
9533f8a743aSAppaRao Puli 
9543f8a743aSAppaRao Puli             asyncResp->res.jsonValue["Updateable"] = false;
955eee0013eSWilly Tu             sw_util::getSwUpdatableStatus(asyncResp, swId);
956c711bf86SEd Tanous             },
957c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper",
958c711bf86SEd Tanous             "/xyz/openbmc_project/object_mapper",
959271584abSEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
960271584abSEd Tanous             static_cast<int32_t>(0),
961002d39b4SEd Tanous             std::array<const char*, 1>{"xyz.openbmc_project.Software.Version"});
9627e860f15SJohn Edward Broadbent         });
9636c4eb9deSJennifer Lee }
964729dae72SJennifer Lee 
965729dae72SJennifer Lee } // namespace redfish
966