xref: /openbmc/bmcweb/features/redfish/lib/update_service.hpp (revision 3ccb3adb9a14783f6bef601506de9f8bcae22d51)
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 
20*3ccb3adbSEd Tanous #include "app.hpp"
21*3ccb3adbSEd Tanous #include "dbus_utility.hpp"
22*3ccb3adbSEd Tanous #include "query.hpp"
23*3ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
24*3ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
25*3ccb3adbSEd Tanous #include "utils/sw_utils.hpp"
26*3ccb3adbSEd Tanous 
271e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
28*3ccb3adbSEd Tanous #include <sdbusplus/bus/match.hpp>
29d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
301214b7e7SGunnar Mills 
311abe55efSEd Tanous namespace redfish
321abe55efSEd Tanous {
3327826b5fSEd Tanous 
340e7de46fSAndrew Geissler // Match signals added on software path
35cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
3659d494eeSPatrick Williams static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateMatcher;
37cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
3859d494eeSPatrick Williams static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateErrorMatcher;
390e7de46fSAndrew Geissler // Only allow one update at a time
40cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
410e7de46fSAndrew Geissler static bool fwUpdateInProgress = false;
4286adcd6dSAndrew Geissler // Timer for software available
43cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
44271584abSEd Tanous static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer;
4586adcd6dSAndrew Geissler 
467e860f15SJohn Edward Broadbent inline static void cleanUp()
4786adcd6dSAndrew Geissler {
4886adcd6dSAndrew Geissler     fwUpdateInProgress = false;
4986adcd6dSAndrew Geissler     fwUpdateMatcher = nullptr;
504cde5d90SJames Feist     fwUpdateErrorMatcher = nullptr;
5186adcd6dSAndrew Geissler }
527e860f15SJohn Edward Broadbent inline static void activateImage(const std::string& objPath,
5386adcd6dSAndrew Geissler                                  const std::string& service)
5486adcd6dSAndrew Geissler {
5586adcd6dSAndrew Geissler     BMCWEB_LOG_DEBUG << "Activate image for " << objPath << " " << service;
5686adcd6dSAndrew Geissler     crow::connections::systemBus->async_method_call(
5781ce609eSEd Tanous         [](const boost::system::error_code errorCode) {
5881ce609eSEd Tanous         if (errorCode)
5986adcd6dSAndrew Geissler         {
6081ce609eSEd Tanous             BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
6181ce609eSEd Tanous             BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message();
6286adcd6dSAndrew Geissler         }
6386adcd6dSAndrew Geissler         },
6486adcd6dSAndrew Geissler         service, objPath, "org.freedesktop.DBus.Properties", "Set",
6586adcd6dSAndrew Geissler         "xyz.openbmc_project.Software.Activation", "RequestedActivation",
66168e20c1SEd Tanous         dbus::utility::DbusVariantType(
670fda0f12SGeorge Liu             "xyz.openbmc_project.Software.Activation.RequestedActivations.Active"));
6886adcd6dSAndrew Geissler }
690554c984SAndrew Geissler 
700554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr
710554c984SAndrew Geissler // then no asyncResp updates will occur
728d1b46d7Szhanghch05 static void
738d1b46d7Szhanghch05     softwareInterfaceAdded(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7459d494eeSPatrick Williams                            sdbusplus::message_t& m, task::Payload&& payload)
7586adcd6dSAndrew Geissler {
76b9d36b47SEd Tanous     dbus::utility::DBusInteracesMap interfacesProperties;
7786adcd6dSAndrew Geissler 
7886adcd6dSAndrew Geissler     sdbusplus::message::object_path objPath;
7986adcd6dSAndrew Geissler 
8086adcd6dSAndrew Geissler     m.read(objPath, interfacesProperties);
8186adcd6dSAndrew Geissler 
8286adcd6dSAndrew Geissler     BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
83e3eb3d63SEd Tanous     for (const auto& interface : interfacesProperties)
8486adcd6dSAndrew Geissler     {
8586adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "interface = " << interface.first;
8686adcd6dSAndrew Geissler 
8786adcd6dSAndrew Geissler         if (interface.first == "xyz.openbmc_project.Software.Activation")
8886adcd6dSAndrew Geissler         {
8986adcd6dSAndrew Geissler             // Retrieve service and activate
9086adcd6dSAndrew Geissler             crow::connections::systemBus->async_method_call(
91a3e65892SEd Tanous                 [objPath, asyncResp, payload(std::move(payload))](
92a3e65892SEd Tanous                     const boost::system::error_code errorCode,
93a3e65892SEd Tanous                     const std::vector<
94a3e65892SEd Tanous                         std::pair<std::string, std::vector<std::string>>>&
95a3e65892SEd Tanous                         objInfo) mutable {
9681ce609eSEd Tanous                 if (errorCode)
9786adcd6dSAndrew Geissler                 {
9881ce609eSEd Tanous                     BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
99002d39b4SEd Tanous                     BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message();
1000554c984SAndrew Geissler                     if (asyncResp)
1010554c984SAndrew Geissler                     {
10286adcd6dSAndrew Geissler                         messages::internalError(asyncResp->res);
1030554c984SAndrew Geissler                     }
10486adcd6dSAndrew Geissler                     cleanUp();
10586adcd6dSAndrew Geissler                     return;
10686adcd6dSAndrew Geissler                 }
10786adcd6dSAndrew Geissler                 // Ensure we only got one service back
10886adcd6dSAndrew Geissler                 if (objInfo.size() != 1)
10986adcd6dSAndrew Geissler                 {
11086adcd6dSAndrew Geissler                     BMCWEB_LOG_ERROR << "Invalid Object Size "
11186adcd6dSAndrew Geissler                                      << objInfo.size();
1120554c984SAndrew Geissler                     if (asyncResp)
1130554c984SAndrew Geissler                     {
11486adcd6dSAndrew Geissler                         messages::internalError(asyncResp->res);
1150554c984SAndrew Geissler                     }
11686adcd6dSAndrew Geissler                     cleanUp();
11786adcd6dSAndrew Geissler                     return;
11886adcd6dSAndrew Geissler                 }
11986adcd6dSAndrew Geissler                 // cancel timer only when
12086adcd6dSAndrew Geissler                 // xyz.openbmc_project.Software.Activation interface
12186adcd6dSAndrew Geissler                 // is added
12286adcd6dSAndrew Geissler                 fwAvailableTimer = nullptr;
12386adcd6dSAndrew Geissler 
12486adcd6dSAndrew Geissler                 activateImage(objPath.str, objInfo[0].first);
1250554c984SAndrew Geissler                 if (asyncResp)
1260554c984SAndrew Geissler                 {
12732898ceaSJames Feist                     std::shared_ptr<task::TaskData> task =
12832898ceaSJames Feist                         task::TaskData::createTask(
12932898ceaSJames Feist                             [](boost::system::error_code ec,
13059d494eeSPatrick Williams                                sdbusplus::message_t& msg,
1311214b7e7SGunnar Mills                                const std::shared_ptr<task::TaskData>&
1321214b7e7SGunnar Mills                                    taskData) {
13332898ceaSJames Feist                         if (ec)
13432898ceaSJames Feist                         {
13532898ceaSJames Feist                             return task::completed;
13632898ceaSJames Feist                         }
13732898ceaSJames Feist 
13832898ceaSJames Feist                         std::string iface;
139b9d36b47SEd Tanous                         dbus::utility::DBusPropertiesMap values;
140fd9ab9e1SJames Feist 
141002d39b4SEd Tanous                         std::string index = std::to_string(taskData->index);
14232898ceaSJames Feist                         msg.read(iface, values);
143fd9ab9e1SJames Feist 
144002d39b4SEd Tanous                         if (iface == "xyz.openbmc_project.Software.Activation")
145fd9ab9e1SJames Feist                         {
1460fb5b505SGayathri Leburu                             const std::string* state = nullptr;
147b9d36b47SEd Tanous                             for (const auto& property : values)
14832898ceaSJames Feist                             {
149b9d36b47SEd Tanous                                 if (property.first == "Activation")
150b9d36b47SEd Tanous                                 {
1510fb5b505SGayathri Leburu                                     state = std::get_if<std::string>(
152b9d36b47SEd Tanous                                         &property.second);
1530fb5b505SGayathri Leburu                                     if (state == nullptr)
154b9d36b47SEd Tanous                                     {
155002d39b4SEd Tanous                                         taskData->messages.emplace_back(
156002d39b4SEd Tanous                                             messages::internalError());
157b9d36b47SEd Tanous                                         return task::completed;
158b9d36b47SEd Tanous                                     }
159b9d36b47SEd Tanous                                 }
160b9d36b47SEd Tanous                             }
16132898ceaSJames Feist 
16232898ceaSJames Feist                             if (state == nullptr)
16332898ceaSJames Feist                             {
164b9d36b47SEd Tanous                                 return !task::completed;
16532898ceaSJames Feist                             }
16632898ceaSJames Feist 
16711ba3979SEd Tanous                             if (state->ends_with("Invalid") ||
16811ba3979SEd Tanous                                 state->ends_with("Failed"))
16932898ceaSJames Feist                             {
17032898ceaSJames Feist                                 taskData->state = "Exception";
17132898ceaSJames Feist                                 taskData->status = "Warning";
17232898ceaSJames Feist                                 taskData->messages.emplace_back(
173e5d5006bSJames Feist                                     messages::taskAborted(index));
17432898ceaSJames Feist                                 return task::completed;
17532898ceaSJames Feist                             }
17632898ceaSJames Feist 
17711ba3979SEd Tanous                             if (state->ends_with("Staged"))
17832898ceaSJames Feist                             {
179fd9ab9e1SJames Feist                                 taskData->state = "Stopping";
180fd9ab9e1SJames Feist                                 taskData->messages.emplace_back(
181fd9ab9e1SJames Feist                                     messages::taskPaused(index));
182fd9ab9e1SJames Feist 
183fd9ab9e1SJames Feist                                 // its staged, set a long timer to
184fd9ab9e1SJames Feist                                 // allow them time to complete the
185fd9ab9e1SJames Feist                                 // update (probably cycle the
186fd9ab9e1SJames Feist                                 // system) if this expires then
187fd9ab9e1SJames Feist                                 // task will be cancelled
188002d39b4SEd Tanous                                 taskData->extendTimer(std::chrono::hours(5));
18932898ceaSJames Feist                                 return !task::completed;
19032898ceaSJames Feist                             }
19132898ceaSJames Feist 
19211ba3979SEd Tanous                             if (state->ends_with("Active"))
19332898ceaSJames Feist                             {
19432898ceaSJames Feist                                 taskData->messages.emplace_back(
195002d39b4SEd Tanous                                     messages::taskCompletedOK(index));
19632898ceaSJames Feist                                 taskData->state = "Completed";
19732898ceaSJames Feist                                 return task::completed;
19832898ceaSJames Feist                             }
199fd9ab9e1SJames Feist                         }
2000fda0f12SGeorge Liu                         else if (
2010fda0f12SGeorge Liu                             iface ==
2020fda0f12SGeorge Liu                             "xyz.openbmc_project.Software.ActivationProgress")
203fd9ab9e1SJames Feist                         {
204b9d36b47SEd Tanous 
205b9d36b47SEd Tanous                             const uint8_t* progress = nullptr;
206b9d36b47SEd Tanous                             for (const auto& property : values)
207fd9ab9e1SJames Feist                             {
208b9d36b47SEd Tanous                                 if (property.first == "Progress")
209b9d36b47SEd Tanous                                 {
2100fb5b505SGayathri Leburu                                     progress =
2110fb5b505SGayathri Leburu                                         std::get_if<uint8_t>(&property.second);
2120fb5b505SGayathri Leburu                                     if (progress == nullptr)
213b9d36b47SEd Tanous                                     {
214002d39b4SEd Tanous                                         taskData->messages.emplace_back(
215002d39b4SEd Tanous                                             messages::internalError());
216b9d36b47SEd Tanous                                         return task::completed;
217fd9ab9e1SJames Feist                                     }
218b9d36b47SEd Tanous                                 }
219b9d36b47SEd Tanous                             }
220fd9ab9e1SJames Feist 
221fd9ab9e1SJames Feist                             if (progress == nullptr)
222fd9ab9e1SJames Feist                             {
223b9d36b47SEd Tanous                                 return !task::completed;
224fd9ab9e1SJames Feist                             }
2250fb5b505SGayathri Leburu                             taskData->percentComplete = *progress;
226fd9ab9e1SJames Feist                             taskData->messages.emplace_back(
2270fb5b505SGayathri Leburu                                 messages::taskProgressChanged(index,
2280fb5b505SGayathri Leburu                                                               *progress));
229fd9ab9e1SJames Feist 
230fd9ab9e1SJames Feist                             // if we're getting status updates it's
231fd9ab9e1SJames Feist                             // still alive, update timer
232002d39b4SEd Tanous                             taskData->extendTimer(std::chrono::minutes(5));
233fd9ab9e1SJames Feist                         }
23432898ceaSJames Feist 
23532898ceaSJames Feist                         // as firmware update often results in a
23632898ceaSJames Feist                         // reboot, the task  may never "complete"
23732898ceaSJames Feist                         // unless it is an error
23832898ceaSJames Feist 
23932898ceaSJames Feist                         return !task::completed;
24032898ceaSJames Feist                             },
2410fda0f12SGeorge Liu                             "type='signal',interface='org.freedesktop.DBus.Properties',"
242fd9ab9e1SJames Feist                             "member='PropertiesChanged',path='" +
24332898ceaSJames Feist                                 objPath.str + "'");
24432898ceaSJames Feist                     task->startTimer(std::chrono::minutes(5));
24532898ceaSJames Feist                     task->populateResp(asyncResp->res);
246a3e65892SEd Tanous                     task->payload.emplace(std::move(payload));
2470554c984SAndrew Geissler                 }
24886adcd6dSAndrew Geissler                 fwUpdateInProgress = false;
24986adcd6dSAndrew Geissler                 },
25086adcd6dSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper",
25186adcd6dSAndrew Geissler                 "/xyz/openbmc_project/object_mapper",
25286adcd6dSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str,
25386adcd6dSAndrew Geissler                 std::array<const char*, 1>{
25486adcd6dSAndrew Geissler                     "xyz.openbmc_project.Software.Activation"});
25562bafc01SPatrick Williams 
25662bafc01SPatrick Williams             break;
25786adcd6dSAndrew Geissler         }
25886adcd6dSAndrew Geissler     }
25986adcd6dSAndrew Geissler }
26086adcd6dSAndrew Geissler 
2610554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr
2620554c984SAndrew Geissler // then no asyncResp updates will occur
263b5a76932SEd Tanous static void monitorForSoftwareAvailable(
2648d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2658d1b46d7Szhanghch05     const crow::Request& req, const std::string& url,
2665d138943SGunnar Mills     int timeoutTimeSeconds = 25)
26786adcd6dSAndrew Geissler {
26886adcd6dSAndrew Geissler     // Only allow one FW update at a time
269e05aec50SEd Tanous     if (fwUpdateInProgress)
27086adcd6dSAndrew Geissler     {
2710554c984SAndrew Geissler         if (asyncResp)
2720554c984SAndrew Geissler         {
27386adcd6dSAndrew Geissler             messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
2740554c984SAndrew Geissler         }
27586adcd6dSAndrew Geissler         return;
27686adcd6dSAndrew Geissler     }
27786adcd6dSAndrew Geissler 
2780554c984SAndrew Geissler     fwAvailableTimer =
279271584abSEd Tanous         std::make_unique<boost::asio::steady_timer>(*req.ioService);
28086adcd6dSAndrew Geissler 
281271584abSEd Tanous     fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds));
28286adcd6dSAndrew Geissler 
28386adcd6dSAndrew Geissler     fwAvailableTimer->async_wait(
28486adcd6dSAndrew Geissler         [asyncResp](const boost::system::error_code& ec) {
28586adcd6dSAndrew Geissler         cleanUp();
28686adcd6dSAndrew Geissler         if (ec == boost::asio::error::operation_aborted)
28786adcd6dSAndrew Geissler         {
28886adcd6dSAndrew Geissler             // expected, we were canceled before the timer completed.
28986adcd6dSAndrew Geissler             return;
29086adcd6dSAndrew Geissler         }
29186adcd6dSAndrew Geissler         BMCWEB_LOG_ERROR
29286adcd6dSAndrew Geissler             << "Timed out waiting for firmware object being created";
293002d39b4SEd Tanous         BMCWEB_LOG_ERROR << "FW image may has already been uploaded to server";
29486adcd6dSAndrew Geissler         if (ec)
29586adcd6dSAndrew Geissler         {
29686adcd6dSAndrew Geissler             BMCWEB_LOG_ERROR << "Async_wait failed" << ec;
29786adcd6dSAndrew Geissler             return;
29886adcd6dSAndrew Geissler         }
2990554c984SAndrew Geissler         if (asyncResp)
3000554c984SAndrew Geissler         {
30186adcd6dSAndrew Geissler             redfish::messages::internalError(asyncResp->res);
3020554c984SAndrew Geissler         }
30386adcd6dSAndrew Geissler     });
304a3e65892SEd Tanous     task::Payload payload(req);
30559d494eeSPatrick Williams     auto callback = [asyncResp, payload](sdbusplus::message_t& m) mutable {
30686adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "Match fired";
307a3e65892SEd Tanous         softwareInterfaceAdded(asyncResp, m, std::move(payload));
30886adcd6dSAndrew Geissler     };
30986adcd6dSAndrew Geissler 
31086adcd6dSAndrew Geissler     fwUpdateInProgress = true;
31186adcd6dSAndrew Geissler 
31259d494eeSPatrick Williams     fwUpdateMatcher = std::make_unique<sdbusplus::bus::match_t>(
31386adcd6dSAndrew Geissler         *crow::connections::systemBus,
31486adcd6dSAndrew Geissler         "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
31586adcd6dSAndrew Geissler         "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
31686adcd6dSAndrew Geissler         callback);
3174cde5d90SJames Feist 
31859d494eeSPatrick Williams     fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match_t>(
3194cde5d90SJames Feist         *crow::connections::systemBus,
320e1cc4828SBrian Ma         "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
321e1cc4828SBrian Ma         "member='InterfacesAdded',"
322e1cc4828SBrian Ma         "path='/xyz/openbmc_project/logging'",
32359d494eeSPatrick Williams         [asyncResp, url](sdbusplus::message_t& m) {
324002d39b4SEd Tanous         std::vector<std::pair<std::string, dbus::utility::DBusPropertiesMap>>
325e1cc4828SBrian Ma             interfacesProperties;
326e1cc4828SBrian Ma         sdbusplus::message::object_path objPath;
327e1cc4828SBrian Ma         m.read(objPath, interfacesProperties);
328e1cc4828SBrian Ma         BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
329e1cc4828SBrian Ma         for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>&
330e1cc4828SBrian Ma                  interface : interfacesProperties)
331e1cc4828SBrian Ma         {
332e1cc4828SBrian Ma             if (interface.first == "xyz.openbmc_project.Logging.Entry")
333e1cc4828SBrian Ma             {
334711ac7a9SEd Tanous                 for (const std::pair<std::string,
335002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& value :
336002d39b4SEd Tanous                      interface.second)
3374cde5d90SJames Feist                 {
338711ac7a9SEd Tanous                     if (value.first != "Message")
339711ac7a9SEd Tanous                     {
340711ac7a9SEd Tanous                         continue;
3414cde5d90SJames Feist                     }
342e1cc4828SBrian Ma                     const std::string* type =
343711ac7a9SEd Tanous                         std::get_if<std::string>(&value.second);
3444cde5d90SJames Feist                     if (type == nullptr)
3454cde5d90SJames Feist                     {
346e1cc4828SBrian Ma                         // if this was our message, timeout will cover it
3474cde5d90SJames Feist                         return;
3484cde5d90SJames Feist                     }
349e1cc4828SBrian Ma                     fwAvailableTimer = nullptr;
3504cde5d90SJames Feist                     if (*type ==
3514cde5d90SJames Feist                         "xyz.openbmc_project.Software.Image.Error.UnTarFailure")
3524cde5d90SJames Feist                     {
353002d39b4SEd Tanous                         redfish::messages::invalidUpload(asyncResp->res, url,
354002d39b4SEd Tanous                                                          "Invalid archive");
3554cde5d90SJames Feist                     }
356e1cc4828SBrian Ma                     else if (*type ==
357e1cc4828SBrian Ma                              "xyz.openbmc_project.Software.Image.Error."
358e1cc4828SBrian Ma                              "ManifestFileFailure")
3594cde5d90SJames Feist                     {
360002d39b4SEd Tanous                         redfish::messages::invalidUpload(asyncResp->res, url,
361002d39b4SEd Tanous                                                          "Invalid manifest");
3624cde5d90SJames Feist                     }
363e1cc4828SBrian Ma                     else if (
364e1cc4828SBrian Ma                         *type ==
3654cde5d90SJames Feist                         "xyz.openbmc_project.Software.Image.Error.ImageFailure")
3664cde5d90SJames Feist                     {
367e1cc4828SBrian Ma                         redfish::messages::invalidUpload(
368e1cc4828SBrian Ma                             asyncResp->res, url, "Invalid image format");
3694cde5d90SJames Feist                     }
370e1cc4828SBrian Ma                     else if (
371e1cc4828SBrian Ma                         *type ==
3720fda0f12SGeorge Liu                         "xyz.openbmc_project.Software.Version.Error.AlreadyExists")
37388b3dd12SGunnar Mills                     {
37488b3dd12SGunnar Mills                         redfish::messages::invalidUpload(
375e1cc4828SBrian Ma                             asyncResp->res, url,
376e1cc4828SBrian Ma                             "Image version already exists");
37788b3dd12SGunnar Mills 
37888b3dd12SGunnar Mills                         redfish::messages::resourceAlreadyExists(
379d8a5d5d8SJiaqing Zhao                             asyncResp->res, "UpdateService", "Version",
380e1cc4828SBrian Ma                             "uploaded version");
38188b3dd12SGunnar Mills                     }
382e1cc4828SBrian Ma                     else if (
383e1cc4828SBrian Ma                         *type ==
3844cde5d90SJames Feist                         "xyz.openbmc_project.Software.Image.Error.BusyFailure")
3854cde5d90SJames Feist                     {
386002d39b4SEd Tanous                         redfish::messages::resourceExhaustion(asyncResp->res,
387002d39b4SEd Tanous                                                               url);
3884cde5d90SJames Feist                     }
3894cde5d90SJames Feist                     else
3904cde5d90SJames Feist                     {
3914cde5d90SJames Feist                         redfish::messages::internalError(asyncResp->res);
3924cde5d90SJames Feist                     }
393e1cc4828SBrian Ma                 }
394e1cc4828SBrian Ma             }
395711ac7a9SEd Tanous         }
3964cde5d90SJames Feist         });
39786adcd6dSAndrew Geissler }
398729dae72SJennifer Lee 
3990554c984SAndrew Geissler /**
4000554c984SAndrew Geissler  * UpdateServiceActionsSimpleUpdate class supports handle POST method for
4010554c984SAndrew Geissler  * SimpleUpdate action.
4020554c984SAndrew Geissler  */
4037e860f15SJohn Edward Broadbent inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app)
4040554c984SAndrew Geissler {
4057e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
4067e860f15SJohn Edward Broadbent         app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
407ed398213SEd Tanous         .privileges(redfish::privileges::postUpdateService)
408002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
409002d39b4SEd Tanous             [&app](const crow::Request& req,
4107e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
4113ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
41245ca1b86SEd Tanous         {
41345ca1b86SEd Tanous             return;
41445ca1b86SEd Tanous         }
41545ca1b86SEd Tanous 
4160554c984SAndrew Geissler         std::optional<std::string> transferProtocol;
4170554c984SAndrew Geissler         std::string imageURI;
4180554c984SAndrew Geissler 
4190554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost";
4200554c984SAndrew Geissler 
4210554c984SAndrew Geissler         // User can pass in both TransferProtocol and ImageURI parameters or
4224e0453b1SGunnar Mills         // they can pass in just the ImageURI with the transfer protocol
4234e0453b1SGunnar Mills         // embedded within it.
4240554c984SAndrew Geissler         // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
4250554c984SAndrew Geissler         // 2) ImageURI:tftp://1.1.1.1/myfile.bin
4260554c984SAndrew Geissler 
427002d39b4SEd Tanous         if (!json_util::readJsonAction(req, asyncResp->res, "TransferProtocol",
428002d39b4SEd Tanous                                        transferProtocol, "ImageURI", imageURI))
4290554c984SAndrew Geissler         {
4300554c984SAndrew Geissler             BMCWEB_LOG_DEBUG
4310554c984SAndrew Geissler                 << "Missing TransferProtocol or ImageURI parameter";
4320554c984SAndrew Geissler             return;
4330554c984SAndrew Geissler         }
4340554c984SAndrew Geissler         if (!transferProtocol)
4350554c984SAndrew Geissler         {
4360554c984SAndrew Geissler             // Must be option 2
4370554c984SAndrew Geissler             // Verify ImageURI has transfer protocol in it
438f23b7296SEd Tanous             size_t separator = imageURI.find(':');
4390554c984SAndrew Geissler             if ((separator == std::string::npos) ||
4400554c984SAndrew Geissler                 ((separator + 1) > imageURI.size()))
4410554c984SAndrew Geissler             {
4420554c984SAndrew Geissler                 messages::actionParameterValueTypeError(
4430554c984SAndrew Geissler                     asyncResp->res, imageURI, "ImageURI",
4440554c984SAndrew Geissler                     "UpdateService.SimpleUpdate");
4450554c984SAndrew Geissler                 BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: "
4460554c984SAndrew Geissler                                  << imageURI;
4470554c984SAndrew Geissler                 return;
4480554c984SAndrew Geissler             }
4490554c984SAndrew Geissler             transferProtocol = imageURI.substr(0, separator);
4507e860f15SJohn Edward Broadbent             // Ensure protocol is upper case for a common comparison path
4517e860f15SJohn Edward Broadbent             // below
4520554c984SAndrew Geissler             boost::to_upper(*transferProtocol);
4530554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Encoded transfer protocol "
4540554c984SAndrew Geissler                              << *transferProtocol;
4550554c984SAndrew Geissler 
4560554c984SAndrew Geissler             // Adjust imageURI to not have the protocol on it for parsing
4570554c984SAndrew Geissler             // below
4580554c984SAndrew Geissler             // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin
4590554c984SAndrew Geissler             imageURI = imageURI.substr(separator + 3);
4600554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI;
4610554c984SAndrew Geissler         }
4620554c984SAndrew Geissler 
4630554c984SAndrew Geissler         // OpenBMC currently only supports TFTP
4640554c984SAndrew Geissler         if (*transferProtocol != "TFTP")
4650554c984SAndrew Geissler         {
466002d39b4SEd Tanous             messages::actionParameterNotSupported(asyncResp->res,
467002d39b4SEd Tanous                                                   "TransferProtocol",
4680554c984SAndrew Geissler                                                   "UpdateService.SimpleUpdate");
4690554c984SAndrew Geissler             BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: "
4700554c984SAndrew Geissler                              << *transferProtocol;
4710554c984SAndrew Geissler             return;
4720554c984SAndrew Geissler         }
4730554c984SAndrew Geissler 
4740554c984SAndrew Geissler         // Format should be <IP or Hostname>/<file> for imageURI
475f23b7296SEd Tanous         size_t separator = imageURI.find('/');
4760554c984SAndrew Geissler         if ((separator == std::string::npos) ||
4770554c984SAndrew Geissler             ((separator + 1) > imageURI.size()))
4780554c984SAndrew Geissler         {
4790554c984SAndrew Geissler             messages::actionParameterValueTypeError(
4800554c984SAndrew Geissler                 asyncResp->res, imageURI, "ImageURI",
4810554c984SAndrew Geissler                 "UpdateService.SimpleUpdate");
4820554c984SAndrew Geissler             BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI;
4830554c984SAndrew Geissler             return;
4840554c984SAndrew Geissler         }
4850554c984SAndrew Geissler 
4860554c984SAndrew Geissler         std::string tftpServer = imageURI.substr(0, separator);
4870554c984SAndrew Geissler         std::string fwFile = imageURI.substr(separator + 1);
4880554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile;
4890554c984SAndrew Geissler 
4900554c984SAndrew Geissler         // Setup callback for when new software detected
4912618d5e3SGunnar Mills         // Give TFTP 10 minutes to complete
4924cde5d90SJames Feist         monitorForSoftwareAvailable(
493d7a596bdSAlbert Zhang             asyncResp, req,
4944cde5d90SJames Feist             "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate",
4952618d5e3SGunnar Mills             600);
4960554c984SAndrew Geissler 
4972618d5e3SGunnar Mills         // TFTP can take up to 10 minutes depending on image size and
4980554c984SAndrew Geissler         // connection speed. Return to caller as soon as the TFTP operation
4990554c984SAndrew Geissler         // has been started. The callback above will ensure the activate
5000554c984SAndrew Geissler         // is started once the download has completed
5010554c984SAndrew Geissler         redfish::messages::success(asyncResp->res);
5020554c984SAndrew Geissler 
5030554c984SAndrew Geissler         // Call TFTP service
5040554c984SAndrew Geissler         crow::connections::systemBus->async_method_call(
5050554c984SAndrew Geissler             [](const boost::system::error_code ec) {
5060554c984SAndrew Geissler             if (ec)
5070554c984SAndrew Geissler             {
5080554c984SAndrew Geissler                 // messages::internalError(asyncResp->res);
5090554c984SAndrew Geissler                 cleanUp();
5100554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG << "error_code = " << ec;
5110554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
5120554c984SAndrew Geissler             }
5130554c984SAndrew Geissler             else
5140554c984SAndrew Geissler             {
5150554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success";
5160554c984SAndrew Geissler             }
5170554c984SAndrew Geissler             },
5180554c984SAndrew Geissler             "xyz.openbmc_project.Software.Download",
519002d39b4SEd Tanous             "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP",
520002d39b4SEd Tanous             "DownloadViaTFTP", fwFile, tftpServer);
5210554c984SAndrew Geissler 
5220554c984SAndrew Geissler         BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost";
5237e860f15SJohn Edward Broadbent         });
524729dae72SJennifer Lee }
525729dae72SJennifer Lee 
526c2051d11SEd Tanous inline void
527c2051d11SEd Tanous     handleUpdateServicePost(App& app, const crow::Request& req,
528c2051d11SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
529c2051d11SEd Tanous {
5303ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
531c2051d11SEd Tanous     {
532c2051d11SEd Tanous         return;
533c2051d11SEd Tanous     }
534c2051d11SEd Tanous     BMCWEB_LOG_DEBUG << "doPost...";
535c2051d11SEd Tanous 
536c2051d11SEd Tanous     // Setup callback for when new software detected
537c2051d11SEd Tanous     monitorForSoftwareAvailable(asyncResp, req, "/redfish/v1/UpdateService");
538c2051d11SEd Tanous 
539c2051d11SEd Tanous     std::string filepath(
540c2051d11SEd Tanous         "/tmp/images/" +
541c2051d11SEd Tanous         boost::uuids::to_string(boost::uuids::random_generator()()));
542c2051d11SEd Tanous     BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
543c2051d11SEd Tanous     std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
544c2051d11SEd Tanous                                     std::ofstream::trunc);
545c2051d11SEd Tanous     out << req.body;
546c2051d11SEd Tanous     out.close();
547c2051d11SEd Tanous     BMCWEB_LOG_DEBUG << "file upload complete!!";
548c2051d11SEd Tanous }
549c2051d11SEd Tanous 
5507e860f15SJohn Edward Broadbent inline void requestRoutesUpdateService(App& app)
5511abe55efSEd Tanous {
5527e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
553ed398213SEd Tanous         .privileges(redfish::privileges::getUpdateService)
554002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
555002d39b4SEd Tanous             [&app](const crow::Request& req,
556002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
5573ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
55845ca1b86SEd Tanous         {
55945ca1b86SEd Tanous             return;
56045ca1b86SEd Tanous         }
5618d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
5620588a3b9SChicago Duan             "#UpdateService.v1_5_0.UpdateService";
5638d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
5648d1b46d7Szhanghch05         asyncResp->res.jsonValue["Id"] = "UpdateService";
565002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "Service for Software Update";
5668d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Update Service";
5674dc23f3fSEd Tanous 
56832ca38afSEd Tanous #ifdef BMCWEB_ENABLE_REDFISH_UPDATESERVICE_OLD_POST_URL
56932ca38afSEd Tanous         // See note about later on in this file about why this is neccesary
57032ca38afSEd Tanous         // This is "Wrong" per the standard, but is done temporarily to
57132ca38afSEd Tanous         // avoid noise in failing tests as people transition to having this
57232ca38afSEd Tanous         // option disabled
57332ca38afSEd Tanous         asyncResp->res.addHeader(boost::beast::http::field::allow,
57432ca38afSEd Tanous                                  "GET, PATCH, HEAD");
57532ca38afSEd Tanous #endif
57632ca38afSEd Tanous 
5777e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["HttpPushUri"] =
5784dc23f3fSEd Tanous             "/redfish/v1/UpdateService/update";
5794dc23f3fSEd Tanous 
5800f74e643SEd Tanous         // UpdateService cannot be disabled
5818d1b46d7Szhanghch05         asyncResp->res.jsonValue["ServiceEnabled"] = true;
5821476687dSEd Tanous         asyncResp->res.jsonValue["FirmwareInventory"]["@odata.id"] =
5831476687dSEd Tanous             "/redfish/v1/UpdateService/FirmwareInventory";
584d61e5194STejas Patil         // Get the MaxImageSizeBytes
585d61e5194STejas Patil         asyncResp->res.jsonValue["MaxImageSizeBytes"] =
586d61e5194STejas Patil             bmcwebHttpReqBodyLimitMb * 1024 * 1024;
587d61e5194STejas Patil 
5880554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
5890554c984SAndrew Geissler         // Update Actions object.
5900554c984SAndrew Geissler         nlohmann::json& updateSvcSimpleUpdate =
591002d39b4SEd Tanous             asyncResp->res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
5920554c984SAndrew Geissler         updateSvcSimpleUpdate["target"] =
5930554c984SAndrew Geissler             "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
594002d39b4SEd Tanous         updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = {
595002d39b4SEd Tanous             "TFTP"};
5960554c984SAndrew Geissler #endif
597274dfe62SJayashankar Padath         // Get the current ApplyTime value
5981e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
5991e1e598dSJonathan Doman             *crow::connections::systemBus, "xyz.openbmc_project.Settings",
6001e1e598dSJonathan Doman             "/xyz/openbmc_project/software/apply_time",
6011e1e598dSJonathan Doman             "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime",
6028d1b46d7Szhanghch05             [asyncResp](const boost::system::error_code ec,
6031e1e598dSJonathan Doman                         const std::string& applyTime) {
604274dfe62SJayashankar Padath             if (ec)
605274dfe62SJayashankar Padath             {
606274dfe62SJayashankar Padath                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
6078d1b46d7Szhanghch05                 messages::internalError(asyncResp->res);
608274dfe62SJayashankar Padath                 return;
609274dfe62SJayashankar Padath             }
610274dfe62SJayashankar Padath 
611274dfe62SJayashankar Padath             // Store the ApplyTime Value
6121e1e598dSJonathan Doman             if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
6131e1e598dSJonathan Doman                              "RequestedApplyTimes.Immediate")
614274dfe62SJayashankar Padath             {
615002d39b4SEd Tanous                 asyncResp->res.jsonValue["HttpPushUriOptions"]
6167e860f15SJohn Edward Broadbent                                         ["HttpPushUriApplyTime"]["ApplyTime"] =
6177e860f15SJohn Edward Broadbent                     "Immediate";
618274dfe62SJayashankar Padath             }
619002d39b4SEd Tanous             else if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
6201e1e598dSJonathan Doman                                   "RequestedApplyTimes.OnReset")
621274dfe62SJayashankar Padath             {
622002d39b4SEd Tanous                 asyncResp->res.jsonValue["HttpPushUriOptions"]
6237e860f15SJohn Edward Broadbent                                         ["HttpPushUriApplyTime"]["ApplyTime"] =
6247e860f15SJohn Edward Broadbent                     "OnReset";
625274dfe62SJayashankar Padath             }
6261e1e598dSJonathan Doman             });
6277e860f15SJohn Edward Broadbent         });
6287e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
629ed398213SEd Tanous         .privileges(redfish::privileges::patchUpdateService)
630002d39b4SEd Tanous         .methods(boost::beast::http::verb::patch)(
631002d39b4SEd Tanous             [&app](const crow::Request& req,
632002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
6333ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
63445ca1b86SEd Tanous         {
63545ca1b86SEd Tanous             return;
63645ca1b86SEd Tanous         }
637fa1a5a38SJayashankar Padath         BMCWEB_LOG_DEBUG << "doPatch...";
638fa1a5a38SJayashankar Padath 
639274dfe62SJayashankar Padath         std::optional<nlohmann::json> pushUriOptions;
640002d39b4SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "HttpPushUriOptions",
641002d39b4SEd Tanous                                       pushUriOptions))
642fa1a5a38SJayashankar Padath         {
643fa1a5a38SJayashankar Padath             return;
644fa1a5a38SJayashankar Padath         }
645fa1a5a38SJayashankar Padath 
646274dfe62SJayashankar Padath         if (pushUriOptions)
647274dfe62SJayashankar Padath         {
648274dfe62SJayashankar Padath             std::optional<nlohmann::json> pushUriApplyTime;
6498d1b46d7Szhanghch05             if (!json_util::readJson(*pushUriOptions, asyncResp->res,
650002d39b4SEd Tanous                                      "HttpPushUriApplyTime", pushUriApplyTime))
651274dfe62SJayashankar Padath             {
652274dfe62SJayashankar Padath                 return;
653274dfe62SJayashankar Padath             }
654274dfe62SJayashankar Padath 
655274dfe62SJayashankar Padath             if (pushUriApplyTime)
656274dfe62SJayashankar Padath             {
657274dfe62SJayashankar Padath                 std::optional<std::string> applyTime;
6580fda0f12SGeorge Liu                 if (!json_util::readJson(*pushUriApplyTime, asyncResp->res,
6590fda0f12SGeorge Liu                                          "ApplyTime", applyTime))
660274dfe62SJayashankar Padath                 {
661274dfe62SJayashankar Padath                     return;
662274dfe62SJayashankar Padath                 }
663274dfe62SJayashankar Padath 
664274dfe62SJayashankar Padath                 if (applyTime)
665fa1a5a38SJayashankar Padath                 {
666fa1a5a38SJayashankar Padath                     std::string applyTimeNewVal;
667fa1a5a38SJayashankar Padath                     if (applyTime == "Immediate")
668fa1a5a38SJayashankar Padath                     {
669274dfe62SJayashankar Padath                         applyTimeNewVal =
6700fda0f12SGeorge Liu                             "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate";
671fa1a5a38SJayashankar Padath                     }
672274dfe62SJayashankar Padath                     else if (applyTime == "OnReset")
673274dfe62SJayashankar Padath                     {
674274dfe62SJayashankar Padath                         applyTimeNewVal =
6750fda0f12SGeorge Liu                             "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
676274dfe62SJayashankar Padath                     }
677fa1a5a38SJayashankar Padath                     else
678fa1a5a38SJayashankar Padath                     {
679274dfe62SJayashankar Padath                         BMCWEB_LOG_INFO
6800fda0f12SGeorge Liu                             << "ApplyTime value is not in the list of acceptable values";
681274dfe62SJayashankar Padath                         messages::propertyValueNotInList(
682274dfe62SJayashankar Padath                             asyncResp->res, *applyTime, "ApplyTime");
683274dfe62SJayashankar Padath                         return;
684fa1a5a38SJayashankar Padath                     }
685fa1a5a38SJayashankar Padath 
686fa1a5a38SJayashankar Padath                     // Set the requested image apply time value
687fa1a5a38SJayashankar Padath                     crow::connections::systemBus->async_method_call(
6880fda0f12SGeorge Liu                         [asyncResp](const boost::system::error_code ec) {
689fa1a5a38SJayashankar Padath                         if (ec)
690fa1a5a38SJayashankar Padath                         {
691002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
692fa1a5a38SJayashankar Padath                             messages::internalError(asyncResp->res);
693fa1a5a38SJayashankar Padath                             return;
694fa1a5a38SJayashankar Padath                         }
695fa1a5a38SJayashankar Padath                         messages::success(asyncResp->res);
696fa1a5a38SJayashankar Padath                         },
697fa1a5a38SJayashankar Padath                         "xyz.openbmc_project.Settings",
698fa1a5a38SJayashankar Padath                         "/xyz/openbmc_project/software/apply_time",
699fa1a5a38SJayashankar Padath                         "org.freedesktop.DBus.Properties", "Set",
700274dfe62SJayashankar Padath                         "xyz.openbmc_project.Software.ApplyTime",
701274dfe62SJayashankar Padath                         "RequestedApplyTime",
702168e20c1SEd Tanous                         dbus::utility::DbusVariantType{applyTimeNewVal});
703fa1a5a38SJayashankar Padath                 }
704274dfe62SJayashankar Padath             }
705fa1a5a38SJayashankar Padath         }
7067e860f15SJohn Edward Broadbent         });
707c2051d11SEd Tanous 
7084dc23f3fSEd Tanous // The "old" behavior of the update service URI causes redfish-service validator
7094dc23f3fSEd Tanous // failures when the Allow header is supported, given that in the spec,
7104dc23f3fSEd Tanous // UpdateService does not allow POST.  in openbmc, we unfortunately reused that
7114dc23f3fSEd Tanous // resource as our HttpPushUri as well.  A number of services, including the
7124dc23f3fSEd Tanous // openbmc tests, and documentation have hardcoded that erroneous API, instead
7134dc23f3fSEd Tanous // of relying on HttpPushUri as the spec requires.  This option will exist
7144dc23f3fSEd Tanous // temporarily to allow the old behavior until Q4 2022, at which time it will be
7154dc23f3fSEd Tanous // removed.
7164dc23f3fSEd Tanous #ifdef BMCWEB_ENABLE_REDFISH_UPDATESERVICE_OLD_POST_URL
7177e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
718ed398213SEd Tanous         .privileges(redfish::privileges::postUpdateService)
719002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
720002d39b4SEd Tanous             [&app](const crow::Request& req,
7214dc23f3fSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
7224dc23f3fSEd Tanous         asyncResp->res.addHeader(
7234dc23f3fSEd Tanous             boost::beast::http::field::warning,
7244dc23f3fSEd Tanous             "299 - \"POST to /redfish/v1/UpdateService is deprecated. Use "
7254dc23f3fSEd Tanous             "the value contained within HttpPushUri.\"");
7264dc23f3fSEd Tanous         handleUpdateServicePost(app, req, asyncResp);
7274dc23f3fSEd Tanous         });
7284dc23f3fSEd Tanous #endif
7294dc23f3fSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/update/")
7304dc23f3fSEd Tanous         .privileges(redfish::privileges::postUpdateService)
7317e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
732c2051d11SEd Tanous             std::bind_front(handleUpdateServicePost, std::ref(app)));
733729dae72SJennifer Lee }
734729dae72SJennifer Lee 
7357e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventoryCollection(App& app)
7361abe55efSEd Tanous {
7377e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/")
738ed398213SEd Tanous         .privileges(redfish::privileges::getSoftwareInventoryCollection)
7391476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
7401476687dSEd Tanous             [&app](const crow::Request& req,
7411476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
7423ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
74345ca1b86SEd Tanous         {
74445ca1b86SEd Tanous             return;
74545ca1b86SEd Tanous         }
7468d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
7470f74e643SEd Tanous             "#SoftwareInventoryCollection.SoftwareInventoryCollection";
7488d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
7490f74e643SEd Tanous             "/redfish/v1/UpdateService/FirmwareInventory";
750002d39b4SEd Tanous         asyncResp->res.jsonValue["Name"] = "Software Inventory Collection";
751c711bf86SEd Tanous 
752c711bf86SEd Tanous         crow::connections::systemBus->async_method_call(
753002d39b4SEd Tanous             [asyncResp](
754002d39b4SEd Tanous                 const boost::system::error_code ec,
755002d39b4SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
7561abe55efSEd Tanous             if (ec)
7571abe55efSEd Tanous             {
758f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
7596c4eb9deSJennifer Lee                 return;
760729dae72SJennifer Lee             }
761002d39b4SEd Tanous             asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
762c711bf86SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = 0;
7636c4eb9deSJennifer Lee 
7649eb808c1SEd Tanous             for (const auto& obj : subtree)
7651abe55efSEd Tanous             {
7662dfd18efSEd Tanous                 sdbusplus::message::object_path path(obj.first);
7672dfd18efSEd Tanous                 std::string swId = path.filename();
7682dfd18efSEd Tanous                 if (swId.empty())
769f4b65ab1SJennifer Lee                 {
770f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
771f4b65ab1SJennifer Lee                     BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
772f4b65ab1SJennifer Lee                     return;
773f4b65ab1SJennifer Lee                 }
774f4b65ab1SJennifer Lee 
775002d39b4SEd Tanous                 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
7761476687dSEd Tanous                 nlohmann::json::object_t member;
7771476687dSEd Tanous                 member["@odata.id"] =
778002d39b4SEd Tanous                     "/redfish/v1/UpdateService/FirmwareInventory/" + swId;
7791476687dSEd Tanous                 members.push_back(std::move(member));
780e0dd8057SAndrew Geissler                 asyncResp->res.jsonValue["Members@odata.count"] =
781c711bf86SEd Tanous                     members.size();
7826c4eb9deSJennifer Lee             }
783c711bf86SEd Tanous             },
7847e860f15SJohn Edward Broadbent             // Note that only firmware levels associated with a device
7857e860f15SJohn Edward Broadbent             // are stored under /xyz/openbmc_project/software therefore
7867e860f15SJohn Edward Broadbent             // to ensure only real FirmwareInventory items are returned,
7877e860f15SJohn Edward Broadbent             // this full object path must be used here as input to
7887e860f15SJohn Edward Broadbent             // mapper
789c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper",
790c711bf86SEd Tanous             "/xyz/openbmc_project/object_mapper",
7912830a9cfSAndrew Geissler             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
7922830a9cfSAndrew Geissler             "/xyz/openbmc_project/software", static_cast<int32_t>(0),
793002d39b4SEd Tanous             std::array<const char*, 1>{"xyz.openbmc_project.Software.Version"});
7947e860f15SJohn Edward Broadbent         });
795729dae72SJennifer Lee }
79687d84729SAndrew Geissler /* Fill related item links (i.e. bmc, bios) in for inventory */
7977e860f15SJohn Edward Broadbent inline static void
7987e860f15SJohn Edward Broadbent     getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
79987d84729SAndrew Geissler                     const std::string& purpose)
80087d84729SAndrew Geissler {
801eee0013eSWilly Tu     if (purpose == sw_util::bmcPurpose)
80287d84729SAndrew Geissler     {
8035af690f5SAbhishek Patel         nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
8041476687dSEd Tanous         nlohmann::json::object_t item;
8051476687dSEd Tanous         item["@odata.id"] = "/redfish/v1/Managers/bmc";
8061476687dSEd Tanous         relatedItem.push_back(std::move(item));
8077e860f15SJohn Edward Broadbent         aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
80887d84729SAndrew Geissler     }
809eee0013eSWilly Tu     else if (purpose == sw_util::biosPurpose)
81087d84729SAndrew Geissler     {
8115af690f5SAbhishek Patel         nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
8121476687dSEd Tanous         nlohmann::json::object_t item;
8131476687dSEd Tanous         item["@odata.id"] = "/redfish/v1/Systems/system/Bios";
8141476687dSEd Tanous         relatedItem.push_back(std::move(item));
8151a6e51aeSJiaqing Zhao         aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
81687d84729SAndrew Geissler     }
81787d84729SAndrew Geissler     else
81887d84729SAndrew Geissler     {
81987d84729SAndrew Geissler         BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
82087d84729SAndrew Geissler     }
82187d84729SAndrew Geissler }
82287d84729SAndrew Geissler 
823af24660dSWilly Tu inline void
824af24660dSWilly Tu     getSoftwareVersion(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
825af24660dSWilly Tu                        const std::string& service, const std::string& path,
826af24660dSWilly Tu                        const std::string& swId)
827af24660dSWilly Tu {
828d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
829d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, service, path,
830d1bde9e5SKrzysztof Grobelny         "xyz.openbmc_project.Software.Version",
831af24660dSWilly Tu         [asyncResp,
832af24660dSWilly Tu          swId](const boost::system::error_code errorCode,
833af24660dSWilly Tu                const dbus::utility::DBusPropertiesMap& propertiesList) {
834af24660dSWilly Tu         if (errorCode)
835af24660dSWilly Tu         {
836af24660dSWilly Tu             messages::internalError(asyncResp->res);
837af24660dSWilly Tu             return;
838af24660dSWilly Tu         }
839d1bde9e5SKrzysztof Grobelny 
840af24660dSWilly Tu         const std::string* swInvPurpose = nullptr;
841af24660dSWilly Tu         const std::string* version = nullptr;
842d1bde9e5SKrzysztof Grobelny 
843d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
844d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList, "Purpose",
845d1bde9e5SKrzysztof Grobelny             swInvPurpose, "Version", version);
846d1bde9e5SKrzysztof Grobelny 
847d1bde9e5SKrzysztof Grobelny         if (!success)
848af24660dSWilly Tu         {
849d1bde9e5SKrzysztof Grobelny             messages::internalError(asyncResp->res);
850d1bde9e5SKrzysztof Grobelny             return;
851af24660dSWilly Tu         }
852af24660dSWilly Tu 
853af24660dSWilly Tu         if (swInvPurpose == nullptr)
854af24660dSWilly Tu         {
855af24660dSWilly Tu             BMCWEB_LOG_DEBUG << "Can't find property \"Purpose\"!";
856af24660dSWilly Tu             messages::internalError(asyncResp->res);
857af24660dSWilly Tu             return;
858af24660dSWilly Tu         }
859af24660dSWilly Tu 
860af24660dSWilly Tu         BMCWEB_LOG_DEBUG << "swInvPurpose = " << *swInvPurpose;
861af24660dSWilly Tu 
862af24660dSWilly Tu         if (version == nullptr)
863af24660dSWilly Tu         {
864af24660dSWilly Tu             BMCWEB_LOG_DEBUG << "Can't find property \"Version\"!";
865af24660dSWilly Tu 
866af24660dSWilly Tu             messages::internalError(asyncResp->res);
867af24660dSWilly Tu 
868af24660dSWilly Tu             return;
869af24660dSWilly Tu         }
870af24660dSWilly Tu         asyncResp->res.jsonValue["Version"] = *version;
871af24660dSWilly Tu         asyncResp->res.jsonValue["Id"] = swId;
872af24660dSWilly Tu 
873af24660dSWilly Tu         // swInvPurpose is of format:
874af24660dSWilly Tu         // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
875af24660dSWilly Tu         // Translate this to "ABC image"
876af24660dSWilly Tu         size_t endDesc = swInvPurpose->rfind('.');
877af24660dSWilly Tu         if (endDesc == std::string::npos)
878af24660dSWilly Tu         {
879af24660dSWilly Tu             messages::internalError(asyncResp->res);
880af24660dSWilly Tu             return;
881af24660dSWilly Tu         }
882af24660dSWilly Tu         endDesc++;
883af24660dSWilly Tu         if (endDesc >= swInvPurpose->size())
884af24660dSWilly Tu         {
885af24660dSWilly Tu             messages::internalError(asyncResp->res);
886af24660dSWilly Tu             return;
887af24660dSWilly Tu         }
888af24660dSWilly Tu 
889af24660dSWilly Tu         std::string formatDesc = swInvPurpose->substr(endDesc);
890af24660dSWilly Tu         asyncResp->res.jsonValue["Description"] = formatDesc + " image";
891af24660dSWilly Tu         getRelatedItems(asyncResp, *swInvPurpose);
892d1bde9e5SKrzysztof Grobelny         });
893af24660dSWilly Tu }
894af24660dSWilly Tu 
8957e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventory(App& app)
8961abe55efSEd Tanous {
8977e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/")
898ed398213SEd Tanous         .privileges(redfish::privileges::getSoftwareInventory)
899002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
900002d39b4SEd Tanous             [&app](const crow::Request& req,
90145ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9027e860f15SJohn Edward Broadbent                    const std::string& param) {
9033ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
90445ca1b86SEd Tanous         {
90545ca1b86SEd Tanous             return;
90645ca1b86SEd Tanous         }
9073ae837c9SEd Tanous         std::shared_ptr<std::string> swId =
9087e860f15SJohn Edward Broadbent             std::make_shared<std::string>(param);
909c711bf86SEd Tanous 
9108d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
9113ae837c9SEd Tanous             "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
912c711bf86SEd Tanous 
913c711bf86SEd Tanous         crow::connections::systemBus->async_method_call(
914b9d36b47SEd Tanous             [asyncResp,
915b9d36b47SEd Tanous              swId](const boost::system::error_code ec,
916b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
91755c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "doGet callback...";
9181abe55efSEd Tanous             if (ec)
9191abe55efSEd Tanous             {
920f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
9216c4eb9deSJennifer Lee                 return;
9226c4eb9deSJennifer Lee             }
9236c4eb9deSJennifer Lee 
9246913228dSAndrew Geissler             // Ensure we find our input swId, otherwise return an error
9256913228dSAndrew Geissler             bool found = false;
926002d39b4SEd Tanous             for (const std::pair<std::string,
9277e860f15SJohn Edward Broadbent                                  std::vector<std::pair<
928002d39b4SEd Tanous                                      std::string, std::vector<std::string>>>>&
929002d39b4SEd Tanous                      obj : subtree)
9301abe55efSEd Tanous             {
93111ba3979SEd Tanous                 if (!obj.first.ends_with(*swId))
9321abe55efSEd Tanous                 {
933acb7cfb4SJennifer Lee                     continue;
934acb7cfb4SJennifer Lee                 }
935acb7cfb4SJennifer Lee 
93626f6976fSEd Tanous                 if (obj.second.empty())
9371abe55efSEd Tanous                 {
938acb7cfb4SJennifer Lee                     continue;
939acb7cfb4SJennifer Lee                 }
9406c4eb9deSJennifer Lee 
9416913228dSAndrew Geissler                 found = true;
942eee0013eSWilly Tu                 sw_util::getSwStatus(asyncResp, swId, obj.second[0].first);
943af24660dSWilly Tu                 getSoftwareVersion(asyncResp, obj.second[0].first, obj.first,
944af24660dSWilly Tu                                    *swId);
9456c4eb9deSJennifer Lee             }
9466913228dSAndrew Geissler             if (!found)
9476913228dSAndrew Geissler             {
948002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Input swID " << *swId << " not found!";
9496913228dSAndrew Geissler                 messages::resourceMissingAtURI(
950002d39b4SEd Tanous                     asyncResp->res, crow::utility::urlFromPieces(
951ace85d60SEd Tanous                                         "redfish", "v1", "UpdateService",
952ace85d60SEd Tanous                                         "FirmwareInventory", *swId));
9536913228dSAndrew Geissler                 return;
9546913228dSAndrew Geissler             }
9554e68c45bSAyushi Smriti             asyncResp->res.jsonValue["@odata.type"] =
9564e68c45bSAyushi Smriti                 "#SoftwareInventory.v1_1_0.SoftwareInventory";
9574e68c45bSAyushi Smriti             asyncResp->res.jsonValue["Name"] = "Software Inventory";
9584e68c45bSAyushi Smriti             asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
9593f8a743aSAppaRao Puli 
9603f8a743aSAppaRao Puli             asyncResp->res.jsonValue["Updateable"] = false;
961eee0013eSWilly Tu             sw_util::getSwUpdatableStatus(asyncResp, swId);
962c711bf86SEd Tanous             },
963c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper",
964c711bf86SEd Tanous             "/xyz/openbmc_project/object_mapper",
965271584abSEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
966271584abSEd Tanous             static_cast<int32_t>(0),
967002d39b4SEd Tanous             std::array<const char*, 1>{"xyz.openbmc_project.Software.Version"});
9687e860f15SJohn Edward Broadbent         });
9696c4eb9deSJennifer Lee }
970729dae72SJennifer Lee 
971729dae72SJennifer Lee } // namespace redfish
972