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 203ccb3adbSEd Tanous #include "app.hpp" 213ccb3adbSEd Tanous #include "dbus_utility.hpp" 220ed80c8cSGeorge Liu #include "multipart_parser.hpp" 232c6ffdb0SEd Tanous #include "ossl_random.hpp" 243ccb3adbSEd Tanous #include "query.hpp" 253ccb3adbSEd Tanous #include "registries/privilege_registry.hpp" 26a8e884fcSEd Tanous #include "task.hpp" 2708d81adaSJohn Edward Broadbent #include "utils/collection.hpp" 283ccb3adbSEd Tanous #include "utils/dbus_utils.hpp" 293ccb3adbSEd Tanous #include "utils/sw_utils.hpp" 303ccb3adbSEd Tanous 31d093c996SEd Tanous #include <boost/algorithm/string/case_conv.hpp> 32e99073f5SGeorge Liu #include <boost/system/error_code.hpp> 33ef4c65b7SEd Tanous #include <boost/url/format.hpp> 341e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp> 353ccb3adbSEd Tanous #include <sdbusplus/bus/match.hpp> 36d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp> 371214b7e7SGunnar Mills 382b73119cSGeorge Liu #include <array> 390ed80c8cSGeorge Liu #include <filesystem> 402b73119cSGeorge Liu #include <string_view> 412b73119cSGeorge Liu 421abe55efSEd Tanous namespace redfish 431abe55efSEd Tanous { 4427826b5fSEd Tanous 450e7de46fSAndrew Geissler // Match signals added on software path 46cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 4759d494eeSPatrick Williams static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateMatcher; 48cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 4959d494eeSPatrick Williams static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateErrorMatcher; 500e7de46fSAndrew Geissler // Only allow one update at a time 51cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 520e7de46fSAndrew Geissler static bool fwUpdateInProgress = false; 5386adcd6dSAndrew Geissler // Timer for software available 54cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 55271584abSEd Tanous static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer; 5686adcd6dSAndrew Geissler 577e860f15SJohn Edward Broadbent inline static void cleanUp() 5886adcd6dSAndrew Geissler { 5986adcd6dSAndrew Geissler fwUpdateInProgress = false; 6086adcd6dSAndrew Geissler fwUpdateMatcher = nullptr; 614cde5d90SJames Feist fwUpdateErrorMatcher = nullptr; 6286adcd6dSAndrew Geissler } 637e860f15SJohn Edward Broadbent inline static void activateImage(const std::string& objPath, 6486adcd6dSAndrew Geissler const std::string& service) 6586adcd6dSAndrew Geissler { 66*62598e31SEd Tanous BMCWEB_LOG_DEBUG("Activate image for {} {}", objPath, service); 679ae226faSGeorge Liu sdbusplus::asio::setProperty( 689ae226faSGeorge Liu *crow::connections::systemBus, service, objPath, 699ae226faSGeorge Liu "xyz.openbmc_project.Software.Activation", "RequestedActivation", 709ae226faSGeorge Liu "xyz.openbmc_project.Software.Activation.RequestedActivations.Active", 718b24275dSEd Tanous [](const boost::system::error_code& ec) { 728b24275dSEd Tanous if (ec) 7386adcd6dSAndrew Geissler { 74*62598e31SEd Tanous BMCWEB_LOG_DEBUG("error_code = {}", ec); 75*62598e31SEd Tanous BMCWEB_LOG_DEBUG("error msg = {}", ec.message()); 7686adcd6dSAndrew Geissler } 779ae226faSGeorge Liu }); 7886adcd6dSAndrew Geissler } 790554c984SAndrew Geissler 800554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr 810554c984SAndrew Geissler // then no asyncResp updates will occur 828d1b46d7Szhanghch05 static void 838d1b46d7Szhanghch05 softwareInterfaceAdded(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 8459d494eeSPatrick Williams sdbusplus::message_t& m, task::Payload&& payload) 8586adcd6dSAndrew Geissler { 86b9d36b47SEd Tanous dbus::utility::DBusInteracesMap interfacesProperties; 8786adcd6dSAndrew Geissler 8886adcd6dSAndrew Geissler sdbusplus::message::object_path objPath; 8986adcd6dSAndrew Geissler 9086adcd6dSAndrew Geissler m.read(objPath, interfacesProperties); 9186adcd6dSAndrew Geissler 92*62598e31SEd Tanous BMCWEB_LOG_DEBUG("obj path = {}", objPath.str); 93e3eb3d63SEd Tanous for (const auto& interface : interfacesProperties) 9486adcd6dSAndrew Geissler { 95*62598e31SEd Tanous BMCWEB_LOG_DEBUG("interface = {}", interface.first); 9686adcd6dSAndrew Geissler 9786adcd6dSAndrew Geissler if (interface.first == "xyz.openbmc_project.Software.Activation") 9886adcd6dSAndrew Geissler { 9986adcd6dSAndrew Geissler // Retrieve service and activate 1002b73119cSGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 1012b73119cSGeorge Liu "xyz.openbmc_project.Software.Activation"}; 1022b73119cSGeorge Liu dbus::utility::getDbusObject( 1032b73119cSGeorge Liu objPath.str, interfaces, 104a3e65892SEd Tanous [objPath, asyncResp, payload(std::move(payload))]( 1058b24275dSEd Tanous const boost::system::error_code& ec, 106a3e65892SEd Tanous const std::vector< 107a3e65892SEd Tanous std::pair<std::string, std::vector<std::string>>>& 108a3e65892SEd Tanous objInfo) mutable { 1098b24275dSEd Tanous if (ec) 11086adcd6dSAndrew Geissler { 111*62598e31SEd Tanous BMCWEB_LOG_DEBUG("error_code = {}", ec); 112*62598e31SEd Tanous BMCWEB_LOG_DEBUG("error msg = {}", ec.message()); 1130554c984SAndrew Geissler if (asyncResp) 1140554c984SAndrew Geissler { 11586adcd6dSAndrew Geissler messages::internalError(asyncResp->res); 1160554c984SAndrew Geissler } 11786adcd6dSAndrew Geissler cleanUp(); 11886adcd6dSAndrew Geissler return; 11986adcd6dSAndrew Geissler } 12086adcd6dSAndrew Geissler // Ensure we only got one service back 12186adcd6dSAndrew Geissler if (objInfo.size() != 1) 12286adcd6dSAndrew Geissler { 123*62598e31SEd Tanous BMCWEB_LOG_ERROR("Invalid Object Size {}", objInfo.size()); 1240554c984SAndrew Geissler if (asyncResp) 1250554c984SAndrew Geissler { 12686adcd6dSAndrew Geissler messages::internalError(asyncResp->res); 1270554c984SAndrew Geissler } 12886adcd6dSAndrew Geissler cleanUp(); 12986adcd6dSAndrew Geissler return; 13086adcd6dSAndrew Geissler } 13186adcd6dSAndrew Geissler // cancel timer only when 13286adcd6dSAndrew Geissler // xyz.openbmc_project.Software.Activation interface 13386adcd6dSAndrew Geissler // is added 13486adcd6dSAndrew Geissler fwAvailableTimer = nullptr; 13586adcd6dSAndrew Geissler 13686adcd6dSAndrew Geissler activateImage(objPath.str, objInfo[0].first); 1370554c984SAndrew Geissler if (asyncResp) 1380554c984SAndrew Geissler { 13932898ceaSJames Feist std::shared_ptr<task::TaskData> task = 14032898ceaSJames Feist task::TaskData::createTask( 1418b24275dSEd Tanous [](const boost::system::error_code& ec2, 14259d494eeSPatrick Williams sdbusplus::message_t& msg, 1431214b7e7SGunnar Mills const std::shared_ptr<task::TaskData>& 1441214b7e7SGunnar Mills taskData) { 1458b24275dSEd Tanous if (ec2) 14632898ceaSJames Feist { 14732898ceaSJames Feist return task::completed; 14832898ceaSJames Feist } 14932898ceaSJames Feist 15032898ceaSJames Feist std::string iface; 151b9d36b47SEd Tanous dbus::utility::DBusPropertiesMap values; 152fd9ab9e1SJames Feist 153002d39b4SEd Tanous std::string index = std::to_string(taskData->index); 15432898ceaSJames Feist msg.read(iface, values); 155fd9ab9e1SJames Feist 156002d39b4SEd Tanous if (iface == "xyz.openbmc_project.Software.Activation") 157fd9ab9e1SJames Feist { 1580fb5b505SGayathri Leburu const std::string* state = nullptr; 159b9d36b47SEd Tanous for (const auto& property : values) 16032898ceaSJames Feist { 161b9d36b47SEd Tanous if (property.first == "Activation") 162b9d36b47SEd Tanous { 1630fb5b505SGayathri Leburu state = std::get_if<std::string>( 164b9d36b47SEd Tanous &property.second); 1650fb5b505SGayathri Leburu if (state == nullptr) 166b9d36b47SEd Tanous { 167002d39b4SEd Tanous taskData->messages.emplace_back( 168002d39b4SEd Tanous messages::internalError()); 169b9d36b47SEd Tanous return task::completed; 170b9d36b47SEd Tanous } 171b9d36b47SEd Tanous } 172b9d36b47SEd Tanous } 17332898ceaSJames Feist 17432898ceaSJames Feist if (state == nullptr) 17532898ceaSJames Feist { 176b9d36b47SEd Tanous return !task::completed; 17732898ceaSJames Feist } 17832898ceaSJames Feist 17911ba3979SEd Tanous if (state->ends_with("Invalid") || 18011ba3979SEd Tanous state->ends_with("Failed")) 18132898ceaSJames Feist { 18232898ceaSJames Feist taskData->state = "Exception"; 18332898ceaSJames Feist taskData->status = "Warning"; 18432898ceaSJames Feist taskData->messages.emplace_back( 185e5d5006bSJames Feist messages::taskAborted(index)); 18632898ceaSJames Feist return task::completed; 18732898ceaSJames Feist } 18832898ceaSJames Feist 18911ba3979SEd Tanous if (state->ends_with("Staged")) 19032898ceaSJames Feist { 191fd9ab9e1SJames Feist taskData->state = "Stopping"; 192fd9ab9e1SJames Feist taskData->messages.emplace_back( 193fd9ab9e1SJames Feist messages::taskPaused(index)); 194fd9ab9e1SJames Feist 195fd9ab9e1SJames Feist // its staged, set a long timer to 196fd9ab9e1SJames Feist // allow them time to complete the 197fd9ab9e1SJames Feist // update (probably cycle the 198fd9ab9e1SJames Feist // system) if this expires then 199fd9ab9e1SJames Feist // task will be cancelled 200002d39b4SEd Tanous taskData->extendTimer(std::chrono::hours(5)); 20132898ceaSJames Feist return !task::completed; 20232898ceaSJames Feist } 20332898ceaSJames Feist 20411ba3979SEd Tanous if (state->ends_with("Active")) 20532898ceaSJames Feist { 20632898ceaSJames Feist taskData->messages.emplace_back( 207002d39b4SEd Tanous messages::taskCompletedOK(index)); 20832898ceaSJames Feist taskData->state = "Completed"; 20932898ceaSJames Feist return task::completed; 21032898ceaSJames Feist } 211fd9ab9e1SJames Feist } 2120fda0f12SGeorge Liu else if ( 2130fda0f12SGeorge Liu iface == 2140fda0f12SGeorge Liu "xyz.openbmc_project.Software.ActivationProgress") 215fd9ab9e1SJames Feist { 216b9d36b47SEd Tanous const uint8_t* progress = nullptr; 217b9d36b47SEd Tanous for (const auto& property : values) 218fd9ab9e1SJames Feist { 219b9d36b47SEd Tanous if (property.first == "Progress") 220b9d36b47SEd Tanous { 2210fb5b505SGayathri Leburu progress = 2220fb5b505SGayathri Leburu std::get_if<uint8_t>(&property.second); 2230fb5b505SGayathri Leburu if (progress == nullptr) 224b9d36b47SEd Tanous { 225002d39b4SEd Tanous taskData->messages.emplace_back( 226002d39b4SEd Tanous messages::internalError()); 227b9d36b47SEd Tanous return task::completed; 228fd9ab9e1SJames Feist } 229b9d36b47SEd Tanous } 230b9d36b47SEd Tanous } 231fd9ab9e1SJames Feist 232fd9ab9e1SJames Feist if (progress == nullptr) 233fd9ab9e1SJames Feist { 234b9d36b47SEd Tanous return !task::completed; 235fd9ab9e1SJames Feist } 2360fb5b505SGayathri Leburu taskData->percentComplete = *progress; 237fd9ab9e1SJames Feist taskData->messages.emplace_back( 2380fb5b505SGayathri Leburu messages::taskProgressChanged(index, 2390fb5b505SGayathri Leburu *progress)); 240fd9ab9e1SJames Feist 241fd9ab9e1SJames Feist // if we're getting status updates it's 242fd9ab9e1SJames Feist // still alive, update timer 243002d39b4SEd Tanous taskData->extendTimer(std::chrono::minutes(5)); 244fd9ab9e1SJames Feist } 24532898ceaSJames Feist 24632898ceaSJames Feist // as firmware update often results in a 24732898ceaSJames Feist // reboot, the task may never "complete" 24832898ceaSJames Feist // unless it is an error 24932898ceaSJames Feist 25032898ceaSJames Feist return !task::completed; 25132898ceaSJames Feist }, 2520fda0f12SGeorge Liu "type='signal',interface='org.freedesktop.DBus.Properties'," 253fd9ab9e1SJames Feist "member='PropertiesChanged',path='" + 25432898ceaSJames Feist objPath.str + "'"); 25532898ceaSJames Feist task->startTimer(std::chrono::minutes(5)); 25632898ceaSJames Feist task->populateResp(asyncResp->res); 257a3e65892SEd Tanous task->payload.emplace(std::move(payload)); 2580554c984SAndrew Geissler } 25986adcd6dSAndrew Geissler fwUpdateInProgress = false; 2602b73119cSGeorge Liu }); 26162bafc01SPatrick Williams 26262bafc01SPatrick Williams break; 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, 2725d138943SGunnar Mills int timeoutTimeSeconds = 25) 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 } 297*62598e31SEd Tanous BMCWEB_LOG_ERROR("Timed out waiting for firmware object being created"); 298*62598e31SEd Tanous BMCWEB_LOG_ERROR("FW image may has already been uploaded to server"); 29986adcd6dSAndrew Geissler if (ec) 30086adcd6dSAndrew Geissler { 301*62598e31SEd Tanous BMCWEB_LOG_ERROR("Async_wait failed{}", ec); 30286adcd6dSAndrew Geissler return; 30386adcd6dSAndrew Geissler } 3040554c984SAndrew Geissler if (asyncResp) 3050554c984SAndrew Geissler { 30686adcd6dSAndrew Geissler redfish::messages::internalError(asyncResp->res); 3070554c984SAndrew Geissler } 30886adcd6dSAndrew Geissler }); 309a3e65892SEd Tanous task::Payload payload(req); 31059d494eeSPatrick Williams auto callback = [asyncResp, payload](sdbusplus::message_t& m) mutable { 311*62598e31SEd Tanous BMCWEB_LOG_DEBUG("Match fired"); 312a3e65892SEd Tanous softwareInterfaceAdded(asyncResp, m, std::move(payload)); 31386adcd6dSAndrew Geissler }; 31486adcd6dSAndrew Geissler 31586adcd6dSAndrew Geissler fwUpdateInProgress = true; 31686adcd6dSAndrew Geissler 31759d494eeSPatrick Williams fwUpdateMatcher = std::make_unique<sdbusplus::bus::match_t>( 31886adcd6dSAndrew Geissler *crow::connections::systemBus, 31986adcd6dSAndrew Geissler "interface='org.freedesktop.DBus.ObjectManager',type='signal'," 32086adcd6dSAndrew Geissler "member='InterfacesAdded',path='/xyz/openbmc_project/software'", 32186adcd6dSAndrew Geissler callback); 3224cde5d90SJames Feist 32359d494eeSPatrick Williams fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match_t>( 3244cde5d90SJames Feist *crow::connections::systemBus, 325e1cc4828SBrian Ma "interface='org.freedesktop.DBus.ObjectManager',type='signal'," 326e1cc4828SBrian Ma "member='InterfacesAdded'," 327e1cc4828SBrian Ma "path='/xyz/openbmc_project/logging'", 32859d494eeSPatrick Williams [asyncResp, url](sdbusplus::message_t& m) { 329002d39b4SEd Tanous std::vector<std::pair<std::string, dbus::utility::DBusPropertiesMap>> 330e1cc4828SBrian Ma interfacesProperties; 331e1cc4828SBrian Ma sdbusplus::message::object_path objPath; 332e1cc4828SBrian Ma m.read(objPath, interfacesProperties); 333*62598e31SEd Tanous BMCWEB_LOG_DEBUG("obj path = {}", objPath.str); 334e1cc4828SBrian Ma for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>& 335e1cc4828SBrian Ma interface : interfacesProperties) 336e1cc4828SBrian Ma { 337e1cc4828SBrian Ma if (interface.first == "xyz.openbmc_project.Logging.Entry") 338e1cc4828SBrian Ma { 339711ac7a9SEd Tanous for (const std::pair<std::string, 340002d39b4SEd Tanous dbus::utility::DbusVariantType>& value : 341002d39b4SEd Tanous interface.second) 3424cde5d90SJames Feist { 343711ac7a9SEd Tanous if (value.first != "Message") 344711ac7a9SEd Tanous { 345711ac7a9SEd Tanous continue; 3464cde5d90SJames Feist } 347e1cc4828SBrian Ma const std::string* type = 348711ac7a9SEd Tanous std::get_if<std::string>(&value.second); 3494cde5d90SJames Feist if (type == nullptr) 3504cde5d90SJames Feist { 351e1cc4828SBrian Ma // if this was our message, timeout will cover it 3524cde5d90SJames Feist return; 3534cde5d90SJames Feist } 354e1cc4828SBrian Ma fwAvailableTimer = nullptr; 3554cde5d90SJames Feist if (*type == 3564cde5d90SJames Feist "xyz.openbmc_project.Software.Image.Error.UnTarFailure") 3574cde5d90SJames Feist { 358002d39b4SEd Tanous redfish::messages::invalidUpload(asyncResp->res, url, 359002d39b4SEd Tanous "Invalid archive"); 3604cde5d90SJames Feist } 361e1cc4828SBrian Ma else if (*type == 362e1cc4828SBrian Ma "xyz.openbmc_project.Software.Image.Error." 363e1cc4828SBrian Ma "ManifestFileFailure") 3644cde5d90SJames Feist { 365002d39b4SEd Tanous redfish::messages::invalidUpload(asyncResp->res, url, 366002d39b4SEd Tanous "Invalid manifest"); 3674cde5d90SJames Feist } 368e1cc4828SBrian Ma else if ( 369e1cc4828SBrian Ma *type == 3704cde5d90SJames Feist "xyz.openbmc_project.Software.Image.Error.ImageFailure") 3714cde5d90SJames Feist { 372e1cc4828SBrian Ma redfish::messages::invalidUpload( 373e1cc4828SBrian Ma asyncResp->res, url, "Invalid image format"); 3744cde5d90SJames Feist } 375e1cc4828SBrian Ma else if ( 376e1cc4828SBrian Ma *type == 3770fda0f12SGeorge Liu "xyz.openbmc_project.Software.Version.Error.AlreadyExists") 37888b3dd12SGunnar Mills { 37988b3dd12SGunnar Mills redfish::messages::invalidUpload( 380e1cc4828SBrian Ma asyncResp->res, url, 381e1cc4828SBrian Ma "Image version already exists"); 38288b3dd12SGunnar Mills 38388b3dd12SGunnar Mills redfish::messages::resourceAlreadyExists( 384d8a5d5d8SJiaqing Zhao asyncResp->res, "UpdateService", "Version", 385e1cc4828SBrian Ma "uploaded version"); 38688b3dd12SGunnar Mills } 387e1cc4828SBrian Ma else if ( 388e1cc4828SBrian Ma *type == 3894cde5d90SJames Feist "xyz.openbmc_project.Software.Image.Error.BusyFailure") 3904cde5d90SJames Feist { 391002d39b4SEd Tanous redfish::messages::resourceExhaustion(asyncResp->res, 392002d39b4SEd Tanous url); 3934cde5d90SJames Feist } 3944cde5d90SJames Feist else 3954cde5d90SJames Feist { 3964cde5d90SJames Feist redfish::messages::internalError(asyncResp->res); 3974cde5d90SJames Feist } 398e1cc4828SBrian Ma } 399e1cc4828SBrian Ma } 400711ac7a9SEd Tanous } 4014cde5d90SJames Feist }); 40286adcd6dSAndrew Geissler } 403729dae72SJennifer Lee 4040554c984SAndrew Geissler /** 4050554c984SAndrew Geissler * UpdateServiceActionsSimpleUpdate class supports handle POST method for 4060554c984SAndrew Geissler * SimpleUpdate action. 4070554c984SAndrew Geissler */ 4087e860f15SJohn Edward Broadbent inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app) 4090554c984SAndrew Geissler { 4107e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 4117e860f15SJohn Edward Broadbent app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/") 412ed398213SEd Tanous .privileges(redfish::privileges::postUpdateService) 413002d39b4SEd Tanous .methods(boost::beast::http::verb::post)( 414002d39b4SEd Tanous [&app](const crow::Request& req, 4157e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 4163ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 41745ca1b86SEd Tanous { 41845ca1b86SEd Tanous return; 41945ca1b86SEd Tanous } 42045ca1b86SEd Tanous 4210554c984SAndrew Geissler std::optional<std::string> transferProtocol; 4220554c984SAndrew Geissler std::string imageURI; 4230554c984SAndrew Geissler 424*62598e31SEd Tanous BMCWEB_LOG_DEBUG("Enter UpdateService.SimpleUpdate doPost"); 4250554c984SAndrew Geissler 4260554c984SAndrew Geissler // User can pass in both TransferProtocol and ImageURI parameters or 4274e0453b1SGunnar Mills // they can pass in just the ImageURI with the transfer protocol 4284e0453b1SGunnar Mills // embedded within it. 4290554c984SAndrew Geissler // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin 4300554c984SAndrew Geissler // 2) ImageURI:tftp://1.1.1.1/myfile.bin 4310554c984SAndrew Geissler 432002d39b4SEd Tanous if (!json_util::readJsonAction(req, asyncResp->res, "TransferProtocol", 433002d39b4SEd Tanous transferProtocol, "ImageURI", imageURI)) 4340554c984SAndrew Geissler { 435*62598e31SEd Tanous BMCWEB_LOG_DEBUG("Missing TransferProtocol or ImageURI parameter"); 4360554c984SAndrew Geissler return; 4370554c984SAndrew Geissler } 4380554c984SAndrew Geissler if (!transferProtocol) 4390554c984SAndrew Geissler { 4400554c984SAndrew Geissler // Must be option 2 4410554c984SAndrew Geissler // Verify ImageURI has transfer protocol in it 442f23b7296SEd Tanous size_t separator = imageURI.find(':'); 4430554c984SAndrew Geissler if ((separator == std::string::npos) || 4440554c984SAndrew Geissler ((separator + 1) > imageURI.size())) 4450554c984SAndrew Geissler { 4460554c984SAndrew Geissler messages::actionParameterValueTypeError( 4470554c984SAndrew Geissler asyncResp->res, imageURI, "ImageURI", 4480554c984SAndrew Geissler "UpdateService.SimpleUpdate"); 449*62598e31SEd Tanous BMCWEB_LOG_ERROR("ImageURI missing transfer protocol: {}", 450*62598e31SEd Tanous imageURI); 4510554c984SAndrew Geissler return; 4520554c984SAndrew Geissler } 4530554c984SAndrew Geissler transferProtocol = imageURI.substr(0, separator); 4547e860f15SJohn Edward Broadbent // Ensure protocol is upper case for a common comparison path 4557e860f15SJohn Edward Broadbent // below 4560554c984SAndrew Geissler boost::to_upper(*transferProtocol); 457*62598e31SEd Tanous BMCWEB_LOG_DEBUG("Encoded transfer protocol {}", *transferProtocol); 4580554c984SAndrew Geissler 4590554c984SAndrew Geissler // Adjust imageURI to not have the protocol on it for parsing 4600554c984SAndrew Geissler // below 4610554c984SAndrew Geissler // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin 4620554c984SAndrew Geissler imageURI = imageURI.substr(separator + 3); 463*62598e31SEd Tanous BMCWEB_LOG_DEBUG("Adjusted imageUri {}", imageURI); 4640554c984SAndrew Geissler } 4650554c984SAndrew Geissler 4660554c984SAndrew Geissler // OpenBMC currently only supports TFTP 4670554c984SAndrew Geissler if (*transferProtocol != "TFTP") 4680554c984SAndrew Geissler { 469002d39b4SEd Tanous messages::actionParameterNotSupported(asyncResp->res, 470002d39b4SEd Tanous "TransferProtocol", 4710554c984SAndrew Geissler "UpdateService.SimpleUpdate"); 472*62598e31SEd Tanous BMCWEB_LOG_ERROR("Request incorrect protocol parameter: {}", 473*62598e31SEd Tanous *transferProtocol); 4740554c984SAndrew Geissler return; 4750554c984SAndrew Geissler } 4760554c984SAndrew Geissler 4770554c984SAndrew Geissler // Format should be <IP or Hostname>/<file> for imageURI 478f23b7296SEd Tanous size_t separator = imageURI.find('/'); 4790554c984SAndrew Geissler if ((separator == std::string::npos) || 4800554c984SAndrew Geissler ((separator + 1) > imageURI.size())) 4810554c984SAndrew Geissler { 4820554c984SAndrew Geissler messages::actionParameterValueTypeError( 4830554c984SAndrew Geissler asyncResp->res, imageURI, "ImageURI", 4840554c984SAndrew Geissler "UpdateService.SimpleUpdate"); 485*62598e31SEd Tanous BMCWEB_LOG_ERROR("Invalid ImageURI: {}", imageURI); 4860554c984SAndrew Geissler return; 4870554c984SAndrew Geissler } 4880554c984SAndrew Geissler 4890554c984SAndrew Geissler std::string tftpServer = imageURI.substr(0, separator); 4900554c984SAndrew Geissler std::string fwFile = imageURI.substr(separator + 1); 491*62598e31SEd Tanous BMCWEB_LOG_DEBUG("Server: {}{}", tftpServer + " File: ", fwFile); 4920554c984SAndrew Geissler 4930554c984SAndrew Geissler // Setup callback for when new software detected 4942618d5e3SGunnar Mills // Give TFTP 10 minutes to complete 4954cde5d90SJames Feist monitorForSoftwareAvailable( 496d7a596bdSAlbert Zhang asyncResp, req, 4974cde5d90SJames Feist "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate", 4982618d5e3SGunnar Mills 600); 4990554c984SAndrew Geissler 5002618d5e3SGunnar Mills // TFTP can take up to 10 minutes depending on image size and 5010554c984SAndrew Geissler // connection speed. Return to caller as soon as the TFTP operation 5020554c984SAndrew Geissler // has been started. The callback above will ensure the activate 5030554c984SAndrew Geissler // is started once the download has completed 5040554c984SAndrew Geissler redfish::messages::success(asyncResp->res); 5050554c984SAndrew Geissler 5060554c984SAndrew Geissler // Call TFTP service 5070554c984SAndrew Geissler crow::connections::systemBus->async_method_call( 5085e7e2dc5SEd Tanous [](const boost::system::error_code& ec) { 5090554c984SAndrew Geissler if (ec) 5100554c984SAndrew Geissler { 5110554c984SAndrew Geissler // messages::internalError(asyncResp->res); 5120554c984SAndrew Geissler cleanUp(); 513*62598e31SEd Tanous BMCWEB_LOG_DEBUG("error_code = {}", ec); 514*62598e31SEd Tanous BMCWEB_LOG_DEBUG("error msg = {}", ec.message()); 5150554c984SAndrew Geissler } 5160554c984SAndrew Geissler else 5170554c984SAndrew Geissler { 518*62598e31SEd Tanous BMCWEB_LOG_DEBUG("Call to DownloaViaTFTP Success"); 5190554c984SAndrew Geissler } 5200554c984SAndrew Geissler }, 5210554c984SAndrew Geissler "xyz.openbmc_project.Software.Download", 522002d39b4SEd Tanous "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP", 523002d39b4SEd Tanous "DownloadViaTFTP", fwFile, tftpServer); 5240554c984SAndrew Geissler 525*62598e31SEd Tanous BMCWEB_LOG_DEBUG("Exit UpdateService.SimpleUpdate doPost"); 5267e860f15SJohn Edward Broadbent }); 527729dae72SJennifer Lee } 528729dae72SJennifer Lee 5290ed80c8cSGeorge Liu inline void uploadImageFile(crow::Response& res, std::string_view body) 5300ed80c8cSGeorge Liu { 5312c6ffdb0SEd Tanous std::filesystem::path filepath("/tmp/images/" + bmcweb::getRandomUUID()); 5322c6ffdb0SEd Tanous 533*62598e31SEd Tanous BMCWEB_LOG_DEBUG("Writing file to {}", filepath.string()); 5340ed80c8cSGeorge Liu std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary | 5350ed80c8cSGeorge Liu std::ofstream::trunc); 5360ed80c8cSGeorge Liu // set the permission of the file to 640 53789492a15SPatrick Williams std::filesystem::perms permission = std::filesystem::perms::owner_read | 53889492a15SPatrick Williams std::filesystem::perms::group_read; 5390ed80c8cSGeorge Liu std::filesystem::permissions(filepath, permission); 5400ed80c8cSGeorge Liu out << body; 5410ed80c8cSGeorge Liu 5420ed80c8cSGeorge Liu if (out.bad()) 5430ed80c8cSGeorge Liu { 5440ed80c8cSGeorge Liu messages::internalError(res); 5450ed80c8cSGeorge Liu cleanUp(); 5460ed80c8cSGeorge Liu } 5470ed80c8cSGeorge Liu } 5480ed80c8cSGeorge Liu 5490ed80c8cSGeorge Liu inline void setApplyTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 5500ed80c8cSGeorge Liu const std::string& applyTime) 5510ed80c8cSGeorge Liu { 5520ed80c8cSGeorge Liu std::string applyTimeNewVal; 5530ed80c8cSGeorge Liu if (applyTime == "Immediate") 5540ed80c8cSGeorge Liu { 5550ed80c8cSGeorge Liu applyTimeNewVal = 5560ed80c8cSGeorge Liu "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate"; 5570ed80c8cSGeorge Liu } 5580ed80c8cSGeorge Liu else if (applyTime == "OnReset") 5590ed80c8cSGeorge Liu { 5600ed80c8cSGeorge Liu applyTimeNewVal = 5610ed80c8cSGeorge Liu "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset"; 5620ed80c8cSGeorge Liu } 5630ed80c8cSGeorge Liu else 5640ed80c8cSGeorge Liu { 565*62598e31SEd Tanous BMCWEB_LOG_INFO( 566*62598e31SEd Tanous "ApplyTime value is not in the list of acceptable values"); 5670ed80c8cSGeorge Liu messages::propertyValueNotInList(asyncResp->res, applyTime, 5680ed80c8cSGeorge Liu "ApplyTime"); 5690ed80c8cSGeorge Liu return; 5700ed80c8cSGeorge Liu } 5710ed80c8cSGeorge Liu 5720ed80c8cSGeorge Liu // Set the requested image apply time value 5739ae226faSGeorge Liu sdbusplus::asio::setProperty( 5749ae226faSGeorge Liu *crow::connections::systemBus, "xyz.openbmc_project.Settings", 5759ae226faSGeorge Liu "/xyz/openbmc_project/software/apply_time", 5769ae226faSGeorge Liu "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime", 5779ae226faSGeorge Liu applyTimeNewVal, [asyncResp](const boost::system::error_code& ec) { 5780ed80c8cSGeorge Liu if (ec) 5790ed80c8cSGeorge Liu { 580*62598e31SEd Tanous BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec); 5810ed80c8cSGeorge Liu messages::internalError(asyncResp->res); 5820ed80c8cSGeorge Liu return; 5830ed80c8cSGeorge Liu } 5840ed80c8cSGeorge Liu messages::success(asyncResp->res); 5859ae226faSGeorge Liu }); 5860ed80c8cSGeorge Liu } 5870ed80c8cSGeorge Liu 5880ed80c8cSGeorge Liu inline void 5890ed80c8cSGeorge Liu updateMultipartContext(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 5900ed80c8cSGeorge Liu const MultipartParser& parser) 5910ed80c8cSGeorge Liu { 5920ed80c8cSGeorge Liu const std::string* uploadData = nullptr; 5930ed80c8cSGeorge Liu std::optional<std::string> applyTime = "OnReset"; 5940ed80c8cSGeorge Liu bool targetFound = false; 5950ed80c8cSGeorge Liu for (const FormPart& formpart : parser.mime_fields) 5960ed80c8cSGeorge Liu { 5970ed80c8cSGeorge Liu boost::beast::http::fields::const_iterator it = 5980ed80c8cSGeorge Liu formpart.fields.find("Content-Disposition"); 5990ed80c8cSGeorge Liu if (it == formpart.fields.end()) 6000ed80c8cSGeorge Liu { 601*62598e31SEd Tanous BMCWEB_LOG_ERROR("Couldn't find Content-Disposition"); 6020ed80c8cSGeorge Liu return; 6030ed80c8cSGeorge Liu } 604*62598e31SEd Tanous BMCWEB_LOG_INFO("Parsing value {}", it->value()); 6050ed80c8cSGeorge Liu 6060ed80c8cSGeorge Liu // The construction parameters of param_list must start with `;` 6070ed80c8cSGeorge Liu size_t index = it->value().find(';'); 6080ed80c8cSGeorge Liu if (index == std::string::npos) 6090ed80c8cSGeorge Liu { 6100ed80c8cSGeorge Liu continue; 6110ed80c8cSGeorge Liu } 6120ed80c8cSGeorge Liu 61389492a15SPatrick Williams for (const auto& param : 6140ed80c8cSGeorge Liu boost::beast::http::param_list{it->value().substr(index)}) 6150ed80c8cSGeorge Liu { 6160ed80c8cSGeorge Liu if (param.first != "name" || param.second.empty()) 6170ed80c8cSGeorge Liu { 6180ed80c8cSGeorge Liu continue; 6190ed80c8cSGeorge Liu } 6200ed80c8cSGeorge Liu 6210ed80c8cSGeorge Liu if (param.second == "UpdateParameters") 6220ed80c8cSGeorge Liu { 6230ed80c8cSGeorge Liu std::vector<std::string> targets; 6240ed80c8cSGeorge Liu nlohmann::json content = 6250ed80c8cSGeorge Liu nlohmann::json::parse(formpart.content); 6260ed80c8cSGeorge Liu if (!json_util::readJson(content, asyncResp->res, "Targets", 6270ed80c8cSGeorge Liu targets, "@Redfish.OperationApplyTime", 6280ed80c8cSGeorge Liu applyTime)) 6290ed80c8cSGeorge Liu { 6300ed80c8cSGeorge Liu return; 6310ed80c8cSGeorge Liu } 6320ed80c8cSGeorge Liu if (targets.size() != 1) 6330ed80c8cSGeorge Liu { 6340ed80c8cSGeorge Liu messages::propertyValueFormatError(asyncResp->res, 6350ed80c8cSGeorge Liu "Targets", ""); 6360ed80c8cSGeorge Liu return; 6370ed80c8cSGeorge Liu } 6380ed80c8cSGeorge Liu if (targets[0] != "/redfish/v1/Managers/bmc") 6390ed80c8cSGeorge Liu { 6400ed80c8cSGeorge Liu messages::propertyValueNotInList(asyncResp->res, 6410ed80c8cSGeorge Liu "Targets/0", targets[0]); 6420ed80c8cSGeorge Liu return; 6430ed80c8cSGeorge Liu } 6440ed80c8cSGeorge Liu targetFound = true; 6450ed80c8cSGeorge Liu } 6460ed80c8cSGeorge Liu else if (param.second == "UpdateFile") 6470ed80c8cSGeorge Liu { 6480ed80c8cSGeorge Liu uploadData = &(formpart.content); 6490ed80c8cSGeorge Liu } 6500ed80c8cSGeorge Liu } 6510ed80c8cSGeorge Liu } 6520ed80c8cSGeorge Liu 6530ed80c8cSGeorge Liu if (uploadData == nullptr) 6540ed80c8cSGeorge Liu { 655*62598e31SEd Tanous BMCWEB_LOG_ERROR("Upload data is NULL"); 6560ed80c8cSGeorge Liu messages::propertyMissing(asyncResp->res, "UpdateFile"); 6570ed80c8cSGeorge Liu return; 6580ed80c8cSGeorge Liu } 6590ed80c8cSGeorge Liu if (!targetFound) 6600ed80c8cSGeorge Liu { 6610ed80c8cSGeorge Liu messages::propertyMissing(asyncResp->res, "targets"); 6620ed80c8cSGeorge Liu return; 6630ed80c8cSGeorge Liu } 6640ed80c8cSGeorge Liu 6650ed80c8cSGeorge Liu setApplyTime(asyncResp, *applyTime); 6660ed80c8cSGeorge Liu 6670ed80c8cSGeorge Liu uploadImageFile(asyncResp->res, *uploadData); 6680ed80c8cSGeorge Liu } 6690ed80c8cSGeorge Liu 670c2051d11SEd Tanous inline void 671c2051d11SEd Tanous handleUpdateServicePost(App& app, const crow::Request& req, 672c2051d11SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 673c2051d11SEd Tanous { 6743ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 675c2051d11SEd Tanous { 676c2051d11SEd Tanous return; 677c2051d11SEd Tanous } 678b33a4327SNinad Palsule std::string_view contentType = req.getHeaderValue("Content-Type"); 679b33a4327SNinad Palsule 680*62598e31SEd Tanous BMCWEB_LOG_DEBUG("doPost: contentType={}", contentType); 681b33a4327SNinad Palsule 682b33a4327SNinad Palsule // Make sure that content type is application/octet-stream or 683b33a4327SNinad Palsule // multipart/form-data 684b33a4327SNinad Palsule if (boost::iequals(contentType, "application/octet-stream")) 685b33a4327SNinad Palsule { 686b33a4327SNinad Palsule // Setup callback for when new software detected 687b33a4327SNinad Palsule monitorForSoftwareAvailable(asyncResp, req, 688b33a4327SNinad Palsule "/redfish/v1/UpdateService"); 689b33a4327SNinad Palsule 690b33a4327SNinad Palsule uploadImageFile(asyncResp->res, req.body()); 691b33a4327SNinad Palsule } 692b33a4327SNinad Palsule else if (contentType.starts_with("multipart/form-data")) 693b33a4327SNinad Palsule { 694b33a4327SNinad Palsule MultipartParser parser; 695c2051d11SEd Tanous 696c2051d11SEd Tanous // Setup callback for when new software detected 697b33a4327SNinad Palsule monitorForSoftwareAvailable(asyncResp, req, 698b33a4327SNinad Palsule "/redfish/v1/UpdateService"); 699c2051d11SEd Tanous 7000ed80c8cSGeorge Liu ParserError ec = parser.parse(req); 7010ed80c8cSGeorge Liu if (ec != ParserError::PARSER_SUCCESS) 7020ed80c8cSGeorge Liu { 7030ed80c8cSGeorge Liu // handle error 704*62598e31SEd Tanous BMCWEB_LOG_ERROR("MIME parse failed, ec : {}", 705*62598e31SEd Tanous static_cast<int>(ec)); 7060ed80c8cSGeorge Liu messages::internalError(asyncResp->res); 7070ed80c8cSGeorge Liu return; 7080ed80c8cSGeorge Liu } 7090ed80c8cSGeorge Liu updateMultipartContext(asyncResp, parser); 710c2051d11SEd Tanous } 711b33a4327SNinad Palsule else 712b33a4327SNinad Palsule { 713*62598e31SEd Tanous BMCWEB_LOG_DEBUG("Bad content type specified:{}", contentType); 714b33a4327SNinad Palsule asyncResp->res.result(boost::beast::http::status::bad_request); 715b33a4327SNinad Palsule } 716b33a4327SNinad Palsule } 717c2051d11SEd Tanous 7187e860f15SJohn Edward Broadbent inline void requestRoutesUpdateService(App& app) 7191abe55efSEd Tanous { 7207e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") 721ed398213SEd Tanous .privileges(redfish::privileges::getUpdateService) 722002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 723002d39b4SEd Tanous [&app](const crow::Request& req, 724002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 7253ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 72645ca1b86SEd Tanous { 72745ca1b86SEd Tanous return; 72845ca1b86SEd Tanous } 7298d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 7300ed80c8cSGeorge Liu "#UpdateService.v1_11_1.UpdateService"; 7318d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService"; 7328d1b46d7Szhanghch05 asyncResp->res.jsonValue["Id"] = "UpdateService"; 733002d39b4SEd Tanous asyncResp->res.jsonValue["Description"] = "Service for Software Update"; 7348d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "Update Service"; 7354dc23f3fSEd Tanous 7367e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["HttpPushUri"] = 7374dc23f3fSEd Tanous "/redfish/v1/UpdateService/update"; 7380ed80c8cSGeorge Liu asyncResp->res.jsonValue["MultipartHttpPushUri"] = 7390ed80c8cSGeorge Liu "/redfish/v1/UpdateService/update"; 7404dc23f3fSEd Tanous 7410f74e643SEd Tanous // UpdateService cannot be disabled 7428d1b46d7Szhanghch05 asyncResp->res.jsonValue["ServiceEnabled"] = true; 7431476687dSEd Tanous asyncResp->res.jsonValue["FirmwareInventory"]["@odata.id"] = 7441476687dSEd Tanous "/redfish/v1/UpdateService/FirmwareInventory"; 745d61e5194STejas Patil // Get the MaxImageSizeBytes 746d61e5194STejas Patil asyncResp->res.jsonValue["MaxImageSizeBytes"] = 747d61e5194STejas Patil bmcwebHttpReqBodyLimitMb * 1024 * 1024; 748d61e5194STejas Patil 7490554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE 7500554c984SAndrew Geissler // Update Actions object. 7510554c984SAndrew Geissler nlohmann::json& updateSvcSimpleUpdate = 752002d39b4SEd Tanous asyncResp->res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"]; 7530554c984SAndrew Geissler updateSvcSimpleUpdate["target"] = 7540554c984SAndrew Geissler "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate"; 755002d39b4SEd Tanous updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = { 756002d39b4SEd Tanous "TFTP"}; 7570554c984SAndrew Geissler #endif 758274dfe62SJayashankar Padath // Get the current ApplyTime value 7591e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::string>( 7601e1e598dSJonathan Doman *crow::connections::systemBus, "xyz.openbmc_project.Settings", 7611e1e598dSJonathan Doman "/xyz/openbmc_project/software/apply_time", 7621e1e598dSJonathan Doman "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime", 7635e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec, 7641e1e598dSJonathan Doman const std::string& applyTime) { 765274dfe62SJayashankar Padath if (ec) 766274dfe62SJayashankar Padath { 767*62598e31SEd Tanous BMCWEB_LOG_DEBUG("DBUS response error {}", ec); 7688d1b46d7Szhanghch05 messages::internalError(asyncResp->res); 769274dfe62SJayashankar Padath return; 770274dfe62SJayashankar Padath } 771274dfe62SJayashankar Padath 772274dfe62SJayashankar Padath // Store the ApplyTime Value 7731e1e598dSJonathan Doman if (applyTime == "xyz.openbmc_project.Software.ApplyTime." 7741e1e598dSJonathan Doman "RequestedApplyTimes.Immediate") 775274dfe62SJayashankar Padath { 776002d39b4SEd Tanous asyncResp->res.jsonValue["HttpPushUriOptions"] 7777e860f15SJohn Edward Broadbent ["HttpPushUriApplyTime"]["ApplyTime"] = 7787e860f15SJohn Edward Broadbent "Immediate"; 779274dfe62SJayashankar Padath } 780002d39b4SEd Tanous else if (applyTime == "xyz.openbmc_project.Software.ApplyTime." 7811e1e598dSJonathan Doman "RequestedApplyTimes.OnReset") 782274dfe62SJayashankar Padath { 783002d39b4SEd Tanous asyncResp->res.jsonValue["HttpPushUriOptions"] 7847e860f15SJohn Edward Broadbent ["HttpPushUriApplyTime"]["ApplyTime"] = 7857e860f15SJohn Edward Broadbent "OnReset"; 786274dfe62SJayashankar Padath } 7871e1e598dSJonathan Doman }); 7887e860f15SJohn Edward Broadbent }); 7897e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") 790ed398213SEd Tanous .privileges(redfish::privileges::patchUpdateService) 791002d39b4SEd Tanous .methods(boost::beast::http::verb::patch)( 792002d39b4SEd Tanous [&app](const crow::Request& req, 793002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 7943ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 79545ca1b86SEd Tanous { 79645ca1b86SEd Tanous return; 79745ca1b86SEd Tanous } 798*62598e31SEd Tanous BMCWEB_LOG_DEBUG("doPatch..."); 799fa1a5a38SJayashankar Padath 800274dfe62SJayashankar Padath std::optional<nlohmann::json> pushUriOptions; 801002d39b4SEd Tanous if (!json_util::readJsonPatch(req, asyncResp->res, "HttpPushUriOptions", 802002d39b4SEd Tanous pushUriOptions)) 803fa1a5a38SJayashankar Padath { 804fa1a5a38SJayashankar Padath return; 805fa1a5a38SJayashankar Padath } 806fa1a5a38SJayashankar Padath 807274dfe62SJayashankar Padath if (pushUriOptions) 808274dfe62SJayashankar Padath { 809274dfe62SJayashankar Padath std::optional<nlohmann::json> pushUriApplyTime; 8108d1b46d7Szhanghch05 if (!json_util::readJson(*pushUriOptions, asyncResp->res, 811002d39b4SEd Tanous "HttpPushUriApplyTime", pushUriApplyTime)) 812274dfe62SJayashankar Padath { 813274dfe62SJayashankar Padath return; 814274dfe62SJayashankar Padath } 815274dfe62SJayashankar Padath 816274dfe62SJayashankar Padath if (pushUriApplyTime) 817274dfe62SJayashankar Padath { 818274dfe62SJayashankar Padath std::optional<std::string> applyTime; 8190fda0f12SGeorge Liu if (!json_util::readJson(*pushUriApplyTime, asyncResp->res, 8200fda0f12SGeorge Liu "ApplyTime", applyTime)) 821274dfe62SJayashankar Padath { 822274dfe62SJayashankar Padath return; 823274dfe62SJayashankar Padath } 824274dfe62SJayashankar Padath 825274dfe62SJayashankar Padath if (applyTime) 826fa1a5a38SJayashankar Padath { 8270ed80c8cSGeorge Liu setApplyTime(asyncResp, *applyTime); 828fa1a5a38SJayashankar Padath } 829274dfe62SJayashankar Padath } 830fa1a5a38SJayashankar Padath } 8317e860f15SJohn Edward Broadbent }); 832c2051d11SEd Tanous 8334dc23f3fSEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/update/") 8344dc23f3fSEd Tanous .privileges(redfish::privileges::postUpdateService) 8357e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 836c2051d11SEd Tanous std::bind_front(handleUpdateServicePost, std::ref(app))); 837729dae72SJennifer Lee } 838729dae72SJennifer Lee 8397e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventoryCollection(App& app) 8401abe55efSEd Tanous { 8417e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/") 842ed398213SEd Tanous .privileges(redfish::privileges::getSoftwareInventoryCollection) 8431476687dSEd Tanous .methods(boost::beast::http::verb::get)( 8441476687dSEd Tanous [&app](const crow::Request& req, 8451476687dSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 8463ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 84745ca1b86SEd Tanous { 84845ca1b86SEd Tanous return; 84945ca1b86SEd Tanous } 8508d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 8510f74e643SEd Tanous "#SoftwareInventoryCollection.SoftwareInventoryCollection"; 8528d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.id"] = 8530f74e643SEd Tanous "/redfish/v1/UpdateService/FirmwareInventory"; 854002d39b4SEd Tanous asyncResp->res.jsonValue["Name"] = "Software Inventory Collection"; 85508d81adaSJohn Edward Broadbent const std::array<const std::string_view, 1> iface = { 856e99073f5SGeorge Liu "xyz.openbmc_project.Software.Version"}; 8576c4eb9deSJennifer Lee 85808d81adaSJohn Edward Broadbent redfish::collection_util::getCollectionMembers( 85908d81adaSJohn Edward Broadbent asyncResp, 86008d81adaSJohn Edward Broadbent boost::urls::url("/redfish/v1/UpdateService/FirmwareInventory"), 86108d81adaSJohn Edward Broadbent iface, "/xyz/openbmc_project/software"); 8627e860f15SJohn Edward Broadbent }); 863729dae72SJennifer Lee } 86487d84729SAndrew Geissler /* Fill related item links (i.e. bmc, bios) in for inventory */ 8657e860f15SJohn Edward Broadbent inline static void 866ac106bf6SEd Tanous getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 86787d84729SAndrew Geissler const std::string& purpose) 86887d84729SAndrew Geissler { 869eee0013eSWilly Tu if (purpose == sw_util::bmcPurpose) 87087d84729SAndrew Geissler { 871ac106bf6SEd Tanous nlohmann::json& relatedItem = asyncResp->res.jsonValue["RelatedItem"]; 8721476687dSEd Tanous nlohmann::json::object_t item; 8731476687dSEd Tanous item["@odata.id"] = "/redfish/v1/Managers/bmc"; 874b2ba3072SPatrick Williams relatedItem.emplace_back(std::move(item)); 875ac106bf6SEd Tanous asyncResp->res.jsonValue["RelatedItem@odata.count"] = 876ac106bf6SEd Tanous relatedItem.size(); 87787d84729SAndrew Geissler } 878eee0013eSWilly Tu else if (purpose == sw_util::biosPurpose) 87987d84729SAndrew Geissler { 880ac106bf6SEd Tanous nlohmann::json& relatedItem = asyncResp->res.jsonValue["RelatedItem"]; 8811476687dSEd Tanous nlohmann::json::object_t item; 8821476687dSEd Tanous item["@odata.id"] = "/redfish/v1/Systems/system/Bios"; 883b2ba3072SPatrick Williams relatedItem.emplace_back(std::move(item)); 884ac106bf6SEd Tanous asyncResp->res.jsonValue["RelatedItem@odata.count"] = 885ac106bf6SEd Tanous relatedItem.size(); 88687d84729SAndrew Geissler } 88787d84729SAndrew Geissler else 88887d84729SAndrew Geissler { 889*62598e31SEd Tanous BMCWEB_LOG_ERROR("Unknown software purpose {}", purpose); 89087d84729SAndrew Geissler } 89187d84729SAndrew Geissler } 89287d84729SAndrew Geissler 893af24660dSWilly Tu inline void 894af24660dSWilly Tu getSoftwareVersion(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 895af24660dSWilly Tu const std::string& service, const std::string& path, 896af24660dSWilly Tu const std::string& swId) 897af24660dSWilly Tu { 898d1bde9e5SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 899d1bde9e5SKrzysztof Grobelny *crow::connections::systemBus, service, path, 900d1bde9e5SKrzysztof Grobelny "xyz.openbmc_project.Software.Version", 901af24660dSWilly Tu [asyncResp, 9028b24275dSEd Tanous swId](const boost::system::error_code& ec, 903af24660dSWilly Tu const dbus::utility::DBusPropertiesMap& propertiesList) { 9048b24275dSEd Tanous if (ec) 905af24660dSWilly Tu { 906af24660dSWilly Tu messages::internalError(asyncResp->res); 907af24660dSWilly Tu return; 908af24660dSWilly Tu } 909d1bde9e5SKrzysztof Grobelny 910af24660dSWilly Tu const std::string* swInvPurpose = nullptr; 911af24660dSWilly Tu const std::string* version = nullptr; 912d1bde9e5SKrzysztof Grobelny 913d1bde9e5SKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 914d1bde9e5SKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), propertiesList, "Purpose", 915d1bde9e5SKrzysztof Grobelny swInvPurpose, "Version", version); 916d1bde9e5SKrzysztof Grobelny 917d1bde9e5SKrzysztof Grobelny if (!success) 918af24660dSWilly Tu { 919d1bde9e5SKrzysztof Grobelny messages::internalError(asyncResp->res); 920d1bde9e5SKrzysztof Grobelny return; 921af24660dSWilly Tu } 922af24660dSWilly Tu 923af24660dSWilly Tu if (swInvPurpose == nullptr) 924af24660dSWilly Tu { 925*62598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't find property \"Purpose\"!"); 926af24660dSWilly Tu messages::internalError(asyncResp->res); 927af24660dSWilly Tu return; 928af24660dSWilly Tu } 929af24660dSWilly Tu 930*62598e31SEd Tanous BMCWEB_LOG_DEBUG("swInvPurpose = {}", *swInvPurpose); 931af24660dSWilly Tu 932af24660dSWilly Tu if (version == nullptr) 933af24660dSWilly Tu { 934*62598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't find property \"Version\"!"); 935af24660dSWilly Tu 936af24660dSWilly Tu messages::internalError(asyncResp->res); 937af24660dSWilly Tu 938af24660dSWilly Tu return; 939af24660dSWilly Tu } 940af24660dSWilly Tu asyncResp->res.jsonValue["Version"] = *version; 941af24660dSWilly Tu asyncResp->res.jsonValue["Id"] = swId; 942af24660dSWilly Tu 943af24660dSWilly Tu // swInvPurpose is of format: 944af24660dSWilly Tu // xyz.openbmc_project.Software.Version.VersionPurpose.ABC 945af24660dSWilly Tu // Translate this to "ABC image" 946af24660dSWilly Tu size_t endDesc = swInvPurpose->rfind('.'); 947af24660dSWilly Tu if (endDesc == std::string::npos) 948af24660dSWilly Tu { 949af24660dSWilly Tu messages::internalError(asyncResp->res); 950af24660dSWilly Tu return; 951af24660dSWilly Tu } 952af24660dSWilly Tu endDesc++; 953af24660dSWilly Tu if (endDesc >= swInvPurpose->size()) 954af24660dSWilly Tu { 955af24660dSWilly Tu messages::internalError(asyncResp->res); 956af24660dSWilly Tu return; 957af24660dSWilly Tu } 958af24660dSWilly Tu 959af24660dSWilly Tu std::string formatDesc = swInvPurpose->substr(endDesc); 960af24660dSWilly Tu asyncResp->res.jsonValue["Description"] = formatDesc + " image"; 961af24660dSWilly Tu getRelatedItems(asyncResp, *swInvPurpose); 962d1bde9e5SKrzysztof Grobelny }); 963af24660dSWilly Tu } 964af24660dSWilly Tu 9657e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventory(App& app) 9661abe55efSEd Tanous { 9677e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/") 968ed398213SEd Tanous .privileges(redfish::privileges::getSoftwareInventory) 969002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 970002d39b4SEd Tanous [&app](const crow::Request& req, 97145ca1b86SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 9727e860f15SJohn Edward Broadbent const std::string& param) { 9733ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 97445ca1b86SEd Tanous { 97545ca1b86SEd Tanous return; 97645ca1b86SEd Tanous } 9773ae837c9SEd Tanous std::shared_ptr<std::string> swId = 9787e860f15SJohn Edward Broadbent std::make_shared<std::string>(param); 979c711bf86SEd Tanous 980ef4c65b7SEd Tanous asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( 981ef4c65b7SEd Tanous "/redfish/v1/UpdateService/FirmwareInventory/{}", *swId); 982c711bf86SEd Tanous 983e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 984e99073f5SGeorge Liu "xyz.openbmc_project.Software.Version"}; 985e99073f5SGeorge Liu dbus::utility::getSubTree( 986e99073f5SGeorge Liu "/", 0, interfaces, 987b9d36b47SEd Tanous [asyncResp, 988e99073f5SGeorge Liu swId](const boost::system::error_code& ec, 989b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 990*62598e31SEd Tanous BMCWEB_LOG_DEBUG("doGet callback..."); 9911abe55efSEd Tanous if (ec) 9921abe55efSEd Tanous { 993f12894f8SJason M. Bills messages::internalError(asyncResp->res); 9946c4eb9deSJennifer Lee return; 9956c4eb9deSJennifer Lee } 9966c4eb9deSJennifer Lee 9976913228dSAndrew Geissler // Ensure we find our input swId, otherwise return an error 9986913228dSAndrew Geissler bool found = false; 999002d39b4SEd Tanous for (const std::pair<std::string, 10007e860f15SJohn Edward Broadbent std::vector<std::pair< 1001002d39b4SEd Tanous std::string, std::vector<std::string>>>>& 1002002d39b4SEd Tanous obj : subtree) 10031abe55efSEd Tanous { 100411ba3979SEd Tanous if (!obj.first.ends_with(*swId)) 10051abe55efSEd Tanous { 1006acb7cfb4SJennifer Lee continue; 1007acb7cfb4SJennifer Lee } 1008acb7cfb4SJennifer Lee 100926f6976fSEd Tanous if (obj.second.empty()) 10101abe55efSEd Tanous { 1011acb7cfb4SJennifer Lee continue; 1012acb7cfb4SJennifer Lee } 10136c4eb9deSJennifer Lee 10146913228dSAndrew Geissler found = true; 1015eee0013eSWilly Tu sw_util::getSwStatus(asyncResp, swId, obj.second[0].first); 1016af24660dSWilly Tu getSoftwareVersion(asyncResp, obj.second[0].first, obj.first, 1017af24660dSWilly Tu *swId); 10186c4eb9deSJennifer Lee } 10196913228dSAndrew Geissler if (!found) 10206913228dSAndrew Geissler { 1021*62598e31SEd Tanous BMCWEB_LOG_WARNING("Input swID {} not found!", *swId); 10226913228dSAndrew Geissler messages::resourceMissingAtURI( 1023ef4c65b7SEd Tanous asyncResp->res, 1024ef4c65b7SEd Tanous boost::urls::format( 1025ef4c65b7SEd Tanous "/redfish/v1/UpdateService/FirmwareInventory/{}", 1026ef4c65b7SEd Tanous *swId)); 10276913228dSAndrew Geissler return; 10286913228dSAndrew Geissler } 10294e68c45bSAyushi Smriti asyncResp->res.jsonValue["@odata.type"] = 10304e68c45bSAyushi Smriti "#SoftwareInventory.v1_1_0.SoftwareInventory"; 10314e68c45bSAyushi Smriti asyncResp->res.jsonValue["Name"] = "Software Inventory"; 10324e68c45bSAyushi Smriti asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK"; 10333f8a743aSAppaRao Puli 10343f8a743aSAppaRao Puli asyncResp->res.jsonValue["Updateable"] = false; 1035eee0013eSWilly Tu sw_util::getSwUpdatableStatus(asyncResp, swId); 1036e99073f5SGeorge Liu }); 10377e860f15SJohn Edward Broadbent }); 10386c4eb9deSJennifer Lee } 1039729dae72SJennifer Lee 1040729dae72SJennifer Lee } // namespace redfish 1041