1729dae72SJennifer Lee /* 2729dae72SJennifer Lee // Copyright (c) 2018 Intel Corporation 3729dae72SJennifer Lee // 4729dae72SJennifer Lee // Licensed under the Apache License, Version 2.0 (the "License"); 5729dae72SJennifer Lee // you may not use this file except in compliance with the License. 6729dae72SJennifer Lee // You may obtain a copy of the License at 7729dae72SJennifer Lee // 8729dae72SJennifer Lee // http://www.apache.org/licenses/LICENSE-2.0 9729dae72SJennifer Lee // 10729dae72SJennifer Lee // Unless required by applicable law or agreed to in writing, software 11729dae72SJennifer Lee // distributed under the License is distributed on an "AS IS" BASIS, 12729dae72SJennifer Lee // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13729dae72SJennifer Lee // See the License for the specific language governing permissions and 14729dae72SJennifer Lee // limitations under the License. 15729dae72SJennifer Lee */ 16729dae72SJennifer Lee #pragma once 17729dae72SJennifer Lee 18d61e5194STejas Patil #include "bmcweb_config.h" 19d61e5194STejas Patil 207e860f15SJohn Edward Broadbent #include <app.hpp> 21168e20c1SEd Tanous #include <dbus_utility.hpp> 2245ca1b86SEd Tanous #include <query.hpp> 23ed398213SEd Tanous #include <registries/privilege_registry.hpp> 241e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp> 2587d84729SAndrew Geissler #include <utils/fw_utils.hpp> 261214b7e7SGunnar Mills 271abe55efSEd Tanous namespace redfish 281abe55efSEd Tanous { 2927826b5fSEd Tanous 300e7de46fSAndrew Geissler // Match signals added on software path 31acb7cfb4SJennifer Lee static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher; 324cde5d90SJames Feist static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateErrorMatcher; 330e7de46fSAndrew Geissler // Only allow one update at a time 340e7de46fSAndrew Geissler static bool fwUpdateInProgress = false; 3586adcd6dSAndrew Geissler // Timer for software available 36271584abSEd Tanous static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer; 3786adcd6dSAndrew Geissler 387e860f15SJohn Edward Broadbent inline static void cleanUp() 3986adcd6dSAndrew Geissler { 4086adcd6dSAndrew Geissler fwUpdateInProgress = false; 4186adcd6dSAndrew Geissler fwUpdateMatcher = nullptr; 424cde5d90SJames Feist fwUpdateErrorMatcher = nullptr; 4386adcd6dSAndrew Geissler } 447e860f15SJohn Edward Broadbent inline static void activateImage(const std::string& objPath, 4586adcd6dSAndrew Geissler const std::string& service) 4686adcd6dSAndrew Geissler { 4786adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "Activate image for " << objPath << " " << service; 4886adcd6dSAndrew Geissler crow::connections::systemBus->async_method_call( 4981ce609eSEd Tanous [](const boost::system::error_code errorCode) { 5081ce609eSEd Tanous if (errorCode) 5186adcd6dSAndrew Geissler { 5281ce609eSEd Tanous BMCWEB_LOG_DEBUG << "error_code = " << errorCode; 5381ce609eSEd Tanous BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message(); 5486adcd6dSAndrew Geissler } 5586adcd6dSAndrew Geissler }, 5686adcd6dSAndrew Geissler service, objPath, "org.freedesktop.DBus.Properties", "Set", 5786adcd6dSAndrew Geissler "xyz.openbmc_project.Software.Activation", "RequestedActivation", 58168e20c1SEd Tanous dbus::utility::DbusVariantType( 590fda0f12SGeorge Liu "xyz.openbmc_project.Software.Activation.RequestedActivations.Active")); 6086adcd6dSAndrew Geissler } 610554c984SAndrew Geissler 620554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr 630554c984SAndrew Geissler // then no asyncResp updates will occur 648d1b46d7Szhanghch05 static void 658d1b46d7Szhanghch05 softwareInterfaceAdded(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 66fe306728SJames Feist sdbusplus::message::message& m, 67a3e65892SEd Tanous task::Payload&& payload) 6886adcd6dSAndrew Geissler { 69b9d36b47SEd Tanous dbus::utility::DBusInteracesMap interfacesProperties; 7086adcd6dSAndrew Geissler 7186adcd6dSAndrew Geissler sdbusplus::message::object_path objPath; 7286adcd6dSAndrew Geissler 7386adcd6dSAndrew Geissler m.read(objPath, interfacesProperties); 7486adcd6dSAndrew Geissler 7586adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "obj path = " << objPath.str; 7686adcd6dSAndrew Geissler for (auto& interface : interfacesProperties) 7786adcd6dSAndrew Geissler { 7886adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "interface = " << interface.first; 7986adcd6dSAndrew Geissler 8086adcd6dSAndrew Geissler if (interface.first == "xyz.openbmc_project.Software.Activation") 8186adcd6dSAndrew Geissler { 8286adcd6dSAndrew Geissler // Retrieve service and activate 8386adcd6dSAndrew Geissler crow::connections::systemBus->async_method_call( 84a3e65892SEd Tanous [objPath, asyncResp, payload(std::move(payload))]( 85a3e65892SEd Tanous const boost::system::error_code errorCode, 86a3e65892SEd Tanous const std::vector< 87a3e65892SEd Tanous std::pair<std::string, std::vector<std::string>>>& 88a3e65892SEd Tanous objInfo) mutable { 8981ce609eSEd Tanous if (errorCode) 9086adcd6dSAndrew Geissler { 9181ce609eSEd Tanous BMCWEB_LOG_DEBUG << "error_code = " << errorCode; 9286adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "error msg = " 9381ce609eSEd Tanous << errorCode.message(); 940554c984SAndrew Geissler if (asyncResp) 950554c984SAndrew Geissler { 9686adcd6dSAndrew Geissler messages::internalError(asyncResp->res); 970554c984SAndrew Geissler } 9886adcd6dSAndrew Geissler cleanUp(); 9986adcd6dSAndrew Geissler return; 10086adcd6dSAndrew Geissler } 10186adcd6dSAndrew Geissler // Ensure we only got one service back 10286adcd6dSAndrew Geissler if (objInfo.size() != 1) 10386adcd6dSAndrew Geissler { 10486adcd6dSAndrew Geissler BMCWEB_LOG_ERROR << "Invalid Object Size " 10586adcd6dSAndrew Geissler << objInfo.size(); 1060554c984SAndrew Geissler if (asyncResp) 1070554c984SAndrew Geissler { 10886adcd6dSAndrew Geissler messages::internalError(asyncResp->res); 1090554c984SAndrew Geissler } 11086adcd6dSAndrew Geissler cleanUp(); 11186adcd6dSAndrew Geissler return; 11286adcd6dSAndrew Geissler } 11386adcd6dSAndrew Geissler // cancel timer only when 11486adcd6dSAndrew Geissler // xyz.openbmc_project.Software.Activation interface 11586adcd6dSAndrew Geissler // is added 11686adcd6dSAndrew Geissler fwAvailableTimer = nullptr; 11786adcd6dSAndrew Geissler 11886adcd6dSAndrew Geissler activateImage(objPath.str, objInfo[0].first); 1190554c984SAndrew Geissler if (asyncResp) 1200554c984SAndrew Geissler { 12132898ceaSJames Feist std::shared_ptr<task::TaskData> task = 12232898ceaSJames Feist task::TaskData::createTask( 12332898ceaSJames Feist [](boost::system::error_code ec, 12432898ceaSJames Feist sdbusplus::message::message& msg, 1251214b7e7SGunnar Mills const std::shared_ptr<task::TaskData>& 1261214b7e7SGunnar Mills taskData) { 12732898ceaSJames Feist if (ec) 12832898ceaSJames Feist { 12932898ceaSJames Feist return task::completed; 13032898ceaSJames Feist } 13132898ceaSJames Feist 13232898ceaSJames Feist std::string iface; 133b9d36b47SEd Tanous dbus::utility::DBusPropertiesMap values; 134fd9ab9e1SJames Feist 135fd9ab9e1SJames Feist std::string index = 136fd9ab9e1SJames Feist std::to_string(taskData->index); 13732898ceaSJames Feist msg.read(iface, values); 138fd9ab9e1SJames Feist 1390fda0f12SGeorge Liu if (iface == 1400fda0f12SGeorge Liu "xyz.openbmc_project.Software.Activation") 141fd9ab9e1SJames Feist { 142b9d36b47SEd Tanous std::string* state = nullptr; 143b9d36b47SEd Tanous for (const auto& property : values) 14432898ceaSJames Feist { 145b9d36b47SEd Tanous if (property.first == "Activation") 146b9d36b47SEd Tanous { 147b9d36b47SEd Tanous const std::string* state = 14832898ceaSJames Feist std::get_if<std::string>( 149b9d36b47SEd Tanous &property.second); 150b9d36b47SEd Tanous if (state == nullptr) 151b9d36b47SEd Tanous { 152b9d36b47SEd Tanous taskData->messages 153b9d36b47SEd Tanous .emplace_back( 154b9d36b47SEd Tanous messages:: 155b9d36b47SEd Tanous internalError()); 156b9d36b47SEd Tanous return task::completed; 157b9d36b47SEd Tanous } 158b9d36b47SEd Tanous } 159b9d36b47SEd Tanous } 16032898ceaSJames Feist 16132898ceaSJames Feist if (state == nullptr) 16232898ceaSJames Feist { 163b9d36b47SEd Tanous return !task::completed; 16432898ceaSJames Feist } 16532898ceaSJames Feist 166fd9ab9e1SJames Feist if (boost::ends_with(*state, 167fd9ab9e1SJames Feist "Invalid") || 16832898ceaSJames Feist boost::ends_with(*state, "Failed")) 16932898ceaSJames Feist { 17032898ceaSJames Feist taskData->state = "Exception"; 17132898ceaSJames Feist taskData->status = "Warning"; 17232898ceaSJames Feist taskData->messages.emplace_back( 173e5d5006bSJames Feist messages::taskAborted(index)); 17432898ceaSJames Feist return task::completed; 17532898ceaSJames Feist } 17632898ceaSJames Feist 17732898ceaSJames Feist if (boost::ends_with(*state, "Staged")) 17832898ceaSJames Feist { 179fd9ab9e1SJames Feist taskData->state = "Stopping"; 180fd9ab9e1SJames Feist taskData->messages.emplace_back( 181fd9ab9e1SJames Feist messages::taskPaused(index)); 182fd9ab9e1SJames Feist 183fd9ab9e1SJames Feist // its staged, set a long timer to 184fd9ab9e1SJames Feist // allow them time to complete the 185fd9ab9e1SJames Feist // update (probably cycle the 186fd9ab9e1SJames Feist // system) if this expires then 187fd9ab9e1SJames Feist // task will be cancelled 188fd9ab9e1SJames Feist taskData->extendTimer( 189fd9ab9e1SJames Feist std::chrono::hours(5)); 19032898ceaSJames Feist return !task::completed; 19132898ceaSJames Feist } 19232898ceaSJames Feist 19332898ceaSJames Feist if (boost::ends_with(*state, "Active")) 19432898ceaSJames Feist { 19532898ceaSJames Feist taskData->messages.emplace_back( 196fd9ab9e1SJames Feist messages::taskCompletedOK( 197fd9ab9e1SJames Feist index)); 19832898ceaSJames Feist taskData->state = "Completed"; 19932898ceaSJames Feist return task::completed; 20032898ceaSJames Feist } 201fd9ab9e1SJames Feist } 2020fda0f12SGeorge Liu else if ( 2030fda0f12SGeorge Liu iface == 2040fda0f12SGeorge Liu "xyz.openbmc_project.Software.ActivationProgress") 205fd9ab9e1SJames Feist { 206b9d36b47SEd Tanous 207b9d36b47SEd Tanous const uint8_t* progress = nullptr; 208b9d36b47SEd Tanous for (const auto& property : values) 209fd9ab9e1SJames Feist { 210b9d36b47SEd Tanous if (property.first == "Progress") 211b9d36b47SEd Tanous { 212b9d36b47SEd Tanous const std::string* progress = 213b9d36b47SEd Tanous std::get_if<std::string>( 214b9d36b47SEd Tanous &property.second); 215b9d36b47SEd Tanous if (progress == nullptr) 216b9d36b47SEd Tanous { 217b9d36b47SEd Tanous taskData->messages 218b9d36b47SEd Tanous .emplace_back( 219b9d36b47SEd Tanous messages:: 220b9d36b47SEd Tanous internalError()); 221b9d36b47SEd Tanous return task::completed; 222fd9ab9e1SJames Feist } 223b9d36b47SEd Tanous } 224b9d36b47SEd Tanous } 225fd9ab9e1SJames Feist 226fd9ab9e1SJames Feist if (progress == nullptr) 227fd9ab9e1SJames Feist { 228b9d36b47SEd Tanous return !task::completed; 229fd9ab9e1SJames Feist } 2306868ff50SGeorge Liu taskData->percentComplete = 2316868ff50SGeorge Liu static_cast<int>(*progress); 232fd9ab9e1SJames Feist taskData->messages.emplace_back( 233fd9ab9e1SJames Feist messages::taskProgressChanged( 234fd9ab9e1SJames Feist index, static_cast<size_t>( 235fd9ab9e1SJames Feist *progress))); 236fd9ab9e1SJames Feist 237fd9ab9e1SJames Feist // if we're getting status updates it's 238fd9ab9e1SJames Feist // still alive, update timer 239fd9ab9e1SJames Feist taskData->extendTimer( 240fd9ab9e1SJames Feist std::chrono::minutes(5)); 241fd9ab9e1SJames Feist } 24232898ceaSJames Feist 24332898ceaSJames Feist // as firmware update often results in a 24432898ceaSJames Feist // reboot, the task may never "complete" 24532898ceaSJames Feist // unless it is an error 24632898ceaSJames Feist 24732898ceaSJames Feist return !task::completed; 24832898ceaSJames Feist }, 2490fda0f12SGeorge Liu "type='signal',interface='org.freedesktop.DBus.Properties'," 250fd9ab9e1SJames Feist "member='PropertiesChanged',path='" + 25132898ceaSJames Feist objPath.str + "'"); 25232898ceaSJames Feist task->startTimer(std::chrono::minutes(5)); 25332898ceaSJames Feist task->populateResp(asyncResp->res); 254a3e65892SEd Tanous task->payload.emplace(std::move(payload)); 2550554c984SAndrew Geissler } 25686adcd6dSAndrew Geissler fwUpdateInProgress = false; 25786adcd6dSAndrew Geissler }, 25886adcd6dSAndrew Geissler "xyz.openbmc_project.ObjectMapper", 25986adcd6dSAndrew Geissler "/xyz/openbmc_project/object_mapper", 26086adcd6dSAndrew Geissler "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str, 26186adcd6dSAndrew Geissler std::array<const char*, 1>{ 26286adcd6dSAndrew Geissler "xyz.openbmc_project.Software.Activation"}); 26386adcd6dSAndrew Geissler } 26486adcd6dSAndrew Geissler } 26586adcd6dSAndrew Geissler } 26686adcd6dSAndrew Geissler 2670554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr 2680554c984SAndrew Geissler // then no asyncResp updates will occur 269b5a76932SEd Tanous static void monitorForSoftwareAvailable( 2708d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2718d1b46d7Szhanghch05 const crow::Request& req, const std::string& url, 2728d1b46d7Szhanghch05 int timeoutTimeSeconds = 10) 27386adcd6dSAndrew Geissler { 27486adcd6dSAndrew Geissler // Only allow one FW update at a time 275e05aec50SEd Tanous if (fwUpdateInProgress) 27686adcd6dSAndrew Geissler { 2770554c984SAndrew Geissler if (asyncResp) 2780554c984SAndrew Geissler { 27986adcd6dSAndrew Geissler messages::serviceTemporarilyUnavailable(asyncResp->res, "30"); 2800554c984SAndrew Geissler } 28186adcd6dSAndrew Geissler return; 28286adcd6dSAndrew Geissler } 28386adcd6dSAndrew Geissler 2840554c984SAndrew Geissler fwAvailableTimer = 285271584abSEd Tanous std::make_unique<boost::asio::steady_timer>(*req.ioService); 28686adcd6dSAndrew Geissler 287271584abSEd Tanous fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds)); 28886adcd6dSAndrew Geissler 28986adcd6dSAndrew Geissler fwAvailableTimer->async_wait( 29086adcd6dSAndrew Geissler [asyncResp](const boost::system::error_code& ec) { 29186adcd6dSAndrew Geissler cleanUp(); 29286adcd6dSAndrew Geissler if (ec == boost::asio::error::operation_aborted) 29386adcd6dSAndrew Geissler { 29486adcd6dSAndrew Geissler // expected, we were canceled before the timer completed. 29586adcd6dSAndrew Geissler return; 29686adcd6dSAndrew Geissler } 29786adcd6dSAndrew Geissler BMCWEB_LOG_ERROR 29886adcd6dSAndrew Geissler << "Timed out waiting for firmware object being created"; 29986adcd6dSAndrew Geissler BMCWEB_LOG_ERROR 30086adcd6dSAndrew Geissler << "FW image may has already been uploaded to server"; 30186adcd6dSAndrew Geissler if (ec) 30286adcd6dSAndrew Geissler { 30386adcd6dSAndrew Geissler BMCWEB_LOG_ERROR << "Async_wait failed" << ec; 30486adcd6dSAndrew Geissler return; 30586adcd6dSAndrew Geissler } 3060554c984SAndrew Geissler if (asyncResp) 3070554c984SAndrew Geissler { 30886adcd6dSAndrew Geissler redfish::messages::internalError(asyncResp->res); 3090554c984SAndrew Geissler } 31086adcd6dSAndrew Geissler }); 311a3e65892SEd Tanous task::Payload payload(req); 312a3e65892SEd Tanous auto callback = [asyncResp, 313a3e65892SEd Tanous payload](sdbusplus::message::message& m) mutable { 31486adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "Match fired"; 315a3e65892SEd Tanous softwareInterfaceAdded(asyncResp, m, std::move(payload)); 31686adcd6dSAndrew Geissler }; 31786adcd6dSAndrew Geissler 31886adcd6dSAndrew Geissler fwUpdateInProgress = true; 31986adcd6dSAndrew Geissler 32086adcd6dSAndrew Geissler fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>( 32186adcd6dSAndrew Geissler *crow::connections::systemBus, 32286adcd6dSAndrew Geissler "interface='org.freedesktop.DBus.ObjectManager',type='signal'," 32386adcd6dSAndrew Geissler "member='InterfacesAdded',path='/xyz/openbmc_project/software'", 32486adcd6dSAndrew Geissler callback); 3254cde5d90SJames Feist 3264cde5d90SJames Feist fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match::match>( 3274cde5d90SJames Feist *crow::connections::systemBus, 328e1cc4828SBrian Ma "interface='org.freedesktop.DBus.ObjectManager',type='signal'," 329e1cc4828SBrian Ma "member='InterfacesAdded'," 330e1cc4828SBrian Ma "path='/xyz/openbmc_project/logging'", 3314cde5d90SJames Feist [asyncResp, url](sdbusplus::message::message& m) { 332e1cc4828SBrian Ma std::vector< 333e1cc4828SBrian Ma std::pair<std::string, dbus::utility::DBusPropertiesMap>> 334e1cc4828SBrian Ma interfacesProperties; 335e1cc4828SBrian Ma sdbusplus::message::object_path objPath; 336e1cc4828SBrian Ma m.read(objPath, interfacesProperties); 337e1cc4828SBrian Ma BMCWEB_LOG_DEBUG << "obj path = " << objPath.str; 338e1cc4828SBrian Ma for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>& 339e1cc4828SBrian Ma interface : interfacesProperties) 340e1cc4828SBrian Ma { 341e1cc4828SBrian Ma if (interface.first == "xyz.openbmc_project.Logging.Entry") 342e1cc4828SBrian Ma { 343711ac7a9SEd Tanous for (const std::pair<std::string, 344711ac7a9SEd Tanous dbus::utility::DbusVariantType>& 345711ac7a9SEd Tanous value : interface.second) 3464cde5d90SJames Feist { 347711ac7a9SEd Tanous if (value.first != "Message") 348711ac7a9SEd Tanous { 349711ac7a9SEd Tanous continue; 3504cde5d90SJames Feist } 351e1cc4828SBrian Ma const std::string* type = 352711ac7a9SEd Tanous std::get_if<std::string>(&value.second); 3534cde5d90SJames Feist if (type == nullptr) 3544cde5d90SJames Feist { 355e1cc4828SBrian Ma // if this was our message, timeout will cover it 3564cde5d90SJames Feist return; 3574cde5d90SJames Feist } 358e1cc4828SBrian Ma fwAvailableTimer = nullptr; 3594cde5d90SJames Feist if (*type == 3604cde5d90SJames Feist "xyz.openbmc_project.Software.Image.Error.UnTarFailure") 3614cde5d90SJames Feist { 362711ac7a9SEd Tanous redfish::messages::invalidUpload( 363711ac7a9SEd Tanous asyncResp->res, url, "Invalid archive"); 3644cde5d90SJames Feist } 365e1cc4828SBrian Ma else if (*type == 366e1cc4828SBrian Ma "xyz.openbmc_project.Software.Image.Error." 367e1cc4828SBrian Ma "ManifestFileFailure") 3684cde5d90SJames Feist { 369711ac7a9SEd Tanous redfish::messages::invalidUpload( 370711ac7a9SEd Tanous asyncResp->res, url, "Invalid manifest"); 3714cde5d90SJames Feist } 372e1cc4828SBrian Ma else if ( 373e1cc4828SBrian Ma *type == 3744cde5d90SJames Feist "xyz.openbmc_project.Software.Image.Error.ImageFailure") 3754cde5d90SJames Feist { 376e1cc4828SBrian Ma redfish::messages::invalidUpload( 377e1cc4828SBrian Ma asyncResp->res, url, "Invalid image format"); 3784cde5d90SJames Feist } 379e1cc4828SBrian Ma else if ( 380e1cc4828SBrian Ma *type == 3810fda0f12SGeorge Liu "xyz.openbmc_project.Software.Version.Error.AlreadyExists") 38288b3dd12SGunnar Mills { 38388b3dd12SGunnar Mills redfish::messages::invalidUpload( 384e1cc4828SBrian Ma asyncResp->res, url, 385e1cc4828SBrian Ma "Image version already exists"); 38688b3dd12SGunnar Mills 38788b3dd12SGunnar Mills redfish::messages::resourceAlreadyExists( 388e1cc4828SBrian Ma asyncResp->res, 389e1cc4828SBrian Ma "UpdateService.v1_5_0.UpdateService", "Version", 390e1cc4828SBrian Ma "uploaded version"); 39188b3dd12SGunnar Mills } 392e1cc4828SBrian Ma else if ( 393e1cc4828SBrian Ma *type == 3944cde5d90SJames Feist "xyz.openbmc_project.Software.Image.Error.BusyFailure") 3954cde5d90SJames Feist { 396711ac7a9SEd Tanous redfish::messages::resourceExhaustion( 397711ac7a9SEd Tanous asyncResp->res, url); 3984cde5d90SJames Feist } 3994cde5d90SJames Feist else 4004cde5d90SJames Feist { 4014cde5d90SJames Feist redfish::messages::internalError(asyncResp->res); 4024cde5d90SJames Feist } 403e1cc4828SBrian Ma } 404e1cc4828SBrian Ma } 405711ac7a9SEd Tanous } 4064cde5d90SJames Feist }); 40786adcd6dSAndrew Geissler } 408729dae72SJennifer Lee 4090554c984SAndrew Geissler /** 4100554c984SAndrew Geissler * UpdateServiceActionsSimpleUpdate class supports handle POST method for 4110554c984SAndrew Geissler * SimpleUpdate action. 4120554c984SAndrew Geissler */ 4137e860f15SJohn Edward Broadbent inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app) 4140554c984SAndrew Geissler { 4157e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 4167e860f15SJohn Edward Broadbent app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/") 417ed398213SEd Tanous .privileges(redfish::privileges::postUpdateService) 4187e860f15SJohn Edward Broadbent .methods( 4197e860f15SJohn Edward Broadbent boost::beast::http::verb:: 42045ca1b86SEd Tanous post)([&app]( 42145ca1b86SEd Tanous const crow::Request& req, 4227e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 42345ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 42445ca1b86SEd Tanous { 42545ca1b86SEd Tanous return; 42645ca1b86SEd Tanous } 42745ca1b86SEd Tanous 4280554c984SAndrew Geissler std::optional<std::string> transferProtocol; 4290554c984SAndrew Geissler std::string imageURI; 4300554c984SAndrew Geissler 4310554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost"; 4320554c984SAndrew Geissler 4330554c984SAndrew Geissler // User can pass in both TransferProtocol and ImageURI parameters or 4344e0453b1SGunnar Mills // they can pass in just the ImageURI with the transfer protocol 4354e0453b1SGunnar Mills // embedded within it. 4360554c984SAndrew Geissler // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin 4370554c984SAndrew Geissler // 2) ImageURI:tftp://1.1.1.1/myfile.bin 4380554c984SAndrew Geissler 43915ed6780SWilly Tu if (!json_util::readJsonAction(req, asyncResp->res, 44015ed6780SWilly Tu "TransferProtocol", transferProtocol, 44115ed6780SWilly Tu "ImageURI", imageURI)) 4420554c984SAndrew Geissler { 4430554c984SAndrew Geissler BMCWEB_LOG_DEBUG 4440554c984SAndrew Geissler << "Missing TransferProtocol or ImageURI parameter"; 4450554c984SAndrew Geissler return; 4460554c984SAndrew Geissler } 4470554c984SAndrew Geissler if (!transferProtocol) 4480554c984SAndrew Geissler { 4490554c984SAndrew Geissler // Must be option 2 4500554c984SAndrew Geissler // Verify ImageURI has transfer protocol in it 451f23b7296SEd Tanous size_t separator = imageURI.find(':'); 4520554c984SAndrew Geissler if ((separator == std::string::npos) || 4530554c984SAndrew Geissler ((separator + 1) > imageURI.size())) 4540554c984SAndrew Geissler { 4550554c984SAndrew Geissler messages::actionParameterValueTypeError( 4560554c984SAndrew Geissler asyncResp->res, imageURI, "ImageURI", 4570554c984SAndrew Geissler "UpdateService.SimpleUpdate"); 4580554c984SAndrew Geissler BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: " 4590554c984SAndrew Geissler << imageURI; 4600554c984SAndrew Geissler return; 4610554c984SAndrew Geissler } 4620554c984SAndrew Geissler transferProtocol = imageURI.substr(0, separator); 4637e860f15SJohn Edward Broadbent // Ensure protocol is upper case for a common comparison path 4647e860f15SJohn Edward Broadbent // below 4650554c984SAndrew Geissler boost::to_upper(*transferProtocol); 4660554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Encoded transfer protocol " 4670554c984SAndrew Geissler << *transferProtocol; 4680554c984SAndrew Geissler 4690554c984SAndrew Geissler // Adjust imageURI to not have the protocol on it for parsing 4700554c984SAndrew Geissler // below 4710554c984SAndrew Geissler // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin 4720554c984SAndrew Geissler imageURI = imageURI.substr(separator + 3); 4730554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI; 4740554c984SAndrew Geissler } 4750554c984SAndrew Geissler 4760554c984SAndrew Geissler // OpenBMC currently only supports TFTP 4770554c984SAndrew Geissler if (*transferProtocol != "TFTP") 4780554c984SAndrew Geissler { 4797e860f15SJohn Edward Broadbent messages::actionParameterNotSupported( 4807e860f15SJohn Edward Broadbent asyncResp->res, "TransferProtocol", 4810554c984SAndrew Geissler "UpdateService.SimpleUpdate"); 4820554c984SAndrew Geissler BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: " 4830554c984SAndrew Geissler << *transferProtocol; 4840554c984SAndrew Geissler return; 4850554c984SAndrew Geissler } 4860554c984SAndrew Geissler 4870554c984SAndrew Geissler // Format should be <IP or Hostname>/<file> for imageURI 488f23b7296SEd Tanous size_t separator = imageURI.find('/'); 4890554c984SAndrew Geissler if ((separator == std::string::npos) || 4900554c984SAndrew Geissler ((separator + 1) > imageURI.size())) 4910554c984SAndrew Geissler { 4920554c984SAndrew Geissler messages::actionParameterValueTypeError( 4930554c984SAndrew Geissler asyncResp->res, imageURI, "ImageURI", 4940554c984SAndrew Geissler "UpdateService.SimpleUpdate"); 4950554c984SAndrew Geissler BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI; 4960554c984SAndrew Geissler return; 4970554c984SAndrew Geissler } 4980554c984SAndrew Geissler 4990554c984SAndrew Geissler std::string tftpServer = imageURI.substr(0, separator); 5000554c984SAndrew Geissler std::string fwFile = imageURI.substr(separator + 1); 5010554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile; 5020554c984SAndrew Geissler 5030554c984SAndrew Geissler // Setup callback for when new software detected 5042618d5e3SGunnar Mills // Give TFTP 10 minutes to complete 5054cde5d90SJames Feist monitorForSoftwareAvailable( 506d7a596bdSAlbert Zhang asyncResp, req, 5074cde5d90SJames Feist "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate", 5082618d5e3SGunnar Mills 600); 5090554c984SAndrew Geissler 5102618d5e3SGunnar Mills // TFTP can take up to 10 minutes depending on image size and 5110554c984SAndrew Geissler // connection speed. Return to caller as soon as the TFTP operation 5120554c984SAndrew Geissler // has been started. The callback above will ensure the activate 5130554c984SAndrew Geissler // is started once the download has completed 5140554c984SAndrew Geissler redfish::messages::success(asyncResp->res); 5150554c984SAndrew Geissler 5160554c984SAndrew Geissler // Call TFTP service 5170554c984SAndrew Geissler crow::connections::systemBus->async_method_call( 5180554c984SAndrew Geissler [](const boost::system::error_code ec) { 5190554c984SAndrew Geissler if (ec) 5200554c984SAndrew Geissler { 5210554c984SAndrew Geissler // messages::internalError(asyncResp->res); 5220554c984SAndrew Geissler cleanUp(); 5230554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "error_code = " << ec; 5240554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "error msg = " << ec.message(); 5250554c984SAndrew Geissler } 5260554c984SAndrew Geissler else 5270554c984SAndrew Geissler { 5280554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success"; 5290554c984SAndrew Geissler } 5300554c984SAndrew Geissler }, 5310554c984SAndrew Geissler "xyz.openbmc_project.Software.Download", 5327e860f15SJohn Edward Broadbent "/xyz/openbmc_project/software", 5337e860f15SJohn Edward Broadbent "xyz.openbmc_project.Common.TFTP", "DownloadViaTFTP", fwFile, 5347e860f15SJohn Edward Broadbent tftpServer); 5350554c984SAndrew Geissler 5360554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost"; 5377e860f15SJohn Edward Broadbent }); 538729dae72SJennifer Lee } 539729dae72SJennifer Lee 540c2051d11SEd Tanous inline void 541c2051d11SEd Tanous handleUpdateServicePost(App& app, const crow::Request& req, 542c2051d11SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 543c2051d11SEd Tanous { 544c2051d11SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 545c2051d11SEd Tanous { 546c2051d11SEd Tanous return; 547c2051d11SEd Tanous } 548c2051d11SEd Tanous BMCWEB_LOG_DEBUG << "doPost..."; 549c2051d11SEd Tanous 550c2051d11SEd Tanous // Setup callback for when new software detected 551c2051d11SEd Tanous monitorForSoftwareAvailable(asyncResp, req, "/redfish/v1/UpdateService"); 552c2051d11SEd Tanous 553c2051d11SEd Tanous std::string filepath( 554c2051d11SEd Tanous "/tmp/images/" + 555c2051d11SEd Tanous boost::uuids::to_string(boost::uuids::random_generator()())); 556c2051d11SEd Tanous BMCWEB_LOG_DEBUG << "Writing file to " << filepath; 557c2051d11SEd Tanous std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary | 558c2051d11SEd Tanous std::ofstream::trunc); 559c2051d11SEd Tanous out << req.body; 560c2051d11SEd Tanous out.close(); 561c2051d11SEd Tanous BMCWEB_LOG_DEBUG << "file upload complete!!"; 562c2051d11SEd Tanous } 563c2051d11SEd Tanous 5647e860f15SJohn Edward Broadbent inline void requestRoutesUpdateService(App& app) 5651abe55efSEd Tanous { 5667e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") 567ed398213SEd Tanous .privileges(redfish::privileges::getUpdateService) 56845ca1b86SEd Tanous .methods(boost::beast::http::verb::get)([&app](const crow::Request& req, 56945ca1b86SEd Tanous const std::shared_ptr< 57045ca1b86SEd Tanous bmcweb::AsyncResp>& 57145ca1b86SEd Tanous asyncResp) { 57245ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 57345ca1b86SEd Tanous { 57445ca1b86SEd Tanous return; 57545ca1b86SEd Tanous } 5768d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 5770588a3b9SChicago Duan "#UpdateService.v1_5_0.UpdateService"; 5788d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService"; 5798d1b46d7Szhanghch05 asyncResp->res.jsonValue["Id"] = "UpdateService"; 5807e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["Description"] = 5817e860f15SJohn Edward Broadbent "Service for Software Update"; 5828d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "Update Service"; 5834dc23f3fSEd Tanous 584*32ca38afSEd Tanous #ifdef BMCWEB_ENABLE_REDFISH_UPDATESERVICE_OLD_POST_URL 585*32ca38afSEd Tanous // See note about later on in this file about why this is neccesary 586*32ca38afSEd Tanous // This is "Wrong" per the standard, but is done temporarily to 587*32ca38afSEd Tanous // avoid noise in failing tests as people transition to having this 588*32ca38afSEd Tanous // option disabled 589*32ca38afSEd Tanous asyncResp->res.addHeader(boost::beast::http::field::allow, 590*32ca38afSEd Tanous "GET, PATCH, HEAD"); 591*32ca38afSEd Tanous #endif 592*32ca38afSEd Tanous 5937e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["HttpPushUri"] = 5944dc23f3fSEd Tanous "/redfish/v1/UpdateService/update"; 5954dc23f3fSEd Tanous 5960f74e643SEd Tanous // UpdateService cannot be disabled 5978d1b46d7Szhanghch05 asyncResp->res.jsonValue["ServiceEnabled"] = true; 5981476687dSEd Tanous asyncResp->res.jsonValue["FirmwareInventory"]["@odata.id"] = 5991476687dSEd Tanous "/redfish/v1/UpdateService/FirmwareInventory"; 600d61e5194STejas Patil // Get the MaxImageSizeBytes 601d61e5194STejas Patil asyncResp->res.jsonValue["MaxImageSizeBytes"] = 602d61e5194STejas Patil bmcwebHttpReqBodyLimitMb * 1024 * 1024; 603d61e5194STejas Patil 6040554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE 6050554c984SAndrew Geissler // Update Actions object. 6060554c984SAndrew Geissler nlohmann::json& updateSvcSimpleUpdate = 6077e860f15SJohn Edward Broadbent asyncResp->res 6087e860f15SJohn Edward Broadbent .jsonValue["Actions"]["#UpdateService.SimpleUpdate"]; 6090554c984SAndrew Geissler updateSvcSimpleUpdate["target"] = 6100554c984SAndrew Geissler "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate"; 6117e860f15SJohn Edward Broadbent updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = 6127e860f15SJohn Edward Broadbent {"TFTP"}; 6130554c984SAndrew Geissler #endif 614274dfe62SJayashankar Padath // Get the current ApplyTime value 6151e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::string>( 6161e1e598dSJonathan Doman *crow::connections::systemBus, "xyz.openbmc_project.Settings", 6171e1e598dSJonathan Doman "/xyz/openbmc_project/software/apply_time", 6181e1e598dSJonathan Doman "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime", 6198d1b46d7Szhanghch05 [asyncResp](const boost::system::error_code ec, 6201e1e598dSJonathan Doman const std::string& applyTime) { 621274dfe62SJayashankar Padath if (ec) 622274dfe62SJayashankar Padath { 623274dfe62SJayashankar Padath BMCWEB_LOG_DEBUG << "DBUS response error " << ec; 6248d1b46d7Szhanghch05 messages::internalError(asyncResp->res); 625274dfe62SJayashankar Padath return; 626274dfe62SJayashankar Padath } 627274dfe62SJayashankar Padath 628274dfe62SJayashankar Padath // Store the ApplyTime Value 6291e1e598dSJonathan Doman if (applyTime == "xyz.openbmc_project.Software.ApplyTime." 6301e1e598dSJonathan Doman "RequestedApplyTimes.Immediate") 631274dfe62SJayashankar Padath { 6328d1b46d7Szhanghch05 asyncResp->res 6337e860f15SJohn Edward Broadbent .jsonValue["HttpPushUriOptions"] 6347e860f15SJohn Edward Broadbent ["HttpPushUriApplyTime"]["ApplyTime"] = 6357e860f15SJohn Edward Broadbent "Immediate"; 636274dfe62SJayashankar Padath } 6371e1e598dSJonathan Doman else if (applyTime == 6381e1e598dSJonathan Doman "xyz.openbmc_project.Software.ApplyTime." 6391e1e598dSJonathan Doman "RequestedApplyTimes.OnReset") 640274dfe62SJayashankar Padath { 6418d1b46d7Szhanghch05 asyncResp->res 6427e860f15SJohn Edward Broadbent .jsonValue["HttpPushUriOptions"] 6437e860f15SJohn Edward Broadbent ["HttpPushUriApplyTime"]["ApplyTime"] = 6447e860f15SJohn Edward Broadbent "OnReset"; 645274dfe62SJayashankar Padath } 6461e1e598dSJonathan Doman }); 6477e860f15SJohn Edward Broadbent }); 6487e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") 649ed398213SEd Tanous .privileges(redfish::privileges::patchUpdateService) 6500fda0f12SGeorge Liu .methods( 65145ca1b86SEd Tanous boost::beast::http::verb::patch)([&app](const crow::Request& req, 65245ca1b86SEd Tanous const std::shared_ptr< 65345ca1b86SEd Tanous bmcweb::AsyncResp>& 65445ca1b86SEd Tanous asyncResp) { 65545ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 65645ca1b86SEd Tanous { 65745ca1b86SEd Tanous return; 65845ca1b86SEd Tanous } 659fa1a5a38SJayashankar Padath BMCWEB_LOG_DEBUG << "doPatch..."; 660fa1a5a38SJayashankar Padath 661274dfe62SJayashankar Padath std::optional<nlohmann::json> pushUriOptions; 66215ed6780SWilly Tu if (!json_util::readJsonPatch(req, asyncResp->res, 66315ed6780SWilly Tu "HttpPushUriOptions", pushUriOptions)) 664fa1a5a38SJayashankar Padath { 665fa1a5a38SJayashankar Padath return; 666fa1a5a38SJayashankar Padath } 667fa1a5a38SJayashankar Padath 668274dfe62SJayashankar Padath if (pushUriOptions) 669274dfe62SJayashankar Padath { 670274dfe62SJayashankar Padath std::optional<nlohmann::json> pushUriApplyTime; 6718d1b46d7Szhanghch05 if (!json_util::readJson(*pushUriOptions, asyncResp->res, 6727e860f15SJohn Edward Broadbent "HttpPushUriApplyTime", 6737e860f15SJohn Edward Broadbent pushUriApplyTime)) 674274dfe62SJayashankar Padath { 675274dfe62SJayashankar Padath return; 676274dfe62SJayashankar Padath } 677274dfe62SJayashankar Padath 678274dfe62SJayashankar Padath if (pushUriApplyTime) 679274dfe62SJayashankar Padath { 680274dfe62SJayashankar Padath std::optional<std::string> applyTime; 6810fda0f12SGeorge Liu if (!json_util::readJson(*pushUriApplyTime, asyncResp->res, 6820fda0f12SGeorge Liu "ApplyTime", applyTime)) 683274dfe62SJayashankar Padath { 684274dfe62SJayashankar Padath return; 685274dfe62SJayashankar Padath } 686274dfe62SJayashankar Padath 687274dfe62SJayashankar Padath if (applyTime) 688fa1a5a38SJayashankar Padath { 689fa1a5a38SJayashankar Padath std::string applyTimeNewVal; 690fa1a5a38SJayashankar Padath if (applyTime == "Immediate") 691fa1a5a38SJayashankar Padath { 692274dfe62SJayashankar Padath applyTimeNewVal = 6930fda0f12SGeorge Liu "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate"; 694fa1a5a38SJayashankar Padath } 695274dfe62SJayashankar Padath else if (applyTime == "OnReset") 696274dfe62SJayashankar Padath { 697274dfe62SJayashankar Padath applyTimeNewVal = 6980fda0f12SGeorge Liu "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset"; 699274dfe62SJayashankar Padath } 700fa1a5a38SJayashankar Padath else 701fa1a5a38SJayashankar Padath { 702274dfe62SJayashankar Padath BMCWEB_LOG_INFO 7030fda0f12SGeorge Liu << "ApplyTime value is not in the list of acceptable values"; 704274dfe62SJayashankar Padath messages::propertyValueNotInList( 705274dfe62SJayashankar Padath asyncResp->res, *applyTime, "ApplyTime"); 706274dfe62SJayashankar Padath return; 707fa1a5a38SJayashankar Padath } 708fa1a5a38SJayashankar Padath 709fa1a5a38SJayashankar Padath // Set the requested image apply time value 710fa1a5a38SJayashankar Padath crow::connections::systemBus->async_method_call( 7110fda0f12SGeorge Liu [asyncResp](const boost::system::error_code ec) { 712fa1a5a38SJayashankar Padath if (ec) 713fa1a5a38SJayashankar Padath { 7147e860f15SJohn Edward Broadbent BMCWEB_LOG_ERROR 7157e860f15SJohn Edward Broadbent << "D-Bus responses error: " << ec; 716fa1a5a38SJayashankar Padath messages::internalError(asyncResp->res); 717fa1a5a38SJayashankar Padath return; 718fa1a5a38SJayashankar Padath } 719fa1a5a38SJayashankar Padath messages::success(asyncResp->res); 720fa1a5a38SJayashankar Padath }, 721fa1a5a38SJayashankar Padath "xyz.openbmc_project.Settings", 722fa1a5a38SJayashankar Padath "/xyz/openbmc_project/software/apply_time", 723fa1a5a38SJayashankar Padath "org.freedesktop.DBus.Properties", "Set", 724274dfe62SJayashankar Padath "xyz.openbmc_project.Software.ApplyTime", 725274dfe62SJayashankar Padath "RequestedApplyTime", 726168e20c1SEd Tanous dbus::utility::DbusVariantType{applyTimeNewVal}); 727fa1a5a38SJayashankar Padath } 728274dfe62SJayashankar Padath } 729fa1a5a38SJayashankar Padath } 7307e860f15SJohn Edward Broadbent }); 731c2051d11SEd Tanous 7324dc23f3fSEd Tanous // The "old" behavior of the update service URI causes redfish-service validator 7334dc23f3fSEd Tanous // failures when the Allow header is supported, given that in the spec, 7344dc23f3fSEd Tanous // UpdateService does not allow POST. in openbmc, we unfortunately reused that 7354dc23f3fSEd Tanous // resource as our HttpPushUri as well. A number of services, including the 7364dc23f3fSEd Tanous // openbmc tests, and documentation have hardcoded that erroneous API, instead 7374dc23f3fSEd Tanous // of relying on HttpPushUri as the spec requires. This option will exist 7384dc23f3fSEd Tanous // temporarily to allow the old behavior until Q4 2022, at which time it will be 7394dc23f3fSEd Tanous // removed. 7404dc23f3fSEd Tanous #ifdef BMCWEB_ENABLE_REDFISH_UPDATESERVICE_OLD_POST_URL 7417e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") 742ed398213SEd Tanous .privileges(redfish::privileges::postUpdateService) 7434dc23f3fSEd Tanous .methods( 7444dc23f3fSEd Tanous boost::beast::http::verb:: 7454dc23f3fSEd Tanous post)([&app]( 7464dc23f3fSEd Tanous const crow::Request& req, 7474dc23f3fSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 7484dc23f3fSEd Tanous asyncResp->res.addHeader( 7494dc23f3fSEd Tanous boost::beast::http::field::warning, 7504dc23f3fSEd Tanous "299 - \"POST to /redfish/v1/UpdateService is deprecated. Use " 7514dc23f3fSEd Tanous "the value contained within HttpPushUri.\""); 7524dc23f3fSEd Tanous handleUpdateServicePost(app, req, asyncResp); 7534dc23f3fSEd Tanous }); 7544dc23f3fSEd Tanous #endif 7554dc23f3fSEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/update/") 7564dc23f3fSEd Tanous .privileges(redfish::privileges::postUpdateService) 7577e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 758c2051d11SEd Tanous std::bind_front(handleUpdateServicePost, std::ref(app))); 759729dae72SJennifer Lee } 760729dae72SJennifer Lee 7617e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventoryCollection(App& app) 7621abe55efSEd Tanous { 7637e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/") 764ed398213SEd Tanous .privileges(redfish::privileges::getSoftwareInventoryCollection) 7651476687dSEd Tanous .methods(boost::beast::http::verb::get)( 7661476687dSEd Tanous [&app](const crow::Request& req, 7671476687dSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 76845ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 76945ca1b86SEd Tanous { 77045ca1b86SEd Tanous return; 77145ca1b86SEd Tanous } 7728d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 7730f74e643SEd Tanous "#SoftwareInventoryCollection.SoftwareInventoryCollection"; 7748d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.id"] = 7750f74e643SEd Tanous "/redfish/v1/UpdateService/FirmwareInventory"; 7761476687dSEd Tanous asyncResp->res.jsonValue["Name"] = 7771476687dSEd Tanous "Software Inventory Collection"; 778c711bf86SEd Tanous 779c711bf86SEd Tanous crow::connections::systemBus->async_method_call( 7801476687dSEd Tanous [asyncResp](const boost::system::error_code ec, 7811476687dSEd Tanous const dbus::utility::MapperGetSubTreeResponse& 7821476687dSEd Tanous subtree) { 7831abe55efSEd Tanous if (ec) 7841abe55efSEd Tanous { 785f12894f8SJason M. Bills messages::internalError(asyncResp->res); 7866c4eb9deSJennifer Lee return; 787729dae72SJennifer Lee } 7887e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["Members"] = 7897e860f15SJohn Edward Broadbent nlohmann::json::array(); 790c711bf86SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = 0; 7916c4eb9deSJennifer Lee 7929eb808c1SEd Tanous for (const auto& obj : subtree) 7931abe55efSEd Tanous { 7942dfd18efSEd Tanous sdbusplus::message::object_path path(obj.first); 7952dfd18efSEd Tanous std::string swId = path.filename(); 7962dfd18efSEd Tanous if (swId.empty()) 797f4b65ab1SJennifer Lee { 798f12894f8SJason M. Bills messages::internalError(asyncResp->res); 799f4b65ab1SJennifer Lee BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!"; 800f4b65ab1SJennifer Lee return; 801f4b65ab1SJennifer Lee } 802f4b65ab1SJennifer Lee 803c711bf86SEd Tanous nlohmann::json& members = 804c711bf86SEd Tanous asyncResp->res.jsonValue["Members"]; 8051476687dSEd Tanous nlohmann::json::object_t member; 8061476687dSEd Tanous member["@odata.id"] = 8070fda0f12SGeorge Liu "/redfish/v1/UpdateService/FirmwareInventory/" + 8081476687dSEd Tanous swId; 8091476687dSEd Tanous members.push_back(std::move(member)); 810e0dd8057SAndrew Geissler asyncResp->res.jsonValue["Members@odata.count"] = 811c711bf86SEd Tanous members.size(); 8126c4eb9deSJennifer Lee } 813c711bf86SEd Tanous }, 8147e860f15SJohn Edward Broadbent // Note that only firmware levels associated with a device 8157e860f15SJohn Edward Broadbent // are stored under /xyz/openbmc_project/software therefore 8167e860f15SJohn Edward Broadbent // to ensure only real FirmwareInventory items are returned, 8177e860f15SJohn Edward Broadbent // this full object path must be used here as input to 8187e860f15SJohn Edward Broadbent // mapper 819c711bf86SEd Tanous "xyz.openbmc_project.ObjectMapper", 820c711bf86SEd Tanous "/xyz/openbmc_project/object_mapper", 8212830a9cfSAndrew Geissler "xyz.openbmc_project.ObjectMapper", "GetSubTree", 8222830a9cfSAndrew Geissler "/xyz/openbmc_project/software", static_cast<int32_t>(0), 8237e860f15SJohn Edward Broadbent std::array<const char*, 1>{ 8247e860f15SJohn Edward Broadbent "xyz.openbmc_project.Software.Version"}); 8257e860f15SJohn Edward Broadbent }); 826729dae72SJennifer Lee } 82787d84729SAndrew Geissler /* Fill related item links (i.e. bmc, bios) in for inventory */ 8287e860f15SJohn Edward Broadbent inline static void 8297e860f15SJohn Edward Broadbent getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& aResp, 83087d84729SAndrew Geissler const std::string& purpose) 83187d84729SAndrew Geissler { 83287d84729SAndrew Geissler if (purpose == fw_util::bmcPurpose) 83387d84729SAndrew Geissler { 8345af690f5SAbhishek Patel nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"]; 8351476687dSEd Tanous nlohmann::json::object_t item; 8361476687dSEd Tanous item["@odata.id"] = "/redfish/v1/Managers/bmc"; 8371476687dSEd Tanous relatedItem.push_back(std::move(item)); 8387e860f15SJohn Edward Broadbent aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size(); 83987d84729SAndrew Geissler } 84087d84729SAndrew Geissler else if (purpose == fw_util::biosPurpose) 84187d84729SAndrew Geissler { 8425af690f5SAbhishek Patel nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"]; 8431476687dSEd Tanous nlohmann::json::object_t item; 8441476687dSEd Tanous item["@odata.id"] = "/redfish/v1/Systems/system/Bios"; 8451476687dSEd Tanous relatedItem.push_back(std::move(item)); 8461a6e51aeSJiaqing Zhao aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size(); 84787d84729SAndrew Geissler } 84887d84729SAndrew Geissler else 84987d84729SAndrew Geissler { 85087d84729SAndrew Geissler BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose; 85187d84729SAndrew Geissler } 85287d84729SAndrew Geissler } 85387d84729SAndrew Geissler 8547e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventory(App& app) 8551abe55efSEd Tanous { 8567e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/") 857ed398213SEd Tanous .privileges(redfish::privileges::getSoftwareInventory) 8587e860f15SJohn Edward Broadbent .methods( 85945ca1b86SEd Tanous boost::beast::http::verb:: 86045ca1b86SEd Tanous get)([&app](const crow::Request& req, 86145ca1b86SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 8627e860f15SJohn Edward Broadbent const std::string& param) { 86345ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 86445ca1b86SEd Tanous { 86545ca1b86SEd Tanous return; 86645ca1b86SEd Tanous } 8673ae837c9SEd Tanous std::shared_ptr<std::string> swId = 8687e860f15SJohn Edward Broadbent std::make_shared<std::string>(param); 869c711bf86SEd Tanous 8708d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.id"] = 8713ae837c9SEd Tanous "/redfish/v1/UpdateService/FirmwareInventory/" + *swId; 872c711bf86SEd Tanous 873c711bf86SEd Tanous crow::connections::systemBus->async_method_call( 874b9d36b47SEd Tanous [asyncResp, 875b9d36b47SEd Tanous swId](const boost::system::error_code ec, 876b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 87755c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "doGet callback..."; 8781abe55efSEd Tanous if (ec) 8791abe55efSEd Tanous { 880f12894f8SJason M. Bills messages::internalError(asyncResp->res); 8816c4eb9deSJennifer Lee return; 8826c4eb9deSJennifer Lee } 8836c4eb9deSJennifer Lee 8846913228dSAndrew Geissler // Ensure we find our input swId, otherwise return an error 8856913228dSAndrew Geissler bool found = false; 8861abe55efSEd Tanous for (const std::pair< 8871abe55efSEd Tanous std::string, 8887e860f15SJohn Edward Broadbent std::vector<std::pair< 8897e860f15SJohn Edward Broadbent std::string, std::vector<std::string>>>>& obj : 8907e860f15SJohn Edward Broadbent subtree) 8911abe55efSEd Tanous { 892e05aec50SEd Tanous if (!boost::ends_with(obj.first, *swId)) 8931abe55efSEd Tanous { 894acb7cfb4SJennifer Lee continue; 895acb7cfb4SJennifer Lee } 896acb7cfb4SJennifer Lee 89726f6976fSEd Tanous if (obj.second.empty()) 8981abe55efSEd Tanous { 899acb7cfb4SJennifer Lee continue; 900acb7cfb4SJennifer Lee } 9016c4eb9deSJennifer Lee 9026913228dSAndrew Geissler found = true; 9037e860f15SJohn Edward Broadbent fw_util::getFwStatus(asyncResp, swId, 9047e860f15SJohn Edward Broadbent obj.second[0].first); 905e0dd8057SAndrew Geissler 90655c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 907168e20c1SEd Tanous [asyncResp, 908168e20c1SEd Tanous swId](const boost::system::error_code errorCode, 909b9d36b47SEd Tanous const dbus::utility::DBusPropertiesMap& 910168e20c1SEd Tanous propertiesList) { 91181ce609eSEd Tanous if (errorCode) 9121abe55efSEd Tanous { 913f12894f8SJason M. Bills messages::internalError(asyncResp->res); 9146c4eb9deSJennifer Lee return; 9156c4eb9deSJennifer Lee } 916b9d36b47SEd Tanous const std::string* swInvPurpose = nullptr; 917b9d36b47SEd Tanous const std::string* version = nullptr; 918b9d36b47SEd Tanous for (const auto& property : propertiesList) 9191abe55efSEd Tanous { 920b9d36b47SEd Tanous if (property.first == "Purpose") 921b9d36b47SEd Tanous { 922b9d36b47SEd Tanous swInvPurpose = std::get_if<std::string>( 923b9d36b47SEd Tanous &property.second); 9246c4eb9deSJennifer Lee } 925b9d36b47SEd Tanous if (property.first == "Version") 926b9d36b47SEd Tanous { 927b9d36b47SEd Tanous version = std::get_if<std::string>( 928b9d36b47SEd Tanous &property.second); 929b9d36b47SEd Tanous } 930b9d36b47SEd Tanous } 931b9d36b47SEd Tanous 9323ae837c9SEd Tanous if (swInvPurpose == nullptr) 9331abe55efSEd Tanous { 9340fda0f12SGeorge Liu BMCWEB_LOG_DEBUG 935b9d36b47SEd Tanous << "Can't find property \"Purpose\"!"; 936b9d36b47SEd Tanous messages::internalError(asyncResp->res); 937acb7cfb4SJennifer Lee return; 938acb7cfb4SJennifer Lee } 939c711bf86SEd Tanous 9403ae837c9SEd Tanous BMCWEB_LOG_DEBUG << "swInvPurpose = " 9413ae837c9SEd Tanous << *swInvPurpose; 942f4b65ab1SJennifer Lee 943f4b65ab1SJennifer Lee if (version == nullptr) 9441abe55efSEd Tanous { 9451abe55efSEd Tanous BMCWEB_LOG_DEBUG 9461abe55efSEd Tanous << "Can't find property \"Version\"!"; 947f12894f8SJason M. Bills 948b9d36b47SEd Tanous messages::internalError(asyncResp->res); 949b9d36b47SEd Tanous 9506c4eb9deSJennifer Lee return; 9516c4eb9deSJennifer Lee } 952c711bf86SEd Tanous asyncResp->res.jsonValue["Version"] = *version; 9533ae837c9SEd Tanous asyncResp->res.jsonValue["Id"] = *swId; 95454daabe7SAndrew Geissler 95554daabe7SAndrew Geissler // swInvPurpose is of format: 95654daabe7SAndrew Geissler // xyz.openbmc_project.Software.Version.VersionPurpose.ABC 957e2e96770SJames Feist // Translate this to "ABC image" 958f23b7296SEd Tanous size_t endDesc = swInvPurpose->rfind('.'); 95954daabe7SAndrew Geissler if (endDesc == std::string::npos) 96054daabe7SAndrew Geissler { 96154daabe7SAndrew Geissler messages::internalError(asyncResp->res); 96254daabe7SAndrew Geissler return; 96354daabe7SAndrew Geissler } 96454daabe7SAndrew Geissler endDesc++; 96554daabe7SAndrew Geissler if (endDesc >= swInvPurpose->size()) 96654daabe7SAndrew Geissler { 96754daabe7SAndrew Geissler messages::internalError(asyncResp->res); 96854daabe7SAndrew Geissler return; 96954daabe7SAndrew Geissler } 97054daabe7SAndrew Geissler 97154daabe7SAndrew Geissler std::string formatDesc = 97254daabe7SAndrew Geissler swInvPurpose->substr(endDesc); 97354daabe7SAndrew Geissler asyncResp->res.jsonValue["Description"] = 974e2e96770SJames Feist formatDesc + " image"; 97587d84729SAndrew Geissler getRelatedItems(asyncResp, *swInvPurpose); 9766c4eb9deSJennifer Lee }, 977c711bf86SEd Tanous obj.second[0].first, obj.first, 978c711bf86SEd Tanous "org.freedesktop.DBus.Properties", "GetAll", 979c711bf86SEd Tanous "xyz.openbmc_project.Software.Version"); 9806c4eb9deSJennifer Lee } 9816913228dSAndrew Geissler if (!found) 9826913228dSAndrew Geissler { 9838cc8edecSEd Tanous BMCWEB_LOG_ERROR << "Input swID " << *swId 9848cc8edecSEd Tanous << " not found!"; 9856913228dSAndrew Geissler messages::resourceMissingAtURI( 9866913228dSAndrew Geissler asyncResp->res, 987ace85d60SEd Tanous crow::utility::urlFromPieces( 988ace85d60SEd Tanous "redfish", "v1", "UpdateService", 989ace85d60SEd Tanous "FirmwareInventory", *swId)); 9906913228dSAndrew Geissler return; 9916913228dSAndrew Geissler } 9924e68c45bSAyushi Smriti asyncResp->res.jsonValue["@odata.type"] = 9934e68c45bSAyushi Smriti "#SoftwareInventory.v1_1_0.SoftwareInventory"; 9944e68c45bSAyushi Smriti asyncResp->res.jsonValue["Name"] = "Software Inventory"; 9954e68c45bSAyushi Smriti asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK"; 9963f8a743aSAppaRao Puli 9973f8a743aSAppaRao Puli asyncResp->res.jsonValue["Updateable"] = false; 9983f8a743aSAppaRao Puli fw_util::getFwUpdateableStatus(asyncResp, swId); 999c711bf86SEd Tanous }, 1000c711bf86SEd Tanous "xyz.openbmc_project.ObjectMapper", 1001c711bf86SEd Tanous "/xyz/openbmc_project/object_mapper", 1002271584abSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 1003271584abSEd Tanous static_cast<int32_t>(0), 10047e860f15SJohn Edward Broadbent std::array<const char*, 1>{ 10057e860f15SJohn Edward Broadbent "xyz.openbmc_project.Software.Version"}); 10067e860f15SJohn Edward Broadbent }); 10076c4eb9deSJennifer Lee } 1008729dae72SJennifer Lee 1009729dae72SJennifer Lee } // namespace redfish 1010