1729dae72SJennifer Lee /* 2729dae72SJennifer Lee // Copyright (c) 2018 Intel Corporation 3729dae72SJennifer Lee // 4729dae72SJennifer Lee // Licensed under the Apache License, Version 2.0 (the "License"); 5729dae72SJennifer Lee // you may not use this file except in compliance with the License. 6729dae72SJennifer Lee // You may obtain a copy of the License at 7729dae72SJennifer Lee // 8729dae72SJennifer Lee // http://www.apache.org/licenses/LICENSE-2.0 9729dae72SJennifer Lee // 10729dae72SJennifer Lee // Unless required by applicable law or agreed to in writing, software 11729dae72SJennifer Lee // distributed under the License is distributed on an "AS IS" BASIS, 12729dae72SJennifer Lee // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13729dae72SJennifer Lee // See the License for the specific language governing permissions and 14729dae72SJennifer Lee // limitations under the License. 15729dae72SJennifer Lee */ 16729dae72SJennifer Lee #pragma once 17729dae72SJennifer Lee 18d61e5194STejas Patil #include "bmcweb_config.h" 19d61e5194STejas Patil 207e860f15SJohn Edward Broadbent #include <app.hpp> 21168e20c1SEd Tanous #include <dbus_utility.hpp> 2245ca1b86SEd Tanous #include <query.hpp> 23ed398213SEd Tanous #include <registries/privilege_registry.hpp> 241e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp> 25d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp> 26d1bde9e5SKrzysztof Grobelny #include <utils/dbus_utils.hpp> 27eee0013eSWilly Tu #include <utils/sw_utils.hpp> 281214b7e7SGunnar Mills 291abe55efSEd Tanous namespace redfish 301abe55efSEd Tanous { 3127826b5fSEd Tanous 320e7de46fSAndrew Geissler // Match signals added on software path 33*cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 3459d494eeSPatrick Williams static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateMatcher; 35*cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 3659d494eeSPatrick Williams static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateErrorMatcher; 370e7de46fSAndrew Geissler // Only allow one update at a time 38*cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 390e7de46fSAndrew Geissler static bool fwUpdateInProgress = false; 4086adcd6dSAndrew Geissler // Timer for software available 41*cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 42271584abSEd Tanous static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer; 4386adcd6dSAndrew Geissler 447e860f15SJohn Edward Broadbent inline static void cleanUp() 4586adcd6dSAndrew Geissler { 4686adcd6dSAndrew Geissler fwUpdateInProgress = false; 4786adcd6dSAndrew Geissler fwUpdateMatcher = nullptr; 484cde5d90SJames Feist fwUpdateErrorMatcher = nullptr; 4986adcd6dSAndrew Geissler } 507e860f15SJohn Edward Broadbent inline static void activateImage(const std::string& objPath, 5186adcd6dSAndrew Geissler const std::string& service) 5286adcd6dSAndrew Geissler { 5386adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "Activate image for " << objPath << " " << service; 5486adcd6dSAndrew Geissler crow::connections::systemBus->async_method_call( 5581ce609eSEd Tanous [](const boost::system::error_code errorCode) { 5681ce609eSEd Tanous if (errorCode) 5786adcd6dSAndrew Geissler { 5881ce609eSEd Tanous BMCWEB_LOG_DEBUG << "error_code = " << errorCode; 5981ce609eSEd Tanous BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message(); 6086adcd6dSAndrew Geissler } 6186adcd6dSAndrew Geissler }, 6286adcd6dSAndrew Geissler service, objPath, "org.freedesktop.DBus.Properties", "Set", 6386adcd6dSAndrew Geissler "xyz.openbmc_project.Software.Activation", "RequestedActivation", 64168e20c1SEd Tanous dbus::utility::DbusVariantType( 650fda0f12SGeorge Liu "xyz.openbmc_project.Software.Activation.RequestedActivations.Active")); 6686adcd6dSAndrew Geissler } 670554c984SAndrew Geissler 680554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr 690554c984SAndrew Geissler // then no asyncResp updates will occur 708d1b46d7Szhanghch05 static void 718d1b46d7Szhanghch05 softwareInterfaceAdded(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 7259d494eeSPatrick Williams sdbusplus::message_t& m, task::Payload&& payload) 7386adcd6dSAndrew Geissler { 74b9d36b47SEd Tanous dbus::utility::DBusInteracesMap interfacesProperties; 7586adcd6dSAndrew Geissler 7686adcd6dSAndrew Geissler sdbusplus::message::object_path objPath; 7786adcd6dSAndrew Geissler 7886adcd6dSAndrew Geissler m.read(objPath, interfacesProperties); 7986adcd6dSAndrew Geissler 8086adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "obj path = " << objPath.str; 81e3eb3d63SEd Tanous for (const auto& interface : interfacesProperties) 8286adcd6dSAndrew Geissler { 8386adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "interface = " << interface.first; 8486adcd6dSAndrew Geissler 8586adcd6dSAndrew Geissler if (interface.first == "xyz.openbmc_project.Software.Activation") 8686adcd6dSAndrew Geissler { 8786adcd6dSAndrew Geissler // Retrieve service and activate 8886adcd6dSAndrew Geissler crow::connections::systemBus->async_method_call( 89a3e65892SEd Tanous [objPath, asyncResp, payload(std::move(payload))]( 90a3e65892SEd Tanous const boost::system::error_code errorCode, 91a3e65892SEd Tanous const std::vector< 92a3e65892SEd Tanous std::pair<std::string, std::vector<std::string>>>& 93a3e65892SEd Tanous objInfo) mutable { 9481ce609eSEd Tanous if (errorCode) 9586adcd6dSAndrew Geissler { 9681ce609eSEd Tanous BMCWEB_LOG_DEBUG << "error_code = " << errorCode; 97002d39b4SEd Tanous BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message(); 980554c984SAndrew Geissler if (asyncResp) 990554c984SAndrew Geissler { 10086adcd6dSAndrew Geissler messages::internalError(asyncResp->res); 1010554c984SAndrew Geissler } 10286adcd6dSAndrew Geissler cleanUp(); 10386adcd6dSAndrew Geissler return; 10486adcd6dSAndrew Geissler } 10586adcd6dSAndrew Geissler // Ensure we only got one service back 10686adcd6dSAndrew Geissler if (objInfo.size() != 1) 10786adcd6dSAndrew Geissler { 10886adcd6dSAndrew Geissler BMCWEB_LOG_ERROR << "Invalid Object Size " 10986adcd6dSAndrew Geissler << objInfo.size(); 1100554c984SAndrew Geissler if (asyncResp) 1110554c984SAndrew Geissler { 11286adcd6dSAndrew Geissler messages::internalError(asyncResp->res); 1130554c984SAndrew Geissler } 11486adcd6dSAndrew Geissler cleanUp(); 11586adcd6dSAndrew Geissler return; 11686adcd6dSAndrew Geissler } 11786adcd6dSAndrew Geissler // cancel timer only when 11886adcd6dSAndrew Geissler // xyz.openbmc_project.Software.Activation interface 11986adcd6dSAndrew Geissler // is added 12086adcd6dSAndrew Geissler fwAvailableTimer = nullptr; 12186adcd6dSAndrew Geissler 12286adcd6dSAndrew Geissler activateImage(objPath.str, objInfo[0].first); 1230554c984SAndrew Geissler if (asyncResp) 1240554c984SAndrew Geissler { 12532898ceaSJames Feist std::shared_ptr<task::TaskData> task = 12632898ceaSJames Feist task::TaskData::createTask( 12732898ceaSJames Feist [](boost::system::error_code ec, 12859d494eeSPatrick Williams sdbusplus::message_t& msg, 1291214b7e7SGunnar Mills const std::shared_ptr<task::TaskData>& 1301214b7e7SGunnar Mills taskData) { 13132898ceaSJames Feist if (ec) 13232898ceaSJames Feist { 13332898ceaSJames Feist return task::completed; 13432898ceaSJames Feist } 13532898ceaSJames Feist 13632898ceaSJames Feist std::string iface; 137b9d36b47SEd Tanous dbus::utility::DBusPropertiesMap values; 138fd9ab9e1SJames Feist 139002d39b4SEd Tanous std::string index = std::to_string(taskData->index); 14032898ceaSJames Feist msg.read(iface, values); 141fd9ab9e1SJames Feist 142002d39b4SEd Tanous if (iface == "xyz.openbmc_project.Software.Activation") 143fd9ab9e1SJames Feist { 1440fb5b505SGayathri Leburu const std::string* state = nullptr; 145b9d36b47SEd Tanous for (const auto& property : values) 14632898ceaSJames Feist { 147b9d36b47SEd Tanous if (property.first == "Activation") 148b9d36b47SEd Tanous { 1490fb5b505SGayathri Leburu state = std::get_if<std::string>( 150b9d36b47SEd Tanous &property.second); 1510fb5b505SGayathri Leburu if (state == nullptr) 152b9d36b47SEd Tanous { 153002d39b4SEd Tanous taskData->messages.emplace_back( 154002d39b4SEd Tanous messages::internalError()); 155b9d36b47SEd Tanous return task::completed; 156b9d36b47SEd Tanous } 157b9d36b47SEd Tanous } 158b9d36b47SEd Tanous } 15932898ceaSJames Feist 16032898ceaSJames Feist if (state == nullptr) 16132898ceaSJames Feist { 162b9d36b47SEd Tanous return !task::completed; 16332898ceaSJames Feist } 16432898ceaSJames Feist 16511ba3979SEd Tanous if (state->ends_with("Invalid") || 16611ba3979SEd Tanous state->ends_with("Failed")) 16732898ceaSJames Feist { 16832898ceaSJames Feist taskData->state = "Exception"; 16932898ceaSJames Feist taskData->status = "Warning"; 17032898ceaSJames Feist taskData->messages.emplace_back( 171e5d5006bSJames Feist messages::taskAborted(index)); 17232898ceaSJames Feist return task::completed; 17332898ceaSJames Feist } 17432898ceaSJames Feist 17511ba3979SEd Tanous if (state->ends_with("Staged")) 17632898ceaSJames Feist { 177fd9ab9e1SJames Feist taskData->state = "Stopping"; 178fd9ab9e1SJames Feist taskData->messages.emplace_back( 179fd9ab9e1SJames Feist messages::taskPaused(index)); 180fd9ab9e1SJames Feist 181fd9ab9e1SJames Feist // its staged, set a long timer to 182fd9ab9e1SJames Feist // allow them time to complete the 183fd9ab9e1SJames Feist // update (probably cycle the 184fd9ab9e1SJames Feist // system) if this expires then 185fd9ab9e1SJames Feist // task will be cancelled 186002d39b4SEd Tanous taskData->extendTimer(std::chrono::hours(5)); 18732898ceaSJames Feist return !task::completed; 18832898ceaSJames Feist } 18932898ceaSJames Feist 19011ba3979SEd Tanous if (state->ends_with("Active")) 19132898ceaSJames Feist { 19232898ceaSJames Feist taskData->messages.emplace_back( 193002d39b4SEd Tanous messages::taskCompletedOK(index)); 19432898ceaSJames Feist taskData->state = "Completed"; 19532898ceaSJames Feist return task::completed; 19632898ceaSJames Feist } 197fd9ab9e1SJames Feist } 1980fda0f12SGeorge Liu else if ( 1990fda0f12SGeorge Liu iface == 2000fda0f12SGeorge Liu "xyz.openbmc_project.Software.ActivationProgress") 201fd9ab9e1SJames Feist { 202b9d36b47SEd Tanous 203b9d36b47SEd Tanous const uint8_t* progress = nullptr; 204b9d36b47SEd Tanous for (const auto& property : values) 205fd9ab9e1SJames Feist { 206b9d36b47SEd Tanous if (property.first == "Progress") 207b9d36b47SEd Tanous { 2080fb5b505SGayathri Leburu progress = 2090fb5b505SGayathri Leburu std::get_if<uint8_t>(&property.second); 2100fb5b505SGayathri Leburu if (progress == nullptr) 211b9d36b47SEd Tanous { 212002d39b4SEd Tanous taskData->messages.emplace_back( 213002d39b4SEd Tanous messages::internalError()); 214b9d36b47SEd Tanous return task::completed; 215fd9ab9e1SJames Feist } 216b9d36b47SEd Tanous } 217b9d36b47SEd Tanous } 218fd9ab9e1SJames Feist 219fd9ab9e1SJames Feist if (progress == nullptr) 220fd9ab9e1SJames Feist { 221b9d36b47SEd Tanous return !task::completed; 222fd9ab9e1SJames Feist } 2230fb5b505SGayathri Leburu taskData->percentComplete = *progress; 224fd9ab9e1SJames Feist taskData->messages.emplace_back( 2250fb5b505SGayathri Leburu messages::taskProgressChanged(index, 2260fb5b505SGayathri Leburu *progress)); 227fd9ab9e1SJames Feist 228fd9ab9e1SJames Feist // if we're getting status updates it's 229fd9ab9e1SJames Feist // still alive, update timer 230002d39b4SEd Tanous taskData->extendTimer(std::chrono::minutes(5)); 231fd9ab9e1SJames Feist } 23232898ceaSJames Feist 23332898ceaSJames Feist // as firmware update often results in a 23432898ceaSJames Feist // reboot, the task may never "complete" 23532898ceaSJames Feist // unless it is an error 23632898ceaSJames Feist 23732898ceaSJames Feist return !task::completed; 23832898ceaSJames Feist }, 2390fda0f12SGeorge Liu "type='signal',interface='org.freedesktop.DBus.Properties'," 240fd9ab9e1SJames Feist "member='PropertiesChanged',path='" + 24132898ceaSJames Feist objPath.str + "'"); 24232898ceaSJames Feist task->startTimer(std::chrono::minutes(5)); 24332898ceaSJames Feist task->populateResp(asyncResp->res); 244a3e65892SEd Tanous task->payload.emplace(std::move(payload)); 2450554c984SAndrew Geissler } 24686adcd6dSAndrew Geissler fwUpdateInProgress = false; 24786adcd6dSAndrew Geissler }, 24886adcd6dSAndrew Geissler "xyz.openbmc_project.ObjectMapper", 24986adcd6dSAndrew Geissler "/xyz/openbmc_project/object_mapper", 25086adcd6dSAndrew Geissler "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str, 25186adcd6dSAndrew Geissler std::array<const char*, 1>{ 25286adcd6dSAndrew Geissler "xyz.openbmc_project.Software.Activation"}); 25362bafc01SPatrick Williams 25462bafc01SPatrick Williams break; 25586adcd6dSAndrew Geissler } 25686adcd6dSAndrew Geissler } 25786adcd6dSAndrew Geissler } 25886adcd6dSAndrew Geissler 2590554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr 2600554c984SAndrew Geissler // then no asyncResp updates will occur 261b5a76932SEd Tanous static void monitorForSoftwareAvailable( 2628d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2638d1b46d7Szhanghch05 const crow::Request& req, const std::string& url, 2645d138943SGunnar Mills int timeoutTimeSeconds = 25) 26586adcd6dSAndrew Geissler { 26686adcd6dSAndrew Geissler // Only allow one FW update at a time 267e05aec50SEd Tanous if (fwUpdateInProgress) 26886adcd6dSAndrew Geissler { 2690554c984SAndrew Geissler if (asyncResp) 2700554c984SAndrew Geissler { 27186adcd6dSAndrew Geissler messages::serviceTemporarilyUnavailable(asyncResp->res, "30"); 2720554c984SAndrew Geissler } 27386adcd6dSAndrew Geissler return; 27486adcd6dSAndrew Geissler } 27586adcd6dSAndrew Geissler 2760554c984SAndrew Geissler fwAvailableTimer = 277271584abSEd Tanous std::make_unique<boost::asio::steady_timer>(*req.ioService); 27886adcd6dSAndrew Geissler 279271584abSEd Tanous fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds)); 28086adcd6dSAndrew Geissler 28186adcd6dSAndrew Geissler fwAvailableTimer->async_wait( 28286adcd6dSAndrew Geissler [asyncResp](const boost::system::error_code& ec) { 28386adcd6dSAndrew Geissler cleanUp(); 28486adcd6dSAndrew Geissler if (ec == boost::asio::error::operation_aborted) 28586adcd6dSAndrew Geissler { 28686adcd6dSAndrew Geissler // expected, we were canceled before the timer completed. 28786adcd6dSAndrew Geissler return; 28886adcd6dSAndrew Geissler } 28986adcd6dSAndrew Geissler BMCWEB_LOG_ERROR 29086adcd6dSAndrew Geissler << "Timed out waiting for firmware object being created"; 291002d39b4SEd Tanous BMCWEB_LOG_ERROR << "FW image may has already been uploaded to server"; 29286adcd6dSAndrew Geissler if (ec) 29386adcd6dSAndrew Geissler { 29486adcd6dSAndrew Geissler BMCWEB_LOG_ERROR << "Async_wait failed" << ec; 29586adcd6dSAndrew Geissler return; 29686adcd6dSAndrew Geissler } 2970554c984SAndrew Geissler if (asyncResp) 2980554c984SAndrew Geissler { 29986adcd6dSAndrew Geissler redfish::messages::internalError(asyncResp->res); 3000554c984SAndrew Geissler } 30186adcd6dSAndrew Geissler }); 302a3e65892SEd Tanous task::Payload payload(req); 30359d494eeSPatrick Williams auto callback = [asyncResp, payload](sdbusplus::message_t& m) mutable { 30486adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "Match fired"; 305a3e65892SEd Tanous softwareInterfaceAdded(asyncResp, m, std::move(payload)); 30686adcd6dSAndrew Geissler }; 30786adcd6dSAndrew Geissler 30886adcd6dSAndrew Geissler fwUpdateInProgress = true; 30986adcd6dSAndrew Geissler 31059d494eeSPatrick Williams fwUpdateMatcher = std::make_unique<sdbusplus::bus::match_t>( 31186adcd6dSAndrew Geissler *crow::connections::systemBus, 31286adcd6dSAndrew Geissler "interface='org.freedesktop.DBus.ObjectManager',type='signal'," 31386adcd6dSAndrew Geissler "member='InterfacesAdded',path='/xyz/openbmc_project/software'", 31486adcd6dSAndrew Geissler callback); 3154cde5d90SJames Feist 31659d494eeSPatrick Williams fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match_t>( 3174cde5d90SJames Feist *crow::connections::systemBus, 318e1cc4828SBrian Ma "interface='org.freedesktop.DBus.ObjectManager',type='signal'," 319e1cc4828SBrian Ma "member='InterfacesAdded'," 320e1cc4828SBrian Ma "path='/xyz/openbmc_project/logging'", 32159d494eeSPatrick Williams [asyncResp, url](sdbusplus::message_t& m) { 322002d39b4SEd Tanous std::vector<std::pair<std::string, dbus::utility::DBusPropertiesMap>> 323e1cc4828SBrian Ma interfacesProperties; 324e1cc4828SBrian Ma sdbusplus::message::object_path objPath; 325e1cc4828SBrian Ma m.read(objPath, interfacesProperties); 326e1cc4828SBrian Ma BMCWEB_LOG_DEBUG << "obj path = " << objPath.str; 327e1cc4828SBrian Ma for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>& 328e1cc4828SBrian Ma interface : interfacesProperties) 329e1cc4828SBrian Ma { 330e1cc4828SBrian Ma if (interface.first == "xyz.openbmc_project.Logging.Entry") 331e1cc4828SBrian Ma { 332711ac7a9SEd Tanous for (const std::pair<std::string, 333002d39b4SEd Tanous dbus::utility::DbusVariantType>& value : 334002d39b4SEd Tanous interface.second) 3354cde5d90SJames Feist { 336711ac7a9SEd Tanous if (value.first != "Message") 337711ac7a9SEd Tanous { 338711ac7a9SEd Tanous continue; 3394cde5d90SJames Feist } 340e1cc4828SBrian Ma const std::string* type = 341711ac7a9SEd Tanous std::get_if<std::string>(&value.second); 3424cde5d90SJames Feist if (type == nullptr) 3434cde5d90SJames Feist { 344e1cc4828SBrian Ma // if this was our message, timeout will cover it 3454cde5d90SJames Feist return; 3464cde5d90SJames Feist } 347e1cc4828SBrian Ma fwAvailableTimer = nullptr; 3484cde5d90SJames Feist if (*type == 3494cde5d90SJames Feist "xyz.openbmc_project.Software.Image.Error.UnTarFailure") 3504cde5d90SJames Feist { 351002d39b4SEd Tanous redfish::messages::invalidUpload(asyncResp->res, url, 352002d39b4SEd Tanous "Invalid archive"); 3534cde5d90SJames Feist } 354e1cc4828SBrian Ma else if (*type == 355e1cc4828SBrian Ma "xyz.openbmc_project.Software.Image.Error." 356e1cc4828SBrian Ma "ManifestFileFailure") 3574cde5d90SJames Feist { 358002d39b4SEd Tanous redfish::messages::invalidUpload(asyncResp->res, url, 359002d39b4SEd Tanous "Invalid manifest"); 3604cde5d90SJames Feist } 361e1cc4828SBrian Ma else if ( 362e1cc4828SBrian Ma *type == 3634cde5d90SJames Feist "xyz.openbmc_project.Software.Image.Error.ImageFailure") 3644cde5d90SJames Feist { 365e1cc4828SBrian Ma redfish::messages::invalidUpload( 366e1cc4828SBrian Ma asyncResp->res, url, "Invalid image format"); 3674cde5d90SJames Feist } 368e1cc4828SBrian Ma else if ( 369e1cc4828SBrian Ma *type == 3700fda0f12SGeorge Liu "xyz.openbmc_project.Software.Version.Error.AlreadyExists") 37188b3dd12SGunnar Mills { 37288b3dd12SGunnar Mills redfish::messages::invalidUpload( 373e1cc4828SBrian Ma asyncResp->res, url, 374e1cc4828SBrian Ma "Image version already exists"); 37588b3dd12SGunnar Mills 37688b3dd12SGunnar Mills redfish::messages::resourceAlreadyExists( 377d8a5d5d8SJiaqing Zhao asyncResp->res, "UpdateService", "Version", 378e1cc4828SBrian Ma "uploaded version"); 37988b3dd12SGunnar Mills } 380e1cc4828SBrian Ma else if ( 381e1cc4828SBrian Ma *type == 3824cde5d90SJames Feist "xyz.openbmc_project.Software.Image.Error.BusyFailure") 3834cde5d90SJames Feist { 384002d39b4SEd Tanous redfish::messages::resourceExhaustion(asyncResp->res, 385002d39b4SEd Tanous url); 3864cde5d90SJames Feist } 3874cde5d90SJames Feist else 3884cde5d90SJames Feist { 3894cde5d90SJames Feist redfish::messages::internalError(asyncResp->res); 3904cde5d90SJames Feist } 391e1cc4828SBrian Ma } 392e1cc4828SBrian Ma } 393711ac7a9SEd Tanous } 3944cde5d90SJames Feist }); 39586adcd6dSAndrew Geissler } 396729dae72SJennifer Lee 3970554c984SAndrew Geissler /** 3980554c984SAndrew Geissler * UpdateServiceActionsSimpleUpdate class supports handle POST method for 3990554c984SAndrew Geissler * SimpleUpdate action. 4000554c984SAndrew Geissler */ 4017e860f15SJohn Edward Broadbent inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app) 4020554c984SAndrew Geissler { 4037e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 4047e860f15SJohn Edward Broadbent app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/") 405ed398213SEd Tanous .privileges(redfish::privileges::postUpdateService) 406002d39b4SEd Tanous .methods(boost::beast::http::verb::post)( 407002d39b4SEd Tanous [&app](const crow::Request& req, 4087e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 4093ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 41045ca1b86SEd Tanous { 41145ca1b86SEd Tanous return; 41245ca1b86SEd Tanous } 41345ca1b86SEd Tanous 4140554c984SAndrew Geissler std::optional<std::string> transferProtocol; 4150554c984SAndrew Geissler std::string imageURI; 4160554c984SAndrew Geissler 4170554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost"; 4180554c984SAndrew Geissler 4190554c984SAndrew Geissler // User can pass in both TransferProtocol and ImageURI parameters or 4204e0453b1SGunnar Mills // they can pass in just the ImageURI with the transfer protocol 4214e0453b1SGunnar Mills // embedded within it. 4220554c984SAndrew Geissler // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin 4230554c984SAndrew Geissler // 2) ImageURI:tftp://1.1.1.1/myfile.bin 4240554c984SAndrew Geissler 425002d39b4SEd Tanous if (!json_util::readJsonAction(req, asyncResp->res, "TransferProtocol", 426002d39b4SEd Tanous transferProtocol, "ImageURI", imageURI)) 4270554c984SAndrew Geissler { 4280554c984SAndrew Geissler BMCWEB_LOG_DEBUG 4290554c984SAndrew Geissler << "Missing TransferProtocol or ImageURI parameter"; 4300554c984SAndrew Geissler return; 4310554c984SAndrew Geissler } 4320554c984SAndrew Geissler if (!transferProtocol) 4330554c984SAndrew Geissler { 4340554c984SAndrew Geissler // Must be option 2 4350554c984SAndrew Geissler // Verify ImageURI has transfer protocol in it 436f23b7296SEd Tanous size_t separator = imageURI.find(':'); 4370554c984SAndrew Geissler if ((separator == std::string::npos) || 4380554c984SAndrew Geissler ((separator + 1) > imageURI.size())) 4390554c984SAndrew Geissler { 4400554c984SAndrew Geissler messages::actionParameterValueTypeError( 4410554c984SAndrew Geissler asyncResp->res, imageURI, "ImageURI", 4420554c984SAndrew Geissler "UpdateService.SimpleUpdate"); 4430554c984SAndrew Geissler BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: " 4440554c984SAndrew Geissler << imageURI; 4450554c984SAndrew Geissler return; 4460554c984SAndrew Geissler } 4470554c984SAndrew Geissler transferProtocol = imageURI.substr(0, separator); 4487e860f15SJohn Edward Broadbent // Ensure protocol is upper case for a common comparison path 4497e860f15SJohn Edward Broadbent // below 4500554c984SAndrew Geissler boost::to_upper(*transferProtocol); 4510554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Encoded transfer protocol " 4520554c984SAndrew Geissler << *transferProtocol; 4530554c984SAndrew Geissler 4540554c984SAndrew Geissler // Adjust imageURI to not have the protocol on it for parsing 4550554c984SAndrew Geissler // below 4560554c984SAndrew Geissler // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin 4570554c984SAndrew Geissler imageURI = imageURI.substr(separator + 3); 4580554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI; 4590554c984SAndrew Geissler } 4600554c984SAndrew Geissler 4610554c984SAndrew Geissler // OpenBMC currently only supports TFTP 4620554c984SAndrew Geissler if (*transferProtocol != "TFTP") 4630554c984SAndrew Geissler { 464002d39b4SEd Tanous messages::actionParameterNotSupported(asyncResp->res, 465002d39b4SEd Tanous "TransferProtocol", 4660554c984SAndrew Geissler "UpdateService.SimpleUpdate"); 4670554c984SAndrew Geissler BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: " 4680554c984SAndrew Geissler << *transferProtocol; 4690554c984SAndrew Geissler return; 4700554c984SAndrew Geissler } 4710554c984SAndrew Geissler 4720554c984SAndrew Geissler // Format should be <IP or Hostname>/<file> for imageURI 473f23b7296SEd Tanous size_t separator = imageURI.find('/'); 4740554c984SAndrew Geissler if ((separator == std::string::npos) || 4750554c984SAndrew Geissler ((separator + 1) > imageURI.size())) 4760554c984SAndrew Geissler { 4770554c984SAndrew Geissler messages::actionParameterValueTypeError( 4780554c984SAndrew Geissler asyncResp->res, imageURI, "ImageURI", 4790554c984SAndrew Geissler "UpdateService.SimpleUpdate"); 4800554c984SAndrew Geissler BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI; 4810554c984SAndrew Geissler return; 4820554c984SAndrew Geissler } 4830554c984SAndrew Geissler 4840554c984SAndrew Geissler std::string tftpServer = imageURI.substr(0, separator); 4850554c984SAndrew Geissler std::string fwFile = imageURI.substr(separator + 1); 4860554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile; 4870554c984SAndrew Geissler 4880554c984SAndrew Geissler // Setup callback for when new software detected 4892618d5e3SGunnar Mills // Give TFTP 10 minutes to complete 4904cde5d90SJames Feist monitorForSoftwareAvailable( 491d7a596bdSAlbert Zhang asyncResp, req, 4924cde5d90SJames Feist "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate", 4932618d5e3SGunnar Mills 600); 4940554c984SAndrew Geissler 4952618d5e3SGunnar Mills // TFTP can take up to 10 minutes depending on image size and 4960554c984SAndrew Geissler // connection speed. Return to caller as soon as the TFTP operation 4970554c984SAndrew Geissler // has been started. The callback above will ensure the activate 4980554c984SAndrew Geissler // is started once the download has completed 4990554c984SAndrew Geissler redfish::messages::success(asyncResp->res); 5000554c984SAndrew Geissler 5010554c984SAndrew Geissler // Call TFTP service 5020554c984SAndrew Geissler crow::connections::systemBus->async_method_call( 5030554c984SAndrew Geissler [](const boost::system::error_code ec) { 5040554c984SAndrew Geissler if (ec) 5050554c984SAndrew Geissler { 5060554c984SAndrew Geissler // messages::internalError(asyncResp->res); 5070554c984SAndrew Geissler cleanUp(); 5080554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "error_code = " << ec; 5090554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "error msg = " << ec.message(); 5100554c984SAndrew Geissler } 5110554c984SAndrew Geissler else 5120554c984SAndrew Geissler { 5130554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success"; 5140554c984SAndrew Geissler } 5150554c984SAndrew Geissler }, 5160554c984SAndrew Geissler "xyz.openbmc_project.Software.Download", 517002d39b4SEd Tanous "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP", 518002d39b4SEd Tanous "DownloadViaTFTP", fwFile, tftpServer); 5190554c984SAndrew Geissler 5200554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost"; 5217e860f15SJohn Edward Broadbent }); 522729dae72SJennifer Lee } 523729dae72SJennifer Lee 524c2051d11SEd Tanous inline void 525c2051d11SEd Tanous handleUpdateServicePost(App& app, const crow::Request& req, 526c2051d11SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 527c2051d11SEd Tanous { 5283ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 529c2051d11SEd Tanous { 530c2051d11SEd Tanous return; 531c2051d11SEd Tanous } 532c2051d11SEd Tanous BMCWEB_LOG_DEBUG << "doPost..."; 533c2051d11SEd Tanous 534c2051d11SEd Tanous // Setup callback for when new software detected 535c2051d11SEd Tanous monitorForSoftwareAvailable(asyncResp, req, "/redfish/v1/UpdateService"); 536c2051d11SEd Tanous 537c2051d11SEd Tanous std::string filepath( 538c2051d11SEd Tanous "/tmp/images/" + 539c2051d11SEd Tanous boost::uuids::to_string(boost::uuids::random_generator()())); 540c2051d11SEd Tanous BMCWEB_LOG_DEBUG << "Writing file to " << filepath; 541c2051d11SEd Tanous std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary | 542c2051d11SEd Tanous std::ofstream::trunc); 543c2051d11SEd Tanous out << req.body; 544c2051d11SEd Tanous out.close(); 545c2051d11SEd Tanous BMCWEB_LOG_DEBUG << "file upload complete!!"; 546c2051d11SEd Tanous } 547c2051d11SEd Tanous 5487e860f15SJohn Edward Broadbent inline void requestRoutesUpdateService(App& app) 5491abe55efSEd Tanous { 5507e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") 551ed398213SEd Tanous .privileges(redfish::privileges::getUpdateService) 552002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 553002d39b4SEd Tanous [&app](const crow::Request& req, 554002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 5553ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 55645ca1b86SEd Tanous { 55745ca1b86SEd Tanous return; 55845ca1b86SEd Tanous } 5598d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 5600588a3b9SChicago Duan "#UpdateService.v1_5_0.UpdateService"; 5618d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService"; 5628d1b46d7Szhanghch05 asyncResp->res.jsonValue["Id"] = "UpdateService"; 563002d39b4SEd Tanous asyncResp->res.jsonValue["Description"] = "Service for Software Update"; 5648d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "Update Service"; 5654dc23f3fSEd Tanous 56632ca38afSEd Tanous #ifdef BMCWEB_ENABLE_REDFISH_UPDATESERVICE_OLD_POST_URL 56732ca38afSEd Tanous // See note about later on in this file about why this is neccesary 56832ca38afSEd Tanous // This is "Wrong" per the standard, but is done temporarily to 56932ca38afSEd Tanous // avoid noise in failing tests as people transition to having this 57032ca38afSEd Tanous // option disabled 57132ca38afSEd Tanous asyncResp->res.addHeader(boost::beast::http::field::allow, 57232ca38afSEd Tanous "GET, PATCH, HEAD"); 57332ca38afSEd Tanous #endif 57432ca38afSEd Tanous 5757e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["HttpPushUri"] = 5764dc23f3fSEd Tanous "/redfish/v1/UpdateService/update"; 5774dc23f3fSEd Tanous 5780f74e643SEd Tanous // UpdateService cannot be disabled 5798d1b46d7Szhanghch05 asyncResp->res.jsonValue["ServiceEnabled"] = true; 5801476687dSEd Tanous asyncResp->res.jsonValue["FirmwareInventory"]["@odata.id"] = 5811476687dSEd Tanous "/redfish/v1/UpdateService/FirmwareInventory"; 582d61e5194STejas Patil // Get the MaxImageSizeBytes 583d61e5194STejas Patil asyncResp->res.jsonValue["MaxImageSizeBytes"] = 584d61e5194STejas Patil bmcwebHttpReqBodyLimitMb * 1024 * 1024; 585d61e5194STejas Patil 5860554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE 5870554c984SAndrew Geissler // Update Actions object. 5880554c984SAndrew Geissler nlohmann::json& updateSvcSimpleUpdate = 589002d39b4SEd Tanous asyncResp->res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"]; 5900554c984SAndrew Geissler updateSvcSimpleUpdate["target"] = 5910554c984SAndrew Geissler "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate"; 592002d39b4SEd Tanous updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = { 593002d39b4SEd Tanous "TFTP"}; 5940554c984SAndrew Geissler #endif 595274dfe62SJayashankar Padath // Get the current ApplyTime value 5961e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::string>( 5971e1e598dSJonathan Doman *crow::connections::systemBus, "xyz.openbmc_project.Settings", 5981e1e598dSJonathan Doman "/xyz/openbmc_project/software/apply_time", 5991e1e598dSJonathan Doman "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime", 6008d1b46d7Szhanghch05 [asyncResp](const boost::system::error_code ec, 6011e1e598dSJonathan Doman const std::string& applyTime) { 602274dfe62SJayashankar Padath if (ec) 603274dfe62SJayashankar Padath { 604274dfe62SJayashankar Padath BMCWEB_LOG_DEBUG << "DBUS response error " << ec; 6058d1b46d7Szhanghch05 messages::internalError(asyncResp->res); 606274dfe62SJayashankar Padath return; 607274dfe62SJayashankar Padath } 608274dfe62SJayashankar Padath 609274dfe62SJayashankar Padath // Store the ApplyTime Value 6101e1e598dSJonathan Doman if (applyTime == "xyz.openbmc_project.Software.ApplyTime." 6111e1e598dSJonathan Doman "RequestedApplyTimes.Immediate") 612274dfe62SJayashankar Padath { 613002d39b4SEd Tanous asyncResp->res.jsonValue["HttpPushUriOptions"] 6147e860f15SJohn Edward Broadbent ["HttpPushUriApplyTime"]["ApplyTime"] = 6157e860f15SJohn Edward Broadbent "Immediate"; 616274dfe62SJayashankar Padath } 617002d39b4SEd Tanous else if (applyTime == "xyz.openbmc_project.Software.ApplyTime." 6181e1e598dSJonathan Doman "RequestedApplyTimes.OnReset") 619274dfe62SJayashankar Padath { 620002d39b4SEd Tanous asyncResp->res.jsonValue["HttpPushUriOptions"] 6217e860f15SJohn Edward Broadbent ["HttpPushUriApplyTime"]["ApplyTime"] = 6227e860f15SJohn Edward Broadbent "OnReset"; 623274dfe62SJayashankar Padath } 6241e1e598dSJonathan Doman }); 6257e860f15SJohn Edward Broadbent }); 6267e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") 627ed398213SEd Tanous .privileges(redfish::privileges::patchUpdateService) 628002d39b4SEd Tanous .methods(boost::beast::http::verb::patch)( 629002d39b4SEd Tanous [&app](const crow::Request& req, 630002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 6313ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 63245ca1b86SEd Tanous { 63345ca1b86SEd Tanous return; 63445ca1b86SEd Tanous } 635fa1a5a38SJayashankar Padath BMCWEB_LOG_DEBUG << "doPatch..."; 636fa1a5a38SJayashankar Padath 637274dfe62SJayashankar Padath std::optional<nlohmann::json> pushUriOptions; 638002d39b4SEd Tanous if (!json_util::readJsonPatch(req, asyncResp->res, "HttpPushUriOptions", 639002d39b4SEd Tanous pushUriOptions)) 640fa1a5a38SJayashankar Padath { 641fa1a5a38SJayashankar Padath return; 642fa1a5a38SJayashankar Padath } 643fa1a5a38SJayashankar Padath 644274dfe62SJayashankar Padath if (pushUriOptions) 645274dfe62SJayashankar Padath { 646274dfe62SJayashankar Padath std::optional<nlohmann::json> pushUriApplyTime; 6478d1b46d7Szhanghch05 if (!json_util::readJson(*pushUriOptions, asyncResp->res, 648002d39b4SEd Tanous "HttpPushUriApplyTime", pushUriApplyTime)) 649274dfe62SJayashankar Padath { 650274dfe62SJayashankar Padath return; 651274dfe62SJayashankar Padath } 652274dfe62SJayashankar Padath 653274dfe62SJayashankar Padath if (pushUriApplyTime) 654274dfe62SJayashankar Padath { 655274dfe62SJayashankar Padath std::optional<std::string> applyTime; 6560fda0f12SGeorge Liu if (!json_util::readJson(*pushUriApplyTime, asyncResp->res, 6570fda0f12SGeorge Liu "ApplyTime", applyTime)) 658274dfe62SJayashankar Padath { 659274dfe62SJayashankar Padath return; 660274dfe62SJayashankar Padath } 661274dfe62SJayashankar Padath 662274dfe62SJayashankar Padath if (applyTime) 663fa1a5a38SJayashankar Padath { 664fa1a5a38SJayashankar Padath std::string applyTimeNewVal; 665fa1a5a38SJayashankar Padath if (applyTime == "Immediate") 666fa1a5a38SJayashankar Padath { 667274dfe62SJayashankar Padath applyTimeNewVal = 6680fda0f12SGeorge Liu "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate"; 669fa1a5a38SJayashankar Padath } 670274dfe62SJayashankar Padath else if (applyTime == "OnReset") 671274dfe62SJayashankar Padath { 672274dfe62SJayashankar Padath applyTimeNewVal = 6730fda0f12SGeorge Liu "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset"; 674274dfe62SJayashankar Padath } 675fa1a5a38SJayashankar Padath else 676fa1a5a38SJayashankar Padath { 677274dfe62SJayashankar Padath BMCWEB_LOG_INFO 6780fda0f12SGeorge Liu << "ApplyTime value is not in the list of acceptable values"; 679274dfe62SJayashankar Padath messages::propertyValueNotInList( 680274dfe62SJayashankar Padath asyncResp->res, *applyTime, "ApplyTime"); 681274dfe62SJayashankar Padath return; 682fa1a5a38SJayashankar Padath } 683fa1a5a38SJayashankar Padath 684fa1a5a38SJayashankar Padath // Set the requested image apply time value 685fa1a5a38SJayashankar Padath crow::connections::systemBus->async_method_call( 6860fda0f12SGeorge Liu [asyncResp](const boost::system::error_code ec) { 687fa1a5a38SJayashankar Padath if (ec) 688fa1a5a38SJayashankar Padath { 689002d39b4SEd Tanous BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 690fa1a5a38SJayashankar Padath messages::internalError(asyncResp->res); 691fa1a5a38SJayashankar Padath return; 692fa1a5a38SJayashankar Padath } 693fa1a5a38SJayashankar Padath messages::success(asyncResp->res); 694fa1a5a38SJayashankar Padath }, 695fa1a5a38SJayashankar Padath "xyz.openbmc_project.Settings", 696fa1a5a38SJayashankar Padath "/xyz/openbmc_project/software/apply_time", 697fa1a5a38SJayashankar Padath "org.freedesktop.DBus.Properties", "Set", 698274dfe62SJayashankar Padath "xyz.openbmc_project.Software.ApplyTime", 699274dfe62SJayashankar Padath "RequestedApplyTime", 700168e20c1SEd Tanous dbus::utility::DbusVariantType{applyTimeNewVal}); 701fa1a5a38SJayashankar Padath } 702274dfe62SJayashankar Padath } 703fa1a5a38SJayashankar Padath } 7047e860f15SJohn Edward Broadbent }); 705c2051d11SEd Tanous 7064dc23f3fSEd Tanous // The "old" behavior of the update service URI causes redfish-service validator 7074dc23f3fSEd Tanous // failures when the Allow header is supported, given that in the spec, 7084dc23f3fSEd Tanous // UpdateService does not allow POST. in openbmc, we unfortunately reused that 7094dc23f3fSEd Tanous // resource as our HttpPushUri as well. A number of services, including the 7104dc23f3fSEd Tanous // openbmc tests, and documentation have hardcoded that erroneous API, instead 7114dc23f3fSEd Tanous // of relying on HttpPushUri as the spec requires. This option will exist 7124dc23f3fSEd Tanous // temporarily to allow the old behavior until Q4 2022, at which time it will be 7134dc23f3fSEd Tanous // removed. 7144dc23f3fSEd Tanous #ifdef BMCWEB_ENABLE_REDFISH_UPDATESERVICE_OLD_POST_URL 7157e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") 716ed398213SEd Tanous .privileges(redfish::privileges::postUpdateService) 717002d39b4SEd Tanous .methods(boost::beast::http::verb::post)( 718002d39b4SEd Tanous [&app](const crow::Request& req, 7194dc23f3fSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 7204dc23f3fSEd Tanous asyncResp->res.addHeader( 7214dc23f3fSEd Tanous boost::beast::http::field::warning, 7224dc23f3fSEd Tanous "299 - \"POST to /redfish/v1/UpdateService is deprecated. Use " 7234dc23f3fSEd Tanous "the value contained within HttpPushUri.\""); 7244dc23f3fSEd Tanous handleUpdateServicePost(app, req, asyncResp); 7254dc23f3fSEd Tanous }); 7264dc23f3fSEd Tanous #endif 7274dc23f3fSEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/update/") 7284dc23f3fSEd Tanous .privileges(redfish::privileges::postUpdateService) 7297e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 730c2051d11SEd Tanous std::bind_front(handleUpdateServicePost, std::ref(app))); 731729dae72SJennifer Lee } 732729dae72SJennifer Lee 7337e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventoryCollection(App& app) 7341abe55efSEd Tanous { 7357e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/") 736ed398213SEd Tanous .privileges(redfish::privileges::getSoftwareInventoryCollection) 7371476687dSEd Tanous .methods(boost::beast::http::verb::get)( 7381476687dSEd Tanous [&app](const crow::Request& req, 7391476687dSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 7403ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 74145ca1b86SEd Tanous { 74245ca1b86SEd Tanous return; 74345ca1b86SEd Tanous } 7448d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 7450f74e643SEd Tanous "#SoftwareInventoryCollection.SoftwareInventoryCollection"; 7468d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.id"] = 7470f74e643SEd Tanous "/redfish/v1/UpdateService/FirmwareInventory"; 748002d39b4SEd Tanous asyncResp->res.jsonValue["Name"] = "Software Inventory Collection"; 749c711bf86SEd Tanous 750c711bf86SEd Tanous crow::connections::systemBus->async_method_call( 751002d39b4SEd Tanous [asyncResp]( 752002d39b4SEd Tanous const boost::system::error_code ec, 753002d39b4SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 7541abe55efSEd Tanous if (ec) 7551abe55efSEd Tanous { 756f12894f8SJason M. Bills messages::internalError(asyncResp->res); 7576c4eb9deSJennifer Lee return; 758729dae72SJennifer Lee } 759002d39b4SEd Tanous asyncResp->res.jsonValue["Members"] = nlohmann::json::array(); 760c711bf86SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = 0; 7616c4eb9deSJennifer Lee 7629eb808c1SEd Tanous for (const auto& obj : subtree) 7631abe55efSEd Tanous { 7642dfd18efSEd Tanous sdbusplus::message::object_path path(obj.first); 7652dfd18efSEd Tanous std::string swId = path.filename(); 7662dfd18efSEd Tanous if (swId.empty()) 767f4b65ab1SJennifer Lee { 768f12894f8SJason M. Bills messages::internalError(asyncResp->res); 769f4b65ab1SJennifer Lee BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!"; 770f4b65ab1SJennifer Lee return; 771f4b65ab1SJennifer Lee } 772f4b65ab1SJennifer Lee 773002d39b4SEd Tanous nlohmann::json& members = asyncResp->res.jsonValue["Members"]; 7741476687dSEd Tanous nlohmann::json::object_t member; 7751476687dSEd Tanous member["@odata.id"] = 776002d39b4SEd Tanous "/redfish/v1/UpdateService/FirmwareInventory/" + swId; 7771476687dSEd Tanous members.push_back(std::move(member)); 778e0dd8057SAndrew Geissler asyncResp->res.jsonValue["Members@odata.count"] = 779c711bf86SEd Tanous members.size(); 7806c4eb9deSJennifer Lee } 781c711bf86SEd Tanous }, 7827e860f15SJohn Edward Broadbent // Note that only firmware levels associated with a device 7837e860f15SJohn Edward Broadbent // are stored under /xyz/openbmc_project/software therefore 7847e860f15SJohn Edward Broadbent // to ensure only real FirmwareInventory items are returned, 7857e860f15SJohn Edward Broadbent // this full object path must be used here as input to 7867e860f15SJohn Edward Broadbent // mapper 787c711bf86SEd Tanous "xyz.openbmc_project.ObjectMapper", 788c711bf86SEd Tanous "/xyz/openbmc_project/object_mapper", 7892830a9cfSAndrew Geissler "xyz.openbmc_project.ObjectMapper", "GetSubTree", 7902830a9cfSAndrew Geissler "/xyz/openbmc_project/software", static_cast<int32_t>(0), 791002d39b4SEd Tanous std::array<const char*, 1>{"xyz.openbmc_project.Software.Version"}); 7927e860f15SJohn Edward Broadbent }); 793729dae72SJennifer Lee } 79487d84729SAndrew Geissler /* Fill related item links (i.e. bmc, bios) in for inventory */ 7957e860f15SJohn Edward Broadbent inline static void 7967e860f15SJohn Edward Broadbent getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& aResp, 79787d84729SAndrew Geissler const std::string& purpose) 79887d84729SAndrew Geissler { 799eee0013eSWilly Tu if (purpose == sw_util::bmcPurpose) 80087d84729SAndrew Geissler { 8015af690f5SAbhishek Patel nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"]; 8021476687dSEd Tanous nlohmann::json::object_t item; 8031476687dSEd Tanous item["@odata.id"] = "/redfish/v1/Managers/bmc"; 8041476687dSEd Tanous relatedItem.push_back(std::move(item)); 8057e860f15SJohn Edward Broadbent aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size(); 80687d84729SAndrew Geissler } 807eee0013eSWilly Tu else if (purpose == sw_util::biosPurpose) 80887d84729SAndrew Geissler { 8095af690f5SAbhishek Patel nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"]; 8101476687dSEd Tanous nlohmann::json::object_t item; 8111476687dSEd Tanous item["@odata.id"] = "/redfish/v1/Systems/system/Bios"; 8121476687dSEd Tanous relatedItem.push_back(std::move(item)); 8131a6e51aeSJiaqing Zhao aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size(); 81487d84729SAndrew Geissler } 81587d84729SAndrew Geissler else 81687d84729SAndrew Geissler { 81787d84729SAndrew Geissler BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose; 81887d84729SAndrew Geissler } 81987d84729SAndrew Geissler } 82087d84729SAndrew Geissler 821af24660dSWilly Tu inline void 822af24660dSWilly Tu getSoftwareVersion(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 823af24660dSWilly Tu const std::string& service, const std::string& path, 824af24660dSWilly Tu const std::string& swId) 825af24660dSWilly Tu { 826d1bde9e5SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 827d1bde9e5SKrzysztof Grobelny *crow::connections::systemBus, service, path, 828d1bde9e5SKrzysztof Grobelny "xyz.openbmc_project.Software.Version", 829af24660dSWilly Tu [asyncResp, 830af24660dSWilly Tu swId](const boost::system::error_code errorCode, 831af24660dSWilly Tu const dbus::utility::DBusPropertiesMap& propertiesList) { 832af24660dSWilly Tu if (errorCode) 833af24660dSWilly Tu { 834af24660dSWilly Tu messages::internalError(asyncResp->res); 835af24660dSWilly Tu return; 836af24660dSWilly Tu } 837d1bde9e5SKrzysztof Grobelny 838af24660dSWilly Tu const std::string* swInvPurpose = nullptr; 839af24660dSWilly Tu const std::string* version = nullptr; 840d1bde9e5SKrzysztof Grobelny 841d1bde9e5SKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 842d1bde9e5SKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), propertiesList, "Purpose", 843d1bde9e5SKrzysztof Grobelny swInvPurpose, "Version", version); 844d1bde9e5SKrzysztof Grobelny 845d1bde9e5SKrzysztof Grobelny if (!success) 846af24660dSWilly Tu { 847d1bde9e5SKrzysztof Grobelny messages::internalError(asyncResp->res); 848d1bde9e5SKrzysztof Grobelny return; 849af24660dSWilly Tu } 850af24660dSWilly Tu 851af24660dSWilly Tu if (swInvPurpose == nullptr) 852af24660dSWilly Tu { 853af24660dSWilly Tu BMCWEB_LOG_DEBUG << "Can't find property \"Purpose\"!"; 854af24660dSWilly Tu messages::internalError(asyncResp->res); 855af24660dSWilly Tu return; 856af24660dSWilly Tu } 857af24660dSWilly Tu 858af24660dSWilly Tu BMCWEB_LOG_DEBUG << "swInvPurpose = " << *swInvPurpose; 859af24660dSWilly Tu 860af24660dSWilly Tu if (version == nullptr) 861af24660dSWilly Tu { 862af24660dSWilly Tu BMCWEB_LOG_DEBUG << "Can't find property \"Version\"!"; 863af24660dSWilly Tu 864af24660dSWilly Tu messages::internalError(asyncResp->res); 865af24660dSWilly Tu 866af24660dSWilly Tu return; 867af24660dSWilly Tu } 868af24660dSWilly Tu asyncResp->res.jsonValue["Version"] = *version; 869af24660dSWilly Tu asyncResp->res.jsonValue["Id"] = swId; 870af24660dSWilly Tu 871af24660dSWilly Tu // swInvPurpose is of format: 872af24660dSWilly Tu // xyz.openbmc_project.Software.Version.VersionPurpose.ABC 873af24660dSWilly Tu // Translate this to "ABC image" 874af24660dSWilly Tu size_t endDesc = swInvPurpose->rfind('.'); 875af24660dSWilly Tu if (endDesc == std::string::npos) 876af24660dSWilly Tu { 877af24660dSWilly Tu messages::internalError(asyncResp->res); 878af24660dSWilly Tu return; 879af24660dSWilly Tu } 880af24660dSWilly Tu endDesc++; 881af24660dSWilly Tu if (endDesc >= swInvPurpose->size()) 882af24660dSWilly Tu { 883af24660dSWilly Tu messages::internalError(asyncResp->res); 884af24660dSWilly Tu return; 885af24660dSWilly Tu } 886af24660dSWilly Tu 887af24660dSWilly Tu std::string formatDesc = swInvPurpose->substr(endDesc); 888af24660dSWilly Tu asyncResp->res.jsonValue["Description"] = formatDesc + " image"; 889af24660dSWilly Tu getRelatedItems(asyncResp, *swInvPurpose); 890d1bde9e5SKrzysztof Grobelny }); 891af24660dSWilly Tu } 892af24660dSWilly Tu 8937e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventory(App& app) 8941abe55efSEd Tanous { 8957e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/") 896ed398213SEd Tanous .privileges(redfish::privileges::getSoftwareInventory) 897002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 898002d39b4SEd Tanous [&app](const crow::Request& req, 89945ca1b86SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 9007e860f15SJohn Edward Broadbent const std::string& param) { 9013ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 90245ca1b86SEd Tanous { 90345ca1b86SEd Tanous return; 90445ca1b86SEd Tanous } 9053ae837c9SEd Tanous std::shared_ptr<std::string> swId = 9067e860f15SJohn Edward Broadbent std::make_shared<std::string>(param); 907c711bf86SEd Tanous 9088d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.id"] = 9093ae837c9SEd Tanous "/redfish/v1/UpdateService/FirmwareInventory/" + *swId; 910c711bf86SEd Tanous 911c711bf86SEd Tanous crow::connections::systemBus->async_method_call( 912b9d36b47SEd Tanous [asyncResp, 913b9d36b47SEd Tanous swId](const boost::system::error_code ec, 914b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 91555c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "doGet callback..."; 9161abe55efSEd Tanous if (ec) 9171abe55efSEd Tanous { 918f12894f8SJason M. Bills messages::internalError(asyncResp->res); 9196c4eb9deSJennifer Lee return; 9206c4eb9deSJennifer Lee } 9216c4eb9deSJennifer Lee 9226913228dSAndrew Geissler // Ensure we find our input swId, otherwise return an error 9236913228dSAndrew Geissler bool found = false; 924002d39b4SEd Tanous for (const std::pair<std::string, 9257e860f15SJohn Edward Broadbent std::vector<std::pair< 926002d39b4SEd Tanous std::string, std::vector<std::string>>>>& 927002d39b4SEd Tanous obj : subtree) 9281abe55efSEd Tanous { 92911ba3979SEd Tanous if (!obj.first.ends_with(*swId)) 9301abe55efSEd Tanous { 931acb7cfb4SJennifer Lee continue; 932acb7cfb4SJennifer Lee } 933acb7cfb4SJennifer Lee 93426f6976fSEd Tanous if (obj.second.empty()) 9351abe55efSEd Tanous { 936acb7cfb4SJennifer Lee continue; 937acb7cfb4SJennifer Lee } 9386c4eb9deSJennifer Lee 9396913228dSAndrew Geissler found = true; 940eee0013eSWilly Tu sw_util::getSwStatus(asyncResp, swId, obj.second[0].first); 941af24660dSWilly Tu getSoftwareVersion(asyncResp, obj.second[0].first, obj.first, 942af24660dSWilly Tu *swId); 9436c4eb9deSJennifer Lee } 9446913228dSAndrew Geissler if (!found) 9456913228dSAndrew Geissler { 946002d39b4SEd Tanous BMCWEB_LOG_ERROR << "Input swID " << *swId << " not found!"; 9476913228dSAndrew Geissler messages::resourceMissingAtURI( 948002d39b4SEd Tanous asyncResp->res, crow::utility::urlFromPieces( 949ace85d60SEd Tanous "redfish", "v1", "UpdateService", 950ace85d60SEd Tanous "FirmwareInventory", *swId)); 9516913228dSAndrew Geissler return; 9526913228dSAndrew Geissler } 9534e68c45bSAyushi Smriti asyncResp->res.jsonValue["@odata.type"] = 9544e68c45bSAyushi Smriti "#SoftwareInventory.v1_1_0.SoftwareInventory"; 9554e68c45bSAyushi Smriti asyncResp->res.jsonValue["Name"] = "Software Inventory"; 9564e68c45bSAyushi Smriti asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK"; 9573f8a743aSAppaRao Puli 9583f8a743aSAppaRao Puli asyncResp->res.jsonValue["Updateable"] = false; 959eee0013eSWilly Tu sw_util::getSwUpdatableStatus(asyncResp, swId); 960c711bf86SEd Tanous }, 961c711bf86SEd Tanous "xyz.openbmc_project.ObjectMapper", 962c711bf86SEd Tanous "/xyz/openbmc_project/object_mapper", 963271584abSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 964271584abSEd Tanous static_cast<int32_t>(0), 965002d39b4SEd Tanous std::array<const char*, 1>{"xyz.openbmc_project.Software.Version"}); 9667e860f15SJohn Edward Broadbent }); 9676c4eb9deSJennifer Lee } 968729dae72SJennifer Lee 969729dae72SJennifer Lee } // namespace redfish 970