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" 22*0ed80c8cSGeorge Liu #include "multipart_parser.hpp" 233ccb3adbSEd Tanous #include "query.hpp" 243ccb3adbSEd Tanous #include "registries/privilege_registry.hpp" 25a8e884fcSEd Tanous #include "task.hpp" 263ccb3adbSEd Tanous #include "utils/dbus_utils.hpp" 273ccb3adbSEd Tanous #include "utils/sw_utils.hpp" 283ccb3adbSEd Tanous 29d093c996SEd Tanous #include <boost/algorithm/string/case_conv.hpp> 30e99073f5SGeorge Liu #include <boost/system/error_code.hpp> 311e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp> 323ccb3adbSEd Tanous #include <sdbusplus/bus/match.hpp> 33d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp> 341214b7e7SGunnar Mills 352b73119cSGeorge Liu #include <array> 36*0ed80c8cSGeorge Liu #include <filesystem> 372b73119cSGeorge Liu #include <string_view> 382b73119cSGeorge Liu 391abe55efSEd Tanous namespace redfish 401abe55efSEd Tanous { 4127826b5fSEd Tanous 420e7de46fSAndrew Geissler // Match signals added on software path 43cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 4459d494eeSPatrick Williams static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateMatcher; 45cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 4659d494eeSPatrick Williams static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateErrorMatcher; 470e7de46fSAndrew Geissler // Only allow one update at a time 48cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 490e7de46fSAndrew Geissler static bool fwUpdateInProgress = false; 5086adcd6dSAndrew Geissler // Timer for software available 51cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 52271584abSEd Tanous static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer; 5386adcd6dSAndrew Geissler 547e860f15SJohn Edward Broadbent inline static void cleanUp() 5586adcd6dSAndrew Geissler { 5686adcd6dSAndrew Geissler fwUpdateInProgress = false; 5786adcd6dSAndrew Geissler fwUpdateMatcher = nullptr; 584cde5d90SJames Feist fwUpdateErrorMatcher = nullptr; 5986adcd6dSAndrew Geissler } 607e860f15SJohn Edward Broadbent inline static void activateImage(const std::string& objPath, 6186adcd6dSAndrew Geissler const std::string& service) 6286adcd6dSAndrew Geissler { 6386adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "Activate image for " << objPath << " " << service; 6486adcd6dSAndrew Geissler crow::connections::systemBus->async_method_call( 655e7e2dc5SEd Tanous [](const boost::system::error_code& errorCode) { 6681ce609eSEd Tanous if (errorCode) 6786adcd6dSAndrew Geissler { 6881ce609eSEd Tanous BMCWEB_LOG_DEBUG << "error_code = " << errorCode; 6981ce609eSEd Tanous BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message(); 7086adcd6dSAndrew Geissler } 7186adcd6dSAndrew Geissler }, 7286adcd6dSAndrew Geissler service, objPath, "org.freedesktop.DBus.Properties", "Set", 7386adcd6dSAndrew Geissler "xyz.openbmc_project.Software.Activation", "RequestedActivation", 74168e20c1SEd Tanous dbus::utility::DbusVariantType( 750fda0f12SGeorge Liu "xyz.openbmc_project.Software.Activation.RequestedActivations.Active")); 7686adcd6dSAndrew Geissler } 770554c984SAndrew Geissler 780554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr 790554c984SAndrew Geissler // then no asyncResp updates will occur 808d1b46d7Szhanghch05 static void 818d1b46d7Szhanghch05 softwareInterfaceAdded(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 8259d494eeSPatrick Williams sdbusplus::message_t& m, task::Payload&& payload) 8386adcd6dSAndrew Geissler { 84b9d36b47SEd Tanous dbus::utility::DBusInteracesMap interfacesProperties; 8586adcd6dSAndrew Geissler 8686adcd6dSAndrew Geissler sdbusplus::message::object_path objPath; 8786adcd6dSAndrew Geissler 8886adcd6dSAndrew Geissler m.read(objPath, interfacesProperties); 8986adcd6dSAndrew Geissler 9086adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "obj path = " << objPath.str; 91e3eb3d63SEd Tanous for (const auto& interface : interfacesProperties) 9286adcd6dSAndrew Geissler { 9386adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "interface = " << interface.first; 9486adcd6dSAndrew Geissler 9586adcd6dSAndrew Geissler if (interface.first == "xyz.openbmc_project.Software.Activation") 9686adcd6dSAndrew Geissler { 9786adcd6dSAndrew Geissler // Retrieve service and activate 982b73119cSGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 992b73119cSGeorge Liu "xyz.openbmc_project.Software.Activation"}; 1002b73119cSGeorge Liu dbus::utility::getDbusObject( 1012b73119cSGeorge Liu objPath.str, interfaces, 102a3e65892SEd Tanous [objPath, asyncResp, payload(std::move(payload))]( 1032b73119cSGeorge Liu const boost::system::error_code& errorCode, 104a3e65892SEd Tanous const std::vector< 105a3e65892SEd Tanous std::pair<std::string, std::vector<std::string>>>& 106a3e65892SEd Tanous objInfo) mutable { 10781ce609eSEd Tanous if (errorCode) 10886adcd6dSAndrew Geissler { 10981ce609eSEd Tanous BMCWEB_LOG_DEBUG << "error_code = " << errorCode; 110002d39b4SEd Tanous BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message(); 1110554c984SAndrew Geissler if (asyncResp) 1120554c984SAndrew Geissler { 11386adcd6dSAndrew Geissler messages::internalError(asyncResp->res); 1140554c984SAndrew Geissler } 11586adcd6dSAndrew Geissler cleanUp(); 11686adcd6dSAndrew Geissler return; 11786adcd6dSAndrew Geissler } 11886adcd6dSAndrew Geissler // Ensure we only got one service back 11986adcd6dSAndrew Geissler if (objInfo.size() != 1) 12086adcd6dSAndrew Geissler { 12186adcd6dSAndrew Geissler BMCWEB_LOG_ERROR << "Invalid Object Size " 12286adcd6dSAndrew Geissler << objInfo.size(); 1230554c984SAndrew Geissler if (asyncResp) 1240554c984SAndrew Geissler { 12586adcd6dSAndrew Geissler messages::internalError(asyncResp->res); 1260554c984SAndrew Geissler } 12786adcd6dSAndrew Geissler cleanUp(); 12886adcd6dSAndrew Geissler return; 12986adcd6dSAndrew Geissler } 13086adcd6dSAndrew Geissler // cancel timer only when 13186adcd6dSAndrew Geissler // xyz.openbmc_project.Software.Activation interface 13286adcd6dSAndrew Geissler // is added 13386adcd6dSAndrew Geissler fwAvailableTimer = nullptr; 13486adcd6dSAndrew Geissler 13586adcd6dSAndrew Geissler activateImage(objPath.str, objInfo[0].first); 1360554c984SAndrew Geissler if (asyncResp) 1370554c984SAndrew Geissler { 13832898ceaSJames Feist std::shared_ptr<task::TaskData> task = 13932898ceaSJames Feist task::TaskData::createTask( 1405e7e2dc5SEd Tanous [](const boost::system::error_code& ec, 14159d494eeSPatrick Williams sdbusplus::message_t& msg, 1421214b7e7SGunnar Mills const std::shared_ptr<task::TaskData>& 1431214b7e7SGunnar Mills taskData) { 14432898ceaSJames Feist if (ec) 14532898ceaSJames Feist { 14632898ceaSJames Feist return task::completed; 14732898ceaSJames Feist } 14832898ceaSJames Feist 14932898ceaSJames Feist std::string iface; 150b9d36b47SEd Tanous dbus::utility::DBusPropertiesMap values; 151fd9ab9e1SJames Feist 152002d39b4SEd Tanous std::string index = std::to_string(taskData->index); 15332898ceaSJames Feist msg.read(iface, values); 154fd9ab9e1SJames Feist 155002d39b4SEd Tanous if (iface == "xyz.openbmc_project.Software.Activation") 156fd9ab9e1SJames Feist { 1570fb5b505SGayathri Leburu const std::string* state = nullptr; 158b9d36b47SEd Tanous for (const auto& property : values) 15932898ceaSJames Feist { 160b9d36b47SEd Tanous if (property.first == "Activation") 161b9d36b47SEd Tanous { 1620fb5b505SGayathri Leburu state = std::get_if<std::string>( 163b9d36b47SEd Tanous &property.second); 1640fb5b505SGayathri Leburu if (state == nullptr) 165b9d36b47SEd Tanous { 166002d39b4SEd Tanous taskData->messages.emplace_back( 167002d39b4SEd Tanous messages::internalError()); 168b9d36b47SEd Tanous return task::completed; 169b9d36b47SEd Tanous } 170b9d36b47SEd Tanous } 171b9d36b47SEd Tanous } 17232898ceaSJames Feist 17332898ceaSJames Feist if (state == nullptr) 17432898ceaSJames Feist { 175b9d36b47SEd Tanous return !task::completed; 17632898ceaSJames Feist } 17732898ceaSJames Feist 17811ba3979SEd Tanous if (state->ends_with("Invalid") || 17911ba3979SEd Tanous state->ends_with("Failed")) 18032898ceaSJames Feist { 18132898ceaSJames Feist taskData->state = "Exception"; 18232898ceaSJames Feist taskData->status = "Warning"; 18332898ceaSJames Feist taskData->messages.emplace_back( 184e5d5006bSJames Feist messages::taskAborted(index)); 18532898ceaSJames Feist return task::completed; 18632898ceaSJames Feist } 18732898ceaSJames Feist 18811ba3979SEd Tanous if (state->ends_with("Staged")) 18932898ceaSJames Feist { 190fd9ab9e1SJames Feist taskData->state = "Stopping"; 191fd9ab9e1SJames Feist taskData->messages.emplace_back( 192fd9ab9e1SJames Feist messages::taskPaused(index)); 193fd9ab9e1SJames Feist 194fd9ab9e1SJames Feist // its staged, set a long timer to 195fd9ab9e1SJames Feist // allow them time to complete the 196fd9ab9e1SJames Feist // update (probably cycle the 197fd9ab9e1SJames Feist // system) if this expires then 198fd9ab9e1SJames Feist // task will be cancelled 199002d39b4SEd Tanous taskData->extendTimer(std::chrono::hours(5)); 20032898ceaSJames Feist return !task::completed; 20132898ceaSJames Feist } 20232898ceaSJames Feist 20311ba3979SEd Tanous if (state->ends_with("Active")) 20432898ceaSJames Feist { 20532898ceaSJames Feist taskData->messages.emplace_back( 206002d39b4SEd Tanous messages::taskCompletedOK(index)); 20732898ceaSJames Feist taskData->state = "Completed"; 20832898ceaSJames Feist return task::completed; 20932898ceaSJames Feist } 210fd9ab9e1SJames Feist } 2110fda0f12SGeorge Liu else if ( 2120fda0f12SGeorge Liu iface == 2130fda0f12SGeorge Liu "xyz.openbmc_project.Software.ActivationProgress") 214fd9ab9e1SJames Feist { 215b9d36b47SEd Tanous 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 } 29786adcd6dSAndrew Geissler BMCWEB_LOG_ERROR 29886adcd6dSAndrew Geissler << "Timed out waiting for firmware object being created"; 299002d39b4SEd Tanous BMCWEB_LOG_ERROR << "FW image may has already been uploaded to server"; 30086adcd6dSAndrew Geissler if (ec) 30186adcd6dSAndrew Geissler { 30286adcd6dSAndrew Geissler BMCWEB_LOG_ERROR << "Async_wait failed" << ec; 30386adcd6dSAndrew Geissler return; 30486adcd6dSAndrew Geissler } 3050554c984SAndrew Geissler if (asyncResp) 3060554c984SAndrew Geissler { 30786adcd6dSAndrew Geissler redfish::messages::internalError(asyncResp->res); 3080554c984SAndrew Geissler } 30986adcd6dSAndrew Geissler }); 310a3e65892SEd Tanous task::Payload payload(req); 31159d494eeSPatrick Williams auto callback = [asyncResp, payload](sdbusplus::message_t& m) mutable { 31286adcd6dSAndrew Geissler BMCWEB_LOG_DEBUG << "Match fired"; 313a3e65892SEd Tanous softwareInterfaceAdded(asyncResp, m, std::move(payload)); 31486adcd6dSAndrew Geissler }; 31586adcd6dSAndrew Geissler 31686adcd6dSAndrew Geissler fwUpdateInProgress = true; 31786adcd6dSAndrew Geissler 31859d494eeSPatrick Williams fwUpdateMatcher = std::make_unique<sdbusplus::bus::match_t>( 31986adcd6dSAndrew Geissler *crow::connections::systemBus, 32086adcd6dSAndrew Geissler "interface='org.freedesktop.DBus.ObjectManager',type='signal'," 32186adcd6dSAndrew Geissler "member='InterfacesAdded',path='/xyz/openbmc_project/software'", 32286adcd6dSAndrew Geissler callback); 3234cde5d90SJames Feist 32459d494eeSPatrick Williams fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match_t>( 3254cde5d90SJames Feist *crow::connections::systemBus, 326e1cc4828SBrian Ma "interface='org.freedesktop.DBus.ObjectManager',type='signal'," 327e1cc4828SBrian Ma "member='InterfacesAdded'," 328e1cc4828SBrian Ma "path='/xyz/openbmc_project/logging'", 32959d494eeSPatrick Williams [asyncResp, url](sdbusplus::message_t& m) { 330002d39b4SEd Tanous std::vector<std::pair<std::string, dbus::utility::DBusPropertiesMap>> 331e1cc4828SBrian Ma interfacesProperties; 332e1cc4828SBrian Ma sdbusplus::message::object_path objPath; 333e1cc4828SBrian Ma m.read(objPath, interfacesProperties); 334e1cc4828SBrian Ma BMCWEB_LOG_DEBUG << "obj path = " << objPath.str; 335e1cc4828SBrian Ma for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>& 336e1cc4828SBrian Ma interface : interfacesProperties) 337e1cc4828SBrian Ma { 338e1cc4828SBrian Ma if (interface.first == "xyz.openbmc_project.Logging.Entry") 339e1cc4828SBrian Ma { 340711ac7a9SEd Tanous for (const std::pair<std::string, 341002d39b4SEd Tanous dbus::utility::DbusVariantType>& value : 342002d39b4SEd Tanous interface.second) 3434cde5d90SJames Feist { 344711ac7a9SEd Tanous if (value.first != "Message") 345711ac7a9SEd Tanous { 346711ac7a9SEd Tanous continue; 3474cde5d90SJames Feist } 348e1cc4828SBrian Ma const std::string* type = 349711ac7a9SEd Tanous std::get_if<std::string>(&value.second); 3504cde5d90SJames Feist if (type == nullptr) 3514cde5d90SJames Feist { 352e1cc4828SBrian Ma // if this was our message, timeout will cover it 3534cde5d90SJames Feist return; 3544cde5d90SJames Feist } 355e1cc4828SBrian Ma fwAvailableTimer = nullptr; 3564cde5d90SJames Feist if (*type == 3574cde5d90SJames Feist "xyz.openbmc_project.Software.Image.Error.UnTarFailure") 3584cde5d90SJames Feist { 359002d39b4SEd Tanous redfish::messages::invalidUpload(asyncResp->res, url, 360002d39b4SEd Tanous "Invalid archive"); 3614cde5d90SJames Feist } 362e1cc4828SBrian Ma else if (*type == 363e1cc4828SBrian Ma "xyz.openbmc_project.Software.Image.Error." 364e1cc4828SBrian Ma "ManifestFileFailure") 3654cde5d90SJames Feist { 366002d39b4SEd Tanous redfish::messages::invalidUpload(asyncResp->res, url, 367002d39b4SEd Tanous "Invalid manifest"); 3684cde5d90SJames Feist } 369e1cc4828SBrian Ma else if ( 370e1cc4828SBrian Ma *type == 3714cde5d90SJames Feist "xyz.openbmc_project.Software.Image.Error.ImageFailure") 3724cde5d90SJames Feist { 373e1cc4828SBrian Ma redfish::messages::invalidUpload( 374e1cc4828SBrian Ma asyncResp->res, url, "Invalid image format"); 3754cde5d90SJames Feist } 376e1cc4828SBrian Ma else if ( 377e1cc4828SBrian Ma *type == 3780fda0f12SGeorge Liu "xyz.openbmc_project.Software.Version.Error.AlreadyExists") 37988b3dd12SGunnar Mills { 38088b3dd12SGunnar Mills redfish::messages::invalidUpload( 381e1cc4828SBrian Ma asyncResp->res, url, 382e1cc4828SBrian Ma "Image version already exists"); 38388b3dd12SGunnar Mills 38488b3dd12SGunnar Mills redfish::messages::resourceAlreadyExists( 385d8a5d5d8SJiaqing Zhao asyncResp->res, "UpdateService", "Version", 386e1cc4828SBrian Ma "uploaded version"); 38788b3dd12SGunnar Mills } 388e1cc4828SBrian Ma else if ( 389e1cc4828SBrian Ma *type == 3904cde5d90SJames Feist "xyz.openbmc_project.Software.Image.Error.BusyFailure") 3914cde5d90SJames Feist { 392002d39b4SEd Tanous redfish::messages::resourceExhaustion(asyncResp->res, 393002d39b4SEd Tanous url); 3944cde5d90SJames Feist } 3954cde5d90SJames Feist else 3964cde5d90SJames Feist { 3974cde5d90SJames Feist redfish::messages::internalError(asyncResp->res); 3984cde5d90SJames Feist } 399e1cc4828SBrian Ma } 400e1cc4828SBrian Ma } 401711ac7a9SEd Tanous } 4024cde5d90SJames Feist }); 40386adcd6dSAndrew Geissler } 404729dae72SJennifer Lee 4050554c984SAndrew Geissler /** 4060554c984SAndrew Geissler * UpdateServiceActionsSimpleUpdate class supports handle POST method for 4070554c984SAndrew Geissler * SimpleUpdate action. 4080554c984SAndrew Geissler */ 4097e860f15SJohn Edward Broadbent inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app) 4100554c984SAndrew Geissler { 4117e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 4127e860f15SJohn Edward Broadbent app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/") 413ed398213SEd Tanous .privileges(redfish::privileges::postUpdateService) 414002d39b4SEd Tanous .methods(boost::beast::http::verb::post)( 415002d39b4SEd Tanous [&app](const crow::Request& req, 4167e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 4173ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 41845ca1b86SEd Tanous { 41945ca1b86SEd Tanous return; 42045ca1b86SEd Tanous } 42145ca1b86SEd Tanous 4220554c984SAndrew Geissler std::optional<std::string> transferProtocol; 4230554c984SAndrew Geissler std::string imageURI; 4240554c984SAndrew Geissler 4250554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost"; 4260554c984SAndrew Geissler 4270554c984SAndrew Geissler // User can pass in both TransferProtocol and ImageURI parameters or 4284e0453b1SGunnar Mills // they can pass in just the ImageURI with the transfer protocol 4294e0453b1SGunnar Mills // embedded within it. 4300554c984SAndrew Geissler // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin 4310554c984SAndrew Geissler // 2) ImageURI:tftp://1.1.1.1/myfile.bin 4320554c984SAndrew Geissler 433002d39b4SEd Tanous if (!json_util::readJsonAction(req, asyncResp->res, "TransferProtocol", 434002d39b4SEd Tanous transferProtocol, "ImageURI", imageURI)) 4350554c984SAndrew Geissler { 4360554c984SAndrew Geissler BMCWEB_LOG_DEBUG 4370554c984SAndrew Geissler << "Missing TransferProtocol or ImageURI parameter"; 4380554c984SAndrew Geissler return; 4390554c984SAndrew Geissler } 4400554c984SAndrew Geissler if (!transferProtocol) 4410554c984SAndrew Geissler { 4420554c984SAndrew Geissler // Must be option 2 4430554c984SAndrew Geissler // Verify ImageURI has transfer protocol in it 444f23b7296SEd Tanous size_t separator = imageURI.find(':'); 4450554c984SAndrew Geissler if ((separator == std::string::npos) || 4460554c984SAndrew Geissler ((separator + 1) > imageURI.size())) 4470554c984SAndrew Geissler { 4480554c984SAndrew Geissler messages::actionParameterValueTypeError( 4490554c984SAndrew Geissler asyncResp->res, imageURI, "ImageURI", 4500554c984SAndrew Geissler "UpdateService.SimpleUpdate"); 4510554c984SAndrew Geissler BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: " 4520554c984SAndrew Geissler << imageURI; 4530554c984SAndrew Geissler return; 4540554c984SAndrew Geissler } 4550554c984SAndrew Geissler transferProtocol = imageURI.substr(0, separator); 4567e860f15SJohn Edward Broadbent // Ensure protocol is upper case for a common comparison path 4577e860f15SJohn Edward Broadbent // below 4580554c984SAndrew Geissler boost::to_upper(*transferProtocol); 4590554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Encoded transfer protocol " 4600554c984SAndrew Geissler << *transferProtocol; 4610554c984SAndrew Geissler 4620554c984SAndrew Geissler // Adjust imageURI to not have the protocol on it for parsing 4630554c984SAndrew Geissler // below 4640554c984SAndrew Geissler // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin 4650554c984SAndrew Geissler imageURI = imageURI.substr(separator + 3); 4660554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI; 4670554c984SAndrew Geissler } 4680554c984SAndrew Geissler 4690554c984SAndrew Geissler // OpenBMC currently only supports TFTP 4700554c984SAndrew Geissler if (*transferProtocol != "TFTP") 4710554c984SAndrew Geissler { 472002d39b4SEd Tanous messages::actionParameterNotSupported(asyncResp->res, 473002d39b4SEd Tanous "TransferProtocol", 4740554c984SAndrew Geissler "UpdateService.SimpleUpdate"); 4750554c984SAndrew Geissler BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: " 4760554c984SAndrew Geissler << *transferProtocol; 4770554c984SAndrew Geissler return; 4780554c984SAndrew Geissler } 4790554c984SAndrew Geissler 4800554c984SAndrew Geissler // Format should be <IP or Hostname>/<file> for imageURI 481f23b7296SEd Tanous size_t separator = imageURI.find('/'); 4820554c984SAndrew Geissler if ((separator == std::string::npos) || 4830554c984SAndrew Geissler ((separator + 1) > imageURI.size())) 4840554c984SAndrew Geissler { 4850554c984SAndrew Geissler messages::actionParameterValueTypeError( 4860554c984SAndrew Geissler asyncResp->res, imageURI, "ImageURI", 4870554c984SAndrew Geissler "UpdateService.SimpleUpdate"); 4880554c984SAndrew Geissler BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI; 4890554c984SAndrew Geissler return; 4900554c984SAndrew Geissler } 4910554c984SAndrew Geissler 4920554c984SAndrew Geissler std::string tftpServer = imageURI.substr(0, separator); 4930554c984SAndrew Geissler std::string fwFile = imageURI.substr(separator + 1); 4940554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile; 4950554c984SAndrew Geissler 4960554c984SAndrew Geissler // Setup callback for when new software detected 4972618d5e3SGunnar Mills // Give TFTP 10 minutes to complete 4984cde5d90SJames Feist monitorForSoftwareAvailable( 499d7a596bdSAlbert Zhang asyncResp, req, 5004cde5d90SJames Feist "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate", 5012618d5e3SGunnar Mills 600); 5020554c984SAndrew Geissler 5032618d5e3SGunnar Mills // TFTP can take up to 10 minutes depending on image size and 5040554c984SAndrew Geissler // connection speed. Return to caller as soon as the TFTP operation 5050554c984SAndrew Geissler // has been started. The callback above will ensure the activate 5060554c984SAndrew Geissler // is started once the download has completed 5070554c984SAndrew Geissler redfish::messages::success(asyncResp->res); 5080554c984SAndrew Geissler 5090554c984SAndrew Geissler // Call TFTP service 5100554c984SAndrew Geissler crow::connections::systemBus->async_method_call( 5115e7e2dc5SEd Tanous [](const boost::system::error_code& ec) { 5120554c984SAndrew Geissler if (ec) 5130554c984SAndrew Geissler { 5140554c984SAndrew Geissler // messages::internalError(asyncResp->res); 5150554c984SAndrew Geissler cleanUp(); 5160554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "error_code = " << ec; 5170554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "error msg = " << ec.message(); 5180554c984SAndrew Geissler } 5190554c984SAndrew Geissler else 5200554c984SAndrew Geissler { 5210554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success"; 5220554c984SAndrew Geissler } 5230554c984SAndrew Geissler }, 5240554c984SAndrew Geissler "xyz.openbmc_project.Software.Download", 525002d39b4SEd Tanous "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP", 526002d39b4SEd Tanous "DownloadViaTFTP", fwFile, tftpServer); 5270554c984SAndrew Geissler 5280554c984SAndrew Geissler BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost"; 5297e860f15SJohn Edward Broadbent }); 530729dae72SJennifer Lee } 531729dae72SJennifer Lee 532*0ed80c8cSGeorge Liu inline void uploadImageFile(crow::Response& res, std::string_view body) 533*0ed80c8cSGeorge Liu { 534*0ed80c8cSGeorge Liu std::filesystem::path filepath( 535*0ed80c8cSGeorge Liu "/tmp/images/" + 536*0ed80c8cSGeorge Liu boost::uuids::to_string(boost::uuids::random_generator()())); 537*0ed80c8cSGeorge Liu BMCWEB_LOG_DEBUG << "Writing file to " << filepath; 538*0ed80c8cSGeorge Liu std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary | 539*0ed80c8cSGeorge Liu std::ofstream::trunc); 540*0ed80c8cSGeorge Liu // set the permission of the file to 640 541*0ed80c8cSGeorge Liu std::filesystem::perms permission = 542*0ed80c8cSGeorge Liu std::filesystem::perms::owner_read | std::filesystem::perms::group_read; 543*0ed80c8cSGeorge Liu std::filesystem::permissions(filepath, permission); 544*0ed80c8cSGeorge Liu out << body; 545*0ed80c8cSGeorge Liu 546*0ed80c8cSGeorge Liu if (out.bad()) 547*0ed80c8cSGeorge Liu { 548*0ed80c8cSGeorge Liu messages::internalError(res); 549*0ed80c8cSGeorge Liu cleanUp(); 550*0ed80c8cSGeorge Liu } 551*0ed80c8cSGeorge Liu } 552*0ed80c8cSGeorge Liu 553*0ed80c8cSGeorge Liu inline void setApplyTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 554*0ed80c8cSGeorge Liu const std::string& applyTime) 555*0ed80c8cSGeorge Liu { 556*0ed80c8cSGeorge Liu std::string applyTimeNewVal; 557*0ed80c8cSGeorge Liu if (applyTime == "Immediate") 558*0ed80c8cSGeorge Liu { 559*0ed80c8cSGeorge Liu applyTimeNewVal = 560*0ed80c8cSGeorge Liu "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate"; 561*0ed80c8cSGeorge Liu } 562*0ed80c8cSGeorge Liu else if (applyTime == "OnReset") 563*0ed80c8cSGeorge Liu { 564*0ed80c8cSGeorge Liu applyTimeNewVal = 565*0ed80c8cSGeorge Liu "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset"; 566*0ed80c8cSGeorge Liu } 567*0ed80c8cSGeorge Liu else 568*0ed80c8cSGeorge Liu { 569*0ed80c8cSGeorge Liu BMCWEB_LOG_INFO 570*0ed80c8cSGeorge Liu << "ApplyTime value is not in the list of acceptable values"; 571*0ed80c8cSGeorge Liu messages::propertyValueNotInList(asyncResp->res, applyTime, 572*0ed80c8cSGeorge Liu "ApplyTime"); 573*0ed80c8cSGeorge Liu return; 574*0ed80c8cSGeorge Liu } 575*0ed80c8cSGeorge Liu 576*0ed80c8cSGeorge Liu // Set the requested image apply time value 577*0ed80c8cSGeorge Liu crow::connections::systemBus->async_method_call( 578*0ed80c8cSGeorge Liu [asyncResp](const boost::system::error_code ec) { 579*0ed80c8cSGeorge Liu if (ec) 580*0ed80c8cSGeorge Liu { 581*0ed80c8cSGeorge Liu BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 582*0ed80c8cSGeorge Liu messages::internalError(asyncResp->res); 583*0ed80c8cSGeorge Liu return; 584*0ed80c8cSGeorge Liu } 585*0ed80c8cSGeorge Liu messages::success(asyncResp->res); 586*0ed80c8cSGeorge Liu }, 587*0ed80c8cSGeorge Liu "xyz.openbmc_project.Settings", 588*0ed80c8cSGeorge Liu "/xyz/openbmc_project/software/apply_time", 589*0ed80c8cSGeorge Liu "org.freedesktop.DBus.Properties", "Set", 590*0ed80c8cSGeorge Liu "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime", 591*0ed80c8cSGeorge Liu dbus::utility::DbusVariantType{applyTimeNewVal}); 592*0ed80c8cSGeorge Liu } 593*0ed80c8cSGeorge Liu 594*0ed80c8cSGeorge Liu inline void 595*0ed80c8cSGeorge Liu updateMultipartContext(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 596*0ed80c8cSGeorge Liu const MultipartParser& parser) 597*0ed80c8cSGeorge Liu { 598*0ed80c8cSGeorge Liu const std::string* uploadData = nullptr; 599*0ed80c8cSGeorge Liu std::optional<std::string> applyTime = "OnReset"; 600*0ed80c8cSGeorge Liu bool targetFound = false; 601*0ed80c8cSGeorge Liu for (const FormPart& formpart : parser.mime_fields) 602*0ed80c8cSGeorge Liu { 603*0ed80c8cSGeorge Liu boost::beast::http::fields::const_iterator it = 604*0ed80c8cSGeorge Liu formpart.fields.find("Content-Disposition"); 605*0ed80c8cSGeorge Liu if (it == formpart.fields.end()) 606*0ed80c8cSGeorge Liu { 607*0ed80c8cSGeorge Liu BMCWEB_LOG_ERROR << "Couldn't find Content-Disposition"; 608*0ed80c8cSGeorge Liu return; 609*0ed80c8cSGeorge Liu } 610*0ed80c8cSGeorge Liu BMCWEB_LOG_INFO << "Parsing value " << it->value(); 611*0ed80c8cSGeorge Liu 612*0ed80c8cSGeorge Liu // The construction parameters of param_list must start with `;` 613*0ed80c8cSGeorge Liu size_t index = it->value().find(';'); 614*0ed80c8cSGeorge Liu if (index == std::string::npos) 615*0ed80c8cSGeorge Liu { 616*0ed80c8cSGeorge Liu continue; 617*0ed80c8cSGeorge Liu } 618*0ed80c8cSGeorge Liu 619*0ed80c8cSGeorge Liu for (auto const& param : 620*0ed80c8cSGeorge Liu boost::beast::http::param_list{it->value().substr(index)}) 621*0ed80c8cSGeorge Liu { 622*0ed80c8cSGeorge Liu if (param.first != "name" || param.second.empty()) 623*0ed80c8cSGeorge Liu { 624*0ed80c8cSGeorge Liu continue; 625*0ed80c8cSGeorge Liu } 626*0ed80c8cSGeorge Liu 627*0ed80c8cSGeorge Liu if (param.second == "UpdateParameters") 628*0ed80c8cSGeorge Liu { 629*0ed80c8cSGeorge Liu std::vector<std::string> targets; 630*0ed80c8cSGeorge Liu nlohmann::json content = 631*0ed80c8cSGeorge Liu nlohmann::json::parse(formpart.content); 632*0ed80c8cSGeorge Liu if (!json_util::readJson(content, asyncResp->res, "Targets", 633*0ed80c8cSGeorge Liu targets, "@Redfish.OperationApplyTime", 634*0ed80c8cSGeorge Liu applyTime)) 635*0ed80c8cSGeorge Liu { 636*0ed80c8cSGeorge Liu return; 637*0ed80c8cSGeorge Liu } 638*0ed80c8cSGeorge Liu if (targets.size() != 1) 639*0ed80c8cSGeorge Liu { 640*0ed80c8cSGeorge Liu messages::propertyValueFormatError(asyncResp->res, 641*0ed80c8cSGeorge Liu "Targets", ""); 642*0ed80c8cSGeorge Liu return; 643*0ed80c8cSGeorge Liu } 644*0ed80c8cSGeorge Liu if (targets[0] != "/redfish/v1/Managers/bmc") 645*0ed80c8cSGeorge Liu { 646*0ed80c8cSGeorge Liu messages::propertyValueNotInList(asyncResp->res, 647*0ed80c8cSGeorge Liu "Targets/0", targets[0]); 648*0ed80c8cSGeorge Liu return; 649*0ed80c8cSGeorge Liu } 650*0ed80c8cSGeorge Liu targetFound = true; 651*0ed80c8cSGeorge Liu } 652*0ed80c8cSGeorge Liu else if (param.second == "UpdateFile") 653*0ed80c8cSGeorge Liu { 654*0ed80c8cSGeorge Liu uploadData = &(formpart.content); 655*0ed80c8cSGeorge Liu } 656*0ed80c8cSGeorge Liu } 657*0ed80c8cSGeorge Liu } 658*0ed80c8cSGeorge Liu 659*0ed80c8cSGeorge Liu if (uploadData == nullptr) 660*0ed80c8cSGeorge Liu { 661*0ed80c8cSGeorge Liu BMCWEB_LOG_ERROR << "Upload data is NULL"; 662*0ed80c8cSGeorge Liu messages::propertyMissing(asyncResp->res, "UpdateFile"); 663*0ed80c8cSGeorge Liu return; 664*0ed80c8cSGeorge Liu } 665*0ed80c8cSGeorge Liu if (!targetFound) 666*0ed80c8cSGeorge Liu { 667*0ed80c8cSGeorge Liu messages::propertyMissing(asyncResp->res, "targets"); 668*0ed80c8cSGeorge Liu return; 669*0ed80c8cSGeorge Liu } 670*0ed80c8cSGeorge Liu 671*0ed80c8cSGeorge Liu setApplyTime(asyncResp, *applyTime); 672*0ed80c8cSGeorge Liu 673*0ed80c8cSGeorge Liu uploadImageFile(asyncResp->res, *uploadData); 674*0ed80c8cSGeorge Liu } 675*0ed80c8cSGeorge Liu 676c2051d11SEd Tanous inline void 677c2051d11SEd Tanous handleUpdateServicePost(App& app, const crow::Request& req, 678c2051d11SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 679c2051d11SEd Tanous { 6803ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 681c2051d11SEd Tanous { 682c2051d11SEd Tanous return; 683c2051d11SEd Tanous } 684c2051d11SEd Tanous BMCWEB_LOG_DEBUG << "doPost..."; 685c2051d11SEd Tanous 686c2051d11SEd Tanous // Setup callback for when new software detected 687c2051d11SEd Tanous monitorForSoftwareAvailable(asyncResp, req, "/redfish/v1/UpdateService"); 688c2051d11SEd Tanous 689*0ed80c8cSGeorge Liu MultipartParser parser; 690*0ed80c8cSGeorge Liu ParserError ec = parser.parse(req); 691*0ed80c8cSGeorge Liu if (ec == ParserError::ERROR_BOUNDARY_FORMAT) 692*0ed80c8cSGeorge Liu { 693*0ed80c8cSGeorge Liu // If the request didnt' contain boundary information, assume it was a 694*0ed80c8cSGeorge Liu // POST binary payload. 695*0ed80c8cSGeorge Liu uploadImageFile(asyncResp->res, req.body()); 696*0ed80c8cSGeorge Liu return; 697*0ed80c8cSGeorge Liu } 698*0ed80c8cSGeorge Liu if (ec != ParserError::PARSER_SUCCESS) 699*0ed80c8cSGeorge Liu { 700*0ed80c8cSGeorge Liu // handle error 701*0ed80c8cSGeorge Liu BMCWEB_LOG_ERROR << "MIME parse failed, ec : " << static_cast<int>(ec); 702*0ed80c8cSGeorge Liu messages::internalError(asyncResp->res); 703*0ed80c8cSGeorge Liu return; 704*0ed80c8cSGeorge Liu } 705*0ed80c8cSGeorge Liu updateMultipartContext(asyncResp, parser); 706c2051d11SEd Tanous } 707c2051d11SEd Tanous 7087e860f15SJohn Edward Broadbent inline void requestRoutesUpdateService(App& app) 7091abe55efSEd Tanous { 7107e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") 711ed398213SEd Tanous .privileges(redfish::privileges::getUpdateService) 712002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 713002d39b4SEd Tanous [&app](const crow::Request& req, 714002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 7153ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 71645ca1b86SEd Tanous { 71745ca1b86SEd Tanous return; 71845ca1b86SEd Tanous } 7198d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 720*0ed80c8cSGeorge Liu "#UpdateService.v1_11_1.UpdateService"; 7218d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService"; 7228d1b46d7Szhanghch05 asyncResp->res.jsonValue["Id"] = "UpdateService"; 723002d39b4SEd Tanous asyncResp->res.jsonValue["Description"] = "Service for Software Update"; 7248d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "Update Service"; 7254dc23f3fSEd Tanous 72632ca38afSEd Tanous #ifdef BMCWEB_ENABLE_REDFISH_UPDATESERVICE_OLD_POST_URL 72732ca38afSEd Tanous // See note about later on in this file about why this is neccesary 72832ca38afSEd Tanous // This is "Wrong" per the standard, but is done temporarily to 72932ca38afSEd Tanous // avoid noise in failing tests as people transition to having this 73032ca38afSEd Tanous // option disabled 73132ca38afSEd Tanous asyncResp->res.addHeader(boost::beast::http::field::allow, 73232ca38afSEd Tanous "GET, PATCH, HEAD"); 73332ca38afSEd Tanous #endif 73432ca38afSEd Tanous 7357e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["HttpPushUri"] = 7364dc23f3fSEd Tanous "/redfish/v1/UpdateService/update"; 737*0ed80c8cSGeorge Liu asyncResp->res.jsonValue["MultipartHttpPushUri"] = 738*0ed80c8cSGeorge Liu "/redfish/v1/UpdateService/update"; 7394dc23f3fSEd Tanous 7400f74e643SEd Tanous // UpdateService cannot be disabled 7418d1b46d7Szhanghch05 asyncResp->res.jsonValue["ServiceEnabled"] = true; 7421476687dSEd Tanous asyncResp->res.jsonValue["FirmwareInventory"]["@odata.id"] = 7431476687dSEd Tanous "/redfish/v1/UpdateService/FirmwareInventory"; 744d61e5194STejas Patil // Get the MaxImageSizeBytes 745d61e5194STejas Patil asyncResp->res.jsonValue["MaxImageSizeBytes"] = 746d61e5194STejas Patil bmcwebHttpReqBodyLimitMb * 1024 * 1024; 747d61e5194STejas Patil 7480554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE 7490554c984SAndrew Geissler // Update Actions object. 7500554c984SAndrew Geissler nlohmann::json& updateSvcSimpleUpdate = 751002d39b4SEd Tanous asyncResp->res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"]; 7520554c984SAndrew Geissler updateSvcSimpleUpdate["target"] = 7530554c984SAndrew Geissler "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate"; 754002d39b4SEd Tanous updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = { 755002d39b4SEd Tanous "TFTP"}; 7560554c984SAndrew Geissler #endif 757274dfe62SJayashankar Padath // Get the current ApplyTime value 7581e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::string>( 7591e1e598dSJonathan Doman *crow::connections::systemBus, "xyz.openbmc_project.Settings", 7601e1e598dSJonathan Doman "/xyz/openbmc_project/software/apply_time", 7611e1e598dSJonathan Doman "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime", 7625e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec, 7631e1e598dSJonathan Doman const std::string& applyTime) { 764274dfe62SJayashankar Padath if (ec) 765274dfe62SJayashankar Padath { 766274dfe62SJayashankar Padath BMCWEB_LOG_DEBUG << "DBUS response error " << ec; 7678d1b46d7Szhanghch05 messages::internalError(asyncResp->res); 768274dfe62SJayashankar Padath return; 769274dfe62SJayashankar Padath } 770274dfe62SJayashankar Padath 771274dfe62SJayashankar Padath // Store the ApplyTime Value 7721e1e598dSJonathan Doman if (applyTime == "xyz.openbmc_project.Software.ApplyTime." 7731e1e598dSJonathan Doman "RequestedApplyTimes.Immediate") 774274dfe62SJayashankar Padath { 775002d39b4SEd Tanous asyncResp->res.jsonValue["HttpPushUriOptions"] 7767e860f15SJohn Edward Broadbent ["HttpPushUriApplyTime"]["ApplyTime"] = 7777e860f15SJohn Edward Broadbent "Immediate"; 778274dfe62SJayashankar Padath } 779002d39b4SEd Tanous else if (applyTime == "xyz.openbmc_project.Software.ApplyTime." 7801e1e598dSJonathan Doman "RequestedApplyTimes.OnReset") 781274dfe62SJayashankar Padath { 782002d39b4SEd Tanous asyncResp->res.jsonValue["HttpPushUriOptions"] 7837e860f15SJohn Edward Broadbent ["HttpPushUriApplyTime"]["ApplyTime"] = 7847e860f15SJohn Edward Broadbent "OnReset"; 785274dfe62SJayashankar Padath } 7861e1e598dSJonathan Doman }); 7877e860f15SJohn Edward Broadbent }); 7887e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") 789ed398213SEd Tanous .privileges(redfish::privileges::patchUpdateService) 790002d39b4SEd Tanous .methods(boost::beast::http::verb::patch)( 791002d39b4SEd Tanous [&app](const crow::Request& req, 792002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 7933ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 79445ca1b86SEd Tanous { 79545ca1b86SEd Tanous return; 79645ca1b86SEd Tanous } 797fa1a5a38SJayashankar Padath BMCWEB_LOG_DEBUG << "doPatch..."; 798fa1a5a38SJayashankar Padath 799274dfe62SJayashankar Padath std::optional<nlohmann::json> pushUriOptions; 800002d39b4SEd Tanous if (!json_util::readJsonPatch(req, asyncResp->res, "HttpPushUriOptions", 801002d39b4SEd Tanous pushUriOptions)) 802fa1a5a38SJayashankar Padath { 803fa1a5a38SJayashankar Padath return; 804fa1a5a38SJayashankar Padath } 805fa1a5a38SJayashankar Padath 806274dfe62SJayashankar Padath if (pushUriOptions) 807274dfe62SJayashankar Padath { 808274dfe62SJayashankar Padath std::optional<nlohmann::json> pushUriApplyTime; 8098d1b46d7Szhanghch05 if (!json_util::readJson(*pushUriOptions, asyncResp->res, 810002d39b4SEd Tanous "HttpPushUriApplyTime", pushUriApplyTime)) 811274dfe62SJayashankar Padath { 812274dfe62SJayashankar Padath return; 813274dfe62SJayashankar Padath } 814274dfe62SJayashankar Padath 815274dfe62SJayashankar Padath if (pushUriApplyTime) 816274dfe62SJayashankar Padath { 817274dfe62SJayashankar Padath std::optional<std::string> applyTime; 8180fda0f12SGeorge Liu if (!json_util::readJson(*pushUriApplyTime, asyncResp->res, 8190fda0f12SGeorge Liu "ApplyTime", applyTime)) 820274dfe62SJayashankar Padath { 821274dfe62SJayashankar Padath return; 822274dfe62SJayashankar Padath } 823274dfe62SJayashankar Padath 824274dfe62SJayashankar Padath if (applyTime) 825fa1a5a38SJayashankar Padath { 826*0ed80c8cSGeorge Liu setApplyTime(asyncResp, *applyTime); 827fa1a5a38SJayashankar Padath } 828274dfe62SJayashankar Padath } 829fa1a5a38SJayashankar Padath } 8307e860f15SJohn Edward Broadbent }); 831c2051d11SEd Tanous 8324dc23f3fSEd Tanous // The "old" behavior of the update service URI causes redfish-service validator 8334dc23f3fSEd Tanous // failures when the Allow header is supported, given that in the spec, 8344dc23f3fSEd Tanous // UpdateService does not allow POST. in openbmc, we unfortunately reused that 8354dc23f3fSEd Tanous // resource as our HttpPushUri as well. A number of services, including the 8364dc23f3fSEd Tanous // openbmc tests, and documentation have hardcoded that erroneous API, instead 8374dc23f3fSEd Tanous // of relying on HttpPushUri as the spec requires. This option will exist 8384dc23f3fSEd Tanous // temporarily to allow the old behavior until Q4 2022, at which time it will be 8394dc23f3fSEd Tanous // removed. 8404dc23f3fSEd Tanous #ifdef BMCWEB_ENABLE_REDFISH_UPDATESERVICE_OLD_POST_URL 8417e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") 842ed398213SEd Tanous .privileges(redfish::privileges::postUpdateService) 843002d39b4SEd Tanous .methods(boost::beast::http::verb::post)( 844002d39b4SEd Tanous [&app](const crow::Request& req, 8454dc23f3fSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 8464dc23f3fSEd Tanous asyncResp->res.addHeader( 8474dc23f3fSEd Tanous boost::beast::http::field::warning, 8484dc23f3fSEd Tanous "299 - \"POST to /redfish/v1/UpdateService is deprecated. Use " 8494dc23f3fSEd Tanous "the value contained within HttpPushUri.\""); 8504dc23f3fSEd Tanous handleUpdateServicePost(app, req, asyncResp); 8514dc23f3fSEd Tanous }); 8524dc23f3fSEd Tanous #endif 8534dc23f3fSEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/update/") 8544dc23f3fSEd Tanous .privileges(redfish::privileges::postUpdateService) 8557e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 856c2051d11SEd Tanous std::bind_front(handleUpdateServicePost, std::ref(app))); 857729dae72SJennifer Lee } 858729dae72SJennifer Lee 8597e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventoryCollection(App& app) 8601abe55efSEd Tanous { 8617e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/") 862ed398213SEd Tanous .privileges(redfish::privileges::getSoftwareInventoryCollection) 8631476687dSEd Tanous .methods(boost::beast::http::verb::get)( 8641476687dSEd Tanous [&app](const crow::Request& req, 8651476687dSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 8663ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 86745ca1b86SEd Tanous { 86845ca1b86SEd Tanous return; 86945ca1b86SEd Tanous } 8708d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 8710f74e643SEd Tanous "#SoftwareInventoryCollection.SoftwareInventoryCollection"; 8728d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.id"] = 8730f74e643SEd Tanous "/redfish/v1/UpdateService/FirmwareInventory"; 874002d39b4SEd Tanous asyncResp->res.jsonValue["Name"] = "Software Inventory Collection"; 875c711bf86SEd Tanous 876e99073f5SGeorge Liu // Note that only firmware levels associated with a device 877e99073f5SGeorge Liu // are stored under /xyz/openbmc_project/software therefore 878e99073f5SGeorge Liu // to ensure only real FirmwareInventory items are returned, 879e99073f5SGeorge Liu // this full object path must be used here as input to 880e99073f5SGeorge Liu // mapper 881e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 882e99073f5SGeorge Liu "xyz.openbmc_project.Software.Version"}; 883e99073f5SGeorge Liu dbus::utility::getSubTree( 884e99073f5SGeorge Liu "/xyz/openbmc_project/software", 0, interfaces, 885002d39b4SEd Tanous [asyncResp]( 886e99073f5SGeorge Liu const boost::system::error_code& ec, 887002d39b4SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 8881abe55efSEd Tanous if (ec) 8891abe55efSEd Tanous { 890f12894f8SJason M. Bills messages::internalError(asyncResp->res); 8916c4eb9deSJennifer Lee return; 892729dae72SJennifer Lee } 893002d39b4SEd Tanous asyncResp->res.jsonValue["Members"] = nlohmann::json::array(); 894c711bf86SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = 0; 8956c4eb9deSJennifer Lee 8969eb808c1SEd Tanous for (const auto& obj : subtree) 8971abe55efSEd Tanous { 8982dfd18efSEd Tanous sdbusplus::message::object_path path(obj.first); 8992dfd18efSEd Tanous std::string swId = path.filename(); 9002dfd18efSEd Tanous if (swId.empty()) 901f4b65ab1SJennifer Lee { 902f12894f8SJason M. Bills messages::internalError(asyncResp->res); 903f4b65ab1SJennifer Lee BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!"; 904f4b65ab1SJennifer Lee return; 905f4b65ab1SJennifer Lee } 906f4b65ab1SJennifer Lee 907002d39b4SEd Tanous nlohmann::json& members = asyncResp->res.jsonValue["Members"]; 9081476687dSEd Tanous nlohmann::json::object_t member; 909eddfc437SWilly Tu member["@odata.id"] = crow::utility::urlFromPieces( 910eddfc437SWilly Tu "redfish", "v1", "UpdateService", "FirmwareInventory", 911eddfc437SWilly Tu swId); 9121476687dSEd Tanous members.push_back(std::move(member)); 913e0dd8057SAndrew Geissler asyncResp->res.jsonValue["Members@odata.count"] = 914c711bf86SEd Tanous members.size(); 9156c4eb9deSJennifer Lee } 916e99073f5SGeorge Liu }); 9177e860f15SJohn Edward Broadbent }); 918729dae72SJennifer Lee } 91987d84729SAndrew Geissler /* Fill related item links (i.e. bmc, bios) in for inventory */ 9207e860f15SJohn Edward Broadbent inline static void 9217e860f15SJohn Edward Broadbent getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& aResp, 92287d84729SAndrew Geissler const std::string& purpose) 92387d84729SAndrew Geissler { 924eee0013eSWilly Tu if (purpose == sw_util::bmcPurpose) 92587d84729SAndrew Geissler { 9265af690f5SAbhishek Patel nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"]; 9271476687dSEd Tanous nlohmann::json::object_t item; 9281476687dSEd Tanous item["@odata.id"] = "/redfish/v1/Managers/bmc"; 9291476687dSEd Tanous relatedItem.push_back(std::move(item)); 9307e860f15SJohn Edward Broadbent aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size(); 93187d84729SAndrew Geissler } 932eee0013eSWilly Tu else if (purpose == sw_util::biosPurpose) 93387d84729SAndrew Geissler { 9345af690f5SAbhishek Patel nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"]; 9351476687dSEd Tanous nlohmann::json::object_t item; 9361476687dSEd Tanous item["@odata.id"] = "/redfish/v1/Systems/system/Bios"; 9371476687dSEd Tanous relatedItem.push_back(std::move(item)); 9381a6e51aeSJiaqing Zhao aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size(); 93987d84729SAndrew Geissler } 94087d84729SAndrew Geissler else 94187d84729SAndrew Geissler { 94287d84729SAndrew Geissler BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose; 94387d84729SAndrew Geissler } 94487d84729SAndrew Geissler } 94587d84729SAndrew Geissler 946af24660dSWilly Tu inline void 947af24660dSWilly Tu getSoftwareVersion(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 948af24660dSWilly Tu const std::string& service, const std::string& path, 949af24660dSWilly Tu const std::string& swId) 950af24660dSWilly Tu { 951d1bde9e5SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 952d1bde9e5SKrzysztof Grobelny *crow::connections::systemBus, service, path, 953d1bde9e5SKrzysztof Grobelny "xyz.openbmc_project.Software.Version", 954af24660dSWilly Tu [asyncResp, 9555e7e2dc5SEd Tanous swId](const boost::system::error_code& errorCode, 956af24660dSWilly Tu const dbus::utility::DBusPropertiesMap& propertiesList) { 957af24660dSWilly Tu if (errorCode) 958af24660dSWilly Tu { 959af24660dSWilly Tu messages::internalError(asyncResp->res); 960af24660dSWilly Tu return; 961af24660dSWilly Tu } 962d1bde9e5SKrzysztof Grobelny 963af24660dSWilly Tu const std::string* swInvPurpose = nullptr; 964af24660dSWilly Tu const std::string* version = nullptr; 965d1bde9e5SKrzysztof Grobelny 966d1bde9e5SKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 967d1bde9e5SKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), propertiesList, "Purpose", 968d1bde9e5SKrzysztof Grobelny swInvPurpose, "Version", version); 969d1bde9e5SKrzysztof Grobelny 970d1bde9e5SKrzysztof Grobelny if (!success) 971af24660dSWilly Tu { 972d1bde9e5SKrzysztof Grobelny messages::internalError(asyncResp->res); 973d1bde9e5SKrzysztof Grobelny return; 974af24660dSWilly Tu } 975af24660dSWilly Tu 976af24660dSWilly Tu if (swInvPurpose == nullptr) 977af24660dSWilly Tu { 978af24660dSWilly Tu BMCWEB_LOG_DEBUG << "Can't find property \"Purpose\"!"; 979af24660dSWilly Tu messages::internalError(asyncResp->res); 980af24660dSWilly Tu return; 981af24660dSWilly Tu } 982af24660dSWilly Tu 983af24660dSWilly Tu BMCWEB_LOG_DEBUG << "swInvPurpose = " << *swInvPurpose; 984af24660dSWilly Tu 985af24660dSWilly Tu if (version == nullptr) 986af24660dSWilly Tu { 987af24660dSWilly Tu BMCWEB_LOG_DEBUG << "Can't find property \"Version\"!"; 988af24660dSWilly Tu 989af24660dSWilly Tu messages::internalError(asyncResp->res); 990af24660dSWilly Tu 991af24660dSWilly Tu return; 992af24660dSWilly Tu } 993af24660dSWilly Tu asyncResp->res.jsonValue["Version"] = *version; 994af24660dSWilly Tu asyncResp->res.jsonValue["Id"] = swId; 995af24660dSWilly Tu 996af24660dSWilly Tu // swInvPurpose is of format: 997af24660dSWilly Tu // xyz.openbmc_project.Software.Version.VersionPurpose.ABC 998af24660dSWilly Tu // Translate this to "ABC image" 999af24660dSWilly Tu size_t endDesc = swInvPurpose->rfind('.'); 1000af24660dSWilly Tu if (endDesc == std::string::npos) 1001af24660dSWilly Tu { 1002af24660dSWilly Tu messages::internalError(asyncResp->res); 1003af24660dSWilly Tu return; 1004af24660dSWilly Tu } 1005af24660dSWilly Tu endDesc++; 1006af24660dSWilly Tu if (endDesc >= swInvPurpose->size()) 1007af24660dSWilly Tu { 1008af24660dSWilly Tu messages::internalError(asyncResp->res); 1009af24660dSWilly Tu return; 1010af24660dSWilly Tu } 1011af24660dSWilly Tu 1012af24660dSWilly Tu std::string formatDesc = swInvPurpose->substr(endDesc); 1013af24660dSWilly Tu asyncResp->res.jsonValue["Description"] = formatDesc + " image"; 1014af24660dSWilly Tu getRelatedItems(asyncResp, *swInvPurpose); 1015d1bde9e5SKrzysztof Grobelny }); 1016af24660dSWilly Tu } 1017af24660dSWilly Tu 10187e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventory(App& app) 10191abe55efSEd Tanous { 10207e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/") 1021ed398213SEd Tanous .privileges(redfish::privileges::getSoftwareInventory) 1022002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 1023002d39b4SEd Tanous [&app](const crow::Request& req, 102445ca1b86SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 10257e860f15SJohn Edward Broadbent const std::string& param) { 10263ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 102745ca1b86SEd Tanous { 102845ca1b86SEd Tanous return; 102945ca1b86SEd Tanous } 10303ae837c9SEd Tanous std::shared_ptr<std::string> swId = 10317e860f15SJohn Edward Broadbent std::make_shared<std::string>(param); 1032c711bf86SEd Tanous 1033eddfc437SWilly Tu asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces( 1034eddfc437SWilly Tu "redfish", "v1", "UpdateService", "FirmwareInventory", *swId); 1035c711bf86SEd Tanous 1036e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 1037e99073f5SGeorge Liu "xyz.openbmc_project.Software.Version"}; 1038e99073f5SGeorge Liu dbus::utility::getSubTree( 1039e99073f5SGeorge Liu "/", 0, interfaces, 1040b9d36b47SEd Tanous [asyncResp, 1041e99073f5SGeorge Liu swId](const boost::system::error_code& ec, 1042b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 104355c7b7a2SEd Tanous BMCWEB_LOG_DEBUG << "doGet callback..."; 10441abe55efSEd Tanous if (ec) 10451abe55efSEd Tanous { 1046f12894f8SJason M. Bills messages::internalError(asyncResp->res); 10476c4eb9deSJennifer Lee return; 10486c4eb9deSJennifer Lee } 10496c4eb9deSJennifer Lee 10506913228dSAndrew Geissler // Ensure we find our input swId, otherwise return an error 10516913228dSAndrew Geissler bool found = false; 1052002d39b4SEd Tanous for (const std::pair<std::string, 10537e860f15SJohn Edward Broadbent std::vector<std::pair< 1054002d39b4SEd Tanous std::string, std::vector<std::string>>>>& 1055002d39b4SEd Tanous obj : subtree) 10561abe55efSEd Tanous { 105711ba3979SEd Tanous if (!obj.first.ends_with(*swId)) 10581abe55efSEd Tanous { 1059acb7cfb4SJennifer Lee continue; 1060acb7cfb4SJennifer Lee } 1061acb7cfb4SJennifer Lee 106226f6976fSEd Tanous if (obj.second.empty()) 10631abe55efSEd Tanous { 1064acb7cfb4SJennifer Lee continue; 1065acb7cfb4SJennifer Lee } 10666c4eb9deSJennifer Lee 10676913228dSAndrew Geissler found = true; 1068eee0013eSWilly Tu sw_util::getSwStatus(asyncResp, swId, obj.second[0].first); 1069af24660dSWilly Tu getSoftwareVersion(asyncResp, obj.second[0].first, obj.first, 1070af24660dSWilly Tu *swId); 10716c4eb9deSJennifer Lee } 10726913228dSAndrew Geissler if (!found) 10736913228dSAndrew Geissler { 1074002d39b4SEd Tanous BMCWEB_LOG_ERROR << "Input swID " << *swId << " not found!"; 10756913228dSAndrew Geissler messages::resourceMissingAtURI( 1076002d39b4SEd Tanous asyncResp->res, crow::utility::urlFromPieces( 1077ace85d60SEd Tanous "redfish", "v1", "UpdateService", 1078ace85d60SEd Tanous "FirmwareInventory", *swId)); 10796913228dSAndrew Geissler return; 10806913228dSAndrew Geissler } 10814e68c45bSAyushi Smriti asyncResp->res.jsonValue["@odata.type"] = 10824e68c45bSAyushi Smriti "#SoftwareInventory.v1_1_0.SoftwareInventory"; 10834e68c45bSAyushi Smriti asyncResp->res.jsonValue["Name"] = "Software Inventory"; 10844e68c45bSAyushi Smriti asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK"; 10853f8a743aSAppaRao Puli 10863f8a743aSAppaRao Puli asyncResp->res.jsonValue["Updateable"] = false; 1087eee0013eSWilly Tu sw_util::getSwUpdatableStatus(asyncResp, swId); 1088e99073f5SGeorge Liu }); 10897e860f15SJohn Edward Broadbent }); 10906c4eb9deSJennifer Lee } 1091729dae72SJennifer Lee 1092729dae72SJennifer Lee } // namespace redfish 1093