xref: /openbmc/bmcweb/features/redfish/lib/update_service.hpp (revision c2051d11f5b28ea7c8ea302c54243d58de1cebc1)
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 
540*c2051d11SEd Tanous inline void
541*c2051d11SEd Tanous     handleUpdateServicePost(App& app, const crow::Request& req,
542*c2051d11SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
543*c2051d11SEd Tanous {
544*c2051d11SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
545*c2051d11SEd Tanous     {
546*c2051d11SEd Tanous         return;
547*c2051d11SEd Tanous     }
548*c2051d11SEd Tanous     BMCWEB_LOG_DEBUG << "doPost...";
549*c2051d11SEd Tanous 
550*c2051d11SEd Tanous     // Setup callback for when new software detected
551*c2051d11SEd Tanous     monitorForSoftwareAvailable(asyncResp, req, "/redfish/v1/UpdateService");
552*c2051d11SEd Tanous 
553*c2051d11SEd Tanous     std::string filepath(
554*c2051d11SEd Tanous         "/tmp/images/" +
555*c2051d11SEd Tanous         boost::uuids::to_string(boost::uuids::random_generator()()));
556*c2051d11SEd Tanous     BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
557*c2051d11SEd Tanous     std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
558*c2051d11SEd Tanous                                     std::ofstream::trunc);
559*c2051d11SEd Tanous     out << req.body;
560*c2051d11SEd Tanous     out.close();
561*c2051d11SEd Tanous     BMCWEB_LOG_DEBUG << "file upload complete!!";
562*c2051d11SEd Tanous }
563*c2051d11SEd 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";
5837e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["HttpPushUri"] =
5847e860f15SJohn Edward Broadbent                 "/redfish/v1/UpdateService";
5850f74e643SEd Tanous             // UpdateService cannot be disabled
5868d1b46d7Szhanghch05             asyncResp->res.jsonValue["ServiceEnabled"] = true;
5871476687dSEd Tanous             asyncResp->res.jsonValue["FirmwareInventory"]["@odata.id"] =
5881476687dSEd Tanous                 "/redfish/v1/UpdateService/FirmwareInventory";
589d61e5194STejas Patil             // Get the MaxImageSizeBytes
590d61e5194STejas Patil             asyncResp->res.jsonValue["MaxImageSizeBytes"] =
591d61e5194STejas Patil                 bmcwebHttpReqBodyLimitMb * 1024 * 1024;
592d61e5194STejas Patil 
5930554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
5940554c984SAndrew Geissler             // Update Actions object.
5950554c984SAndrew Geissler             nlohmann::json& updateSvcSimpleUpdate =
5967e860f15SJohn Edward Broadbent                 asyncResp->res
5977e860f15SJohn Edward Broadbent                     .jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
5980554c984SAndrew Geissler             updateSvcSimpleUpdate["target"] =
5990554c984SAndrew Geissler                 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
6007e860f15SJohn Edward Broadbent             updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] =
6017e860f15SJohn Edward Broadbent                 {"TFTP"};
6020554c984SAndrew Geissler #endif
603274dfe62SJayashankar Padath             // Get the current ApplyTime value
6041e1e598dSJonathan Doman             sdbusplus::asio::getProperty<std::string>(
6051e1e598dSJonathan Doman                 *crow::connections::systemBus, "xyz.openbmc_project.Settings",
6061e1e598dSJonathan Doman                 "/xyz/openbmc_project/software/apply_time",
6071e1e598dSJonathan Doman                 "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime",
6088d1b46d7Szhanghch05                 [asyncResp](const boost::system::error_code ec,
6091e1e598dSJonathan Doman                             const std::string& applyTime) {
610274dfe62SJayashankar Padath                     if (ec)
611274dfe62SJayashankar Padath                     {
612274dfe62SJayashankar Padath                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
6138d1b46d7Szhanghch05                         messages::internalError(asyncResp->res);
614274dfe62SJayashankar Padath                         return;
615274dfe62SJayashankar Padath                     }
616274dfe62SJayashankar Padath 
617274dfe62SJayashankar Padath                     // Store the ApplyTime Value
6181e1e598dSJonathan Doman                     if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
6191e1e598dSJonathan Doman                                      "RequestedApplyTimes.Immediate")
620274dfe62SJayashankar Padath                     {
6218d1b46d7Szhanghch05                         asyncResp->res
6227e860f15SJohn Edward Broadbent                             .jsonValue["HttpPushUriOptions"]
6237e860f15SJohn Edward Broadbent                                       ["HttpPushUriApplyTime"]["ApplyTime"] =
6247e860f15SJohn Edward Broadbent                             "Immediate";
625274dfe62SJayashankar Padath                     }
6261e1e598dSJonathan Doman                     else if (applyTime ==
6271e1e598dSJonathan Doman                              "xyz.openbmc_project.Software.ApplyTime."
6281e1e598dSJonathan Doman                              "RequestedApplyTimes.OnReset")
629274dfe62SJayashankar Padath                     {
6308d1b46d7Szhanghch05                         asyncResp->res
6317e860f15SJohn Edward Broadbent                             .jsonValue["HttpPushUriOptions"]
6327e860f15SJohn Edward Broadbent                                       ["HttpPushUriApplyTime"]["ApplyTime"] =
6337e860f15SJohn Edward Broadbent                             "OnReset";
634274dfe62SJayashankar Padath                     }
6351e1e598dSJonathan Doman                 });
6367e860f15SJohn Edward Broadbent         });
6377e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
638ed398213SEd Tanous         .privileges(redfish::privileges::patchUpdateService)
6390fda0f12SGeorge Liu         .methods(
64045ca1b86SEd Tanous             boost::beast::http::verb::patch)([&app](const crow::Request& req,
64145ca1b86SEd Tanous                                                     const std::shared_ptr<
64245ca1b86SEd Tanous                                                         bmcweb::AsyncResp>&
64345ca1b86SEd Tanous                                                         asyncResp) {
64445ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
64545ca1b86SEd Tanous             {
64645ca1b86SEd Tanous                 return;
64745ca1b86SEd Tanous             }
648fa1a5a38SJayashankar Padath             BMCWEB_LOG_DEBUG << "doPatch...";
649fa1a5a38SJayashankar Padath 
650274dfe62SJayashankar Padath             std::optional<nlohmann::json> pushUriOptions;
65115ed6780SWilly Tu             if (!json_util::readJsonPatch(req, asyncResp->res,
65215ed6780SWilly Tu                                           "HttpPushUriOptions", pushUriOptions))
653fa1a5a38SJayashankar Padath             {
654fa1a5a38SJayashankar Padath                 return;
655fa1a5a38SJayashankar Padath             }
656fa1a5a38SJayashankar Padath 
657274dfe62SJayashankar Padath             if (pushUriOptions)
658274dfe62SJayashankar Padath             {
659274dfe62SJayashankar Padath                 std::optional<nlohmann::json> pushUriApplyTime;
6608d1b46d7Szhanghch05                 if (!json_util::readJson(*pushUriOptions, asyncResp->res,
6617e860f15SJohn Edward Broadbent                                          "HttpPushUriApplyTime",
6627e860f15SJohn Edward Broadbent                                          pushUriApplyTime))
663274dfe62SJayashankar Padath                 {
664274dfe62SJayashankar Padath                     return;
665274dfe62SJayashankar Padath                 }
666274dfe62SJayashankar Padath 
667274dfe62SJayashankar Padath                 if (pushUriApplyTime)
668274dfe62SJayashankar Padath                 {
669274dfe62SJayashankar Padath                     std::optional<std::string> applyTime;
6700fda0f12SGeorge Liu                     if (!json_util::readJson(*pushUriApplyTime, asyncResp->res,
6710fda0f12SGeorge Liu                                              "ApplyTime", applyTime))
672274dfe62SJayashankar Padath                     {
673274dfe62SJayashankar Padath                         return;
674274dfe62SJayashankar Padath                     }
675274dfe62SJayashankar Padath 
676274dfe62SJayashankar Padath                     if (applyTime)
677fa1a5a38SJayashankar Padath                     {
678fa1a5a38SJayashankar Padath                         std::string applyTimeNewVal;
679fa1a5a38SJayashankar Padath                         if (applyTime == "Immediate")
680fa1a5a38SJayashankar Padath                         {
681274dfe62SJayashankar Padath                             applyTimeNewVal =
6820fda0f12SGeorge Liu                                 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate";
683fa1a5a38SJayashankar Padath                         }
684274dfe62SJayashankar Padath                         else if (applyTime == "OnReset")
685274dfe62SJayashankar Padath                         {
686274dfe62SJayashankar Padath                             applyTimeNewVal =
6870fda0f12SGeorge Liu                                 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
688274dfe62SJayashankar Padath                         }
689fa1a5a38SJayashankar Padath                         else
690fa1a5a38SJayashankar Padath                         {
691274dfe62SJayashankar Padath                             BMCWEB_LOG_INFO
6920fda0f12SGeorge Liu                                 << "ApplyTime value is not in the list of acceptable values";
693274dfe62SJayashankar Padath                             messages::propertyValueNotInList(
694274dfe62SJayashankar Padath                                 asyncResp->res, *applyTime, "ApplyTime");
695274dfe62SJayashankar Padath                             return;
696fa1a5a38SJayashankar Padath                         }
697fa1a5a38SJayashankar Padath 
698fa1a5a38SJayashankar Padath                         // Set the requested image apply time value
699fa1a5a38SJayashankar Padath                         crow::connections::systemBus->async_method_call(
7000fda0f12SGeorge Liu                             [asyncResp](const boost::system::error_code ec) {
701fa1a5a38SJayashankar Padath                                 if (ec)
702fa1a5a38SJayashankar Padath                                 {
7037e860f15SJohn Edward Broadbent                                     BMCWEB_LOG_ERROR
7047e860f15SJohn Edward Broadbent                                         << "D-Bus responses error: " << ec;
705fa1a5a38SJayashankar Padath                                     messages::internalError(asyncResp->res);
706fa1a5a38SJayashankar Padath                                     return;
707fa1a5a38SJayashankar Padath                                 }
708fa1a5a38SJayashankar Padath                                 messages::success(asyncResp->res);
709fa1a5a38SJayashankar Padath                             },
710fa1a5a38SJayashankar Padath                             "xyz.openbmc_project.Settings",
711fa1a5a38SJayashankar Padath                             "/xyz/openbmc_project/software/apply_time",
712fa1a5a38SJayashankar Padath                             "org.freedesktop.DBus.Properties", "Set",
713274dfe62SJayashankar Padath                             "xyz.openbmc_project.Software.ApplyTime",
714274dfe62SJayashankar Padath                             "RequestedApplyTime",
715168e20c1SEd Tanous                             dbus::utility::DbusVariantType{applyTimeNewVal});
716fa1a5a38SJayashankar Padath                     }
717274dfe62SJayashankar Padath                 }
718fa1a5a38SJayashankar Padath             }
7197e860f15SJohn Edward Broadbent         });
720*c2051d11SEd Tanous 
7217e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
722ed398213SEd Tanous         .privileges(redfish::privileges::postUpdateService)
7237e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
724*c2051d11SEd Tanous             std::bind_front(handleUpdateServicePost, std::ref(app)));
725729dae72SJennifer Lee }
726729dae72SJennifer Lee 
7277e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventoryCollection(App& app)
7281abe55efSEd Tanous {
7297e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/")
730ed398213SEd Tanous         .privileges(redfish::privileges::getSoftwareInventoryCollection)
7311476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
7321476687dSEd Tanous             [&app](const crow::Request& req,
7331476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
73445ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
73545ca1b86SEd Tanous                 {
73645ca1b86SEd Tanous                     return;
73745ca1b86SEd Tanous                 }
7388d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
7390f74e643SEd Tanous                     "#SoftwareInventoryCollection.SoftwareInventoryCollection";
7408d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] =
7410f74e643SEd Tanous                     "/redfish/v1/UpdateService/FirmwareInventory";
7421476687dSEd Tanous                 asyncResp->res.jsonValue["Name"] =
7431476687dSEd Tanous                     "Software Inventory Collection";
744c711bf86SEd Tanous 
745c711bf86SEd Tanous                 crow::connections::systemBus->async_method_call(
7461476687dSEd Tanous                     [asyncResp](const boost::system::error_code ec,
7471476687dSEd Tanous                                 const dbus::utility::MapperGetSubTreeResponse&
7481476687dSEd Tanous                                     subtree) {
7491abe55efSEd Tanous                         if (ec)
7501abe55efSEd Tanous                         {
751f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
7526c4eb9deSJennifer Lee                             return;
753729dae72SJennifer Lee                         }
7547e860f15SJohn Edward Broadbent                         asyncResp->res.jsonValue["Members"] =
7557e860f15SJohn Edward Broadbent                             nlohmann::json::array();
756c711bf86SEd Tanous                         asyncResp->res.jsonValue["Members@odata.count"] = 0;
7576c4eb9deSJennifer Lee 
7589eb808c1SEd Tanous                         for (const auto& obj : subtree)
7591abe55efSEd Tanous                         {
7602dfd18efSEd Tanous                             sdbusplus::message::object_path path(obj.first);
7612dfd18efSEd Tanous                             std::string swId = path.filename();
7622dfd18efSEd Tanous                             if (swId.empty())
763f4b65ab1SJennifer Lee                             {
764f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
765f4b65ab1SJennifer Lee                                 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
766f4b65ab1SJennifer Lee                                 return;
767f4b65ab1SJennifer Lee                             }
768f4b65ab1SJennifer Lee 
769c711bf86SEd Tanous                             nlohmann::json& members =
770c711bf86SEd Tanous                                 asyncResp->res.jsonValue["Members"];
7711476687dSEd Tanous                             nlohmann::json::object_t member;
7721476687dSEd Tanous                             member["@odata.id"] =
7730fda0f12SGeorge Liu                                 "/redfish/v1/UpdateService/FirmwareInventory/" +
7741476687dSEd Tanous                                 swId;
7751476687dSEd Tanous                             members.push_back(std::move(member));
776e0dd8057SAndrew Geissler                             asyncResp->res.jsonValue["Members@odata.count"] =
777c711bf86SEd Tanous                                 members.size();
7786c4eb9deSJennifer Lee                         }
779c711bf86SEd Tanous                     },
7807e860f15SJohn Edward Broadbent                     // Note that only firmware levels associated with a device
7817e860f15SJohn Edward Broadbent                     // are stored under /xyz/openbmc_project/software therefore
7827e860f15SJohn Edward Broadbent                     // to ensure only real FirmwareInventory items are returned,
7837e860f15SJohn Edward Broadbent                     // this full object path must be used here as input to
7847e860f15SJohn Edward Broadbent                     // mapper
785c711bf86SEd Tanous                     "xyz.openbmc_project.ObjectMapper",
786c711bf86SEd Tanous                     "/xyz/openbmc_project/object_mapper",
7872830a9cfSAndrew Geissler                     "xyz.openbmc_project.ObjectMapper", "GetSubTree",
7882830a9cfSAndrew Geissler                     "/xyz/openbmc_project/software", static_cast<int32_t>(0),
7897e860f15SJohn Edward Broadbent                     std::array<const char*, 1>{
7907e860f15SJohn Edward Broadbent                         "xyz.openbmc_project.Software.Version"});
7917e860f15SJohn Edward Broadbent             });
792729dae72SJennifer Lee }
79387d84729SAndrew Geissler /* Fill related item links (i.e. bmc, bios) in for inventory */
7947e860f15SJohn Edward Broadbent inline static void
7957e860f15SJohn Edward Broadbent     getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
79687d84729SAndrew Geissler                     const std::string& purpose)
79787d84729SAndrew Geissler {
79887d84729SAndrew Geissler     if (purpose == fw_util::bmcPurpose)
79987d84729SAndrew Geissler     {
8005af690f5SAbhishek Patel         nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
8011476687dSEd Tanous         nlohmann::json::object_t item;
8021476687dSEd Tanous         item["@odata.id"] = "/redfish/v1/Managers/bmc";
8031476687dSEd Tanous         relatedItem.push_back(std::move(item));
8047e860f15SJohn Edward Broadbent         aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
80587d84729SAndrew Geissler     }
80687d84729SAndrew Geissler     else if (purpose == fw_util::biosPurpose)
80787d84729SAndrew Geissler     {
8085af690f5SAbhishek Patel         nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
8091476687dSEd Tanous         nlohmann::json::object_t item;
8101476687dSEd Tanous         item["@odata.id"] = "/redfish/v1/Systems/system/Bios";
8111476687dSEd Tanous         relatedItem.push_back(std::move(item));
8121a6e51aeSJiaqing Zhao         aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
81387d84729SAndrew Geissler     }
81487d84729SAndrew Geissler     else
81587d84729SAndrew Geissler     {
81687d84729SAndrew Geissler         BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
81787d84729SAndrew Geissler     }
81887d84729SAndrew Geissler }
81987d84729SAndrew Geissler 
8207e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventory(App& app)
8211abe55efSEd Tanous {
8227e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/")
823ed398213SEd Tanous         .privileges(redfish::privileges::getSoftwareInventory)
8247e860f15SJohn Edward Broadbent         .methods(
82545ca1b86SEd Tanous             boost::beast::http::verb::
82645ca1b86SEd Tanous                 get)([&app](const crow::Request& req,
82745ca1b86SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8287e860f15SJohn Edward Broadbent                             const std::string& param) {
82945ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
83045ca1b86SEd Tanous             {
83145ca1b86SEd Tanous                 return;
83245ca1b86SEd Tanous             }
8333ae837c9SEd Tanous             std::shared_ptr<std::string> swId =
8347e860f15SJohn Edward Broadbent                 std::make_shared<std::string>(param);
835c711bf86SEd Tanous 
8368d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] =
8373ae837c9SEd Tanous                 "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
838c711bf86SEd Tanous 
839c711bf86SEd Tanous             crow::connections::systemBus->async_method_call(
840b9d36b47SEd Tanous                 [asyncResp,
841b9d36b47SEd Tanous                  swId](const boost::system::error_code ec,
842b9d36b47SEd Tanous                        const dbus::utility::MapperGetSubTreeResponse& subtree) {
84355c7b7a2SEd Tanous                     BMCWEB_LOG_DEBUG << "doGet callback...";
8441abe55efSEd Tanous                     if (ec)
8451abe55efSEd Tanous                     {
846f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
8476c4eb9deSJennifer Lee                         return;
8486c4eb9deSJennifer Lee                     }
8496c4eb9deSJennifer Lee 
8506913228dSAndrew Geissler                     // Ensure we find our input swId, otherwise return an error
8516913228dSAndrew Geissler                     bool found = false;
8521abe55efSEd Tanous                     for (const std::pair<
8531abe55efSEd Tanous                              std::string,
8547e860f15SJohn Edward Broadbent                              std::vector<std::pair<
8557e860f15SJohn Edward Broadbent                                  std::string, std::vector<std::string>>>>& obj :
8567e860f15SJohn Edward Broadbent                          subtree)
8571abe55efSEd Tanous                     {
858e05aec50SEd Tanous                         if (!boost::ends_with(obj.first, *swId))
8591abe55efSEd Tanous                         {
860acb7cfb4SJennifer Lee                             continue;
861acb7cfb4SJennifer Lee                         }
862acb7cfb4SJennifer Lee 
86326f6976fSEd Tanous                         if (obj.second.empty())
8641abe55efSEd Tanous                         {
865acb7cfb4SJennifer Lee                             continue;
866acb7cfb4SJennifer Lee                         }
8676c4eb9deSJennifer Lee 
8686913228dSAndrew Geissler                         found = true;
8697e860f15SJohn Edward Broadbent                         fw_util::getFwStatus(asyncResp, swId,
8707e860f15SJohn Edward Broadbent                                              obj.second[0].first);
871e0dd8057SAndrew Geissler 
87255c7b7a2SEd Tanous                         crow::connections::systemBus->async_method_call(
873168e20c1SEd Tanous                             [asyncResp,
874168e20c1SEd Tanous                              swId](const boost::system::error_code errorCode,
875b9d36b47SEd Tanous                                    const dbus::utility::DBusPropertiesMap&
876168e20c1SEd Tanous                                        propertiesList) {
87781ce609eSEd Tanous                                 if (errorCode)
8781abe55efSEd Tanous                                 {
879f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
8806c4eb9deSJennifer Lee                                     return;
8816c4eb9deSJennifer Lee                                 }
882b9d36b47SEd Tanous                                 const std::string* swInvPurpose = nullptr;
883b9d36b47SEd Tanous                                 const std::string* version = nullptr;
884b9d36b47SEd Tanous                                 for (const auto& property : propertiesList)
8851abe55efSEd Tanous                                 {
886b9d36b47SEd Tanous                                     if (property.first == "Purpose")
887b9d36b47SEd Tanous                                     {
888b9d36b47SEd Tanous                                         swInvPurpose = std::get_if<std::string>(
889b9d36b47SEd Tanous                                             &property.second);
8906c4eb9deSJennifer Lee                                     }
891b9d36b47SEd Tanous                                     if (property.first == "Version")
892b9d36b47SEd Tanous                                     {
893b9d36b47SEd Tanous                                         version = std::get_if<std::string>(
894b9d36b47SEd Tanous                                             &property.second);
895b9d36b47SEd Tanous                                     }
896b9d36b47SEd Tanous                                 }
897b9d36b47SEd Tanous 
8983ae837c9SEd Tanous                                 if (swInvPurpose == nullptr)
8991abe55efSEd Tanous                                 {
9000fda0f12SGeorge Liu                                     BMCWEB_LOG_DEBUG
901b9d36b47SEd Tanous                                         << "Can't find property \"Purpose\"!";
902b9d36b47SEd Tanous                                     messages::internalError(asyncResp->res);
903acb7cfb4SJennifer Lee                                     return;
904acb7cfb4SJennifer Lee                                 }
905c711bf86SEd Tanous 
9063ae837c9SEd Tanous                                 BMCWEB_LOG_DEBUG << "swInvPurpose = "
9073ae837c9SEd Tanous                                                  << *swInvPurpose;
908f4b65ab1SJennifer Lee 
909f4b65ab1SJennifer Lee                                 if (version == nullptr)
9101abe55efSEd Tanous                                 {
9111abe55efSEd Tanous                                     BMCWEB_LOG_DEBUG
9121abe55efSEd Tanous                                         << "Can't find property \"Version\"!";
913f12894f8SJason M. Bills 
914b9d36b47SEd Tanous                                     messages::internalError(asyncResp->res);
915b9d36b47SEd Tanous 
9166c4eb9deSJennifer Lee                                     return;
9176c4eb9deSJennifer Lee                                 }
918c711bf86SEd Tanous                                 asyncResp->res.jsonValue["Version"] = *version;
9193ae837c9SEd Tanous                                 asyncResp->res.jsonValue["Id"] = *swId;
92054daabe7SAndrew Geissler 
92154daabe7SAndrew Geissler                                 // swInvPurpose is of format:
92254daabe7SAndrew Geissler                                 // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
923e2e96770SJames Feist                                 // Translate this to "ABC image"
924f23b7296SEd Tanous                                 size_t endDesc = swInvPurpose->rfind('.');
92554daabe7SAndrew Geissler                                 if (endDesc == std::string::npos)
92654daabe7SAndrew Geissler                                 {
92754daabe7SAndrew Geissler                                     messages::internalError(asyncResp->res);
92854daabe7SAndrew Geissler                                     return;
92954daabe7SAndrew Geissler                                 }
93054daabe7SAndrew Geissler                                 endDesc++;
93154daabe7SAndrew Geissler                                 if (endDesc >= swInvPurpose->size())
93254daabe7SAndrew Geissler                                 {
93354daabe7SAndrew Geissler                                     messages::internalError(asyncResp->res);
93454daabe7SAndrew Geissler                                     return;
93554daabe7SAndrew Geissler                                 }
93654daabe7SAndrew Geissler 
93754daabe7SAndrew Geissler                                 std::string formatDesc =
93854daabe7SAndrew Geissler                                     swInvPurpose->substr(endDesc);
93954daabe7SAndrew Geissler                                 asyncResp->res.jsonValue["Description"] =
940e2e96770SJames Feist                                     formatDesc + " image";
94187d84729SAndrew Geissler                                 getRelatedItems(asyncResp, *swInvPurpose);
9426c4eb9deSJennifer Lee                             },
943c711bf86SEd Tanous                             obj.second[0].first, obj.first,
944c711bf86SEd Tanous                             "org.freedesktop.DBus.Properties", "GetAll",
945c711bf86SEd Tanous                             "xyz.openbmc_project.Software.Version");
9466c4eb9deSJennifer Lee                     }
9476913228dSAndrew Geissler                     if (!found)
9486913228dSAndrew Geissler                     {
9498cc8edecSEd Tanous                         BMCWEB_LOG_ERROR << "Input swID " << *swId
9508cc8edecSEd Tanous                                          << " not found!";
9516913228dSAndrew Geissler                         messages::resourceMissingAtURI(
9526913228dSAndrew Geissler                             asyncResp->res,
953ace85d60SEd Tanous                             crow::utility::urlFromPieces(
954ace85d60SEd Tanous                                 "redfish", "v1", "UpdateService",
955ace85d60SEd Tanous                                 "FirmwareInventory", *swId));
9566913228dSAndrew Geissler                         return;
9576913228dSAndrew Geissler                     }
9584e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["@odata.type"] =
9594e68c45bSAyushi Smriti                         "#SoftwareInventory.v1_1_0.SoftwareInventory";
9604e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["Name"] = "Software Inventory";
9614e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
9623f8a743aSAppaRao Puli 
9633f8a743aSAppaRao Puli                     asyncResp->res.jsonValue["Updateable"] = false;
9643f8a743aSAppaRao Puli                     fw_util::getFwUpdateableStatus(asyncResp, swId);
965c711bf86SEd Tanous                 },
966c711bf86SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
967c711bf86SEd Tanous                 "/xyz/openbmc_project/object_mapper",
968271584abSEd Tanous                 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
969271584abSEd Tanous                 static_cast<int32_t>(0),
9707e860f15SJohn Edward Broadbent                 std::array<const char*, 1>{
9717e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Software.Version"});
9727e860f15SJohn Edward Broadbent         });
9736c4eb9deSJennifer Lee }
974729dae72SJennifer Lee 
975729dae72SJennifer Lee } // namespace redfish
976