xref: /openbmc/bmcweb/features/redfish/lib/update_service.hpp (revision 45ca1b868e47978a4d2e8ebb680cb384e804c97e)
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>
22*45ca1b86SEd 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::
420*45ca1b86SEd Tanous                 post)([&app](
421*45ca1b86SEd Tanous                           const crow::Request& req,
4227e860f15SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
423*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
424*45ca1b86SEd Tanous             {
425*45ca1b86SEd Tanous                 return;
426*45ca1b86SEd Tanous             }
427*45ca1b86SEd 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 
5407e860f15SJohn Edward Broadbent inline void requestRoutesUpdateService(App& app)
5411abe55efSEd Tanous {
5427e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
543ed398213SEd Tanous         .privileges(redfish::privileges::getUpdateService)
544*45ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
545*45ca1b86SEd Tanous                                                        const std::shared_ptr<
546*45ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
547*45ca1b86SEd Tanous                                                            asyncResp) {
548*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
549*45ca1b86SEd Tanous             {
550*45ca1b86SEd Tanous                 return;
551*45ca1b86SEd Tanous             }
5528d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.type"] =
5530588a3b9SChicago Duan                 "#UpdateService.v1_5_0.UpdateService";
5548d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
5558d1b46d7Szhanghch05             asyncResp->res.jsonValue["Id"] = "UpdateService";
5567e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
5577e860f15SJohn Edward Broadbent                 "Service for Software Update";
5588d1b46d7Szhanghch05             asyncResp->res.jsonValue["Name"] = "Update Service";
5597e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["HttpPushUri"] =
5607e860f15SJohn Edward Broadbent                 "/redfish/v1/UpdateService";
5610f74e643SEd Tanous             // UpdateService cannot be disabled
5628d1b46d7Szhanghch05             asyncResp->res.jsonValue["ServiceEnabled"] = true;
5638d1b46d7Szhanghch05             asyncResp->res.jsonValue["FirmwareInventory"] = {
5640f74e643SEd Tanous                 {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}};
565d61e5194STejas Patil             // Get the MaxImageSizeBytes
566d61e5194STejas Patil             asyncResp->res.jsonValue["MaxImageSizeBytes"] =
567d61e5194STejas Patil                 bmcwebHttpReqBodyLimitMb * 1024 * 1024;
568d61e5194STejas Patil 
5690554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
5700554c984SAndrew Geissler             // Update Actions object.
5710554c984SAndrew Geissler             nlohmann::json& updateSvcSimpleUpdate =
5727e860f15SJohn Edward Broadbent                 asyncResp->res
5737e860f15SJohn Edward Broadbent                     .jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
5740554c984SAndrew Geissler             updateSvcSimpleUpdate["target"] =
5750554c984SAndrew Geissler                 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
5767e860f15SJohn Edward Broadbent             updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] =
5777e860f15SJohn Edward Broadbent                 {"TFTP"};
5780554c984SAndrew Geissler #endif
579274dfe62SJayashankar Padath             // Get the current ApplyTime value
5801e1e598dSJonathan Doman             sdbusplus::asio::getProperty<std::string>(
5811e1e598dSJonathan Doman                 *crow::connections::systemBus, "xyz.openbmc_project.Settings",
5821e1e598dSJonathan Doman                 "/xyz/openbmc_project/software/apply_time",
5831e1e598dSJonathan Doman                 "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime",
5848d1b46d7Szhanghch05                 [asyncResp](const boost::system::error_code ec,
5851e1e598dSJonathan Doman                             const std::string& applyTime) {
586274dfe62SJayashankar Padath                     if (ec)
587274dfe62SJayashankar Padath                     {
588274dfe62SJayashankar Padath                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
5898d1b46d7Szhanghch05                         messages::internalError(asyncResp->res);
590274dfe62SJayashankar Padath                         return;
591274dfe62SJayashankar Padath                     }
592274dfe62SJayashankar Padath 
593274dfe62SJayashankar Padath                     // Store the ApplyTime Value
5941e1e598dSJonathan Doman                     if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
5951e1e598dSJonathan Doman                                      "RequestedApplyTimes.Immediate")
596274dfe62SJayashankar Padath                     {
5978d1b46d7Szhanghch05                         asyncResp->res
5987e860f15SJohn Edward Broadbent                             .jsonValue["HttpPushUriOptions"]
5997e860f15SJohn Edward Broadbent                                       ["HttpPushUriApplyTime"]["ApplyTime"] =
6007e860f15SJohn Edward Broadbent                             "Immediate";
601274dfe62SJayashankar Padath                     }
6021e1e598dSJonathan Doman                     else if (applyTime ==
6031e1e598dSJonathan Doman                              "xyz.openbmc_project.Software.ApplyTime."
6041e1e598dSJonathan Doman                              "RequestedApplyTimes.OnReset")
605274dfe62SJayashankar Padath                     {
6068d1b46d7Szhanghch05                         asyncResp->res
6077e860f15SJohn Edward Broadbent                             .jsonValue["HttpPushUriOptions"]
6087e860f15SJohn Edward Broadbent                                       ["HttpPushUriApplyTime"]["ApplyTime"] =
6097e860f15SJohn Edward Broadbent                             "OnReset";
610274dfe62SJayashankar Padath                     }
6111e1e598dSJonathan Doman                 });
6127e860f15SJohn Edward Broadbent         });
6137e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
614ed398213SEd Tanous         .privileges(redfish::privileges::patchUpdateService)
6150fda0f12SGeorge Liu         .methods(
616*45ca1b86SEd Tanous             boost::beast::http::verb::patch)([&app](const crow::Request& req,
617*45ca1b86SEd Tanous                                                     const std::shared_ptr<
618*45ca1b86SEd Tanous                                                         bmcweb::AsyncResp>&
619*45ca1b86SEd Tanous                                                         asyncResp) {
620*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
621*45ca1b86SEd Tanous             {
622*45ca1b86SEd Tanous                 return;
623*45ca1b86SEd Tanous             }
624fa1a5a38SJayashankar Padath             BMCWEB_LOG_DEBUG << "doPatch...";
625fa1a5a38SJayashankar Padath 
626274dfe62SJayashankar Padath             std::optional<nlohmann::json> pushUriOptions;
62715ed6780SWilly Tu             if (!json_util::readJsonPatch(req, asyncResp->res,
62815ed6780SWilly Tu                                           "HttpPushUriOptions", pushUriOptions))
629fa1a5a38SJayashankar Padath             {
630fa1a5a38SJayashankar Padath                 return;
631fa1a5a38SJayashankar Padath             }
632fa1a5a38SJayashankar Padath 
633274dfe62SJayashankar Padath             if (pushUriOptions)
634274dfe62SJayashankar Padath             {
635274dfe62SJayashankar Padath                 std::optional<nlohmann::json> pushUriApplyTime;
6368d1b46d7Szhanghch05                 if (!json_util::readJson(*pushUriOptions, asyncResp->res,
6377e860f15SJohn Edward Broadbent                                          "HttpPushUriApplyTime",
6387e860f15SJohn Edward Broadbent                                          pushUriApplyTime))
639274dfe62SJayashankar Padath                 {
640274dfe62SJayashankar Padath                     return;
641274dfe62SJayashankar Padath                 }
642274dfe62SJayashankar Padath 
643274dfe62SJayashankar Padath                 if (pushUriApplyTime)
644274dfe62SJayashankar Padath                 {
645274dfe62SJayashankar Padath                     std::optional<std::string> applyTime;
6460fda0f12SGeorge Liu                     if (!json_util::readJson(*pushUriApplyTime, asyncResp->res,
6470fda0f12SGeorge Liu                                              "ApplyTime", applyTime))
648274dfe62SJayashankar Padath                     {
649274dfe62SJayashankar Padath                         return;
650274dfe62SJayashankar Padath                     }
651274dfe62SJayashankar Padath 
652274dfe62SJayashankar Padath                     if (applyTime)
653fa1a5a38SJayashankar Padath                     {
654fa1a5a38SJayashankar Padath                         std::string applyTimeNewVal;
655fa1a5a38SJayashankar Padath                         if (applyTime == "Immediate")
656fa1a5a38SJayashankar Padath                         {
657274dfe62SJayashankar Padath                             applyTimeNewVal =
6580fda0f12SGeorge Liu                                 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate";
659fa1a5a38SJayashankar Padath                         }
660274dfe62SJayashankar Padath                         else if (applyTime == "OnReset")
661274dfe62SJayashankar Padath                         {
662274dfe62SJayashankar Padath                             applyTimeNewVal =
6630fda0f12SGeorge Liu                                 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
664274dfe62SJayashankar Padath                         }
665fa1a5a38SJayashankar Padath                         else
666fa1a5a38SJayashankar Padath                         {
667274dfe62SJayashankar Padath                             BMCWEB_LOG_INFO
6680fda0f12SGeorge Liu                                 << "ApplyTime value is not in the list of acceptable values";
669274dfe62SJayashankar Padath                             messages::propertyValueNotInList(
670274dfe62SJayashankar Padath                                 asyncResp->res, *applyTime, "ApplyTime");
671274dfe62SJayashankar Padath                             return;
672fa1a5a38SJayashankar Padath                         }
673fa1a5a38SJayashankar Padath 
674fa1a5a38SJayashankar Padath                         // Set the requested image apply time value
675fa1a5a38SJayashankar Padath                         crow::connections::systemBus->async_method_call(
6760fda0f12SGeorge Liu                             [asyncResp](const boost::system::error_code ec) {
677fa1a5a38SJayashankar Padath                                 if (ec)
678fa1a5a38SJayashankar Padath                                 {
6797e860f15SJohn Edward Broadbent                                     BMCWEB_LOG_ERROR
6807e860f15SJohn Edward Broadbent                                         << "D-Bus responses error: " << ec;
681fa1a5a38SJayashankar Padath                                     messages::internalError(asyncResp->res);
682fa1a5a38SJayashankar Padath                                     return;
683fa1a5a38SJayashankar Padath                                 }
684fa1a5a38SJayashankar Padath                                 messages::success(asyncResp->res);
685fa1a5a38SJayashankar Padath                             },
686fa1a5a38SJayashankar Padath                             "xyz.openbmc_project.Settings",
687fa1a5a38SJayashankar Padath                             "/xyz/openbmc_project/software/apply_time",
688fa1a5a38SJayashankar Padath                             "org.freedesktop.DBus.Properties", "Set",
689274dfe62SJayashankar Padath                             "xyz.openbmc_project.Software.ApplyTime",
690274dfe62SJayashankar Padath                             "RequestedApplyTime",
691168e20c1SEd Tanous                             dbus::utility::DbusVariantType{applyTimeNewVal});
692fa1a5a38SJayashankar Padath                     }
693274dfe62SJayashankar Padath                 }
694fa1a5a38SJayashankar Padath             }
6957e860f15SJohn Edward Broadbent         });
6967e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
697ed398213SEd Tanous         .privileges(redfish::privileges::postUpdateService)
6987e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
699*45ca1b86SEd Tanous             [&app](const crow::Request& req,
7007e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
701*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
702*45ca1b86SEd Tanous                 {
703*45ca1b86SEd Tanous                     return;
704*45ca1b86SEd Tanous                 }
705acb7cfb4SJennifer Lee                 BMCWEB_LOG_DEBUG << "doPost...";
706acb7cfb4SJennifer Lee 
70786adcd6dSAndrew Geissler                 // Setup callback for when new software detected
7084cde5d90SJames Feist                 monitorForSoftwareAvailable(asyncResp, req,
7094cde5d90SJames Feist                                             "/redfish/v1/UpdateService");
710acb7cfb4SJennifer Lee 
7117e860f15SJohn Edward Broadbent                 std::string filepath("/tmp/images/" +
7127e860f15SJohn Edward Broadbent                                      boost::uuids::to_string(
7137e860f15SJohn Edward Broadbent                                          boost::uuids::random_generator()()));
714acb7cfb4SJennifer Lee                 BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
7157e860f15SJohn Edward Broadbent                 std::ofstream out(filepath, std::ofstream::out |
7167e860f15SJohn Edward Broadbent                                                 std::ofstream::binary |
717acb7cfb4SJennifer Lee                                                 std::ofstream::trunc);
718acb7cfb4SJennifer Lee                 out << req.body;
719acb7cfb4SJennifer Lee                 out.close();
720acb7cfb4SJennifer Lee                 BMCWEB_LOG_DEBUG << "file upload complete!!";
7217e860f15SJohn Edward Broadbent             });
722729dae72SJennifer Lee }
723729dae72SJennifer Lee 
7247e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventoryCollection(App& app)
7251abe55efSEd Tanous {
7267e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/")
727ed398213SEd Tanous         .privileges(redfish::privileges::getSoftwareInventoryCollection)
728*45ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
729*45ca1b86SEd Tanous                                                        const std::shared_ptr<
730*45ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
731*45ca1b86SEd Tanous                                                            asyncResp) {
732*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
733*45ca1b86SEd Tanous             {
734*45ca1b86SEd Tanous                 return;
735*45ca1b86SEd Tanous             }
7368d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.type"] =
7370f74e643SEd Tanous                 "#SoftwareInventoryCollection.SoftwareInventoryCollection";
7388d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] =
7390f74e643SEd Tanous                 "/redfish/v1/UpdateService/FirmwareInventory";
7400fda0f12SGeorge Liu             asyncResp->res.jsonValue["Name"] = "Software Inventory Collection";
741c711bf86SEd Tanous 
742c711bf86SEd Tanous             crow::connections::systemBus->async_method_call(
743c711bf86SEd Tanous                 [asyncResp](
744c711bf86SEd Tanous                     const boost::system::error_code ec,
745b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
7461abe55efSEd Tanous                     if (ec)
7471abe55efSEd Tanous                     {
748f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
7496c4eb9deSJennifer Lee                         return;
750729dae72SJennifer Lee                     }
7517e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"] =
7527e860f15SJohn Edward Broadbent                         nlohmann::json::array();
753c711bf86SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] = 0;
7546c4eb9deSJennifer Lee 
7559eb808c1SEd Tanous                     for (const auto& obj : subtree)
7561abe55efSEd Tanous                     {
7572dfd18efSEd Tanous                         sdbusplus::message::object_path path(obj.first);
7582dfd18efSEd Tanous                         std::string swId = path.filename();
7592dfd18efSEd Tanous                         if (swId.empty())
760f4b65ab1SJennifer Lee                         {
761f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
762f4b65ab1SJennifer Lee                             BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
763f4b65ab1SJennifer Lee                             return;
764f4b65ab1SJennifer Lee                         }
765f4b65ab1SJennifer Lee 
766c711bf86SEd Tanous                         nlohmann::json& members =
767c711bf86SEd Tanous                             asyncResp->res.jsonValue["Members"];
768c711bf86SEd Tanous                         members.push_back(
7690fda0f12SGeorge Liu                             {{"@odata.id",
7700fda0f12SGeorge Liu                               "/redfish/v1/UpdateService/FirmwareInventory/" +
771f4b65ab1SJennifer Lee                                   swId}});
772e0dd8057SAndrew Geissler                         asyncResp->res.jsonValue["Members@odata.count"] =
773c711bf86SEd Tanous                             members.size();
7746c4eb9deSJennifer Lee                     }
775c711bf86SEd Tanous                 },
7767e860f15SJohn Edward Broadbent                 // Note that only firmware levels associated with a device
7777e860f15SJohn Edward Broadbent                 // are stored under /xyz/openbmc_project/software therefore
7787e860f15SJohn Edward Broadbent                 // to ensure only real FirmwareInventory items are returned,
7797e860f15SJohn Edward Broadbent                 // this full object path must be used here as input to
7807e860f15SJohn Edward Broadbent                 // mapper
781c711bf86SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
782c711bf86SEd Tanous                 "/xyz/openbmc_project/object_mapper",
7832830a9cfSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
7842830a9cfSAndrew Geissler                 "/xyz/openbmc_project/software", static_cast<int32_t>(0),
7857e860f15SJohn Edward Broadbent                 std::array<const char*, 1>{
7867e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Software.Version"});
7877e860f15SJohn Edward Broadbent         });
788729dae72SJennifer Lee }
78987d84729SAndrew Geissler /* Fill related item links (i.e. bmc, bios) in for inventory */
7907e860f15SJohn Edward Broadbent inline static void
7917e860f15SJohn Edward Broadbent     getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
79287d84729SAndrew Geissler                     const std::string& purpose)
79387d84729SAndrew Geissler {
79487d84729SAndrew Geissler     if (purpose == fw_util::bmcPurpose)
79587d84729SAndrew Geissler     {
7965af690f5SAbhishek Patel         nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
7975af690f5SAbhishek Patel         relatedItem.push_back({{"@odata.id", "/redfish/v1/Managers/bmc"}});
7987e860f15SJohn Edward Broadbent         aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
79987d84729SAndrew Geissler     }
80087d84729SAndrew Geissler     else if (purpose == fw_util::biosPurpose)
80187d84729SAndrew Geissler     {
8025af690f5SAbhishek Patel         nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
8035af690f5SAbhishek Patel         relatedItem.push_back(
804f723d733SGunnar Mills             {{"@odata.id", "/redfish/v1/Systems/system/Bios"}});
8051a6e51aeSJiaqing Zhao         aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
80687d84729SAndrew Geissler     }
80787d84729SAndrew Geissler     else
80887d84729SAndrew Geissler     {
80987d84729SAndrew Geissler         BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
81087d84729SAndrew Geissler     }
81187d84729SAndrew Geissler }
81287d84729SAndrew Geissler 
8137e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventory(App& app)
8141abe55efSEd Tanous {
8157e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/")
816ed398213SEd Tanous         .privileges(redfish::privileges::getSoftwareInventory)
8177e860f15SJohn Edward Broadbent         .methods(
818*45ca1b86SEd Tanous             boost::beast::http::verb::
819*45ca1b86SEd Tanous                 get)([&app](const crow::Request& req,
820*45ca1b86SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8217e860f15SJohn Edward Broadbent                             const std::string& param) {
822*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
823*45ca1b86SEd Tanous             {
824*45ca1b86SEd Tanous                 return;
825*45ca1b86SEd Tanous             }
8263ae837c9SEd Tanous             std::shared_ptr<std::string> swId =
8277e860f15SJohn Edward Broadbent                 std::make_shared<std::string>(param);
828c711bf86SEd Tanous 
8298d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] =
8303ae837c9SEd Tanous                 "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
831c711bf86SEd Tanous 
832c711bf86SEd Tanous             crow::connections::systemBus->async_method_call(
833b9d36b47SEd Tanous                 [asyncResp,
834b9d36b47SEd Tanous                  swId](const boost::system::error_code ec,
835b9d36b47SEd Tanous                        const dbus::utility::MapperGetSubTreeResponse& subtree) {
83655c7b7a2SEd Tanous                     BMCWEB_LOG_DEBUG << "doGet callback...";
8371abe55efSEd Tanous                     if (ec)
8381abe55efSEd Tanous                     {
839f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
8406c4eb9deSJennifer Lee                         return;
8416c4eb9deSJennifer Lee                     }
8426c4eb9deSJennifer Lee 
8436913228dSAndrew Geissler                     // Ensure we find our input swId, otherwise return an error
8446913228dSAndrew Geissler                     bool found = false;
8451abe55efSEd Tanous                     for (const std::pair<
8461abe55efSEd Tanous                              std::string,
8477e860f15SJohn Edward Broadbent                              std::vector<std::pair<
8487e860f15SJohn Edward Broadbent                                  std::string, std::vector<std::string>>>>& obj :
8497e860f15SJohn Edward Broadbent                          subtree)
8501abe55efSEd Tanous                     {
851e05aec50SEd Tanous                         if (!boost::ends_with(obj.first, *swId))
8521abe55efSEd Tanous                         {
853acb7cfb4SJennifer Lee                             continue;
854acb7cfb4SJennifer Lee                         }
855acb7cfb4SJennifer Lee 
85626f6976fSEd Tanous                         if (obj.second.empty())
8571abe55efSEd Tanous                         {
858acb7cfb4SJennifer Lee                             continue;
859acb7cfb4SJennifer Lee                         }
8606c4eb9deSJennifer Lee 
8616913228dSAndrew Geissler                         found = true;
8627e860f15SJohn Edward Broadbent                         fw_util::getFwStatus(asyncResp, swId,
8637e860f15SJohn Edward Broadbent                                              obj.second[0].first);
864e0dd8057SAndrew Geissler 
86555c7b7a2SEd Tanous                         crow::connections::systemBus->async_method_call(
866168e20c1SEd Tanous                             [asyncResp,
867168e20c1SEd Tanous                              swId](const boost::system::error_code errorCode,
868b9d36b47SEd Tanous                                    const dbus::utility::DBusPropertiesMap&
869168e20c1SEd Tanous                                        propertiesList) {
87081ce609eSEd Tanous                                 if (errorCode)
8711abe55efSEd Tanous                                 {
872f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
8736c4eb9deSJennifer Lee                                     return;
8746c4eb9deSJennifer Lee                                 }
875b9d36b47SEd Tanous                                 const std::string* swInvPurpose = nullptr;
876b9d36b47SEd Tanous                                 const std::string* version = nullptr;
877b9d36b47SEd Tanous                                 for (const auto& property : propertiesList)
8781abe55efSEd Tanous                                 {
879b9d36b47SEd Tanous                                     if (property.first == "Purpose")
880b9d36b47SEd Tanous                                     {
881b9d36b47SEd Tanous                                         swInvPurpose = std::get_if<std::string>(
882b9d36b47SEd Tanous                                             &property.second);
8836c4eb9deSJennifer Lee                                     }
884b9d36b47SEd Tanous                                     if (property.first == "Version")
885b9d36b47SEd Tanous                                     {
886b9d36b47SEd Tanous                                         version = std::get_if<std::string>(
887b9d36b47SEd Tanous                                             &property.second);
888b9d36b47SEd Tanous                                     }
889b9d36b47SEd Tanous                                 }
890b9d36b47SEd Tanous 
8913ae837c9SEd Tanous                                 if (swInvPurpose == nullptr)
8921abe55efSEd Tanous                                 {
8930fda0f12SGeorge Liu                                     BMCWEB_LOG_DEBUG
894b9d36b47SEd Tanous                                         << "Can't find property \"Purpose\"!";
895b9d36b47SEd Tanous                                     messages::internalError(asyncResp->res);
896acb7cfb4SJennifer Lee                                     return;
897acb7cfb4SJennifer Lee                                 }
898c711bf86SEd Tanous 
8993ae837c9SEd Tanous                                 BMCWEB_LOG_DEBUG << "swInvPurpose = "
9003ae837c9SEd Tanous                                                  << *swInvPurpose;
901f4b65ab1SJennifer Lee 
902f4b65ab1SJennifer Lee                                 if (version == nullptr)
9031abe55efSEd Tanous                                 {
9041abe55efSEd Tanous                                     BMCWEB_LOG_DEBUG
9051abe55efSEd Tanous                                         << "Can't find property \"Version\"!";
906f12894f8SJason M. Bills 
907b9d36b47SEd Tanous                                     messages::internalError(asyncResp->res);
908b9d36b47SEd Tanous 
9096c4eb9deSJennifer Lee                                     return;
9106c4eb9deSJennifer Lee                                 }
911c711bf86SEd Tanous                                 asyncResp->res.jsonValue["Version"] = *version;
9123ae837c9SEd Tanous                                 asyncResp->res.jsonValue["Id"] = *swId;
91354daabe7SAndrew Geissler 
91454daabe7SAndrew Geissler                                 // swInvPurpose is of format:
91554daabe7SAndrew Geissler                                 // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
916e2e96770SJames Feist                                 // Translate this to "ABC image"
917f23b7296SEd Tanous                                 size_t endDesc = swInvPurpose->rfind('.');
91854daabe7SAndrew Geissler                                 if (endDesc == std::string::npos)
91954daabe7SAndrew Geissler                                 {
92054daabe7SAndrew Geissler                                     messages::internalError(asyncResp->res);
92154daabe7SAndrew Geissler                                     return;
92254daabe7SAndrew Geissler                                 }
92354daabe7SAndrew Geissler                                 endDesc++;
92454daabe7SAndrew Geissler                                 if (endDesc >= swInvPurpose->size())
92554daabe7SAndrew Geissler                                 {
92654daabe7SAndrew Geissler                                     messages::internalError(asyncResp->res);
92754daabe7SAndrew Geissler                                     return;
92854daabe7SAndrew Geissler                                 }
92954daabe7SAndrew Geissler 
93054daabe7SAndrew Geissler                                 std::string formatDesc =
93154daabe7SAndrew Geissler                                     swInvPurpose->substr(endDesc);
93254daabe7SAndrew Geissler                                 asyncResp->res.jsonValue["Description"] =
933e2e96770SJames Feist                                     formatDesc + " image";
93487d84729SAndrew Geissler                                 getRelatedItems(asyncResp, *swInvPurpose);
9356c4eb9deSJennifer Lee                             },
936c711bf86SEd Tanous                             obj.second[0].first, obj.first,
937c711bf86SEd Tanous                             "org.freedesktop.DBus.Properties", "GetAll",
938c711bf86SEd Tanous                             "xyz.openbmc_project.Software.Version");
9396c4eb9deSJennifer Lee                     }
9406913228dSAndrew Geissler                     if (!found)
9416913228dSAndrew Geissler                     {
9428cc8edecSEd Tanous                         BMCWEB_LOG_ERROR << "Input swID " << *swId
9438cc8edecSEd Tanous                                          << " not found!";
9446913228dSAndrew Geissler                         messages::resourceMissingAtURI(
9456913228dSAndrew Geissler                             asyncResp->res,
946ace85d60SEd Tanous                             crow::utility::urlFromPieces(
947ace85d60SEd Tanous                                 "redfish", "v1", "UpdateService",
948ace85d60SEd Tanous                                 "FirmwareInventory", *swId));
9496913228dSAndrew Geissler                         return;
9506913228dSAndrew Geissler                     }
9514e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["@odata.type"] =
9524e68c45bSAyushi Smriti                         "#SoftwareInventory.v1_1_0.SoftwareInventory";
9534e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["Name"] = "Software Inventory";
9544e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
9553f8a743aSAppaRao Puli 
9563f8a743aSAppaRao Puli                     asyncResp->res.jsonValue["Updateable"] = false;
9573f8a743aSAppaRao Puli                     fw_util::getFwUpdateableStatus(asyncResp, swId);
958c711bf86SEd Tanous                 },
959c711bf86SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
960c711bf86SEd Tanous                 "/xyz/openbmc_project/object_mapper",
961271584abSEd Tanous                 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
962271584abSEd Tanous                 static_cast<int32_t>(0),
9637e860f15SJohn Edward Broadbent                 std::array<const char*, 1>{
9647e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Software.Version"});
9657e860f15SJohn Edward Broadbent         });
9666c4eb9deSJennifer Lee }
967729dae72SJennifer Lee 
968729dae72SJennifer Lee } // namespace redfish
969