xref: /openbmc/bmcweb/features/redfish/lib/update_service.hpp (revision b9d36b4791d77a47e1f3c5c4564fcdf7cc68c115)
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>
22ed398213SEd Tanous #include <registries/privilege_registry.hpp>
231e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
2487d84729SAndrew Geissler #include <utils/fw_utils.hpp>
251214b7e7SGunnar Mills 
261abe55efSEd Tanous namespace redfish
271abe55efSEd Tanous {
2827826b5fSEd Tanous 
290e7de46fSAndrew Geissler // Match signals added on software path
30acb7cfb4SJennifer Lee static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher;
314cde5d90SJames Feist static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateErrorMatcher;
320e7de46fSAndrew Geissler // Only allow one update at a time
330e7de46fSAndrew Geissler static bool fwUpdateInProgress = false;
3486adcd6dSAndrew Geissler // Timer for software available
35271584abSEd Tanous static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer;
3686adcd6dSAndrew Geissler 
377e860f15SJohn Edward Broadbent inline static void cleanUp()
3886adcd6dSAndrew Geissler {
3986adcd6dSAndrew Geissler     fwUpdateInProgress = false;
4086adcd6dSAndrew Geissler     fwUpdateMatcher = nullptr;
414cde5d90SJames Feist     fwUpdateErrorMatcher = nullptr;
4286adcd6dSAndrew Geissler }
437e860f15SJohn Edward Broadbent inline static void activateImage(const std::string& objPath,
4486adcd6dSAndrew Geissler                                  const std::string& service)
4586adcd6dSAndrew Geissler {
4686adcd6dSAndrew Geissler     BMCWEB_LOG_DEBUG << "Activate image for " << objPath << " " << service;
4786adcd6dSAndrew Geissler     crow::connections::systemBus->async_method_call(
4881ce609eSEd Tanous         [](const boost::system::error_code errorCode) {
4981ce609eSEd Tanous             if (errorCode)
5086adcd6dSAndrew Geissler             {
5181ce609eSEd Tanous                 BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
5281ce609eSEd Tanous                 BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message();
5386adcd6dSAndrew Geissler             }
5486adcd6dSAndrew Geissler         },
5586adcd6dSAndrew Geissler         service, objPath, "org.freedesktop.DBus.Properties", "Set",
5686adcd6dSAndrew Geissler         "xyz.openbmc_project.Software.Activation", "RequestedActivation",
57168e20c1SEd Tanous         dbus::utility::DbusVariantType(
580fda0f12SGeorge Liu             "xyz.openbmc_project.Software.Activation.RequestedActivations.Active"));
5986adcd6dSAndrew Geissler }
600554c984SAndrew Geissler 
610554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr
620554c984SAndrew Geissler // then no asyncResp updates will occur
638d1b46d7Szhanghch05 static void
648d1b46d7Szhanghch05     softwareInterfaceAdded(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
65fe306728SJames Feist                            sdbusplus::message::message& m,
66a3e65892SEd Tanous                            task::Payload&& payload)
6786adcd6dSAndrew Geissler {
68*b9d36b47SEd Tanous     dbus::utility::DBusInteracesMap interfacesProperties;
6986adcd6dSAndrew Geissler 
7086adcd6dSAndrew Geissler     sdbusplus::message::object_path objPath;
7186adcd6dSAndrew Geissler 
7286adcd6dSAndrew Geissler     m.read(objPath, interfacesProperties);
7386adcd6dSAndrew Geissler 
7486adcd6dSAndrew Geissler     BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
7586adcd6dSAndrew Geissler     for (auto& interface : interfacesProperties)
7686adcd6dSAndrew Geissler     {
7786adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "interface = " << interface.first;
7886adcd6dSAndrew Geissler 
7986adcd6dSAndrew Geissler         if (interface.first == "xyz.openbmc_project.Software.Activation")
8086adcd6dSAndrew Geissler         {
8186adcd6dSAndrew Geissler             // Retrieve service and activate
8286adcd6dSAndrew Geissler             crow::connections::systemBus->async_method_call(
83a3e65892SEd Tanous                 [objPath, asyncResp, payload(std::move(payload))](
84a3e65892SEd Tanous                     const boost::system::error_code errorCode,
85a3e65892SEd Tanous                     const std::vector<
86a3e65892SEd Tanous                         std::pair<std::string, std::vector<std::string>>>&
87a3e65892SEd Tanous                         objInfo) mutable {
8881ce609eSEd Tanous                     if (errorCode)
8986adcd6dSAndrew Geissler                     {
9081ce609eSEd Tanous                         BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
9186adcd6dSAndrew Geissler                         BMCWEB_LOG_DEBUG << "error msg = "
9281ce609eSEd Tanous                                          << errorCode.message();
930554c984SAndrew Geissler                         if (asyncResp)
940554c984SAndrew Geissler                         {
9586adcd6dSAndrew Geissler                             messages::internalError(asyncResp->res);
960554c984SAndrew Geissler                         }
9786adcd6dSAndrew Geissler                         cleanUp();
9886adcd6dSAndrew Geissler                         return;
9986adcd6dSAndrew Geissler                     }
10086adcd6dSAndrew Geissler                     // Ensure we only got one service back
10186adcd6dSAndrew Geissler                     if (objInfo.size() != 1)
10286adcd6dSAndrew Geissler                     {
10386adcd6dSAndrew Geissler                         BMCWEB_LOG_ERROR << "Invalid Object Size "
10486adcd6dSAndrew Geissler                                          << objInfo.size();
1050554c984SAndrew Geissler                         if (asyncResp)
1060554c984SAndrew Geissler                         {
10786adcd6dSAndrew Geissler                             messages::internalError(asyncResp->res);
1080554c984SAndrew Geissler                         }
10986adcd6dSAndrew Geissler                         cleanUp();
11086adcd6dSAndrew Geissler                         return;
11186adcd6dSAndrew Geissler                     }
11286adcd6dSAndrew Geissler                     // cancel timer only when
11386adcd6dSAndrew Geissler                     // xyz.openbmc_project.Software.Activation interface
11486adcd6dSAndrew Geissler                     // is added
11586adcd6dSAndrew Geissler                     fwAvailableTimer = nullptr;
11686adcd6dSAndrew Geissler 
11786adcd6dSAndrew Geissler                     activateImage(objPath.str, objInfo[0].first);
1180554c984SAndrew Geissler                     if (asyncResp)
1190554c984SAndrew Geissler                     {
12032898ceaSJames Feist                         std::shared_ptr<task::TaskData> task =
12132898ceaSJames Feist                             task::TaskData::createTask(
12232898ceaSJames Feist                                 [](boost::system::error_code ec,
12332898ceaSJames Feist                                    sdbusplus::message::message& msg,
1241214b7e7SGunnar Mills                                    const std::shared_ptr<task::TaskData>&
1251214b7e7SGunnar Mills                                        taskData) {
12632898ceaSJames Feist                                     if (ec)
12732898ceaSJames Feist                                     {
12832898ceaSJames Feist                                         return task::completed;
12932898ceaSJames Feist                                     }
13032898ceaSJames Feist 
13132898ceaSJames Feist                                     std::string iface;
132*b9d36b47SEd Tanous                                     dbus::utility::DBusPropertiesMap values;
133fd9ab9e1SJames Feist 
134fd9ab9e1SJames Feist                                     std::string index =
135fd9ab9e1SJames Feist                                         std::to_string(taskData->index);
13632898ceaSJames Feist                                     msg.read(iface, values);
137fd9ab9e1SJames Feist 
1380fda0f12SGeorge Liu                                     if (iface ==
1390fda0f12SGeorge Liu                                         "xyz.openbmc_project.Software.Activation")
140fd9ab9e1SJames Feist                                     {
141*b9d36b47SEd Tanous                                         std::string* state = nullptr;
142*b9d36b47SEd Tanous                                         for (const auto& property : values)
14332898ceaSJames Feist                                         {
144*b9d36b47SEd Tanous                                             if (property.first == "Activation")
145*b9d36b47SEd Tanous                                             {
146*b9d36b47SEd Tanous                                                 const std::string* state =
14732898ceaSJames Feist                                                     std::get_if<std::string>(
148*b9d36b47SEd Tanous                                                         &property.second);
149*b9d36b47SEd Tanous                                                 if (state == nullptr)
150*b9d36b47SEd Tanous                                                 {
151*b9d36b47SEd Tanous                                                     taskData->messages
152*b9d36b47SEd Tanous                                                         .emplace_back(
153*b9d36b47SEd Tanous                                                             messages::
154*b9d36b47SEd Tanous                                                                 internalError());
155*b9d36b47SEd Tanous                                                     return task::completed;
156*b9d36b47SEd Tanous                                                 }
157*b9d36b47SEd Tanous                                             }
158*b9d36b47SEd Tanous                                         }
15932898ceaSJames Feist 
16032898ceaSJames Feist                                         if (state == nullptr)
16132898ceaSJames Feist                                         {
162*b9d36b47SEd Tanous                                             return !task::completed;
16332898ceaSJames Feist                                         }
16432898ceaSJames Feist 
165fd9ab9e1SJames Feist                                         if (boost::ends_with(*state,
166fd9ab9e1SJames Feist                                                              "Invalid") ||
16732898ceaSJames Feist                                             boost::ends_with(*state, "Failed"))
16832898ceaSJames Feist                                         {
16932898ceaSJames Feist                                             taskData->state = "Exception";
17032898ceaSJames Feist                                             taskData->status = "Warning";
17132898ceaSJames Feist                                             taskData->messages.emplace_back(
172e5d5006bSJames Feist                                                 messages::taskAborted(index));
17332898ceaSJames Feist                                             return task::completed;
17432898ceaSJames Feist                                         }
17532898ceaSJames Feist 
17632898ceaSJames Feist                                         if (boost::ends_with(*state, "Staged"))
17732898ceaSJames Feist                                         {
178fd9ab9e1SJames Feist                                             taskData->state = "Stopping";
179fd9ab9e1SJames Feist                                             taskData->messages.emplace_back(
180fd9ab9e1SJames Feist                                                 messages::taskPaused(index));
181fd9ab9e1SJames Feist 
182fd9ab9e1SJames Feist                                             // its staged, set a long timer to
183fd9ab9e1SJames Feist                                             // allow them time to complete the
184fd9ab9e1SJames Feist                                             // update (probably cycle the
185fd9ab9e1SJames Feist                                             // system) if this expires then
186fd9ab9e1SJames Feist                                             // task will be cancelled
187fd9ab9e1SJames Feist                                             taskData->extendTimer(
188fd9ab9e1SJames Feist                                                 std::chrono::hours(5));
18932898ceaSJames Feist                                             return !task::completed;
19032898ceaSJames Feist                                         }
19132898ceaSJames Feist 
19232898ceaSJames Feist                                         if (boost::ends_with(*state, "Active"))
19332898ceaSJames Feist                                         {
19432898ceaSJames Feist                                             taskData->messages.emplace_back(
195fd9ab9e1SJames Feist                                                 messages::taskCompletedOK(
196fd9ab9e1SJames Feist                                                     index));
19732898ceaSJames Feist                                             taskData->state = "Completed";
19832898ceaSJames Feist                                             return task::completed;
19932898ceaSJames Feist                                         }
200fd9ab9e1SJames Feist                                     }
2010fda0f12SGeorge Liu                                     else if (
2020fda0f12SGeorge Liu                                         iface ==
2030fda0f12SGeorge Liu                                         "xyz.openbmc_project.Software.ActivationProgress")
204fd9ab9e1SJames Feist                                     {
205*b9d36b47SEd Tanous 
206*b9d36b47SEd Tanous                                         const uint8_t* progress = nullptr;
207*b9d36b47SEd Tanous                                         for (const auto& property : values)
208fd9ab9e1SJames Feist                                         {
209*b9d36b47SEd Tanous                                             if (property.first == "Progress")
210*b9d36b47SEd Tanous                                             {
211*b9d36b47SEd Tanous                                                 const std::string* progress =
212*b9d36b47SEd Tanous                                                     std::get_if<std::string>(
213*b9d36b47SEd Tanous                                                         &property.second);
214*b9d36b47SEd Tanous                                                 if (progress == nullptr)
215*b9d36b47SEd Tanous                                                 {
216*b9d36b47SEd Tanous                                                     taskData->messages
217*b9d36b47SEd Tanous                                                         .emplace_back(
218*b9d36b47SEd Tanous                                                             messages::
219*b9d36b47SEd Tanous                                                                 internalError());
220*b9d36b47SEd Tanous                                                     return task::completed;
221fd9ab9e1SJames Feist                                                 }
222*b9d36b47SEd Tanous                                             }
223*b9d36b47SEd Tanous                                         }
224fd9ab9e1SJames Feist 
225fd9ab9e1SJames Feist                                         if (progress == nullptr)
226fd9ab9e1SJames Feist                                         {
227*b9d36b47SEd Tanous                                             return !task::completed;
228fd9ab9e1SJames Feist                                         }
2296868ff50SGeorge Liu                                         taskData->percentComplete =
2306868ff50SGeorge Liu                                             static_cast<int>(*progress);
231fd9ab9e1SJames Feist                                         taskData->messages.emplace_back(
232fd9ab9e1SJames Feist                                             messages::taskProgressChanged(
233fd9ab9e1SJames Feist                                                 index, static_cast<size_t>(
234fd9ab9e1SJames Feist                                                            *progress)));
235fd9ab9e1SJames Feist 
236fd9ab9e1SJames Feist                                         // if we're getting status updates it's
237fd9ab9e1SJames Feist                                         // still alive, update timer
238fd9ab9e1SJames Feist                                         taskData->extendTimer(
239fd9ab9e1SJames Feist                                             std::chrono::minutes(5));
240fd9ab9e1SJames Feist                                     }
24132898ceaSJames Feist 
24232898ceaSJames Feist                                     // as firmware update often results in a
24332898ceaSJames Feist                                     // reboot, the task  may never "complete"
24432898ceaSJames Feist                                     // unless it is an error
24532898ceaSJames Feist 
24632898ceaSJames Feist                                     return !task::completed;
24732898ceaSJames Feist                                 },
2480fda0f12SGeorge Liu                                 "type='signal',interface='org.freedesktop.DBus.Properties',"
249fd9ab9e1SJames Feist                                 "member='PropertiesChanged',path='" +
25032898ceaSJames Feist                                     objPath.str + "'");
25132898ceaSJames Feist                         task->startTimer(std::chrono::minutes(5));
25232898ceaSJames Feist                         task->populateResp(asyncResp->res);
253a3e65892SEd Tanous                         task->payload.emplace(std::move(payload));
2540554c984SAndrew Geissler                     }
25586adcd6dSAndrew Geissler                     fwUpdateInProgress = false;
25686adcd6dSAndrew Geissler                 },
25786adcd6dSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper",
25886adcd6dSAndrew Geissler                 "/xyz/openbmc_project/object_mapper",
25986adcd6dSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str,
26086adcd6dSAndrew Geissler                 std::array<const char*, 1>{
26186adcd6dSAndrew Geissler                     "xyz.openbmc_project.Software.Activation"});
26286adcd6dSAndrew Geissler         }
26386adcd6dSAndrew Geissler     }
26486adcd6dSAndrew Geissler }
26586adcd6dSAndrew Geissler 
2660554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr
2670554c984SAndrew Geissler // then no asyncResp updates will occur
268b5a76932SEd Tanous static void monitorForSoftwareAvailable(
2698d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2708d1b46d7Szhanghch05     const crow::Request& req, const std::string& url,
2718d1b46d7Szhanghch05     int timeoutTimeSeconds = 10)
27286adcd6dSAndrew Geissler {
27386adcd6dSAndrew Geissler     // Only allow one FW update at a time
274e05aec50SEd Tanous     if (fwUpdateInProgress)
27586adcd6dSAndrew Geissler     {
2760554c984SAndrew Geissler         if (asyncResp)
2770554c984SAndrew Geissler         {
27886adcd6dSAndrew Geissler             messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
2790554c984SAndrew Geissler         }
28086adcd6dSAndrew Geissler         return;
28186adcd6dSAndrew Geissler     }
28286adcd6dSAndrew Geissler 
2830554c984SAndrew Geissler     fwAvailableTimer =
284271584abSEd Tanous         std::make_unique<boost::asio::steady_timer>(*req.ioService);
28586adcd6dSAndrew Geissler 
286271584abSEd Tanous     fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds));
28786adcd6dSAndrew Geissler 
28886adcd6dSAndrew Geissler     fwAvailableTimer->async_wait(
28986adcd6dSAndrew Geissler         [asyncResp](const boost::system::error_code& ec) {
29086adcd6dSAndrew Geissler             cleanUp();
29186adcd6dSAndrew Geissler             if (ec == boost::asio::error::operation_aborted)
29286adcd6dSAndrew Geissler             {
29386adcd6dSAndrew Geissler                 // expected, we were canceled before the timer completed.
29486adcd6dSAndrew Geissler                 return;
29586adcd6dSAndrew Geissler             }
29686adcd6dSAndrew Geissler             BMCWEB_LOG_ERROR
29786adcd6dSAndrew Geissler                 << "Timed out waiting for firmware object being created";
29886adcd6dSAndrew Geissler             BMCWEB_LOG_ERROR
29986adcd6dSAndrew Geissler                 << "FW image may has already been uploaded to server";
30086adcd6dSAndrew Geissler             if (ec)
30186adcd6dSAndrew Geissler             {
30286adcd6dSAndrew Geissler                 BMCWEB_LOG_ERROR << "Async_wait failed" << ec;
30386adcd6dSAndrew Geissler                 return;
30486adcd6dSAndrew Geissler             }
3050554c984SAndrew Geissler             if (asyncResp)
3060554c984SAndrew Geissler             {
30786adcd6dSAndrew Geissler                 redfish::messages::internalError(asyncResp->res);
3080554c984SAndrew Geissler             }
30986adcd6dSAndrew Geissler         });
310a3e65892SEd Tanous     task::Payload payload(req);
311a3e65892SEd Tanous     auto callback = [asyncResp,
312a3e65892SEd Tanous                      payload](sdbusplus::message::message& m) mutable {
31386adcd6dSAndrew Geissler         BMCWEB_LOG_DEBUG << "Match fired";
314a3e65892SEd Tanous         softwareInterfaceAdded(asyncResp, m, std::move(payload));
31586adcd6dSAndrew Geissler     };
31686adcd6dSAndrew Geissler 
31786adcd6dSAndrew Geissler     fwUpdateInProgress = true;
31886adcd6dSAndrew Geissler 
31986adcd6dSAndrew Geissler     fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
32086adcd6dSAndrew Geissler         *crow::connections::systemBus,
32186adcd6dSAndrew Geissler         "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
32286adcd6dSAndrew Geissler         "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
32386adcd6dSAndrew Geissler         callback);
3244cde5d90SJames Feist 
3254cde5d90SJames Feist     fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match::match>(
3264cde5d90SJames Feist         *crow::connections::systemBus,
327e1cc4828SBrian Ma         "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
328e1cc4828SBrian Ma         "member='InterfacesAdded',"
329e1cc4828SBrian Ma         "path='/xyz/openbmc_project/logging'",
3304cde5d90SJames Feist         [asyncResp, url](sdbusplus::message::message& m) {
331e1cc4828SBrian Ma             std::vector<
332e1cc4828SBrian Ma                 std::pair<std::string, dbus::utility::DBusPropertiesMap>>
333e1cc4828SBrian Ma                 interfacesProperties;
334e1cc4828SBrian Ma             sdbusplus::message::object_path objPath;
335e1cc4828SBrian Ma             m.read(objPath, interfacesProperties);
336e1cc4828SBrian Ma             BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
337e1cc4828SBrian Ma             for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>&
338e1cc4828SBrian Ma                      interface : interfacesProperties)
339e1cc4828SBrian Ma             {
340e1cc4828SBrian Ma                 if (interface.first == "xyz.openbmc_project.Logging.Entry")
341e1cc4828SBrian Ma                 {
342711ac7a9SEd Tanous                     for (const std::pair<std::string,
343711ac7a9SEd Tanous                                          dbus::utility::DbusVariantType>&
344711ac7a9SEd Tanous                              value : interface.second)
3454cde5d90SJames Feist                     {
346711ac7a9SEd Tanous                         if (value.first != "Message")
347711ac7a9SEd Tanous                         {
348711ac7a9SEd Tanous                             continue;
3494cde5d90SJames Feist                         }
350e1cc4828SBrian Ma                         const std::string* type =
351711ac7a9SEd Tanous                             std::get_if<std::string>(&value.second);
3524cde5d90SJames Feist                         if (type == nullptr)
3534cde5d90SJames Feist                         {
354e1cc4828SBrian Ma                             // if this was our message, timeout will cover it
3554cde5d90SJames Feist                             return;
3564cde5d90SJames Feist                         }
357e1cc4828SBrian Ma                         fwAvailableTimer = nullptr;
3584cde5d90SJames Feist                         if (*type ==
3594cde5d90SJames Feist                             "xyz.openbmc_project.Software.Image.Error.UnTarFailure")
3604cde5d90SJames Feist                         {
361711ac7a9SEd Tanous                             redfish::messages::invalidUpload(
362711ac7a9SEd Tanous                                 asyncResp->res, url, "Invalid archive");
3634cde5d90SJames Feist                         }
364e1cc4828SBrian Ma                         else if (*type ==
365e1cc4828SBrian Ma                                  "xyz.openbmc_project.Software.Image.Error."
366e1cc4828SBrian Ma                                  "ManifestFileFailure")
3674cde5d90SJames Feist                         {
368711ac7a9SEd Tanous                             redfish::messages::invalidUpload(
369711ac7a9SEd Tanous                                 asyncResp->res, url, "Invalid manifest");
3704cde5d90SJames Feist                         }
371e1cc4828SBrian Ma                         else if (
372e1cc4828SBrian Ma                             *type ==
3734cde5d90SJames Feist                             "xyz.openbmc_project.Software.Image.Error.ImageFailure")
3744cde5d90SJames Feist                         {
375e1cc4828SBrian Ma                             redfish::messages::invalidUpload(
376e1cc4828SBrian Ma                                 asyncResp->res, url, "Invalid image format");
3774cde5d90SJames Feist                         }
378e1cc4828SBrian Ma                         else if (
379e1cc4828SBrian Ma                             *type ==
3800fda0f12SGeorge Liu                             "xyz.openbmc_project.Software.Version.Error.AlreadyExists")
38188b3dd12SGunnar Mills                         {
38288b3dd12SGunnar Mills                             redfish::messages::invalidUpload(
383e1cc4828SBrian Ma                                 asyncResp->res, url,
384e1cc4828SBrian Ma                                 "Image version already exists");
38588b3dd12SGunnar Mills 
38688b3dd12SGunnar Mills                             redfish::messages::resourceAlreadyExists(
387e1cc4828SBrian Ma                                 asyncResp->res,
388e1cc4828SBrian Ma                                 "UpdateService.v1_5_0.UpdateService", "Version",
389e1cc4828SBrian Ma                                 "uploaded version");
39088b3dd12SGunnar Mills                         }
391e1cc4828SBrian Ma                         else if (
392e1cc4828SBrian Ma                             *type ==
3934cde5d90SJames Feist                             "xyz.openbmc_project.Software.Image.Error.BusyFailure")
3944cde5d90SJames Feist                         {
395711ac7a9SEd Tanous                             redfish::messages::resourceExhaustion(
396711ac7a9SEd Tanous                                 asyncResp->res, url);
3974cde5d90SJames Feist                         }
3984cde5d90SJames Feist                         else
3994cde5d90SJames Feist                         {
4004cde5d90SJames Feist                             redfish::messages::internalError(asyncResp->res);
4014cde5d90SJames Feist                         }
402e1cc4828SBrian Ma                     }
403e1cc4828SBrian Ma                 }
404711ac7a9SEd Tanous             }
4054cde5d90SJames Feist         });
40686adcd6dSAndrew Geissler }
407729dae72SJennifer Lee 
4080554c984SAndrew Geissler /**
4090554c984SAndrew Geissler  * UpdateServiceActionsSimpleUpdate class supports handle POST method for
4100554c984SAndrew Geissler  * SimpleUpdate action.
4110554c984SAndrew Geissler  */
4127e860f15SJohn Edward Broadbent inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app)
4130554c984SAndrew Geissler {
4147e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
4157e860f15SJohn Edward Broadbent         app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
416ed398213SEd Tanous         .privileges(redfish::privileges::postUpdateService)
4177e860f15SJohn Edward Broadbent         .methods(
4187e860f15SJohn Edward Broadbent             boost::beast::http::verb::
4197e860f15SJohn Edward Broadbent                 post)([](const crow::Request& req,
4207e860f15SJohn Edward Broadbent                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
4210554c984SAndrew Geissler             std::optional<std::string> transferProtocol;
4220554c984SAndrew Geissler             std::string imageURI;
4230554c984SAndrew Geissler 
4240554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost";
4250554c984SAndrew Geissler 
4260554c984SAndrew Geissler             // User can pass in both TransferProtocol and ImageURI parameters or
4274e0453b1SGunnar Mills             // they can pass in just the ImageURI with the transfer protocol
4284e0453b1SGunnar Mills             // embedded within it.
4290554c984SAndrew Geissler             // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
4300554c984SAndrew Geissler             // 2) ImageURI:tftp://1.1.1.1/myfile.bin
4310554c984SAndrew Geissler 
43215ed6780SWilly Tu             if (!json_util::readJsonAction(req, asyncResp->res,
43315ed6780SWilly Tu                                            "TransferProtocol", transferProtocol,
43415ed6780SWilly Tu                                            "ImageURI", imageURI))
4350554c984SAndrew Geissler             {
4360554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG
4370554c984SAndrew Geissler                     << "Missing TransferProtocol or ImageURI parameter";
4380554c984SAndrew Geissler                 return;
4390554c984SAndrew Geissler             }
4400554c984SAndrew Geissler             if (!transferProtocol)
4410554c984SAndrew Geissler             {
4420554c984SAndrew Geissler                 // Must be option 2
4430554c984SAndrew Geissler                 // Verify ImageURI has transfer protocol in it
444f23b7296SEd Tanous                 size_t separator = imageURI.find(':');
4450554c984SAndrew Geissler                 if ((separator == std::string::npos) ||
4460554c984SAndrew Geissler                     ((separator + 1) > imageURI.size()))
4470554c984SAndrew Geissler                 {
4480554c984SAndrew Geissler                     messages::actionParameterValueTypeError(
4490554c984SAndrew Geissler                         asyncResp->res, imageURI, "ImageURI",
4500554c984SAndrew Geissler                         "UpdateService.SimpleUpdate");
4510554c984SAndrew Geissler                     BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: "
4520554c984SAndrew Geissler                                      << imageURI;
4530554c984SAndrew Geissler                     return;
4540554c984SAndrew Geissler                 }
4550554c984SAndrew Geissler                 transferProtocol = imageURI.substr(0, separator);
4567e860f15SJohn Edward Broadbent                 // Ensure protocol is upper case for a common comparison path
4577e860f15SJohn Edward Broadbent                 // below
4580554c984SAndrew Geissler                 boost::to_upper(*transferProtocol);
4590554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG << "Encoded transfer protocol "
4600554c984SAndrew Geissler                                  << *transferProtocol;
4610554c984SAndrew Geissler 
4620554c984SAndrew Geissler                 // Adjust imageURI to not have the protocol on it for parsing
4630554c984SAndrew Geissler                 // below
4640554c984SAndrew Geissler                 // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin
4650554c984SAndrew Geissler                 imageURI = imageURI.substr(separator + 3);
4660554c984SAndrew Geissler                 BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI;
4670554c984SAndrew Geissler             }
4680554c984SAndrew Geissler 
4690554c984SAndrew Geissler             // OpenBMC currently only supports TFTP
4700554c984SAndrew Geissler             if (*transferProtocol != "TFTP")
4710554c984SAndrew Geissler             {
4727e860f15SJohn Edward Broadbent                 messages::actionParameterNotSupported(
4737e860f15SJohn Edward Broadbent                     asyncResp->res, "TransferProtocol",
4740554c984SAndrew Geissler                     "UpdateService.SimpleUpdate");
4750554c984SAndrew Geissler                 BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: "
4760554c984SAndrew Geissler                                  << *transferProtocol;
4770554c984SAndrew Geissler                 return;
4780554c984SAndrew Geissler             }
4790554c984SAndrew Geissler 
4800554c984SAndrew Geissler             // Format should be <IP or Hostname>/<file> for imageURI
481f23b7296SEd Tanous             size_t separator = imageURI.find('/');
4820554c984SAndrew Geissler             if ((separator == std::string::npos) ||
4830554c984SAndrew Geissler                 ((separator + 1) > imageURI.size()))
4840554c984SAndrew Geissler             {
4850554c984SAndrew Geissler                 messages::actionParameterValueTypeError(
4860554c984SAndrew Geissler                     asyncResp->res, imageURI, "ImageURI",
4870554c984SAndrew Geissler                     "UpdateService.SimpleUpdate");
4880554c984SAndrew Geissler                 BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI;
4890554c984SAndrew Geissler                 return;
4900554c984SAndrew Geissler             }
4910554c984SAndrew Geissler 
4920554c984SAndrew Geissler             std::string tftpServer = imageURI.substr(0, separator);
4930554c984SAndrew Geissler             std::string fwFile = imageURI.substr(separator + 1);
4940554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile;
4950554c984SAndrew Geissler 
4960554c984SAndrew Geissler             // Setup callback for when new software detected
4972618d5e3SGunnar Mills             // Give TFTP 10 minutes to complete
4984cde5d90SJames Feist             monitorForSoftwareAvailable(
499d7a596bdSAlbert Zhang                 asyncResp, req,
5004cde5d90SJames Feist                 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate",
5012618d5e3SGunnar Mills                 600);
5020554c984SAndrew Geissler 
5032618d5e3SGunnar Mills             // TFTP can take up to 10 minutes depending on image size and
5040554c984SAndrew Geissler             // connection speed. Return to caller as soon as the TFTP operation
5050554c984SAndrew Geissler             // has been started. The callback above will ensure the activate
5060554c984SAndrew Geissler             // is started once the download has completed
5070554c984SAndrew Geissler             redfish::messages::success(asyncResp->res);
5080554c984SAndrew Geissler 
5090554c984SAndrew Geissler             // Call TFTP service
5100554c984SAndrew Geissler             crow::connections::systemBus->async_method_call(
5110554c984SAndrew Geissler                 [](const boost::system::error_code ec) {
5120554c984SAndrew Geissler                     if (ec)
5130554c984SAndrew Geissler                     {
5140554c984SAndrew Geissler                         // messages::internalError(asyncResp->res);
5150554c984SAndrew Geissler                         cleanUp();
5160554c984SAndrew Geissler                         BMCWEB_LOG_DEBUG << "error_code = " << ec;
5170554c984SAndrew Geissler                         BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
5180554c984SAndrew Geissler                     }
5190554c984SAndrew Geissler                     else
5200554c984SAndrew Geissler                     {
5210554c984SAndrew Geissler                         BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success";
5220554c984SAndrew Geissler                     }
5230554c984SAndrew Geissler                 },
5240554c984SAndrew Geissler                 "xyz.openbmc_project.Software.Download",
5257e860f15SJohn Edward Broadbent                 "/xyz/openbmc_project/software",
5267e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Common.TFTP", "DownloadViaTFTP", fwFile,
5277e860f15SJohn Edward Broadbent                 tftpServer);
5280554c984SAndrew Geissler 
5290554c984SAndrew Geissler             BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost";
5307e860f15SJohn Edward Broadbent         });
531729dae72SJennifer Lee }
532729dae72SJennifer Lee 
5337e860f15SJohn Edward Broadbent inline void requestRoutesUpdateService(App& app)
5341abe55efSEd Tanous {
5357e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
536ed398213SEd Tanous         .privileges(redfish::privileges::getUpdateService)
5377e860f15SJohn Edward Broadbent         .methods(
5387e860f15SJohn Edward Broadbent             boost::beast::http::verb::
5397e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
5407e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
5418d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.type"] =
5420588a3b9SChicago Duan                 "#UpdateService.v1_5_0.UpdateService";
5438d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
5448d1b46d7Szhanghch05             asyncResp->res.jsonValue["Id"] = "UpdateService";
5457e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
5467e860f15SJohn Edward Broadbent                 "Service for Software Update";
5478d1b46d7Szhanghch05             asyncResp->res.jsonValue["Name"] = "Update Service";
5487e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["HttpPushUri"] =
5497e860f15SJohn Edward Broadbent                 "/redfish/v1/UpdateService";
5500f74e643SEd Tanous             // UpdateService cannot be disabled
5518d1b46d7Szhanghch05             asyncResp->res.jsonValue["ServiceEnabled"] = true;
5528d1b46d7Szhanghch05             asyncResp->res.jsonValue["FirmwareInventory"] = {
5530f74e643SEd Tanous                 {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}};
554d61e5194STejas Patil             // Get the MaxImageSizeBytes
555d61e5194STejas Patil             asyncResp->res.jsonValue["MaxImageSizeBytes"] =
556d61e5194STejas Patil                 bmcwebHttpReqBodyLimitMb * 1024 * 1024;
557d61e5194STejas Patil 
5580554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
5590554c984SAndrew Geissler             // Update Actions object.
5600554c984SAndrew Geissler             nlohmann::json& updateSvcSimpleUpdate =
5617e860f15SJohn Edward Broadbent                 asyncResp->res
5627e860f15SJohn Edward Broadbent                     .jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
5630554c984SAndrew Geissler             updateSvcSimpleUpdate["target"] =
5640554c984SAndrew Geissler                 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
5657e860f15SJohn Edward Broadbent             updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] =
5667e860f15SJohn Edward Broadbent                 {"TFTP"};
5670554c984SAndrew Geissler #endif
568274dfe62SJayashankar Padath             // Get the current ApplyTime value
5691e1e598dSJonathan Doman             sdbusplus::asio::getProperty<std::string>(
5701e1e598dSJonathan Doman                 *crow::connections::systemBus, "xyz.openbmc_project.Settings",
5711e1e598dSJonathan Doman                 "/xyz/openbmc_project/software/apply_time",
5721e1e598dSJonathan Doman                 "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime",
5738d1b46d7Szhanghch05                 [asyncResp](const boost::system::error_code ec,
5741e1e598dSJonathan Doman                             const std::string& applyTime) {
575274dfe62SJayashankar Padath                     if (ec)
576274dfe62SJayashankar Padath                     {
577274dfe62SJayashankar Padath                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
5788d1b46d7Szhanghch05                         messages::internalError(asyncResp->res);
579274dfe62SJayashankar Padath                         return;
580274dfe62SJayashankar Padath                     }
581274dfe62SJayashankar Padath 
582274dfe62SJayashankar Padath                     // Store the ApplyTime Value
5831e1e598dSJonathan Doman                     if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
5841e1e598dSJonathan Doman                                      "RequestedApplyTimes.Immediate")
585274dfe62SJayashankar Padath                     {
5868d1b46d7Szhanghch05                         asyncResp->res
5877e860f15SJohn Edward Broadbent                             .jsonValue["HttpPushUriOptions"]
5887e860f15SJohn Edward Broadbent                                       ["HttpPushUriApplyTime"]["ApplyTime"] =
5897e860f15SJohn Edward Broadbent                             "Immediate";
590274dfe62SJayashankar Padath                     }
5911e1e598dSJonathan Doman                     else if (applyTime ==
5921e1e598dSJonathan Doman                              "xyz.openbmc_project.Software.ApplyTime."
5931e1e598dSJonathan Doman                              "RequestedApplyTimes.OnReset")
594274dfe62SJayashankar Padath                     {
5958d1b46d7Szhanghch05                         asyncResp->res
5967e860f15SJohn Edward Broadbent                             .jsonValue["HttpPushUriOptions"]
5977e860f15SJohn Edward Broadbent                                       ["HttpPushUriApplyTime"]["ApplyTime"] =
5987e860f15SJohn Edward Broadbent                             "OnReset";
599274dfe62SJayashankar Padath                     }
6001e1e598dSJonathan Doman                 });
6017e860f15SJohn Edward Broadbent         });
6027e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
603ed398213SEd Tanous         .privileges(redfish::privileges::patchUpdateService)
6040fda0f12SGeorge Liu         .methods(
6050fda0f12SGeorge Liu             boost::beast::http::verb::
6060fda0f12SGeorge Liu                 patch)([](const crow::Request& req,
6077e860f15SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
608fa1a5a38SJayashankar Padath             BMCWEB_LOG_DEBUG << "doPatch...";
609fa1a5a38SJayashankar Padath 
610274dfe62SJayashankar Padath             std::optional<nlohmann::json> pushUriOptions;
61115ed6780SWilly Tu             if (!json_util::readJsonPatch(req, asyncResp->res,
61215ed6780SWilly Tu                                           "HttpPushUriOptions", pushUriOptions))
613fa1a5a38SJayashankar Padath             {
614fa1a5a38SJayashankar Padath                 return;
615fa1a5a38SJayashankar Padath             }
616fa1a5a38SJayashankar Padath 
617274dfe62SJayashankar Padath             if (pushUriOptions)
618274dfe62SJayashankar Padath             {
619274dfe62SJayashankar Padath                 std::optional<nlohmann::json> pushUriApplyTime;
6208d1b46d7Szhanghch05                 if (!json_util::readJson(*pushUriOptions, asyncResp->res,
6217e860f15SJohn Edward Broadbent                                          "HttpPushUriApplyTime",
6227e860f15SJohn Edward Broadbent                                          pushUriApplyTime))
623274dfe62SJayashankar Padath                 {
624274dfe62SJayashankar Padath                     return;
625274dfe62SJayashankar Padath                 }
626274dfe62SJayashankar Padath 
627274dfe62SJayashankar Padath                 if (pushUriApplyTime)
628274dfe62SJayashankar Padath                 {
629274dfe62SJayashankar Padath                     std::optional<std::string> applyTime;
6300fda0f12SGeorge Liu                     if (!json_util::readJson(*pushUriApplyTime, asyncResp->res,
6310fda0f12SGeorge Liu                                              "ApplyTime", applyTime))
632274dfe62SJayashankar Padath                     {
633274dfe62SJayashankar Padath                         return;
634274dfe62SJayashankar Padath                     }
635274dfe62SJayashankar Padath 
636274dfe62SJayashankar Padath                     if (applyTime)
637fa1a5a38SJayashankar Padath                     {
638fa1a5a38SJayashankar Padath                         std::string applyTimeNewVal;
639fa1a5a38SJayashankar Padath                         if (applyTime == "Immediate")
640fa1a5a38SJayashankar Padath                         {
641274dfe62SJayashankar Padath                             applyTimeNewVal =
6420fda0f12SGeorge Liu                                 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate";
643fa1a5a38SJayashankar Padath                         }
644274dfe62SJayashankar Padath                         else if (applyTime == "OnReset")
645274dfe62SJayashankar Padath                         {
646274dfe62SJayashankar Padath                             applyTimeNewVal =
6470fda0f12SGeorge Liu                                 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
648274dfe62SJayashankar Padath                         }
649fa1a5a38SJayashankar Padath                         else
650fa1a5a38SJayashankar Padath                         {
651274dfe62SJayashankar Padath                             BMCWEB_LOG_INFO
6520fda0f12SGeorge Liu                                 << "ApplyTime value is not in the list of acceptable values";
653274dfe62SJayashankar Padath                             messages::propertyValueNotInList(
654274dfe62SJayashankar Padath                                 asyncResp->res, *applyTime, "ApplyTime");
655274dfe62SJayashankar Padath                             return;
656fa1a5a38SJayashankar Padath                         }
657fa1a5a38SJayashankar Padath 
658fa1a5a38SJayashankar Padath                         // Set the requested image apply time value
659fa1a5a38SJayashankar Padath                         crow::connections::systemBus->async_method_call(
6600fda0f12SGeorge Liu                             [asyncResp](const boost::system::error_code ec) {
661fa1a5a38SJayashankar Padath                                 if (ec)
662fa1a5a38SJayashankar Padath                                 {
6637e860f15SJohn Edward Broadbent                                     BMCWEB_LOG_ERROR
6647e860f15SJohn Edward Broadbent                                         << "D-Bus responses error: " << ec;
665fa1a5a38SJayashankar Padath                                     messages::internalError(asyncResp->res);
666fa1a5a38SJayashankar Padath                                     return;
667fa1a5a38SJayashankar Padath                                 }
668fa1a5a38SJayashankar Padath                                 messages::success(asyncResp->res);
669fa1a5a38SJayashankar Padath                             },
670fa1a5a38SJayashankar Padath                             "xyz.openbmc_project.Settings",
671fa1a5a38SJayashankar Padath                             "/xyz/openbmc_project/software/apply_time",
672fa1a5a38SJayashankar Padath                             "org.freedesktop.DBus.Properties", "Set",
673274dfe62SJayashankar Padath                             "xyz.openbmc_project.Software.ApplyTime",
674274dfe62SJayashankar Padath                             "RequestedApplyTime",
675168e20c1SEd Tanous                             dbus::utility::DbusVariantType{applyTimeNewVal});
676fa1a5a38SJayashankar Padath                     }
677274dfe62SJayashankar Padath                 }
678fa1a5a38SJayashankar Padath             }
6797e860f15SJohn Edward Broadbent         });
6807e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
681ed398213SEd Tanous         .privileges(redfish::privileges::postUpdateService)
6827e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
6837e860f15SJohn Edward Broadbent             [](const crow::Request& req,
6847e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
685acb7cfb4SJennifer Lee                 BMCWEB_LOG_DEBUG << "doPost...";
686acb7cfb4SJennifer Lee 
68786adcd6dSAndrew Geissler                 // Setup callback for when new software detected
6884cde5d90SJames Feist                 monitorForSoftwareAvailable(asyncResp, req,
6894cde5d90SJames Feist                                             "/redfish/v1/UpdateService");
690acb7cfb4SJennifer Lee 
6917e860f15SJohn Edward Broadbent                 std::string filepath("/tmp/images/" +
6927e860f15SJohn Edward Broadbent                                      boost::uuids::to_string(
6937e860f15SJohn Edward Broadbent                                          boost::uuids::random_generator()()));
694acb7cfb4SJennifer Lee                 BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
6957e860f15SJohn Edward Broadbent                 std::ofstream out(filepath, std::ofstream::out |
6967e860f15SJohn Edward Broadbent                                                 std::ofstream::binary |
697acb7cfb4SJennifer Lee                                                 std::ofstream::trunc);
698acb7cfb4SJennifer Lee                 out << req.body;
699acb7cfb4SJennifer Lee                 out.close();
700acb7cfb4SJennifer Lee                 BMCWEB_LOG_DEBUG << "file upload complete!!";
7017e860f15SJohn Edward Broadbent             });
702729dae72SJennifer Lee }
703729dae72SJennifer Lee 
7047e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventoryCollection(App& app)
7051abe55efSEd Tanous {
7067e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/")
707ed398213SEd Tanous         .privileges(redfish::privileges::getSoftwareInventoryCollection)
7080fda0f12SGeorge Liu         .methods(
7090fda0f12SGeorge Liu             boost::beast::http::verb::
7100fda0f12SGeorge Liu                 get)([](const crow::Request&,
7117e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
7128d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.type"] =
7130f74e643SEd Tanous                 "#SoftwareInventoryCollection.SoftwareInventoryCollection";
7148d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] =
7150f74e643SEd Tanous                 "/redfish/v1/UpdateService/FirmwareInventory";
7160fda0f12SGeorge Liu             asyncResp->res.jsonValue["Name"] = "Software Inventory Collection";
717c711bf86SEd Tanous 
718c711bf86SEd Tanous             crow::connections::systemBus->async_method_call(
719c711bf86SEd Tanous                 [asyncResp](
720c711bf86SEd Tanous                     const boost::system::error_code ec,
721*b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
7221abe55efSEd Tanous                     if (ec)
7231abe55efSEd Tanous                     {
724f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
7256c4eb9deSJennifer Lee                         return;
726729dae72SJennifer Lee                     }
7277e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"] =
7287e860f15SJohn Edward Broadbent                         nlohmann::json::array();
729c711bf86SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] = 0;
7306c4eb9deSJennifer Lee 
7319eb808c1SEd Tanous                     for (const auto& obj : subtree)
7321abe55efSEd Tanous                     {
7332dfd18efSEd Tanous                         sdbusplus::message::object_path path(obj.first);
7342dfd18efSEd Tanous                         std::string swId = path.filename();
7352dfd18efSEd Tanous                         if (swId.empty())
736f4b65ab1SJennifer Lee                         {
737f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
738f4b65ab1SJennifer Lee                             BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
739f4b65ab1SJennifer Lee                             return;
740f4b65ab1SJennifer Lee                         }
741f4b65ab1SJennifer Lee 
742c711bf86SEd Tanous                         nlohmann::json& members =
743c711bf86SEd Tanous                             asyncResp->res.jsonValue["Members"];
744c711bf86SEd Tanous                         members.push_back(
7450fda0f12SGeorge Liu                             {{"@odata.id",
7460fda0f12SGeorge Liu                               "/redfish/v1/UpdateService/FirmwareInventory/" +
747f4b65ab1SJennifer Lee                                   swId}});
748e0dd8057SAndrew Geissler                         asyncResp->res.jsonValue["Members@odata.count"] =
749c711bf86SEd Tanous                             members.size();
7506c4eb9deSJennifer Lee                     }
751c711bf86SEd Tanous                 },
7527e860f15SJohn Edward Broadbent                 // Note that only firmware levels associated with a device
7537e860f15SJohn Edward Broadbent                 // are stored under /xyz/openbmc_project/software therefore
7547e860f15SJohn Edward Broadbent                 // to ensure only real FirmwareInventory items are returned,
7557e860f15SJohn Edward Broadbent                 // this full object path must be used here as input to
7567e860f15SJohn Edward Broadbent                 // mapper
757c711bf86SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
758c711bf86SEd Tanous                 "/xyz/openbmc_project/object_mapper",
7592830a9cfSAndrew Geissler                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
7602830a9cfSAndrew Geissler                 "/xyz/openbmc_project/software", static_cast<int32_t>(0),
7617e860f15SJohn Edward Broadbent                 std::array<const char*, 1>{
7627e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Software.Version"});
7637e860f15SJohn Edward Broadbent         });
764729dae72SJennifer Lee }
76587d84729SAndrew Geissler /* Fill related item links (i.e. bmc, bios) in for inventory */
7667e860f15SJohn Edward Broadbent inline static void
7677e860f15SJohn Edward Broadbent     getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
76887d84729SAndrew Geissler                     const std::string& purpose)
76987d84729SAndrew Geissler {
77087d84729SAndrew Geissler     if (purpose == fw_util::bmcPurpose)
77187d84729SAndrew Geissler     {
7725af690f5SAbhishek Patel         nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
7735af690f5SAbhishek Patel         relatedItem.push_back({{"@odata.id", "/redfish/v1/Managers/bmc"}});
7747e860f15SJohn Edward Broadbent         aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
77587d84729SAndrew Geissler     }
77687d84729SAndrew Geissler     else if (purpose == fw_util::biosPurpose)
77787d84729SAndrew Geissler     {
7785af690f5SAbhishek Patel         nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"];
7795af690f5SAbhishek Patel         relatedItem.push_back(
780f723d733SGunnar Mills             {{"@odata.id", "/redfish/v1/Systems/system/Bios"}});
7811a6e51aeSJiaqing Zhao         aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size();
78287d84729SAndrew Geissler     }
78387d84729SAndrew Geissler     else
78487d84729SAndrew Geissler     {
78587d84729SAndrew Geissler         BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
78687d84729SAndrew Geissler     }
78787d84729SAndrew Geissler }
78887d84729SAndrew Geissler 
7897e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventory(App& app)
7901abe55efSEd Tanous {
7917e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/")
792ed398213SEd Tanous         .privileges(redfish::privileges::getSoftwareInventory)
7937e860f15SJohn Edward Broadbent         .methods(
7947e860f15SJohn Edward Broadbent             boost::beast::http::verb::get)([](const crow::Request&,
7957e860f15SJohn Edward Broadbent                                               const std::shared_ptr<
7967e860f15SJohn Edward Broadbent                                                   bmcweb::AsyncResp>& asyncResp,
7977e860f15SJohn Edward Broadbent                                               const std::string& param) {
7983ae837c9SEd Tanous             std::shared_ptr<std::string> swId =
7997e860f15SJohn Edward Broadbent                 std::make_shared<std::string>(param);
800c711bf86SEd Tanous 
8018d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] =
8023ae837c9SEd Tanous                 "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
803c711bf86SEd Tanous 
804c711bf86SEd Tanous             crow::connections::systemBus->async_method_call(
805*b9d36b47SEd Tanous                 [asyncResp,
806*b9d36b47SEd Tanous                  swId](const boost::system::error_code ec,
807*b9d36b47SEd Tanous                        const dbus::utility::MapperGetSubTreeResponse& subtree) {
80855c7b7a2SEd Tanous                     BMCWEB_LOG_DEBUG << "doGet callback...";
8091abe55efSEd Tanous                     if (ec)
8101abe55efSEd Tanous                     {
811f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
8126c4eb9deSJennifer Lee                         return;
8136c4eb9deSJennifer Lee                     }
8146c4eb9deSJennifer Lee 
8156913228dSAndrew Geissler                     // Ensure we find our input swId, otherwise return an error
8166913228dSAndrew Geissler                     bool found = false;
8171abe55efSEd Tanous                     for (const std::pair<
8181abe55efSEd Tanous                              std::string,
8197e860f15SJohn Edward Broadbent                              std::vector<std::pair<
8207e860f15SJohn Edward Broadbent                                  std::string, std::vector<std::string>>>>& obj :
8217e860f15SJohn Edward Broadbent                          subtree)
8221abe55efSEd Tanous                     {
823e05aec50SEd Tanous                         if (!boost::ends_with(obj.first, *swId))
8241abe55efSEd Tanous                         {
825acb7cfb4SJennifer Lee                             continue;
826acb7cfb4SJennifer Lee                         }
827acb7cfb4SJennifer Lee 
82826f6976fSEd Tanous                         if (obj.second.empty())
8291abe55efSEd Tanous                         {
830acb7cfb4SJennifer Lee                             continue;
831acb7cfb4SJennifer Lee                         }
8326c4eb9deSJennifer Lee 
8336913228dSAndrew Geissler                         found = true;
8347e860f15SJohn Edward Broadbent                         fw_util::getFwStatus(asyncResp, swId,
8357e860f15SJohn Edward Broadbent                                              obj.second[0].first);
836e0dd8057SAndrew Geissler 
83755c7b7a2SEd Tanous                         crow::connections::systemBus->async_method_call(
838168e20c1SEd Tanous                             [asyncResp,
839168e20c1SEd Tanous                              swId](const boost::system::error_code errorCode,
840*b9d36b47SEd Tanous                                    const dbus::utility::DBusPropertiesMap&
841168e20c1SEd Tanous                                        propertiesList) {
84281ce609eSEd Tanous                                 if (errorCode)
8431abe55efSEd Tanous                                 {
844f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
8456c4eb9deSJennifer Lee                                     return;
8466c4eb9deSJennifer Lee                                 }
847*b9d36b47SEd Tanous                                 const std::string* swInvPurpose = nullptr;
848*b9d36b47SEd Tanous                                 const std::string* version = nullptr;
849*b9d36b47SEd Tanous                                 for (const auto& property : propertiesList)
8501abe55efSEd Tanous                                 {
851*b9d36b47SEd Tanous                                     if (property.first == "Purpose")
852*b9d36b47SEd Tanous                                     {
853*b9d36b47SEd Tanous                                         swInvPurpose = std::get_if<std::string>(
854*b9d36b47SEd Tanous                                             &property.second);
8556c4eb9deSJennifer Lee                                     }
856*b9d36b47SEd Tanous                                     if (property.first == "Version")
857*b9d36b47SEd Tanous                                     {
858*b9d36b47SEd Tanous                                         version = std::get_if<std::string>(
859*b9d36b47SEd Tanous                                             &property.second);
860*b9d36b47SEd Tanous                                     }
861*b9d36b47SEd Tanous                                 }
862*b9d36b47SEd Tanous 
8633ae837c9SEd Tanous                                 if (swInvPurpose == nullptr)
8641abe55efSEd Tanous                                 {
8650fda0f12SGeorge Liu                                     BMCWEB_LOG_DEBUG
866*b9d36b47SEd Tanous                                         << "Can't find property \"Purpose\"!";
867*b9d36b47SEd Tanous                                     messages::internalError(asyncResp->res);
868acb7cfb4SJennifer Lee                                     return;
869acb7cfb4SJennifer Lee                                 }
870c711bf86SEd Tanous 
8713ae837c9SEd Tanous                                 BMCWEB_LOG_DEBUG << "swInvPurpose = "
8723ae837c9SEd Tanous                                                  << *swInvPurpose;
873f4b65ab1SJennifer Lee 
874f4b65ab1SJennifer Lee                                 if (version == nullptr)
8751abe55efSEd Tanous                                 {
8761abe55efSEd Tanous                                     BMCWEB_LOG_DEBUG
8771abe55efSEd Tanous                                         << "Can't find property \"Version\"!";
878f12894f8SJason M. Bills 
879*b9d36b47SEd Tanous                                     messages::internalError(asyncResp->res);
880*b9d36b47SEd Tanous 
8816c4eb9deSJennifer Lee                                     return;
8826c4eb9deSJennifer Lee                                 }
883c711bf86SEd Tanous                                 asyncResp->res.jsonValue["Version"] = *version;
8843ae837c9SEd Tanous                                 asyncResp->res.jsonValue["Id"] = *swId;
88554daabe7SAndrew Geissler 
88654daabe7SAndrew Geissler                                 // swInvPurpose is of format:
88754daabe7SAndrew Geissler                                 // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
888e2e96770SJames Feist                                 // Translate this to "ABC image"
889f23b7296SEd Tanous                                 size_t endDesc = swInvPurpose->rfind('.');
89054daabe7SAndrew Geissler                                 if (endDesc == std::string::npos)
89154daabe7SAndrew Geissler                                 {
89254daabe7SAndrew Geissler                                     messages::internalError(asyncResp->res);
89354daabe7SAndrew Geissler                                     return;
89454daabe7SAndrew Geissler                                 }
89554daabe7SAndrew Geissler                                 endDesc++;
89654daabe7SAndrew Geissler                                 if (endDesc >= swInvPurpose->size())
89754daabe7SAndrew Geissler                                 {
89854daabe7SAndrew Geissler                                     messages::internalError(asyncResp->res);
89954daabe7SAndrew Geissler                                     return;
90054daabe7SAndrew Geissler                                 }
90154daabe7SAndrew Geissler 
90254daabe7SAndrew Geissler                                 std::string formatDesc =
90354daabe7SAndrew Geissler                                     swInvPurpose->substr(endDesc);
90454daabe7SAndrew Geissler                                 asyncResp->res.jsonValue["Description"] =
905e2e96770SJames Feist                                     formatDesc + " image";
90687d84729SAndrew Geissler                                 getRelatedItems(asyncResp, *swInvPurpose);
9076c4eb9deSJennifer Lee                             },
908c711bf86SEd Tanous                             obj.second[0].first, obj.first,
909c711bf86SEd Tanous                             "org.freedesktop.DBus.Properties", "GetAll",
910c711bf86SEd Tanous                             "xyz.openbmc_project.Software.Version");
9116c4eb9deSJennifer Lee                     }
9126913228dSAndrew Geissler                     if (!found)
9136913228dSAndrew Geissler                     {
9148cc8edecSEd Tanous                         BMCWEB_LOG_ERROR << "Input swID " << *swId
9158cc8edecSEd Tanous                                          << " not found!";
9166913228dSAndrew Geissler                         messages::resourceMissingAtURI(
9176913228dSAndrew Geissler                             asyncResp->res,
918ace85d60SEd Tanous                             crow::utility::urlFromPieces(
919ace85d60SEd Tanous                                 "redfish", "v1", "UpdateService",
920ace85d60SEd Tanous                                 "FirmwareInventory", *swId));
9216913228dSAndrew Geissler                         return;
9226913228dSAndrew Geissler                     }
9234e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["@odata.type"] =
9244e68c45bSAyushi Smriti                         "#SoftwareInventory.v1_1_0.SoftwareInventory";
9254e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["Name"] = "Software Inventory";
9264e68c45bSAyushi Smriti                     asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
9273f8a743aSAppaRao Puli 
9283f8a743aSAppaRao Puli                     asyncResp->res.jsonValue["Updateable"] = false;
9293f8a743aSAppaRao Puli                     fw_util::getFwUpdateableStatus(asyncResp, swId);
930c711bf86SEd Tanous                 },
931c711bf86SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
932c711bf86SEd Tanous                 "/xyz/openbmc_project/object_mapper",
933271584abSEd Tanous                 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
934271584abSEd Tanous                 static_cast<int32_t>(0),
9357e860f15SJohn Edward Broadbent                 std::array<const char*, 1>{
9367e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Software.Version"});
9377e860f15SJohn Edward Broadbent         });
9386c4eb9deSJennifer Lee }
939729dae72SJennifer Lee 
940729dae72SJennifer Lee } // namespace redfish
941