xref: /openbmc/bmcweb/features/redfish/lib/update_service.hpp (revision 4dc23f3fb6c9a7cef84658f8ab3b703d29ec7d57)
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>
2587d84729SAndrew Geissler #include <utils/fw_utils.hpp>
261214b7e7SGunnar Mills 
271abe55efSEd Tanous namespace redfish
281abe55efSEd Tanous {
2927826b5fSEd Tanous 
300e7de46fSAndrew Geissler // Match signals added on software path
31acb7cfb4SJennifer Lee static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher;
324cde5d90SJames Feist static std::unique_ptr<sdbusplus::bus::match::match> 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,
66fe306728SJames Feist                            sdbusplus::message::message& m,
67a3e65892SEd Tanous                            task::Payload&& payload)
6886adcd6dSAndrew Geissler {
69b9d36b47SEd Tanous     dbus::utility::DBusInteracesMap interfacesProperties;
7086adcd6dSAndrew Geissler 
7186adcd6dSAndrew Geissler     sdbusplus::message::object_path objPath;
7286adcd6dSAndrew Geissler 
7386adcd6dSAndrew Geissler     m.read(objPath, interfacesProperties);
7486adcd6dSAndrew Geissler 
7586adcd6dSAndrew Geissler     BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
7686adcd6dSAndrew Geissler     for (auto& interface : interfacesProperties)
7786adcd6dSAndrew Geissler     {
7886adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "interface = " << interface.first;
7986adcd6dSAndrew Geissler 
8086adcd6dSAndrew Geissler         if (interface.first == "xyz.openbmc_project.Software.Activation")
8186adcd6dSAndrew Geissler         {
8286adcd6dSAndrew Geissler             // Retrieve service and activate
8386adcd6dSAndrew Geissler             crow::connections::systemBus->async_method_call(
84a3e65892SEd Tanous                 [objPath, asyncResp, payload(std::move(payload))](
85a3e65892SEd Tanous                     const boost::system::error_code errorCode,
86a3e65892SEd Tanous                     const std::vector<
87a3e65892SEd Tanous                         std::pair<std::string, std::vector<std::string>>>&
88a3e65892SEd Tanous                         objInfo) mutable {
8981ce609eSEd Tanous                     if (errorCode)
9086adcd6dSAndrew Geissler                     {
9181ce609eSEd Tanous                         BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
9286adcd6dSAndrew Geissler                         BMCWEB_LOG_DEBUG << "error msg = "
9381ce609eSEd Tanous                                          << errorCode.message();
940554c984SAndrew Geissler                         if (asyncResp)
950554c984SAndrew Geissler                         {
9686adcd6dSAndrew Geissler                             messages::internalError(asyncResp->res);
970554c984SAndrew Geissler                         }
9886adcd6dSAndrew Geissler                         cleanUp();
9986adcd6dSAndrew Geissler                         return;
10086adcd6dSAndrew Geissler                     }
10186adcd6dSAndrew Geissler                     // Ensure we only got one service back
10286adcd6dSAndrew Geissler                     if (objInfo.size() != 1)
10386adcd6dSAndrew Geissler                     {
10486adcd6dSAndrew Geissler                         BMCWEB_LOG_ERROR << "Invalid Object Size "
10586adcd6dSAndrew Geissler                                          << objInfo.size();
1060554c984SAndrew Geissler                         if (asyncResp)
1070554c984SAndrew Geissler                         {
10886adcd6dSAndrew Geissler                             messages::internalError(asyncResp->res);
1090554c984SAndrew Geissler                         }
11086adcd6dSAndrew Geissler                         cleanUp();
11186adcd6dSAndrew Geissler                         return;
11286adcd6dSAndrew Geissler                     }
11386adcd6dSAndrew Geissler                     // cancel timer only when
11486adcd6dSAndrew Geissler                     // xyz.openbmc_project.Software.Activation interface
11586adcd6dSAndrew Geissler                     // is added
11686adcd6dSAndrew Geissler                     fwAvailableTimer = nullptr;
11786adcd6dSAndrew Geissler 
11886adcd6dSAndrew Geissler                     activateImage(objPath.str, objInfo[0].first);
1190554c984SAndrew Geissler                     if (asyncResp)
1200554c984SAndrew Geissler                     {
12132898ceaSJames Feist                         std::shared_ptr<task::TaskData> task =
12232898ceaSJames Feist                             task::TaskData::createTask(
12332898ceaSJames Feist                                 [](boost::system::error_code ec,
12432898ceaSJames Feist                                    sdbusplus::message::message& msg,
1251214b7e7SGunnar Mills                                    const std::shared_ptr<task::TaskData>&
1261214b7e7SGunnar Mills                                        taskData) {
12732898ceaSJames Feist                                     if (ec)
12832898ceaSJames Feist                                     {
12932898ceaSJames Feist                                         return task::completed;
13032898ceaSJames Feist                                     }
13132898ceaSJames Feist 
13232898ceaSJames Feist                                     std::string iface;
133b9d36b47SEd Tanous                                     dbus::utility::DBusPropertiesMap values;
134fd9ab9e1SJames Feist 
135fd9ab9e1SJames Feist                                     std::string index =
136fd9ab9e1SJames Feist                                         std::to_string(taskData->index);
13732898ceaSJames Feist                                     msg.read(iface, values);
138fd9ab9e1SJames Feist 
1390fda0f12SGeorge Liu                                     if (iface ==
1400fda0f12SGeorge Liu                                         "xyz.openbmc_project.Software.Activation")
141fd9ab9e1SJames Feist                                     {
142b9d36b47SEd Tanous                                         std::string* state = nullptr;
143b9d36b47SEd Tanous                                         for (const auto& property : values)
14432898ceaSJames Feist                                         {
145b9d36b47SEd Tanous                                             if (property.first == "Activation")
146b9d36b47SEd Tanous                                             {
147b9d36b47SEd Tanous                                                 const std::string* state =
14832898ceaSJames Feist                                                     std::get_if<std::string>(
149b9d36b47SEd Tanous                                                         &property.second);
150b9d36b47SEd Tanous                                                 if (state == nullptr)
151b9d36b47SEd Tanous                                                 {
152b9d36b47SEd Tanous                                                     taskData->messages
153b9d36b47SEd Tanous                                                         .emplace_back(
154b9d36b47SEd Tanous                                                             messages::
155b9d36b47SEd Tanous                                                                 internalError());
156b9d36b47SEd Tanous                                                     return task::completed;
157b9d36b47SEd Tanous                                                 }
158b9d36b47SEd Tanous                                             }
159b9d36b47SEd Tanous                                         }
16032898ceaSJames Feist 
16132898ceaSJames Feist                                         if (state == nullptr)
16232898ceaSJames Feist                                         {
163b9d36b47SEd Tanous                                             return !task::completed;
16432898ceaSJames Feist                                         }
16532898ceaSJames Feist 
166fd9ab9e1SJames Feist                                         if (boost::ends_with(*state,
167fd9ab9e1SJames Feist                                                              "Invalid") ||
16832898ceaSJames Feist                                             boost::ends_with(*state, "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 
17732898ceaSJames Feist                                         if (boost::ends_with(*state, "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
188fd9ab9e1SJames Feist                                             taskData->extendTimer(
189fd9ab9e1SJames Feist                                                 std::chrono::hours(5));
19032898ceaSJames Feist                                             return !task::completed;
19132898ceaSJames Feist                                         }
19232898ceaSJames Feist 
19332898ceaSJames Feist                                         if (boost::ends_with(*state, "Active"))
19432898ceaSJames Feist                                         {
19532898ceaSJames Feist                                             taskData->messages.emplace_back(
196fd9ab9e1SJames Feist                                                 messages::taskCompletedOK(
197fd9ab9e1SJames Feist                                                     index));
19832898ceaSJames Feist                                             taskData->state = "Completed";
19932898ceaSJames Feist                                             return task::completed;
20032898ceaSJames Feist                                         }
201fd9ab9e1SJames Feist                                     }
2020fda0f12SGeorge Liu                                     else if (
2030fda0f12SGeorge Liu                                         iface ==
2040fda0f12SGeorge Liu                                         "xyz.openbmc_project.Software.ActivationProgress")
205fd9ab9e1SJames Feist                                     {
206b9d36b47SEd Tanous 
207b9d36b47SEd Tanous                                         const uint8_t* progress = nullptr;
208b9d36b47SEd Tanous                                         for (const auto& property : values)
209fd9ab9e1SJames Feist                                         {
210b9d36b47SEd Tanous                                             if (property.first == "Progress")
211b9d36b47SEd Tanous                                             {
212b9d36b47SEd Tanous                                                 const std::string* progress =
213b9d36b47SEd Tanous                                                     std::get_if<std::string>(
214b9d36b47SEd Tanous                                                         &property.second);
215b9d36b47SEd Tanous                                                 if (progress == nullptr)
216b9d36b47SEd Tanous                                                 {
217b9d36b47SEd Tanous                                                     taskData->messages
218b9d36b47SEd Tanous                                                         .emplace_back(
219b9d36b47SEd Tanous                                                             messages::
220b9d36b47SEd Tanous                                                                 internalError());
221b9d36b47SEd Tanous                                                     return task::completed;
222fd9ab9e1SJames Feist                                                 }
223b9d36b47SEd Tanous                                             }
224b9d36b47SEd Tanous                                         }
225fd9ab9e1SJames Feist 
226fd9ab9e1SJames Feist                                         if (progress == nullptr)
227fd9ab9e1SJames Feist                                         {
228b9d36b47SEd Tanous                                             return !task::completed;
229fd9ab9e1SJames Feist                                         }
2306868ff50SGeorge Liu                                         taskData->percentComplete =
2316868ff50SGeorge Liu                                             static_cast<int>(*progress);
232fd9ab9e1SJames Feist                                         taskData->messages.emplace_back(
233fd9ab9e1SJames Feist                                             messages::taskProgressChanged(
234fd9ab9e1SJames Feist                                                 index, static_cast<size_t>(
235fd9ab9e1SJames Feist                                                            *progress)));
236fd9ab9e1SJames Feist 
237fd9ab9e1SJames Feist                                         // if we're getting status updates it's
238fd9ab9e1SJames Feist                                         // still alive, update timer
239fd9ab9e1SJames Feist                                         taskData->extendTimer(
240fd9ab9e1SJames Feist                                             std::chrono::minutes(5));
241fd9ab9e1SJames Feist                                     }
24232898ceaSJames Feist 
24332898ceaSJames Feist                                     // as firmware update often results in a
24432898ceaSJames Feist                                     // reboot, the task  may never "complete"
24532898ceaSJames Feist                                     // unless it is an error
24632898ceaSJames Feist 
24732898ceaSJames Feist                                     return !task::completed;
24832898ceaSJames Feist                                 },
2490fda0f12SGeorge Liu                                 "type='signal',interface='org.freedesktop.DBus.Properties',"
250fd9ab9e1SJames Feist                                 "member='PropertiesChanged',path='" +
25132898ceaSJames Feist                                     objPath.str + "'");
25232898ceaSJames Feist                         task->startTimer(std::chrono::minutes(5));
25332898ceaSJames Feist                         task->populateResp(asyncResp->res);
254a3e65892SEd Tanous                         task->payload.emplace(std::move(payload));
2550554c984SAndrew Geissler                     }
25686adcd6dSAndrew Geissler                     fwUpdateInProgress = false;
25786adcd6dSAndrew Geissler                 },
25886adcd6dSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper",
25986adcd6dSAndrew Geissler                 "/xyz/openbmc_project/object_mapper",
26086adcd6dSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str,
26186adcd6dSAndrew Geissler                 std::array<const char*, 1>{
26286adcd6dSAndrew Geissler                     "xyz.openbmc_project.Software.Activation"});
26386adcd6dSAndrew Geissler         }
26486adcd6dSAndrew Geissler     }
26586adcd6dSAndrew Geissler }
26686adcd6dSAndrew Geissler 
2670554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr
2680554c984SAndrew Geissler // then no asyncResp updates will occur
269b5a76932SEd Tanous static void monitorForSoftwareAvailable(
2708d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2718d1b46d7Szhanghch05     const crow::Request& req, const std::string& url,
2728d1b46d7Szhanghch05     int timeoutTimeSeconds = 10)
27386adcd6dSAndrew Geissler {
27486adcd6dSAndrew Geissler     // Only allow one FW update at a time
275e05aec50SEd Tanous     if (fwUpdateInProgress)
27686adcd6dSAndrew Geissler     {
2770554c984SAndrew Geissler         if (asyncResp)
2780554c984SAndrew Geissler         {
27986adcd6dSAndrew Geissler             messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
2800554c984SAndrew Geissler         }
28186adcd6dSAndrew Geissler         return;
28286adcd6dSAndrew Geissler     }
28386adcd6dSAndrew Geissler 
2840554c984SAndrew Geissler     fwAvailableTimer =
285271584abSEd Tanous         std::make_unique<boost::asio::steady_timer>(*req.ioService);
28686adcd6dSAndrew Geissler 
287271584abSEd Tanous     fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds));
28886adcd6dSAndrew Geissler 
28986adcd6dSAndrew Geissler     fwAvailableTimer->async_wait(
29086adcd6dSAndrew Geissler         [asyncResp](const boost::system::error_code& ec) {
29186adcd6dSAndrew Geissler             cleanUp();
29286adcd6dSAndrew Geissler             if (ec == boost::asio::error::operation_aborted)
29386adcd6dSAndrew Geissler             {
29486adcd6dSAndrew Geissler                 // expected, we were canceled before the timer completed.
29586adcd6dSAndrew Geissler                 return;
29686adcd6dSAndrew Geissler             }
29786adcd6dSAndrew Geissler             BMCWEB_LOG_ERROR
29886adcd6dSAndrew Geissler                 << "Timed out waiting for firmware object being created";
29986adcd6dSAndrew Geissler             BMCWEB_LOG_ERROR
30086adcd6dSAndrew Geissler                 << "FW image may has already been uploaded to server";
30186adcd6dSAndrew Geissler             if (ec)
30286adcd6dSAndrew Geissler             {
30386adcd6dSAndrew Geissler                 BMCWEB_LOG_ERROR << "Async_wait failed" << ec;
30486adcd6dSAndrew Geissler                 return;
30586adcd6dSAndrew Geissler             }
3060554c984SAndrew Geissler             if (asyncResp)
3070554c984SAndrew Geissler             {
30886adcd6dSAndrew Geissler                 redfish::messages::internalError(asyncResp->res);
3090554c984SAndrew Geissler             }
31086adcd6dSAndrew Geissler         });
311a3e65892SEd Tanous     task::Payload payload(req);
312a3e65892SEd Tanous     auto callback = [asyncResp,
313a3e65892SEd Tanous                      payload](sdbusplus::message::message& m) mutable {
31486adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "Match fired";
315a3e65892SEd Tanous         softwareInterfaceAdded(asyncResp, m, std::move(payload));
31686adcd6dSAndrew Geissler     };
31786adcd6dSAndrew Geissler 
31886adcd6dSAndrew Geissler     fwUpdateInProgress = true;
31986adcd6dSAndrew Geissler 
32086adcd6dSAndrew Geissler     fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
32186adcd6dSAndrew Geissler         *crow::connections::systemBus,
32286adcd6dSAndrew Geissler         "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
32386adcd6dSAndrew Geissler         "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
32486adcd6dSAndrew Geissler         callback);
3254cde5d90SJames Feist 
3264cde5d90SJames Feist     fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match::match>(
3274cde5d90SJames Feist         *crow::connections::systemBus,
328e1cc4828SBrian Ma         "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
329e1cc4828SBrian Ma         "member='InterfacesAdded',"
330e1cc4828SBrian Ma         "path='/xyz/openbmc_project/logging'",
3314cde5d90SJames Feist         [asyncResp, url](sdbusplus::message::message& m) {
332e1cc4828SBrian Ma             std::vector<
333e1cc4828SBrian Ma                 std::pair<std::string, dbus::utility::DBusPropertiesMap>>
334e1cc4828SBrian Ma                 interfacesProperties;
335e1cc4828SBrian Ma             sdbusplus::message::object_path objPath;
336e1cc4828SBrian Ma             m.read(objPath, interfacesProperties);
337e1cc4828SBrian Ma             BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
338e1cc4828SBrian Ma             for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>&
339e1cc4828SBrian Ma                      interface : interfacesProperties)
340e1cc4828SBrian Ma             {
341e1cc4828SBrian Ma                 if (interface.first == "xyz.openbmc_project.Logging.Entry")
342e1cc4828SBrian Ma                 {
343711ac7a9SEd Tanous                     for (const std::pair<std::string,
344711ac7a9SEd Tanous                                          dbus::utility::DbusVariantType>&
345711ac7a9SEd Tanous                              value : interface.second)
3464cde5d90SJames Feist                     {
347711ac7a9SEd Tanous                         if (value.first != "Message")
348711ac7a9SEd Tanous                         {
349711ac7a9SEd Tanous                             continue;
3504cde5d90SJames Feist                         }
351e1cc4828SBrian Ma                         const std::string* type =
352711ac7a9SEd Tanous                             std::get_if<std::string>(&value.second);
3534cde5d90SJames Feist                         if (type == nullptr)
3544cde5d90SJames Feist                         {
355e1cc4828SBrian Ma                             // if this was our message, timeout will cover it
3564cde5d90SJames Feist                             return;
3574cde5d90SJames Feist                         }
358e1cc4828SBrian Ma                         fwAvailableTimer = nullptr;
3594cde5d90SJames Feist                         if (*type ==
3604cde5d90SJames Feist                             "xyz.openbmc_project.Software.Image.Error.UnTarFailure")
3614cde5d90SJames Feist                         {
362711ac7a9SEd Tanous                             redfish::messages::invalidUpload(
363711ac7a9SEd Tanous                                 asyncResp->res, url, "Invalid archive");
3644cde5d90SJames Feist                         }
365e1cc4828SBrian Ma                         else if (*type ==
366e1cc4828SBrian Ma                                  "xyz.openbmc_project.Software.Image.Error."
367e1cc4828SBrian Ma                                  "ManifestFileFailure")
3684cde5d90SJames Feist                         {
369711ac7a9SEd Tanous                             redfish::messages::invalidUpload(
370711ac7a9SEd Tanous                                 asyncResp->res, url, "Invalid manifest");
3714cde5d90SJames Feist                         }
372e1cc4828SBrian Ma                         else if (
373e1cc4828SBrian Ma                             *type ==
3744cde5d90SJames Feist                             "xyz.openbmc_project.Software.Image.Error.ImageFailure")
3754cde5d90SJames Feist                         {
376e1cc4828SBrian Ma                             redfish::messages::invalidUpload(
377e1cc4828SBrian Ma                                 asyncResp->res, url, "Invalid image format");
3784cde5d90SJames Feist                         }
379e1cc4828SBrian Ma                         else if (
380e1cc4828SBrian Ma                             *type ==
3810fda0f12SGeorge Liu                             "xyz.openbmc_project.Software.Version.Error.AlreadyExists")
38288b3dd12SGunnar Mills                         {
38388b3dd12SGunnar Mills                             redfish::messages::invalidUpload(
384e1cc4828SBrian Ma                                 asyncResp->res, url,
385e1cc4828SBrian Ma                                 "Image version already exists");
38688b3dd12SGunnar Mills 
38788b3dd12SGunnar Mills                             redfish::messages::resourceAlreadyExists(
388e1cc4828SBrian Ma                                 asyncResp->res,
389e1cc4828SBrian Ma                                 "UpdateService.v1_5_0.UpdateService", "Version",
390e1cc4828SBrian Ma                                 "uploaded version");
39188b3dd12SGunnar Mills                         }
392e1cc4828SBrian Ma                         else if (
393e1cc4828SBrian Ma                             *type ==
3944cde5d90SJames Feist                             "xyz.openbmc_project.Software.Image.Error.BusyFailure")
3954cde5d90SJames Feist                         {
396711ac7a9SEd Tanous                             redfish::messages::resourceExhaustion(
397711ac7a9SEd Tanous                                 asyncResp->res, url);
3984cde5d90SJames Feist                         }
3994cde5d90SJames Feist                         else
4004cde5d90SJames Feist                         {
4014cde5d90SJames Feist                             redfish::messages::internalError(asyncResp->res);
4024cde5d90SJames Feist                         }
403e1cc4828SBrian Ma                     }
404e1cc4828SBrian Ma                 }
405711ac7a9SEd Tanous             }
4064cde5d90SJames Feist         });
40786adcd6dSAndrew Geissler }
408729dae72SJennifer Lee 
4090554c984SAndrew Geissler /**
4100554c984SAndrew Geissler  * UpdateServiceActionsSimpleUpdate class supports handle POST method for
4110554c984SAndrew Geissler  * SimpleUpdate action.
4120554c984SAndrew Geissler  */
4137e860f15SJohn Edward Broadbent inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app)
4140554c984SAndrew Geissler {
4157e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
4167e860f15SJohn Edward Broadbent         app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
417ed398213SEd Tanous         .privileges(redfish::privileges::postUpdateService)
4187e860f15SJohn Edward Broadbent         .methods(
4197e860f15SJohn Edward Broadbent             boost::beast::http::verb::
42045ca1b86SEd Tanous                 post)([&app](
42145ca1b86SEd Tanous                           const crow::Request& req,
4227e860f15SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
42345ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
42445ca1b86SEd Tanous             {
42545ca1b86SEd Tanous                 return;
42645ca1b86SEd Tanous             }
42745ca1b86SEd Tanous 
4280554c984SAndrew Geissler             std::optional<std::string> transferProtocol;
4290554c984SAndrew Geissler             std::string imageURI;
4300554c984SAndrew Geissler 
4310554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost";
4320554c984SAndrew Geissler 
4330554c984SAndrew Geissler             // User can pass in both TransferProtocol and ImageURI parameters or
4344e0453b1SGunnar Mills             // they can pass in just the ImageURI with the transfer protocol
4354e0453b1SGunnar Mills             // embedded within it.
4360554c984SAndrew Geissler             // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
4370554c984SAndrew Geissler             // 2) ImageURI:tftp://1.1.1.1/myfile.bin
4380554c984SAndrew Geissler 
43915ed6780SWilly Tu             if (!json_util::readJsonAction(req, asyncResp->res,
44015ed6780SWilly Tu                                            "TransferProtocol", transferProtocol,
44115ed6780SWilly Tu                                            "ImageURI", imageURI))
4420554c984SAndrew Geissler             {
4430554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG
4440554c984SAndrew Geissler                     << "Missing TransferProtocol or ImageURI parameter";
4450554c984SAndrew Geissler                 return;
4460554c984SAndrew Geissler             }
4470554c984SAndrew Geissler             if (!transferProtocol)
4480554c984SAndrew Geissler             {
4490554c984SAndrew Geissler                 // Must be option 2
4500554c984SAndrew Geissler                 // Verify ImageURI has transfer protocol in it
451f23b7296SEd Tanous                 size_t separator = imageURI.find(':');
4520554c984SAndrew Geissler                 if ((separator == std::string::npos) ||
4530554c984SAndrew Geissler                     ((separator + 1) > imageURI.size()))
4540554c984SAndrew Geissler                 {
4550554c984SAndrew Geissler                     messages::actionParameterValueTypeError(
4560554c984SAndrew Geissler                         asyncResp->res, imageURI, "ImageURI",
4570554c984SAndrew Geissler                         "UpdateService.SimpleUpdate");
4580554c984SAndrew Geissler                     BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: "
4590554c984SAndrew Geissler                                      << imageURI;
4600554c984SAndrew Geissler                     return;
4610554c984SAndrew Geissler                 }
4620554c984SAndrew Geissler                 transferProtocol = imageURI.substr(0, separator);
4637e860f15SJohn Edward Broadbent                 // Ensure protocol is upper case for a common comparison path
4647e860f15SJohn Edward Broadbent                 // below
4650554c984SAndrew Geissler                 boost::to_upper(*transferProtocol);
4660554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG << "Encoded transfer protocol "
4670554c984SAndrew Geissler                                  << *transferProtocol;
4680554c984SAndrew Geissler 
4690554c984SAndrew Geissler                 // Adjust imageURI to not have the protocol on it for parsing
4700554c984SAndrew Geissler                 // below
4710554c984SAndrew Geissler                 // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin
4720554c984SAndrew Geissler                 imageURI = imageURI.substr(separator + 3);
4730554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI;
4740554c984SAndrew Geissler             }
4750554c984SAndrew Geissler 
4760554c984SAndrew Geissler             // OpenBMC currently only supports TFTP
4770554c984SAndrew Geissler             if (*transferProtocol != "TFTP")
4780554c984SAndrew Geissler             {
4797e860f15SJohn Edward Broadbent                 messages::actionParameterNotSupported(
4807e860f15SJohn Edward Broadbent                     asyncResp->res, "TransferProtocol",
4810554c984SAndrew Geissler                     "UpdateService.SimpleUpdate");
4820554c984SAndrew Geissler                 BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: "
4830554c984SAndrew Geissler                                  << *transferProtocol;
4840554c984SAndrew Geissler                 return;
4850554c984SAndrew Geissler             }
4860554c984SAndrew Geissler 
4870554c984SAndrew Geissler             // Format should be <IP or Hostname>/<file> for imageURI
488f23b7296SEd Tanous             size_t separator = imageURI.find('/');
4890554c984SAndrew Geissler             if ((separator == std::string::npos) ||
4900554c984SAndrew Geissler                 ((separator + 1) > imageURI.size()))
4910554c984SAndrew Geissler             {
4920554c984SAndrew Geissler                 messages::actionParameterValueTypeError(
4930554c984SAndrew Geissler                     asyncResp->res, imageURI, "ImageURI",
4940554c984SAndrew Geissler                     "UpdateService.SimpleUpdate");
4950554c984SAndrew Geissler                 BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI;
4960554c984SAndrew Geissler                 return;
4970554c984SAndrew Geissler             }
4980554c984SAndrew Geissler 
4990554c984SAndrew Geissler             std::string tftpServer = imageURI.substr(0, separator);
5000554c984SAndrew Geissler             std::string fwFile = imageURI.substr(separator + 1);
5010554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile;
5020554c984SAndrew Geissler 
5030554c984SAndrew Geissler             // Setup callback for when new software detected
5042618d5e3SGunnar Mills             // Give TFTP 10 minutes to complete
5054cde5d90SJames Feist             monitorForSoftwareAvailable(
506d7a596bdSAlbert Zhang                 asyncResp, req,
5074cde5d90SJames Feist                 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate",
5082618d5e3SGunnar Mills                 600);
5090554c984SAndrew Geissler 
5102618d5e3SGunnar Mills             // TFTP can take up to 10 minutes depending on image size and
5110554c984SAndrew Geissler             // connection speed. Return to caller as soon as the TFTP operation
5120554c984SAndrew Geissler             // has been started. The callback above will ensure the activate
5130554c984SAndrew Geissler             // is started once the download has completed
5140554c984SAndrew Geissler             redfish::messages::success(asyncResp->res);
5150554c984SAndrew Geissler 
5160554c984SAndrew Geissler             // Call TFTP service
5170554c984SAndrew Geissler             crow::connections::systemBus->async_method_call(
5180554c984SAndrew Geissler                 [](const boost::system::error_code ec) {
5190554c984SAndrew Geissler                     if (ec)
5200554c984SAndrew Geissler                     {
5210554c984SAndrew Geissler                         // messages::internalError(asyncResp->res);
5220554c984SAndrew Geissler                         cleanUp();
5230554c984SAndrew Geissler                         BMCWEB_LOG_DEBUG << "error_code = " << ec;
5240554c984SAndrew Geissler                         BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
5250554c984SAndrew Geissler                     }
5260554c984SAndrew Geissler                     else
5270554c984SAndrew Geissler                     {
5280554c984SAndrew Geissler                         BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success";
5290554c984SAndrew Geissler                     }
5300554c984SAndrew Geissler                 },
5310554c984SAndrew Geissler                 "xyz.openbmc_project.Software.Download",
5327e860f15SJohn Edward Broadbent                 "/xyz/openbmc_project/software",
5337e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Common.TFTP", "DownloadViaTFTP", fwFile,
5347e860f15SJohn Edward Broadbent                 tftpServer);
5350554c984SAndrew Geissler 
5360554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost";
5377e860f15SJohn Edward Broadbent         });
538729dae72SJennifer Lee }
539729dae72SJennifer Lee 
540c2051d11SEd Tanous inline void
541c2051d11SEd Tanous     handleUpdateServicePost(App& app, const crow::Request& req,
542c2051d11SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
543c2051d11SEd Tanous {
544c2051d11SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
545c2051d11SEd Tanous     {
546c2051d11SEd Tanous         return;
547c2051d11SEd Tanous     }
548c2051d11SEd Tanous     BMCWEB_LOG_DEBUG << "doPost...";
549c2051d11SEd Tanous 
550c2051d11SEd Tanous     // Setup callback for when new software detected
551c2051d11SEd Tanous     monitorForSoftwareAvailable(asyncResp, req, "/redfish/v1/UpdateService");
552c2051d11SEd Tanous 
553c2051d11SEd Tanous     std::string filepath(
554c2051d11SEd Tanous         "/tmp/images/" +
555c2051d11SEd Tanous         boost::uuids::to_string(boost::uuids::random_generator()()));
556c2051d11SEd Tanous     BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
557c2051d11SEd Tanous     std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
558c2051d11SEd Tanous                                     std::ofstream::trunc);
559c2051d11SEd Tanous     out << req.body;
560c2051d11SEd Tanous     out.close();
561c2051d11SEd Tanous     BMCWEB_LOG_DEBUG << "file upload complete!!";
562c2051d11SEd Tanous }
563c2051d11SEd Tanous 
5647e860f15SJohn Edward Broadbent inline void requestRoutesUpdateService(App& app)
5651abe55efSEd Tanous {
5667e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
567ed398213SEd Tanous         .privileges(redfish::privileges::getUpdateService)
56845ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
56945ca1b86SEd Tanous                                                        const std::shared_ptr<
57045ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
57145ca1b86SEd Tanous                                                            asyncResp) {
57245ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
57345ca1b86SEd Tanous             {
57445ca1b86SEd Tanous                 return;
57545ca1b86SEd Tanous             }
5768d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.type"] =
5770588a3b9SChicago Duan                 "#UpdateService.v1_5_0.UpdateService";
5788d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
5798d1b46d7Szhanghch05             asyncResp->res.jsonValue["Id"] = "UpdateService";
5807e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
5817e860f15SJohn Edward Broadbent                 "Service for Software Update";
5828d1b46d7Szhanghch05             asyncResp->res.jsonValue["Name"] = "Update Service";
583*4dc23f3fSEd Tanous 
5847e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["HttpPushUri"] =
585*4dc23f3fSEd Tanous                 "/redfish/v1/UpdateService/update";
586*4dc23f3fSEd Tanous 
5870f74e643SEd Tanous             // UpdateService cannot be disabled
5888d1b46d7Szhanghch05             asyncResp->res.jsonValue["ServiceEnabled"] = true;
5891476687dSEd Tanous             asyncResp->res.jsonValue["FirmwareInventory"]["@odata.id"] =
5901476687dSEd Tanous                 "/redfish/v1/UpdateService/FirmwareInventory";
591d61e5194STejas Patil             // Get the MaxImageSizeBytes
592d61e5194STejas Patil             asyncResp->res.jsonValue["MaxImageSizeBytes"] =
593d61e5194STejas Patil                 bmcwebHttpReqBodyLimitMb * 1024 * 1024;
594d61e5194STejas Patil 
5950554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
5960554c984SAndrew Geissler             // Update Actions object.
5970554c984SAndrew Geissler             nlohmann::json& updateSvcSimpleUpdate =
5987e860f15SJohn Edward Broadbent                 asyncResp->res
5997e860f15SJohn Edward Broadbent                     .jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
6000554c984SAndrew Geissler             updateSvcSimpleUpdate["target"] =
6010554c984SAndrew Geissler                 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
6027e860f15SJohn Edward Broadbent             updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] =
6037e860f15SJohn Edward Broadbent                 {"TFTP"};
6040554c984SAndrew Geissler #endif
605274dfe62SJayashankar Padath             // Get the current ApplyTime value
6061e1e598dSJonathan Doman             sdbusplus::asio::getProperty<std::string>(
6071e1e598dSJonathan Doman                 *crow::connections::systemBus, "xyz.openbmc_project.Settings",
6081e1e598dSJonathan Doman                 "/xyz/openbmc_project/software/apply_time",
6091e1e598dSJonathan Doman                 "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime",
6108d1b46d7Szhanghch05                 [asyncResp](const boost::system::error_code ec,
6111e1e598dSJonathan Doman                             const std::string& applyTime) {
612274dfe62SJayashankar Padath                     if (ec)
613274dfe62SJayashankar Padath                     {
614274dfe62SJayashankar Padath                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
6158d1b46d7Szhanghch05                         messages::internalError(asyncResp->res);
616274dfe62SJayashankar Padath                         return;
617274dfe62SJayashankar Padath                     }
618274dfe62SJayashankar Padath 
619274dfe62SJayashankar Padath                     // Store the ApplyTime Value
6201e1e598dSJonathan Doman                     if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
6211e1e598dSJonathan Doman                                      "RequestedApplyTimes.Immediate")
622274dfe62SJayashankar Padath                     {
6238d1b46d7Szhanghch05                         asyncResp->res
6247e860f15SJohn Edward Broadbent                             .jsonValue["HttpPushUriOptions"]
6257e860f15SJohn Edward Broadbent                                       ["HttpPushUriApplyTime"]["ApplyTime"] =
6267e860f15SJohn Edward Broadbent                             "Immediate";
627274dfe62SJayashankar Padath                     }
6281e1e598dSJonathan Doman                     else if (applyTime ==
6291e1e598dSJonathan Doman                              "xyz.openbmc_project.Software.ApplyTime."
6301e1e598dSJonathan Doman                              "RequestedApplyTimes.OnReset")
631274dfe62SJayashankar Padath                     {
6328d1b46d7Szhanghch05                         asyncResp->res
6337e860f15SJohn Edward Broadbent                             .jsonValue["HttpPushUriOptions"]
6347e860f15SJohn Edward Broadbent                                       ["HttpPushUriApplyTime"]["ApplyTime"] =
6357e860f15SJohn Edward Broadbent                             "OnReset";
636274dfe62SJayashankar Padath                     }
6371e1e598dSJonathan Doman                 });
6387e860f15SJohn Edward Broadbent         });
6397e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
640ed398213SEd Tanous         .privileges(redfish::privileges::patchUpdateService)
6410fda0f12SGeorge Liu         .methods(
64245ca1b86SEd Tanous             boost::beast::http::verb::patch)([&app](const crow::Request& req,
64345ca1b86SEd Tanous                                                     const std::shared_ptr<
64445ca1b86SEd Tanous                                                         bmcweb::AsyncResp>&
64545ca1b86SEd Tanous                                                         asyncResp) {
64645ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
64745ca1b86SEd Tanous             {
64845ca1b86SEd Tanous                 return;
64945ca1b86SEd Tanous             }
650fa1a5a38SJayashankar Padath             BMCWEB_LOG_DEBUG << "doPatch...";
651fa1a5a38SJayashankar Padath 
652274dfe62SJayashankar Padath             std::optional<nlohmann::json> pushUriOptions;
65315ed6780SWilly Tu             if (!json_util::readJsonPatch(req, asyncResp->res,
65415ed6780SWilly Tu                                           "HttpPushUriOptions", pushUriOptions))
655fa1a5a38SJayashankar Padath             {
656fa1a5a38SJayashankar Padath                 return;
657fa1a5a38SJayashankar Padath             }
658fa1a5a38SJayashankar Padath 
659274dfe62SJayashankar Padath             if (pushUriOptions)
660274dfe62SJayashankar Padath             {
661274dfe62SJayashankar Padath                 std::optional<nlohmann::json> pushUriApplyTime;
6628d1b46d7Szhanghch05                 if (!json_util::readJson(*pushUriOptions, asyncResp->res,
6637e860f15SJohn Edward Broadbent                                          "HttpPushUriApplyTime",
6647e860f15SJohn Edward Broadbent                                          pushUriApplyTime))
665274dfe62SJayashankar Padath                 {
666274dfe62SJayashankar Padath                     return;
667274dfe62SJayashankar Padath                 }
668274dfe62SJayashankar Padath 
669274dfe62SJayashankar Padath                 if (pushUriApplyTime)
670274dfe62SJayashankar Padath                 {
671274dfe62SJayashankar Padath                     std::optional<std::string> applyTime;
6720fda0f12SGeorge Liu                     if (!json_util::readJson(*pushUriApplyTime, asyncResp->res,
6730fda0f12SGeorge Liu                                              "ApplyTime", applyTime))
674274dfe62SJayashankar Padath                     {
675274dfe62SJayashankar Padath                         return;
676274dfe62SJayashankar Padath                     }
677274dfe62SJayashankar Padath 
678274dfe62SJayashankar Padath                     if (applyTime)
679fa1a5a38SJayashankar Padath                     {
680fa1a5a38SJayashankar Padath                         std::string applyTimeNewVal;
681fa1a5a38SJayashankar Padath                         if (applyTime == "Immediate")
682fa1a5a38SJayashankar Padath                         {
683274dfe62SJayashankar Padath                             applyTimeNewVal =
6840fda0f12SGeorge Liu                                 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate";
685fa1a5a38SJayashankar Padath                         }
686274dfe62SJayashankar Padath                         else if (applyTime == "OnReset")
687274dfe62SJayashankar Padath                         {
688274dfe62SJayashankar Padath                             applyTimeNewVal =
6890fda0f12SGeorge Liu                                 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
690274dfe62SJayashankar Padath                         }
691fa1a5a38SJayashankar Padath                         else
692fa1a5a38SJayashankar Padath                         {
693274dfe62SJayashankar Padath                             BMCWEB_LOG_INFO
6940fda0f12SGeorge Liu                                 << "ApplyTime value is not in the list of acceptable values";
695274dfe62SJayashankar Padath                             messages::propertyValueNotInList(
696274dfe62SJayashankar Padath                                 asyncResp->res, *applyTime, "ApplyTime");
697274dfe62SJayashankar Padath                             return;
698fa1a5a38SJayashankar Padath                         }
699fa1a5a38SJayashankar Padath 
700fa1a5a38SJayashankar Padath                         // Set the requested image apply time value
701fa1a5a38SJayashankar Padath                         crow::connections::systemBus->async_method_call(
7020fda0f12SGeorge Liu                             [asyncResp](const boost::system::error_code ec) {
703fa1a5a38SJayashankar Padath                                 if (ec)
704fa1a5a38SJayashankar Padath                                 {
7057e860f15SJohn Edward Broadbent                                     BMCWEB_LOG_ERROR
7067e860f15SJohn Edward Broadbent                                         << "D-Bus responses error: " << ec;
707fa1a5a38SJayashankar Padath                                     messages::internalError(asyncResp->res);
708fa1a5a38SJayashankar Padath                                     return;
709fa1a5a38SJayashankar Padath                                 }
710fa1a5a38SJayashankar Padath                                 messages::success(asyncResp->res);
711fa1a5a38SJayashankar Padath                             },
712fa1a5a38SJayashankar Padath                             "xyz.openbmc_project.Settings",
713fa1a5a38SJayashankar Padath                             "/xyz/openbmc_project/software/apply_time",
714fa1a5a38SJayashankar Padath                             "org.freedesktop.DBus.Properties", "Set",
715274dfe62SJayashankar Padath                             "xyz.openbmc_project.Software.ApplyTime",
716274dfe62SJayashankar Padath                             "RequestedApplyTime",
717168e20c1SEd Tanous                             dbus::utility::DbusVariantType{applyTimeNewVal});
718fa1a5a38SJayashankar Padath                     }
719274dfe62SJayashankar Padath                 }
720fa1a5a38SJayashankar Padath             }
7217e860f15SJohn Edward Broadbent         });
722c2051d11SEd Tanous 
723*4dc23f3fSEd Tanous // The "old" behavior of the update service URI causes redfish-service validator
724*4dc23f3fSEd Tanous // failures when the Allow header is supported, given that in the spec,
725*4dc23f3fSEd Tanous // UpdateService does not allow POST.  in openbmc, we unfortunately reused that
726*4dc23f3fSEd Tanous // resource as our HttpPushUri as well.  A number of services, including the
727*4dc23f3fSEd Tanous // openbmc tests, and documentation have hardcoded that erroneous API, instead
728*4dc23f3fSEd Tanous // of relying on HttpPushUri as the spec requires.  This option will exist
729*4dc23f3fSEd Tanous // temporarily to allow the old behavior until Q4 2022, at which time it will be
730*4dc23f3fSEd Tanous // removed.
731*4dc23f3fSEd Tanous #ifdef BMCWEB_ENABLE_REDFISH_UPDATESERVICE_OLD_POST_URL
7327e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
733ed398213SEd Tanous         .privileges(redfish::privileges::postUpdateService)
734*4dc23f3fSEd Tanous         .methods(
735*4dc23f3fSEd Tanous             boost::beast::http::verb::
736*4dc23f3fSEd Tanous                 post)([&app](
737*4dc23f3fSEd Tanous                           const crow::Request& req,
738*4dc23f3fSEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
739*4dc23f3fSEd Tanous             asyncResp->res.addHeader(
740*4dc23f3fSEd Tanous                 boost::beast::http::field::warning,
741*4dc23f3fSEd Tanous                 "299 - \"POST to /redfish/v1/UpdateService is deprecated. Use "
742*4dc23f3fSEd Tanous                 "the value contained within HttpPushUri.\"");
743*4dc23f3fSEd Tanous             handleUpdateServicePost(app, req, asyncResp);
744*4dc23f3fSEd Tanous         });
745*4dc23f3fSEd Tanous #endif
746*4dc23f3fSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/update/")
747*4dc23f3fSEd Tanous         .privileges(redfish::privileges::postUpdateService)
7487e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
749c2051d11SEd Tanous             std::bind_front(handleUpdateServicePost, std::ref(app)));
750729dae72SJennifer Lee }
751729dae72SJennifer Lee 
7527e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventoryCollection(App& app)
7531abe55efSEd Tanous {
7547e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/")
755ed398213SEd Tanous         .privileges(redfish::privileges::getSoftwareInventoryCollection)
7561476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
7571476687dSEd Tanous             [&app](const crow::Request& req,
7581476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
75945ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
76045ca1b86SEd Tanous                 {
76145ca1b86SEd Tanous                     return;
76245ca1b86SEd Tanous                 }
7638d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
7640f74e643SEd Tanous                     "#SoftwareInventoryCollection.SoftwareInventoryCollection";
7658d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] =
7660f74e643SEd Tanous                     "/redfish/v1/UpdateService/FirmwareInventory";
7671476687dSEd Tanous                 asyncResp->res.jsonValue["Name"] =
7681476687dSEd Tanous                     "Software Inventory Collection";
769c711bf86SEd Tanous 
770c711bf86SEd Tanous                 crow::connections::systemBus->async_method_call(
7711476687dSEd Tanous                     [asyncResp](const boost::system::error_code ec,
7721476687dSEd Tanous                                 const dbus::utility::MapperGetSubTreeResponse&
7731476687dSEd Tanous                                     subtree) {
7741abe55efSEd Tanous                         if (ec)
7751abe55efSEd Tanous                         {
776f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
7776c4eb9deSJennifer Lee                             return;
778729dae72SJennifer Lee                         }
7797e860f15SJohn Edward Broadbent                         asyncResp->res.jsonValue["Members"] =
7807e860f15SJohn Edward Broadbent                             nlohmann::json::array();
781c711bf86SEd Tanous                         asyncResp->res.jsonValue["Members@odata.count"] = 0;
7826c4eb9deSJennifer Lee 
7839eb808c1SEd Tanous                         for (const auto& obj : subtree)
7841abe55efSEd Tanous                         {
7852dfd18efSEd Tanous                             sdbusplus::message::object_path path(obj.first);
7862dfd18efSEd Tanous                             std::string swId = path.filename();
7872dfd18efSEd Tanous                             if (swId.empty())
788f4b65ab1SJennifer Lee                             {
789f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
790f4b65ab1SJennifer Lee                                 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
791f4b65ab1SJennifer Lee                                 return;
792f4b65ab1SJennifer Lee                             }
793f4b65ab1SJennifer Lee 
794c711bf86SEd Tanous                             nlohmann::json& members =
795c711bf86SEd Tanous                                 asyncResp->res.jsonValue["Members"];
7961476687dSEd Tanous                             nlohmann::json::object_t member;
7971476687dSEd Tanous                             member["@odata.id"] =
7980fda0f12SGeorge Liu                                 "/redfish/v1/UpdateService/FirmwareInventory/" +
7991476687dSEd Tanous                                 swId;
8001476687dSEd Tanous                             members.push_back(std::move(member));
801e0dd8057SAndrew Geissler                             asyncResp->res.jsonValue["Members@odata.count"] =
802c711bf86SEd Tanous                                 members.size();
8036c4eb9deSJennifer Lee                         }
804c711bf86SEd Tanous                     },
8057e860f15SJohn Edward Broadbent                     // Note that only firmware levels associated with a device
8067e860f15SJohn Edward Broadbent                     // are stored under /xyz/openbmc_project/software therefore
8077e860f15SJohn Edward Broadbent                     // to ensure only real FirmwareInventory items are returned,
8087e860f15SJohn Edward Broadbent                     // this full object path must be used here as input to
8097e860f15SJohn Edward Broadbent                     // mapper
810c711bf86SEd Tanous                     "xyz.openbmc_project.ObjectMapper",
811c711bf86SEd Tanous                     "/xyz/openbmc_project/object_mapper",
8122830a9cfSAndrew Geissler                     "xyz.openbmc_project.ObjectMapper", "GetSubTree",
8132830a9cfSAndrew Geissler                     "/xyz/openbmc_project/software", static_cast<int32_t>(0),
8147e860f15SJohn Edward Broadbent                     std::array<const char*, 1>{
8157e860f15SJohn Edward Broadbent                         "xyz.openbmc_project.Software.Version"});
8167e860f15SJohn Edward Broadbent             });
817729dae72SJennifer Lee }
81887d84729SAndrew Geissler /* Fill related item links (i.e. bmc, bios) in for inventory */
8197e860f15SJohn Edward Broadbent inline static void
8207e860f15SJohn Edward Broadbent     getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
82187d84729SAndrew Geissler                     const std::string& purpose)
82287d84729SAndrew Geissler {
82387d84729SAndrew Geissler     if (purpose == fw_util::bmcPurpose)
82487d84729SAndrew Geissler     {
8255af690f5SAbhishek Patel         nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
8261476687dSEd Tanous         nlohmann::json::object_t item;
8271476687dSEd Tanous         item["@odata.id"] = "/redfish/v1/Managers/bmc";
8281476687dSEd Tanous         relatedItem.push_back(std::move(item));
8297e860f15SJohn Edward Broadbent         aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
83087d84729SAndrew Geissler     }
83187d84729SAndrew Geissler     else if (purpose == fw_util::biosPurpose)
83287d84729SAndrew Geissler     {
8335af690f5SAbhishek Patel         nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
8341476687dSEd Tanous         nlohmann::json::object_t item;
8351476687dSEd Tanous         item["@odata.id"] = "/redfish/v1/Systems/system/Bios";
8361476687dSEd Tanous         relatedItem.push_back(std::move(item));
8371a6e51aeSJiaqing Zhao         aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
83887d84729SAndrew Geissler     }
83987d84729SAndrew Geissler     else
84087d84729SAndrew Geissler     {
84187d84729SAndrew Geissler         BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
84287d84729SAndrew Geissler     }
84387d84729SAndrew Geissler }
84487d84729SAndrew Geissler 
8457e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventory(App& app)
8461abe55efSEd Tanous {
8477e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/")
848ed398213SEd Tanous         .privileges(redfish::privileges::getSoftwareInventory)
8497e860f15SJohn Edward Broadbent         .methods(
85045ca1b86SEd Tanous             boost::beast::http::verb::
85145ca1b86SEd Tanous                 get)([&app](const crow::Request& req,
85245ca1b86SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8537e860f15SJohn Edward Broadbent                             const std::string& param) {
85445ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
85545ca1b86SEd Tanous             {
85645ca1b86SEd Tanous                 return;
85745ca1b86SEd Tanous             }
8583ae837c9SEd Tanous             std::shared_ptr<std::string> swId =
8597e860f15SJohn Edward Broadbent                 std::make_shared<std::string>(param);
860c711bf86SEd Tanous 
8618d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] =
8623ae837c9SEd Tanous                 "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
863c711bf86SEd Tanous 
864c711bf86SEd Tanous             crow::connections::systemBus->async_method_call(
865b9d36b47SEd Tanous                 [asyncResp,
866b9d36b47SEd Tanous                  swId](const boost::system::error_code ec,
867b9d36b47SEd Tanous                        const dbus::utility::MapperGetSubTreeResponse& subtree) {
86855c7b7a2SEd Tanous                     BMCWEB_LOG_DEBUG << "doGet callback...";
8691abe55efSEd Tanous                     if (ec)
8701abe55efSEd Tanous                     {
871f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
8726c4eb9deSJennifer Lee                         return;
8736c4eb9deSJennifer Lee                     }
8746c4eb9deSJennifer Lee 
8756913228dSAndrew Geissler                     // Ensure we find our input swId, otherwise return an error
8766913228dSAndrew Geissler                     bool found = false;
8771abe55efSEd Tanous                     for (const std::pair<
8781abe55efSEd Tanous                              std::string,
8797e860f15SJohn Edward Broadbent                              std::vector<std::pair<
8807e860f15SJohn Edward Broadbent                                  std::string, std::vector<std::string>>>>& obj :
8817e860f15SJohn Edward Broadbent                          subtree)
8821abe55efSEd Tanous                     {
883e05aec50SEd Tanous                         if (!boost::ends_with(obj.first, *swId))
8841abe55efSEd Tanous                         {
885acb7cfb4SJennifer Lee                             continue;
886acb7cfb4SJennifer Lee                         }
887acb7cfb4SJennifer Lee 
88826f6976fSEd Tanous                         if (obj.second.empty())
8891abe55efSEd Tanous                         {
890acb7cfb4SJennifer Lee                             continue;
891acb7cfb4SJennifer Lee                         }
8926c4eb9deSJennifer Lee 
8936913228dSAndrew Geissler                         found = true;
8947e860f15SJohn Edward Broadbent                         fw_util::getFwStatus(asyncResp, swId,
8957e860f15SJohn Edward Broadbent                                              obj.second[0].first);
896e0dd8057SAndrew Geissler 
89755c7b7a2SEd Tanous                         crow::connections::systemBus->async_method_call(
898168e20c1SEd Tanous                             [asyncResp,
899168e20c1SEd Tanous                              swId](const boost::system::error_code errorCode,
900b9d36b47SEd Tanous                                    const dbus::utility::DBusPropertiesMap&
901168e20c1SEd Tanous                                        propertiesList) {
90281ce609eSEd Tanous                                 if (errorCode)
9031abe55efSEd Tanous                                 {
904f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
9056c4eb9deSJennifer Lee                                     return;
9066c4eb9deSJennifer Lee                                 }
907b9d36b47SEd Tanous                                 const std::string* swInvPurpose = nullptr;
908b9d36b47SEd Tanous                                 const std::string* version = nullptr;
909b9d36b47SEd Tanous                                 for (const auto& property : propertiesList)
9101abe55efSEd Tanous                                 {
911b9d36b47SEd Tanous                                     if (property.first == "Purpose")
912b9d36b47SEd Tanous                                     {
913b9d36b47SEd Tanous                                         swInvPurpose = std::get_if<std::string>(
914b9d36b47SEd Tanous                                             &property.second);
9156c4eb9deSJennifer Lee                                     }
916b9d36b47SEd Tanous                                     if (property.first == "Version")
917b9d36b47SEd Tanous                                     {
918b9d36b47SEd Tanous                                         version = std::get_if<std::string>(
919b9d36b47SEd Tanous                                             &property.second);
920b9d36b47SEd Tanous                                     }
921b9d36b47SEd Tanous                                 }
922b9d36b47SEd Tanous 
9233ae837c9SEd Tanous                                 if (swInvPurpose == nullptr)
9241abe55efSEd Tanous                                 {
9250fda0f12SGeorge Liu                                     BMCWEB_LOG_DEBUG
926b9d36b47SEd Tanous                                         << "Can't find property \"Purpose\"!";
927b9d36b47SEd Tanous                                     messages::internalError(asyncResp->res);
928acb7cfb4SJennifer Lee                                     return;
929acb7cfb4SJennifer Lee                                 }
930c711bf86SEd Tanous 
9313ae837c9SEd Tanous                                 BMCWEB_LOG_DEBUG << "swInvPurpose = "
9323ae837c9SEd Tanous                                                  << *swInvPurpose;
933f4b65ab1SJennifer Lee 
934f4b65ab1SJennifer Lee                                 if (version == nullptr)
9351abe55efSEd Tanous                                 {
9361abe55efSEd Tanous                                     BMCWEB_LOG_DEBUG
9371abe55efSEd Tanous                                         << "Can't find property \"Version\"!";
938f12894f8SJason M. Bills 
939b9d36b47SEd Tanous                                     messages::internalError(asyncResp->res);
940b9d36b47SEd Tanous 
9416c4eb9deSJennifer Lee                                     return;
9426c4eb9deSJennifer Lee                                 }
943c711bf86SEd Tanous                                 asyncResp->res.jsonValue["Version"] = *version;
9443ae837c9SEd Tanous                                 asyncResp->res.jsonValue["Id"] = *swId;
94554daabe7SAndrew Geissler 
94654daabe7SAndrew Geissler                                 // swInvPurpose is of format:
94754daabe7SAndrew Geissler                                 // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
948e2e96770SJames Feist                                 // Translate this to "ABC image"
949f23b7296SEd Tanous                                 size_t endDesc = swInvPurpose->rfind('.');
95054daabe7SAndrew Geissler                                 if (endDesc == std::string::npos)
95154daabe7SAndrew Geissler                                 {
95254daabe7SAndrew Geissler                                     messages::internalError(asyncResp->res);
95354daabe7SAndrew Geissler                                     return;
95454daabe7SAndrew Geissler                                 }
95554daabe7SAndrew Geissler                                 endDesc++;
95654daabe7SAndrew Geissler                                 if (endDesc >= swInvPurpose->size())
95754daabe7SAndrew Geissler                                 {
95854daabe7SAndrew Geissler                                     messages::internalError(asyncResp->res);
95954daabe7SAndrew Geissler                                     return;
96054daabe7SAndrew Geissler                                 }
96154daabe7SAndrew Geissler 
96254daabe7SAndrew Geissler                                 std::string formatDesc =
96354daabe7SAndrew Geissler                                     swInvPurpose->substr(endDesc);
96454daabe7SAndrew Geissler                                 asyncResp->res.jsonValue["Description"] =
965e2e96770SJames Feist                                     formatDesc + " image";
96687d84729SAndrew Geissler                                 getRelatedItems(asyncResp, *swInvPurpose);
9676c4eb9deSJennifer Lee                             },
968c711bf86SEd Tanous                             obj.second[0].first, obj.first,
969c711bf86SEd Tanous                             "org.freedesktop.DBus.Properties", "GetAll",
970c711bf86SEd Tanous                             "xyz.openbmc_project.Software.Version");
9716c4eb9deSJennifer Lee                     }
9726913228dSAndrew Geissler                     if (!found)
9736913228dSAndrew Geissler                     {
9748cc8edecSEd Tanous                         BMCWEB_LOG_ERROR << "Input swID " << *swId
9758cc8edecSEd Tanous                                          << " not found!";
9766913228dSAndrew Geissler                         messages::resourceMissingAtURI(
9776913228dSAndrew Geissler                             asyncResp->res,
978ace85d60SEd Tanous                             crow::utility::urlFromPieces(
979ace85d60SEd Tanous                                 "redfish", "v1", "UpdateService",
980ace85d60SEd Tanous                                 "FirmwareInventory", *swId));
9816913228dSAndrew Geissler                         return;
9826913228dSAndrew Geissler                     }
9834e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["@odata.type"] =
9844e68c45bSAyushi Smriti                         "#SoftwareInventory.v1_1_0.SoftwareInventory";
9854e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["Name"] = "Software Inventory";
9864e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
9873f8a743aSAppaRao Puli 
9883f8a743aSAppaRao Puli                     asyncResp->res.jsonValue["Updateable"] = false;
9893f8a743aSAppaRao Puli                     fw_util::getFwUpdateableStatus(asyncResp, swId);
990c711bf86SEd Tanous                 },
991c711bf86SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
992c711bf86SEd Tanous                 "/xyz/openbmc_project/object_mapper",
993271584abSEd Tanous                 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
994271584abSEd Tanous                 static_cast<int32_t>(0),
9957e860f15SJohn Edward Broadbent                 std::array<const char*, 1>{
9967e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Software.Version"});
9977e860f15SJohn Edward Broadbent         });
9986c4eb9deSJennifer Lee }
999729dae72SJennifer Lee 
1000729dae72SJennifer Lee } // namespace redfish
1001