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 18729dae72SJennifer Lee #include "node.hpp" 191abe55efSEd Tanous 20729dae72SJennifer Lee #include <boost/container/flat_map.hpp> 2187d84729SAndrew Geissler #include <utils/fw_utils.hpp> 221214b7e7SGunnar Mills 23abf2add6SEd Tanous #include <variant> 24729dae72SJennifer Lee 251abe55efSEd Tanous namespace redfish 261abe55efSEd Tanous { 2727826b5fSEd Tanous 280e7de46fSAndrew Geissler // Match signals added on software path 29acb7cfb4SJennifer Lee static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher; 304cde5d90SJames Feist static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateErrorMatcher; 310e7de46fSAndrew Geissler // Only allow one update at a time 320e7de46fSAndrew Geissler static bool fwUpdateInProgress = false; 3386adcd6dSAndrew Geissler // Timer for software available 34271584abSEd Tanous static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer; 3586adcd6dSAndrew Geissler 3686adcd6dSAndrew Geissler static void cleanUp() 3786adcd6dSAndrew Geissler { 3886adcd6dSAndrew Geissler fwUpdateInProgress = false; 3986adcd6dSAndrew Geissler fwUpdateMatcher = nullptr; 404cde5d90SJames Feist fwUpdateErrorMatcher = nullptr; 4186adcd6dSAndrew Geissler } 4286adcd6dSAndrew Geissler static void activateImage(const std::string& objPath, 4386adcd6dSAndrew Geissler const std::string& service) 4486adcd6dSAndrew Geissler { 4586adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "Activate image for " << objPath << " " << service; 4686adcd6dSAndrew Geissler crow::connections::systemBus->async_method_call( 47*81ce609eSEd Tanous [](const boost::system::error_code errorCode) { 48*81ce609eSEd Tanous if (errorCode) 4986adcd6dSAndrew Geissler { 50*81ce609eSEd Tanous BMCWEB_LOG_DEBUG << "error_code = " << errorCode; 51*81ce609eSEd Tanous BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message(); 5286adcd6dSAndrew Geissler } 5386adcd6dSAndrew Geissler }, 5486adcd6dSAndrew Geissler service, objPath, "org.freedesktop.DBus.Properties", "Set", 5586adcd6dSAndrew Geissler "xyz.openbmc_project.Software.Activation", "RequestedActivation", 5686adcd6dSAndrew Geissler std::variant<std::string>( 5786adcd6dSAndrew Geissler "xyz.openbmc_project.Software.Activation.RequestedActivations." 5886adcd6dSAndrew Geissler "Active")); 5986adcd6dSAndrew Geissler } 600554c984SAndrew Geissler 610554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr 620554c984SAndrew Geissler // then no asyncResp updates will occur 63b5a76932SEd Tanous static void softwareInterfaceAdded(const std::shared_ptr<AsyncResp>& asyncResp, 64fe306728SJames Feist sdbusplus::message::message& m, 65fe306728SJames Feist const crow::Request& req) 6686adcd6dSAndrew Geissler { 6786adcd6dSAndrew Geissler std::vector<std::pair< 6886adcd6dSAndrew Geissler std::string, 6986adcd6dSAndrew Geissler std::vector<std::pair<std::string, std::variant<std::string>>>>> 7086adcd6dSAndrew Geissler interfacesProperties; 7186adcd6dSAndrew Geissler 7286adcd6dSAndrew Geissler sdbusplus::message::object_path objPath; 7386adcd6dSAndrew Geissler 7486adcd6dSAndrew Geissler m.read(objPath, interfacesProperties); 7586adcd6dSAndrew Geissler 7686adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "obj path = " << objPath.str; 7786adcd6dSAndrew Geissler for (auto& interface : interfacesProperties) 7886adcd6dSAndrew Geissler { 7986adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "interface = " << interface.first; 8086adcd6dSAndrew Geissler 8186adcd6dSAndrew Geissler if (interface.first == "xyz.openbmc_project.Software.Activation") 8286adcd6dSAndrew Geissler { 8386adcd6dSAndrew Geissler // Retrieve service and activate 8486adcd6dSAndrew Geissler crow::connections::systemBus->async_method_call( 85fe306728SJames Feist [objPath, asyncResp, 86*81ce609eSEd Tanous req](const boost::system::error_code errorCode, 8786adcd6dSAndrew Geissler const std::vector<std::pair< 8886adcd6dSAndrew Geissler std::string, std::vector<std::string>>>& objInfo) { 89*81ce609eSEd Tanous if (errorCode) 9086adcd6dSAndrew Geissler { 91*81ce609eSEd Tanous BMCWEB_LOG_DEBUG << "error_code = " << errorCode; 9286adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "error msg = " 93*81ce609eSEd 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; 13332898ceaSJames Feist boost::container::flat_map< 134fd9ab9e1SJames Feist std::string, 135fd9ab9e1SJames Feist std::variant<std::string, uint8_t>> 13632898ceaSJames Feist values; 137fd9ab9e1SJames Feist 138fd9ab9e1SJames Feist std::string index = 139fd9ab9e1SJames Feist std::to_string(taskData->index); 14032898ceaSJames Feist msg.read(iface, values); 141fd9ab9e1SJames Feist 142fd9ab9e1SJames Feist if (iface == "xyz.openbmc_project.Software." 143fd9ab9e1SJames Feist "Activation") 144fd9ab9e1SJames Feist { 14532898ceaSJames Feist auto findActivation = 14632898ceaSJames Feist values.find("Activation"); 14732898ceaSJames Feist if (findActivation == values.end()) 14832898ceaSJames Feist { 14932898ceaSJames Feist return !task::completed; 15032898ceaSJames Feist } 15132898ceaSJames Feist std::string* state = 15232898ceaSJames Feist std::get_if<std::string>( 15332898ceaSJames Feist &(findActivation->second)); 15432898ceaSJames Feist 15532898ceaSJames Feist if (state == nullptr) 15632898ceaSJames Feist { 15732898ceaSJames Feist taskData->messages.emplace_back( 15832898ceaSJames Feist messages::internalError()); 15932898ceaSJames Feist return task::completed; 16032898ceaSJames Feist } 16132898ceaSJames Feist 162fd9ab9e1SJames Feist if (boost::ends_with(*state, 163fd9ab9e1SJames Feist "Invalid") || 16432898ceaSJames Feist boost::ends_with(*state, "Failed")) 16532898ceaSJames Feist { 16632898ceaSJames Feist taskData->state = "Exception"; 16732898ceaSJames Feist taskData->status = "Warning"; 16832898ceaSJames Feist taskData->messages.emplace_back( 169e5d5006bSJames Feist messages::taskAborted(index)); 17032898ceaSJames Feist return task::completed; 17132898ceaSJames Feist } 17232898ceaSJames Feist 17332898ceaSJames Feist if (boost::ends_with(*state, "Staged")) 17432898ceaSJames Feist { 175fd9ab9e1SJames Feist taskData->state = "Stopping"; 176fd9ab9e1SJames Feist taskData->messages.emplace_back( 177fd9ab9e1SJames Feist messages::taskPaused(index)); 178fd9ab9e1SJames Feist 179fd9ab9e1SJames Feist // its staged, set a long timer to 180fd9ab9e1SJames Feist // allow them time to complete the 181fd9ab9e1SJames Feist // update (probably cycle the 182fd9ab9e1SJames Feist // system) if this expires then 183fd9ab9e1SJames Feist // task will be cancelled 184fd9ab9e1SJames Feist taskData->extendTimer( 185fd9ab9e1SJames Feist std::chrono::hours(5)); 18632898ceaSJames Feist return !task::completed; 18732898ceaSJames Feist } 18832898ceaSJames Feist 18932898ceaSJames Feist if (boost::ends_with(*state, "Active")) 19032898ceaSJames Feist { 19132898ceaSJames Feist taskData->messages.emplace_back( 192fd9ab9e1SJames Feist messages::taskCompletedOK( 193fd9ab9e1SJames Feist index)); 19432898ceaSJames Feist taskData->state = "Completed"; 19532898ceaSJames Feist return task::completed; 19632898ceaSJames Feist } 197fd9ab9e1SJames Feist } 198fd9ab9e1SJames Feist else if (iface == 199fd9ab9e1SJames Feist "xyz.openbmc_project.Software." 200fd9ab9e1SJames Feist "ActivationProgress") 201fd9ab9e1SJames Feist { 202fd9ab9e1SJames Feist auto findProgress = 203fd9ab9e1SJames Feist values.find("Progress"); 204fd9ab9e1SJames Feist if (findProgress == values.end()) 205fd9ab9e1SJames Feist { 206fd9ab9e1SJames Feist return !task::completed; 207fd9ab9e1SJames Feist } 208fd9ab9e1SJames Feist uint8_t* progress = 209fd9ab9e1SJames Feist std::get_if<uint8_t>( 210fd9ab9e1SJames Feist &(findProgress->second)); 211fd9ab9e1SJames Feist 212fd9ab9e1SJames Feist if (progress == nullptr) 213fd9ab9e1SJames Feist { 214fd9ab9e1SJames Feist taskData->messages.emplace_back( 215fd9ab9e1SJames Feist messages::internalError()); 216fd9ab9e1SJames Feist return task::completed; 217fd9ab9e1SJames Feist } 218fd9ab9e1SJames Feist taskData->messages.emplace_back( 219fd9ab9e1SJames Feist messages::taskProgressChanged( 220fd9ab9e1SJames Feist index, static_cast<size_t>( 221fd9ab9e1SJames Feist *progress))); 222fd9ab9e1SJames Feist 223fd9ab9e1SJames Feist // if we're getting status updates it's 224fd9ab9e1SJames Feist // still alive, update timer 225fd9ab9e1SJames Feist taskData->extendTimer( 226fd9ab9e1SJames Feist std::chrono::minutes(5)); 227fd9ab9e1SJames Feist } 22832898ceaSJames Feist 22932898ceaSJames Feist // as firmware update often results in a 23032898ceaSJames Feist // reboot, the task may never "complete" 23132898ceaSJames Feist // unless it is an error 23232898ceaSJames Feist 23332898ceaSJames Feist return !task::completed; 23432898ceaSJames Feist }, 23532898ceaSJames Feist "type='signal',interface='org.freedesktop.DBus." 23632898ceaSJames Feist "Properties'," 237fd9ab9e1SJames Feist "member='PropertiesChanged',path='" + 23832898ceaSJames Feist objPath.str + "'"); 23932898ceaSJames Feist task->startTimer(std::chrono::minutes(5)); 24032898ceaSJames Feist task->populateResp(asyncResp->res); 241fe306728SJames Feist task->payload.emplace(req); 2420554c984SAndrew Geissler } 24386adcd6dSAndrew Geissler fwUpdateInProgress = false; 24486adcd6dSAndrew Geissler }, 24586adcd6dSAndrew Geissler "xyz.openbmc_project.ObjectMapper", 24686adcd6dSAndrew Geissler "/xyz/openbmc_project/object_mapper", 24786adcd6dSAndrew Geissler "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str, 24886adcd6dSAndrew Geissler std::array<const char*, 1>{ 24986adcd6dSAndrew Geissler "xyz.openbmc_project.Software.Activation"}); 25086adcd6dSAndrew Geissler } 25186adcd6dSAndrew Geissler } 25286adcd6dSAndrew Geissler } 25386adcd6dSAndrew Geissler 2540554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr 2550554c984SAndrew Geissler // then no asyncResp updates will occur 256b5a76932SEd Tanous static void monitorForSoftwareAvailable( 257b5a76932SEd Tanous const std::shared_ptr<AsyncResp>& asyncResp, const crow::Request& req, 258b5a76932SEd Tanous const std::string& url, int timeoutTimeSeconds = 10) 25986adcd6dSAndrew Geissler { 26086adcd6dSAndrew Geissler // Only allow one FW update at a time 26186adcd6dSAndrew Geissler if (fwUpdateInProgress != false) 26286adcd6dSAndrew Geissler { 2630554c984SAndrew Geissler if (asyncResp) 2640554c984SAndrew Geissler { 26586adcd6dSAndrew Geissler messages::serviceTemporarilyUnavailable(asyncResp->res, "30"); 2660554c984SAndrew Geissler } 26786adcd6dSAndrew Geissler return; 26886adcd6dSAndrew Geissler } 26986adcd6dSAndrew Geissler 2700554c984SAndrew Geissler fwAvailableTimer = 271271584abSEd Tanous std::make_unique<boost::asio::steady_timer>(*req.ioService); 27286adcd6dSAndrew Geissler 273271584abSEd Tanous fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds)); 27486adcd6dSAndrew Geissler 27586adcd6dSAndrew Geissler fwAvailableTimer->async_wait( 27686adcd6dSAndrew Geissler [asyncResp](const boost::system::error_code& ec) { 27786adcd6dSAndrew Geissler cleanUp(); 27886adcd6dSAndrew Geissler if (ec == boost::asio::error::operation_aborted) 27986adcd6dSAndrew Geissler { 28086adcd6dSAndrew Geissler // expected, we were canceled before the timer completed. 28186adcd6dSAndrew Geissler return; 28286adcd6dSAndrew Geissler } 28386adcd6dSAndrew Geissler BMCWEB_LOG_ERROR 28486adcd6dSAndrew Geissler << "Timed out waiting for firmware object being created"; 28586adcd6dSAndrew Geissler BMCWEB_LOG_ERROR 28686adcd6dSAndrew Geissler << "FW image may has already been uploaded to server"; 28786adcd6dSAndrew Geissler if (ec) 28886adcd6dSAndrew Geissler { 28986adcd6dSAndrew Geissler BMCWEB_LOG_ERROR << "Async_wait failed" << ec; 29086adcd6dSAndrew Geissler return; 29186adcd6dSAndrew Geissler } 2920554c984SAndrew Geissler if (asyncResp) 2930554c984SAndrew Geissler { 29486adcd6dSAndrew Geissler redfish::messages::internalError(asyncResp->res); 2950554c984SAndrew Geissler } 29686adcd6dSAndrew Geissler }); 29786adcd6dSAndrew Geissler 298fe306728SJames Feist auto callback = [asyncResp, req](sdbusplus::message::message& m) { 29986adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "Match fired"; 300fe306728SJames Feist softwareInterfaceAdded(asyncResp, m, req); 30186adcd6dSAndrew Geissler }; 30286adcd6dSAndrew Geissler 30386adcd6dSAndrew Geissler fwUpdateInProgress = true; 30486adcd6dSAndrew Geissler 30586adcd6dSAndrew Geissler fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>( 30686adcd6dSAndrew Geissler *crow::connections::systemBus, 30786adcd6dSAndrew Geissler "interface='org.freedesktop.DBus.ObjectManager',type='signal'," 30886adcd6dSAndrew Geissler "member='InterfacesAdded',path='/xyz/openbmc_project/software'", 30986adcd6dSAndrew Geissler callback); 3104cde5d90SJames Feist 3114cde5d90SJames Feist fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match::match>( 3124cde5d90SJames Feist *crow::connections::systemBus, 3134cde5d90SJames Feist "type='signal',member='PropertiesChanged',path_namespace='/xyz/" 3144cde5d90SJames Feist "openbmc_project/logging/entry'," 3154cde5d90SJames Feist "arg0='xyz.openbmc_project.Logging.Entry'", 3164cde5d90SJames Feist [asyncResp, url](sdbusplus::message::message& m) { 3174cde5d90SJames Feist BMCWEB_LOG_DEBUG << "Error Match fired"; 3184cde5d90SJames Feist boost::container::flat_map<std::string, std::variant<std::string>> 3194cde5d90SJames Feist values; 3204cde5d90SJames Feist std::string objName; 3214cde5d90SJames Feist m.read(objName, values); 3224cde5d90SJames Feist auto find = values.find("Message"); 3234cde5d90SJames Feist if (find == values.end()) 3244cde5d90SJames Feist { 3254cde5d90SJames Feist return; 3264cde5d90SJames Feist } 3274cde5d90SJames Feist std::string* type = std::get_if<std::string>(&(find->second)); 3284cde5d90SJames Feist if (type == nullptr) 3294cde5d90SJames Feist { 3304cde5d90SJames Feist return; // if this was our message, timeout will cover it 3314cde5d90SJames Feist } 3324cde5d90SJames Feist if (!boost::starts_with(*type, 3334cde5d90SJames Feist "xyz.openbmc_project.Software.Image.Error")) 3344cde5d90SJames Feist { 3354cde5d90SJames Feist return; 3364cde5d90SJames Feist } 3374cde5d90SJames Feist if (*type == 3384cde5d90SJames Feist "xyz.openbmc_project.Software.Image.Error.UnTarFailure") 3394cde5d90SJames Feist { 3404cde5d90SJames Feist redfish::messages::invalidUpload(asyncResp->res, url, 3414cde5d90SJames Feist "Invalid archive"); 3424cde5d90SJames Feist } 3434cde5d90SJames Feist else if (*type == "xyz.openbmc_project.Software.Image.Error." 3444cde5d90SJames Feist "ManifestFileFailure") 3454cde5d90SJames Feist { 3464cde5d90SJames Feist redfish::messages::invalidUpload(asyncResp->res, url, 3474cde5d90SJames Feist "Invalid manifest"); 3484cde5d90SJames Feist } 3494cde5d90SJames Feist else if (*type == 3504cde5d90SJames Feist "xyz.openbmc_project.Software.Image.Error.ImageFailure") 3514cde5d90SJames Feist { 3524cde5d90SJames Feist redfish::messages::invalidUpload(asyncResp->res, url, 3534cde5d90SJames Feist "Invalid image format"); 3544cde5d90SJames Feist } 3554cde5d90SJames Feist else if (*type == 3564cde5d90SJames Feist "xyz.openbmc_project.Software.Image.Error.BusyFailure") 3574cde5d90SJames Feist { 3584cde5d90SJames Feist redfish::messages::resourceExhaustion(asyncResp->res, url); 3594cde5d90SJames Feist } 3604cde5d90SJames Feist else 3614cde5d90SJames Feist { 3624cde5d90SJames Feist redfish::messages::internalError(asyncResp->res); 3634cde5d90SJames Feist } 3644cde5d90SJames Feist fwAvailableTimer = nullptr; 3654cde5d90SJames Feist }); 36686adcd6dSAndrew Geissler } 367729dae72SJennifer Lee 3680554c984SAndrew Geissler /** 3690554c984SAndrew Geissler * UpdateServiceActionsSimpleUpdate class supports handle POST method for 3700554c984SAndrew Geissler * SimpleUpdate action. 3710554c984SAndrew Geissler */ 3720554c984SAndrew Geissler class UpdateServiceActionsSimpleUpdate : public Node 3730554c984SAndrew Geissler { 3740554c984SAndrew Geissler public: 37552cc112dSEd Tanous UpdateServiceActionsSimpleUpdate(App& app) : 3760554c984SAndrew Geissler Node(app, 3770554c984SAndrew Geissler "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/") 3780554c984SAndrew Geissler { 3790554c984SAndrew Geissler entityPrivileges = { 3800554c984SAndrew Geissler {boost::beast::http::verb::get, {{"Login"}}}, 3810554c984SAndrew Geissler {boost::beast::http::verb::head, {{"Login"}}}, 3820554c984SAndrew Geissler {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, 3830554c984SAndrew Geissler {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 3840554c984SAndrew Geissler {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 3850554c984SAndrew Geissler {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 3860554c984SAndrew Geissler } 3870554c984SAndrew Geissler 3880554c984SAndrew Geissler private: 3890554c984SAndrew Geissler void doPost(crow::Response& res, const crow::Request& req, 390cb13a392SEd Tanous const std::vector<std::string>&) override 3910554c984SAndrew Geissler { 3920554c984SAndrew Geissler std::optional<std::string> transferProtocol; 3930554c984SAndrew Geissler std::string imageURI; 3940554c984SAndrew Geissler std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 3950554c984SAndrew Geissler 3960554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost"; 3970554c984SAndrew Geissler 3980554c984SAndrew Geissler // User can pass in both TransferProtocol and ImageURI parameters or 3994e0453b1SGunnar Mills // they can pass in just the ImageURI with the transfer protocol 4004e0453b1SGunnar Mills // embedded within it. 4010554c984SAndrew Geissler // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin 4020554c984SAndrew Geissler // 2) ImageURI:tftp://1.1.1.1/myfile.bin 4030554c984SAndrew Geissler 4040554c984SAndrew Geissler if (!json_util::readJson(req, asyncResp->res, "TransferProtocol", 4050554c984SAndrew Geissler transferProtocol, "ImageURI", imageURI)) 4060554c984SAndrew Geissler { 4070554c984SAndrew Geissler BMCWEB_LOG_DEBUG 4080554c984SAndrew Geissler << "Missing TransferProtocol or ImageURI parameter"; 4090554c984SAndrew Geissler return; 4100554c984SAndrew Geissler } 4110554c984SAndrew Geissler if (!transferProtocol) 4120554c984SAndrew Geissler { 4130554c984SAndrew Geissler // Must be option 2 4140554c984SAndrew Geissler // Verify ImageURI has transfer protocol in it 415f23b7296SEd Tanous size_t separator = imageURI.find(':'); 4160554c984SAndrew Geissler if ((separator == std::string::npos) || 4170554c984SAndrew Geissler ((separator + 1) > imageURI.size())) 4180554c984SAndrew Geissler { 4190554c984SAndrew Geissler messages::actionParameterValueTypeError( 4200554c984SAndrew Geissler asyncResp->res, imageURI, "ImageURI", 4210554c984SAndrew Geissler "UpdateService.SimpleUpdate"); 4220554c984SAndrew Geissler BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: " 4230554c984SAndrew Geissler << imageURI; 4240554c984SAndrew Geissler return; 4250554c984SAndrew Geissler } 4260554c984SAndrew Geissler transferProtocol = imageURI.substr(0, separator); 4270554c984SAndrew Geissler // Ensure protocol is upper case for a common comparison path below 4280554c984SAndrew Geissler boost::to_upper(*transferProtocol); 4290554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Encoded transfer protocol " 4300554c984SAndrew Geissler << *transferProtocol; 4310554c984SAndrew Geissler 4320554c984SAndrew Geissler // Adjust imageURI to not have the protocol on it for parsing 4330554c984SAndrew Geissler // below 4340554c984SAndrew Geissler // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin 4350554c984SAndrew Geissler imageURI = imageURI.substr(separator + 3); 4360554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI; 4370554c984SAndrew Geissler } 4380554c984SAndrew Geissler 4390554c984SAndrew Geissler // OpenBMC currently only supports TFTP 4400554c984SAndrew Geissler if (*transferProtocol != "TFTP") 4410554c984SAndrew Geissler { 4420554c984SAndrew Geissler messages::actionParameterNotSupported(asyncResp->res, 4430554c984SAndrew Geissler "TransferProtocol", 4440554c984SAndrew Geissler "UpdateService.SimpleUpdate"); 4450554c984SAndrew Geissler BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: " 4460554c984SAndrew Geissler << *transferProtocol; 4470554c984SAndrew Geissler return; 4480554c984SAndrew Geissler } 4490554c984SAndrew Geissler 4500554c984SAndrew Geissler // Format should be <IP or Hostname>/<file> for imageURI 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 << "Invalid ImageURI: " << imageURI; 4590554c984SAndrew Geissler return; 4600554c984SAndrew Geissler } 4610554c984SAndrew Geissler 4620554c984SAndrew Geissler std::string tftpServer = imageURI.substr(0, separator); 4630554c984SAndrew Geissler std::string fwFile = imageURI.substr(separator + 1); 4640554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile; 4650554c984SAndrew Geissler 4660554c984SAndrew Geissler // Setup callback for when new software detected 4672618d5e3SGunnar Mills // Give TFTP 10 minutes to complete 4684cde5d90SJames Feist monitorForSoftwareAvailable( 4694cde5d90SJames Feist nullptr, req, 4704cde5d90SJames Feist "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate", 4712618d5e3SGunnar Mills 600); 4720554c984SAndrew Geissler 4732618d5e3SGunnar Mills // TFTP can take up to 10 minutes depending on image size and 4740554c984SAndrew Geissler // connection speed. Return to caller as soon as the TFTP operation 4750554c984SAndrew Geissler // has been started. The callback above will ensure the activate 4760554c984SAndrew Geissler // is started once the download has completed 4770554c984SAndrew Geissler redfish::messages::success(asyncResp->res); 4780554c984SAndrew Geissler 4790554c984SAndrew Geissler // Call TFTP service 4800554c984SAndrew Geissler crow::connections::systemBus->async_method_call( 4810554c984SAndrew Geissler [](const boost::system::error_code ec) { 4820554c984SAndrew Geissler if (ec) 4830554c984SAndrew Geissler { 4840554c984SAndrew Geissler // messages::internalError(asyncResp->res); 4850554c984SAndrew Geissler cleanUp(); 4860554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "error_code = " << ec; 4870554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "error msg = " << ec.message(); 4880554c984SAndrew Geissler } 4890554c984SAndrew Geissler else 4900554c984SAndrew Geissler { 4910554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success"; 4920554c984SAndrew Geissler } 4930554c984SAndrew Geissler }, 4940554c984SAndrew Geissler "xyz.openbmc_project.Software.Download", 4950554c984SAndrew Geissler "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP", 4960554c984SAndrew Geissler "DownloadViaTFTP", fwFile, tftpServer); 4970554c984SAndrew Geissler 4980554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost"; 4990554c984SAndrew Geissler } 5000554c984SAndrew Geissler }; 5010554c984SAndrew Geissler 5021abe55efSEd Tanous class UpdateService : public Node 5031abe55efSEd Tanous { 504729dae72SJennifer Lee public: 50552cc112dSEd Tanous UpdateService(App& app) : Node(app, "/redfish/v1/UpdateService/") 5061abe55efSEd Tanous { 507729dae72SJennifer Lee entityPrivileges = { 508729dae72SJennifer Lee {boost::beast::http::verb::get, {{"Login"}}}, 509729dae72SJennifer Lee {boost::beast::http::verb::head, {{"Login"}}}, 510729dae72SJennifer Lee {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 511729dae72SJennifer Lee {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 512729dae72SJennifer Lee {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 513729dae72SJennifer Lee {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 514729dae72SJennifer Lee } 515729dae72SJennifer Lee 516729dae72SJennifer Lee private: 517cb13a392SEd Tanous void doGet(crow::Response& res, const crow::Request&, 518cb13a392SEd Tanous const std::vector<std::string>&) override 5191abe55efSEd Tanous { 520274dfe62SJayashankar Padath std::shared_ptr<AsyncResp> aResp = std::make_shared<AsyncResp>(res); 521274dfe62SJayashankar Padath res.jsonValue["@odata.type"] = "#UpdateService.v1_4_0.UpdateService"; 5220f74e643SEd Tanous res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService"; 5230f74e643SEd Tanous res.jsonValue["Id"] = "UpdateService"; 5240f74e643SEd Tanous res.jsonValue["Description"] = "Service for Software Update"; 5250f74e643SEd Tanous res.jsonValue["Name"] = "Update Service"; 5260f74e643SEd Tanous res.jsonValue["HttpPushUri"] = "/redfish/v1/UpdateService"; 5270f74e643SEd Tanous // UpdateService cannot be disabled 5280f74e643SEd Tanous res.jsonValue["ServiceEnabled"] = true; 5290f74e643SEd Tanous res.jsonValue["FirmwareInventory"] = { 5300f74e643SEd Tanous {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}}; 5310554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE 5320554c984SAndrew Geissler // Update Actions object. 5330554c984SAndrew Geissler nlohmann::json& updateSvcSimpleUpdate = 5340554c984SAndrew Geissler res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"]; 5350554c984SAndrew Geissler updateSvcSimpleUpdate["target"] = 5360554c984SAndrew Geissler "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate"; 5370554c984SAndrew Geissler updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = { 5380554c984SAndrew Geissler "TFTP"}; 5390554c984SAndrew Geissler #endif 540274dfe62SJayashankar Padath // Get the current ApplyTime value 541274dfe62SJayashankar Padath crow::connections::systemBus->async_method_call( 542274dfe62SJayashankar Padath [aResp](const boost::system::error_code ec, 543274dfe62SJayashankar Padath const std::variant<std::string>& applyTime) { 544274dfe62SJayashankar Padath if (ec) 545274dfe62SJayashankar Padath { 546274dfe62SJayashankar Padath BMCWEB_LOG_DEBUG << "DBUS response error " << ec; 547274dfe62SJayashankar Padath messages::internalError(aResp->res); 548274dfe62SJayashankar Padath return; 549274dfe62SJayashankar Padath } 550274dfe62SJayashankar Padath 551274dfe62SJayashankar Padath const std::string* s = std::get_if<std::string>(&applyTime); 552274dfe62SJayashankar Padath if (s == nullptr) 553274dfe62SJayashankar Padath { 554274dfe62SJayashankar Padath return; 555274dfe62SJayashankar Padath } 556274dfe62SJayashankar Padath // Store the ApplyTime Value 557274dfe62SJayashankar Padath if (*s == "xyz.openbmc_project.Software.ApplyTime." 558274dfe62SJayashankar Padath "RequestedApplyTimes.Immediate") 559274dfe62SJayashankar Padath { 560274dfe62SJayashankar Padath aResp->res.jsonValue["HttpPushUriOptions"] 561274dfe62SJayashankar Padath ["HttpPushUriApplyTime"]["ApplyTime"] = 562274dfe62SJayashankar Padath "Immediate"; 563274dfe62SJayashankar Padath } 564274dfe62SJayashankar Padath else if (*s == "xyz.openbmc_project.Software.ApplyTime." 565274dfe62SJayashankar Padath "RequestedApplyTimes.OnReset") 566274dfe62SJayashankar Padath { 567274dfe62SJayashankar Padath aResp->res.jsonValue["HttpPushUriOptions"] 568274dfe62SJayashankar Padath ["HttpPushUriApplyTime"]["ApplyTime"] = 569274dfe62SJayashankar Padath "OnReset"; 570274dfe62SJayashankar Padath } 571274dfe62SJayashankar Padath }, 572274dfe62SJayashankar Padath "xyz.openbmc_project.Settings", 573274dfe62SJayashankar Padath "/xyz/openbmc_project/software/apply_time", 574274dfe62SJayashankar Padath "org.freedesktop.DBus.Properties", "Get", 575274dfe62SJayashankar Padath "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime"); 576729dae72SJennifer Lee } 5770e7de46fSAndrew Geissler 578fa1a5a38SJayashankar Padath void doPatch(crow::Response& res, const crow::Request& req, 579cb13a392SEd Tanous const std::vector<std::string>&) override 580fa1a5a38SJayashankar Padath { 581fa1a5a38SJayashankar Padath BMCWEB_LOG_DEBUG << "doPatch..."; 582fa1a5a38SJayashankar Padath 583fa1a5a38SJayashankar Padath std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 584fa1a5a38SJayashankar Padath 585274dfe62SJayashankar Padath std::optional<nlohmann::json> pushUriOptions; 586274dfe62SJayashankar Padath if (!json_util::readJson(req, res, "HttpPushUriOptions", 587274dfe62SJayashankar Padath pushUriOptions)) 588fa1a5a38SJayashankar Padath { 589fa1a5a38SJayashankar Padath return; 590fa1a5a38SJayashankar Padath } 591fa1a5a38SJayashankar Padath 592274dfe62SJayashankar Padath if (pushUriOptions) 593274dfe62SJayashankar Padath { 594274dfe62SJayashankar Padath std::optional<nlohmann::json> pushUriApplyTime; 595274dfe62SJayashankar Padath if (!json_util::readJson(*pushUriOptions, res, 596274dfe62SJayashankar Padath "HttpPushUriApplyTime", pushUriApplyTime)) 597274dfe62SJayashankar Padath { 598274dfe62SJayashankar Padath return; 599274dfe62SJayashankar Padath } 600274dfe62SJayashankar Padath 601274dfe62SJayashankar Padath if (pushUriApplyTime) 602274dfe62SJayashankar Padath { 603274dfe62SJayashankar Padath std::optional<std::string> applyTime; 604274dfe62SJayashankar Padath if (!json_util::readJson(*pushUriApplyTime, res, "ApplyTime", 605274dfe62SJayashankar Padath applyTime)) 606274dfe62SJayashankar Padath { 607274dfe62SJayashankar Padath return; 608274dfe62SJayashankar Padath } 609274dfe62SJayashankar Padath 610274dfe62SJayashankar Padath if (applyTime) 611fa1a5a38SJayashankar Padath { 612fa1a5a38SJayashankar Padath std::string applyTimeNewVal; 613fa1a5a38SJayashankar Padath if (applyTime == "Immediate") 614fa1a5a38SJayashankar Padath { 615274dfe62SJayashankar Padath applyTimeNewVal = 616274dfe62SJayashankar Padath "xyz.openbmc_project.Software.ApplyTime." 617fa1a5a38SJayashankar Padath "RequestedApplyTimes.Immediate"; 618fa1a5a38SJayashankar Padath } 619274dfe62SJayashankar Padath else if (applyTime == "OnReset") 620274dfe62SJayashankar Padath { 621274dfe62SJayashankar Padath applyTimeNewVal = 622274dfe62SJayashankar Padath "xyz.openbmc_project.Software.ApplyTime." 623274dfe62SJayashankar Padath "RequestedApplyTimes.OnReset"; 624274dfe62SJayashankar Padath } 625fa1a5a38SJayashankar Padath else 626fa1a5a38SJayashankar Padath { 627274dfe62SJayashankar Padath BMCWEB_LOG_INFO 628274dfe62SJayashankar Padath << "ApplyTime value is not in the list of " 629274dfe62SJayashankar Padath "acceptable values"; 630274dfe62SJayashankar Padath messages::propertyValueNotInList( 631274dfe62SJayashankar Padath asyncResp->res, *applyTime, "ApplyTime"); 632274dfe62SJayashankar Padath return; 633fa1a5a38SJayashankar Padath } 634fa1a5a38SJayashankar Padath 635fa1a5a38SJayashankar Padath // Set the requested image apply time value 636fa1a5a38SJayashankar Padath crow::connections::systemBus->async_method_call( 637fa1a5a38SJayashankar Padath [asyncResp](const boost::system::error_code ec) { 638fa1a5a38SJayashankar Padath if (ec) 639fa1a5a38SJayashankar Padath { 640274dfe62SJayashankar Padath BMCWEB_LOG_ERROR << "D-Bus responses error: " 641274dfe62SJayashankar Padath << ec; 642fa1a5a38SJayashankar Padath messages::internalError(asyncResp->res); 643fa1a5a38SJayashankar Padath return; 644fa1a5a38SJayashankar Padath } 645fa1a5a38SJayashankar Padath messages::success(asyncResp->res); 646fa1a5a38SJayashankar Padath }, 647fa1a5a38SJayashankar Padath "xyz.openbmc_project.Settings", 648fa1a5a38SJayashankar Padath "/xyz/openbmc_project/software/apply_time", 649fa1a5a38SJayashankar Padath "org.freedesktop.DBus.Properties", "Set", 650274dfe62SJayashankar Padath "xyz.openbmc_project.Software.ApplyTime", 651274dfe62SJayashankar Padath "RequestedApplyTime", 652fa1a5a38SJayashankar Padath std::variant<std::string>{applyTimeNewVal}); 653fa1a5a38SJayashankar Padath } 654274dfe62SJayashankar Padath } 655fa1a5a38SJayashankar Padath } 656fa1a5a38SJayashankar Padath } 657fa1a5a38SJayashankar Padath 658acb7cfb4SJennifer Lee void doPost(crow::Response& res, const crow::Request& req, 659cb13a392SEd Tanous const std::vector<std::string>&) override 6601abe55efSEd Tanous { 661acb7cfb4SJennifer Lee BMCWEB_LOG_DEBUG << "doPost..."; 662acb7cfb4SJennifer Lee 6630e7de46fSAndrew Geissler std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 664acb7cfb4SJennifer Lee 66586adcd6dSAndrew Geissler // Setup callback for when new software detected 6664cde5d90SJames Feist monitorForSoftwareAvailable(asyncResp, req, 6674cde5d90SJames Feist "/redfish/v1/UpdateService"); 668acb7cfb4SJennifer Lee 669acb7cfb4SJennifer Lee std::string filepath( 670acb7cfb4SJennifer Lee "/tmp/images/" + 671acb7cfb4SJennifer Lee boost::uuids::to_string(boost::uuids::random_generator()())); 672acb7cfb4SJennifer Lee BMCWEB_LOG_DEBUG << "Writing file to " << filepath; 673acb7cfb4SJennifer Lee std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary | 674acb7cfb4SJennifer Lee std::ofstream::trunc); 675acb7cfb4SJennifer Lee out << req.body; 676acb7cfb4SJennifer Lee out.close(); 677acb7cfb4SJennifer Lee BMCWEB_LOG_DEBUG << "file upload complete!!"; 678acb7cfb4SJennifer Lee } 679729dae72SJennifer Lee }; 680729dae72SJennifer Lee 6811abe55efSEd Tanous class SoftwareInventoryCollection : public Node 6821abe55efSEd Tanous { 683729dae72SJennifer Lee public: 68452cc112dSEd Tanous SoftwareInventoryCollection(App& app) : 6851abe55efSEd Tanous Node(app, "/redfish/v1/UpdateService/FirmwareInventory/") 6861abe55efSEd Tanous { 687729dae72SJennifer Lee entityPrivileges = { 688729dae72SJennifer Lee {boost::beast::http::verb::get, {{"Login"}}}, 689729dae72SJennifer Lee {boost::beast::http::verb::head, {{"Login"}}}, 690729dae72SJennifer Lee {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 691729dae72SJennifer Lee {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 692729dae72SJennifer Lee {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 693729dae72SJennifer Lee {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 694729dae72SJennifer Lee } 695729dae72SJennifer Lee 696729dae72SJennifer Lee private: 697cb13a392SEd Tanous void doGet(crow::Response& res, const crow::Request&, 698cb13a392SEd Tanous const std::vector<std::string>&) override 6991abe55efSEd Tanous { 700c711bf86SEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 7010f74e643SEd Tanous res.jsonValue["@odata.type"] = 7020f74e643SEd Tanous "#SoftwareInventoryCollection.SoftwareInventoryCollection"; 7030f74e643SEd Tanous res.jsonValue["@odata.id"] = 7040f74e643SEd Tanous "/redfish/v1/UpdateService/FirmwareInventory"; 7050f74e643SEd Tanous res.jsonValue["Name"] = "Software Inventory Collection"; 706c711bf86SEd Tanous 707c711bf86SEd Tanous crow::connections::systemBus->async_method_call( 708c711bf86SEd Tanous [asyncResp]( 709c711bf86SEd Tanous const boost::system::error_code ec, 7106c4eb9deSJennifer Lee const std::vector<std::pair< 7111abe55efSEd Tanous std::string, std::vector<std::pair< 7121214b7e7SGunnar Mills std::string, std::vector<std::string>>>>>& 7131214b7e7SGunnar Mills subtree) { 7141abe55efSEd Tanous if (ec) 7151abe55efSEd Tanous { 716f12894f8SJason M. Bills messages::internalError(asyncResp->res); 7176c4eb9deSJennifer Lee return; 718729dae72SJennifer Lee } 719c711bf86SEd Tanous asyncResp->res.jsonValue["Members"] = nlohmann::json::array(); 720c711bf86SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = 0; 7216c4eb9deSJennifer Lee 7221abe55efSEd Tanous for (auto& obj : subtree) 7231abe55efSEd Tanous { 724f4b65ab1SJennifer Lee // if can't parse fw id then return 72527826b5fSEd Tanous std::size_t idPos; 726f23b7296SEd Tanous if ((idPos = obj.first.rfind('/')) == std::string::npos) 727f4b65ab1SJennifer Lee { 728f12894f8SJason M. Bills messages::internalError(asyncResp->res); 729f4b65ab1SJennifer Lee BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!"; 730f4b65ab1SJennifer Lee return; 731f4b65ab1SJennifer Lee } 732f4b65ab1SJennifer Lee std::string swId = obj.first.substr(idPos + 1); 733f4b65ab1SJennifer Lee 734c711bf86SEd Tanous nlohmann::json& members = 735c711bf86SEd Tanous asyncResp->res.jsonValue["Members"]; 736c711bf86SEd Tanous members.push_back( 737f4b65ab1SJennifer Lee {{"@odata.id", "/redfish/v1/UpdateService/" 7381abe55efSEd Tanous "FirmwareInventory/" + 739f4b65ab1SJennifer Lee swId}}); 740e0dd8057SAndrew Geissler asyncResp->res.jsonValue["Members@odata.count"] = 741c711bf86SEd Tanous members.size(); 7426c4eb9deSJennifer Lee } 743c711bf86SEd Tanous }, 7442830a9cfSAndrew Geissler // Note that only firmware levels associated with a device are 7452830a9cfSAndrew Geissler // stored under /xyz/openbmc_project/software therefore to ensure 7462830a9cfSAndrew Geissler // only real FirmwareInventory items are returned, this full object 7472830a9cfSAndrew Geissler // path must be used here as input to mapper 748c711bf86SEd Tanous "xyz.openbmc_project.ObjectMapper", 749c711bf86SEd Tanous "/xyz/openbmc_project/object_mapper", 7502830a9cfSAndrew Geissler "xyz.openbmc_project.ObjectMapper", "GetSubTree", 7512830a9cfSAndrew Geissler "/xyz/openbmc_project/software", static_cast<int32_t>(0), 7521214b7e7SGunnar Mills std::array<const char*, 1>{"xyz.openbmc_project.Software.Version"}); 753729dae72SJennifer Lee } 754729dae72SJennifer Lee }; 755c711bf86SEd Tanous 7561abe55efSEd Tanous class SoftwareInventory : public Node 7571abe55efSEd Tanous { 758729dae72SJennifer Lee public: 75952cc112dSEd Tanous SoftwareInventory(App& app) : 7601abe55efSEd Tanous Node(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/", 7611abe55efSEd Tanous std::string()) 7621abe55efSEd Tanous { 763729dae72SJennifer Lee entityPrivileges = { 764729dae72SJennifer Lee {boost::beast::http::verb::get, {{"Login"}}}, 765729dae72SJennifer Lee {boost::beast::http::verb::head, {{"Login"}}}, 766729dae72SJennifer Lee {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 767729dae72SJennifer Lee {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 768729dae72SJennifer Lee {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 769729dae72SJennifer Lee {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 770729dae72SJennifer Lee } 771729dae72SJennifer Lee 772729dae72SJennifer Lee private: 77387d84729SAndrew Geissler /* Fill related item links (i.e. bmc, bios) in for inventory */ 774b5a76932SEd Tanous static void getRelatedItems(const std::shared_ptr<AsyncResp>& aResp, 77587d84729SAndrew Geissler const std::string& purpose) 77687d84729SAndrew Geissler { 77787d84729SAndrew Geissler if (purpose == fw_util::bmcPurpose) 77887d84729SAndrew Geissler { 77987d84729SAndrew Geissler nlohmann::json& members = aResp->res.jsonValue["RelatedItem"]; 78087d84729SAndrew Geissler members.push_back({{"@odata.id", "/redfish/v1/Managers/bmc"}}); 78187d84729SAndrew Geissler aResp->res.jsonValue["Members@odata.count"] = members.size(); 78287d84729SAndrew Geissler } 78387d84729SAndrew Geissler else if (purpose == fw_util::biosPurpose) 78487d84729SAndrew Geissler { 785f723d733SGunnar Mills nlohmann::json& members = aResp->res.jsonValue["RelatedItem"]; 786f723d733SGunnar Mills members.push_back( 787f723d733SGunnar Mills {{"@odata.id", "/redfish/v1/Systems/system/Bios"}}); 788f723d733SGunnar Mills aResp->res.jsonValue["Members@odata.count"] = members.size(); 78987d84729SAndrew Geissler } 79087d84729SAndrew Geissler else 79187d84729SAndrew Geissler { 79287d84729SAndrew Geissler BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose; 79387d84729SAndrew Geissler } 79487d84729SAndrew Geissler } 79587d84729SAndrew Geissler 796cb13a392SEd Tanous void doGet(crow::Response& res, const crow::Request&, 7971abe55efSEd Tanous const std::vector<std::string>& params) override 7981abe55efSEd Tanous { 799c711bf86SEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 8006c4eb9deSJennifer Lee 8011abe55efSEd Tanous if (params.size() != 1) 8021abe55efSEd Tanous { 803f12894f8SJason M. Bills messages::internalError(res); 804729dae72SJennifer Lee res.end(); 805729dae72SJennifer Lee return; 806729dae72SJennifer Lee } 807729dae72SJennifer Lee 8083ae837c9SEd Tanous std::shared_ptr<std::string> swId = 809c711bf86SEd Tanous std::make_shared<std::string>(params[0]); 810c711bf86SEd Tanous 81155c7b7a2SEd Tanous res.jsonValue["@odata.id"] = 8123ae837c9SEd Tanous "/redfish/v1/UpdateService/FirmwareInventory/" + *swId; 813c711bf86SEd Tanous 814c711bf86SEd Tanous crow::connections::systemBus->async_method_call( 8153ae837c9SEd Tanous [asyncResp, swId]( 816c711bf86SEd Tanous const boost::system::error_code ec, 8176c4eb9deSJennifer Lee const std::vector<std::pair< 8181abe55efSEd Tanous std::string, std::vector<std::pair< 8191214b7e7SGunnar Mills std::string, std::vector<std::string>>>>>& 8201214b7e7SGunnar Mills subtree) { 82155c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "doGet callback..."; 8221abe55efSEd Tanous if (ec) 8231abe55efSEd Tanous { 824f12894f8SJason M. Bills messages::internalError(asyncResp->res); 8256c4eb9deSJennifer Lee return; 8266c4eb9deSJennifer Lee } 8276c4eb9deSJennifer Lee 8286913228dSAndrew Geissler // Ensure we find our input swId, otherwise return an error 8296913228dSAndrew Geissler bool found = false; 8301abe55efSEd Tanous for (const std::pair< 8311abe55efSEd Tanous std::string, 8321abe55efSEd Tanous std::vector< 8331214b7e7SGunnar Mills std::pair<std::string, std::vector<std::string>>>>& 8341214b7e7SGunnar Mills obj : subtree) 8351abe55efSEd Tanous { 8363ae837c9SEd Tanous if (boost::ends_with(obj.first, *swId) != true) 8371abe55efSEd Tanous { 838acb7cfb4SJennifer Lee continue; 839acb7cfb4SJennifer Lee } 840acb7cfb4SJennifer Lee 841f4b65ab1SJennifer Lee if (obj.second.size() < 1) 8421abe55efSEd Tanous { 843acb7cfb4SJennifer Lee continue; 844acb7cfb4SJennifer Lee } 8456c4eb9deSJennifer Lee 8466913228dSAndrew Geissler found = true; 847e0dd8057SAndrew Geissler fw_util::getFwStatus(asyncResp, swId, obj.second[0].first); 848e0dd8057SAndrew Geissler 84955c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 8501abe55efSEd Tanous [asyncResp, 851*81ce609eSEd Tanous swId](const boost::system::error_code errorCode, 8521abe55efSEd Tanous const boost::container::flat_map< 8531abe55efSEd Tanous std::string, VariantType>& propertiesList) { 854*81ce609eSEd Tanous if (errorCode) 8551abe55efSEd Tanous { 856f12894f8SJason M. Bills messages::internalError(asyncResp->res); 8576c4eb9deSJennifer Lee return; 8586c4eb9deSJennifer Lee } 8591abe55efSEd Tanous boost::container::flat_map< 8601abe55efSEd Tanous std::string, VariantType>::const_iterator it = 8616c4eb9deSJennifer Lee propertiesList.find("Purpose"); 8621abe55efSEd Tanous if (it == propertiesList.end()) 8631abe55efSEd Tanous { 8641abe55efSEd Tanous BMCWEB_LOG_DEBUG 8651abe55efSEd Tanous << "Can't find property \"Purpose\"!"; 866f12894f8SJason M. Bills messages::propertyMissing(asyncResp->res, 867f12894f8SJason M. Bills "Purpose"); 8686c4eb9deSJennifer Lee return; 8696c4eb9deSJennifer Lee } 8703ae837c9SEd Tanous const std::string* swInvPurpose = 871abf2add6SEd Tanous std::get_if<std::string>(&it->second); 8723ae837c9SEd Tanous if (swInvPurpose == nullptr) 8731abe55efSEd Tanous { 8741abe55efSEd Tanous BMCWEB_LOG_DEBUG 8751abe55efSEd Tanous << "wrong types for property\"Purpose\"!"; 876f12894f8SJason M. Bills messages::propertyValueTypeError(asyncResp->res, 877f12894f8SJason M. Bills "", "Purpose"); 878acb7cfb4SJennifer Lee return; 879acb7cfb4SJennifer Lee } 880c711bf86SEd Tanous 8813ae837c9SEd Tanous BMCWEB_LOG_DEBUG << "swInvPurpose = " 8823ae837c9SEd Tanous << *swInvPurpose; 883c711bf86SEd Tanous it = propertiesList.find("Version"); 8841abe55efSEd Tanous if (it == propertiesList.end()) 8851abe55efSEd Tanous { 8861abe55efSEd Tanous BMCWEB_LOG_DEBUG 8871abe55efSEd Tanous << "Can't find property \"Version\"!"; 888f12894f8SJason M. Bills messages::propertyMissing(asyncResp->res, 889f12894f8SJason M. Bills "Version"); 890c711bf86SEd Tanous return; 891acb7cfb4SJennifer Lee } 892acb7cfb4SJennifer Lee 893f4b65ab1SJennifer Lee BMCWEB_LOG_DEBUG << "Version found!"; 894c711bf86SEd Tanous 895f4b65ab1SJennifer Lee const std::string* version = 896abf2add6SEd Tanous std::get_if<std::string>(&it->second); 897f4b65ab1SJennifer Lee 898f4b65ab1SJennifer Lee if (version == nullptr) 8991abe55efSEd Tanous { 9001abe55efSEd Tanous BMCWEB_LOG_DEBUG 9011abe55efSEd Tanous << "Can't find property \"Version\"!"; 902f12894f8SJason M. Bills 903f12894f8SJason M. Bills messages::propertyValueTypeError(asyncResp->res, 904f12894f8SJason M. Bills "", "Version"); 9056c4eb9deSJennifer Lee return; 9066c4eb9deSJennifer Lee } 907c711bf86SEd Tanous asyncResp->res.jsonValue["Version"] = *version; 9083ae837c9SEd Tanous asyncResp->res.jsonValue["Id"] = *swId; 90954daabe7SAndrew Geissler 91054daabe7SAndrew Geissler // swInvPurpose is of format: 91154daabe7SAndrew Geissler // xyz.openbmc_project.Software.Version.VersionPurpose.ABC 912e2e96770SJames Feist // Translate this to "ABC image" 913f23b7296SEd Tanous size_t endDesc = swInvPurpose->rfind('.'); 91454daabe7SAndrew Geissler if (endDesc == std::string::npos) 91554daabe7SAndrew Geissler { 91654daabe7SAndrew Geissler messages::internalError(asyncResp->res); 91754daabe7SAndrew Geissler return; 91854daabe7SAndrew Geissler } 91954daabe7SAndrew Geissler endDesc++; 92054daabe7SAndrew Geissler if (endDesc >= swInvPurpose->size()) 92154daabe7SAndrew Geissler { 92254daabe7SAndrew Geissler messages::internalError(asyncResp->res); 92354daabe7SAndrew Geissler return; 92454daabe7SAndrew Geissler } 92554daabe7SAndrew Geissler 92654daabe7SAndrew Geissler std::string formatDesc = 92754daabe7SAndrew Geissler swInvPurpose->substr(endDesc); 92854daabe7SAndrew Geissler asyncResp->res.jsonValue["Description"] = 929e2e96770SJames Feist formatDesc + " image"; 93087d84729SAndrew Geissler getRelatedItems(asyncResp, *swInvPurpose); 9316c4eb9deSJennifer Lee }, 932c711bf86SEd Tanous obj.second[0].first, obj.first, 933c711bf86SEd Tanous "org.freedesktop.DBus.Properties", "GetAll", 934c711bf86SEd Tanous "xyz.openbmc_project.Software.Version"); 9356c4eb9deSJennifer Lee } 9366913228dSAndrew Geissler if (!found) 9376913228dSAndrew Geissler { 9386913228dSAndrew Geissler BMCWEB_LOG_ERROR << "Input swID " + *swId + " not found!"; 9396913228dSAndrew Geissler messages::resourceMissingAtURI( 9406913228dSAndrew Geissler asyncResp->res, 9416913228dSAndrew Geissler "/redfish/v1/UpdateService/FirmwareInventory/" + *swId); 9426913228dSAndrew Geissler return; 9436913228dSAndrew Geissler } 9444e68c45bSAyushi Smriti asyncResp->res.jsonValue["@odata.type"] = 9454e68c45bSAyushi Smriti "#SoftwareInventory.v1_1_0.SoftwareInventory"; 9464e68c45bSAyushi Smriti asyncResp->res.jsonValue["Name"] = "Software Inventory"; 9474e68c45bSAyushi Smriti asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK"; 9483f8a743aSAppaRao Puli 9493f8a743aSAppaRao Puli asyncResp->res.jsonValue["Updateable"] = false; 9503f8a743aSAppaRao Puli fw_util::getFwUpdateableStatus(asyncResp, swId); 951c711bf86SEd Tanous }, 952c711bf86SEd Tanous "xyz.openbmc_project.ObjectMapper", 953c711bf86SEd Tanous "/xyz/openbmc_project/object_mapper", 954271584abSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 955271584abSEd Tanous static_cast<int32_t>(0), 9561214b7e7SGunnar Mills std::array<const char*, 1>{"xyz.openbmc_project.Software.Version"}); 9576c4eb9deSJennifer Lee } 958729dae72SJennifer Lee }; 959729dae72SJennifer Lee 960729dae72SJennifer Lee } // namespace redfish 961