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 31e99073f5SGeorge Liu #include <boost/system/error_code.hpp> 32ef4c65b7SEd Tanous #include <boost/url/format.hpp> 331e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp> 343ccb3adbSEd Tanous #include <sdbusplus/bus/match.hpp> 35d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp> 361214b7e7SGunnar Mills 372b73119cSGeorge Liu #include <array> 380ed80c8cSGeorge Liu #include <filesystem> 397cb59f65SEd Tanous #include <optional> 407cb59f65SEd Tanous #include <string> 412b73119cSGeorge Liu #include <string_view> 422b73119cSGeorge Liu 431abe55efSEd Tanous namespace redfish 441abe55efSEd Tanous { 4527826b5fSEd Tanous 460e7de46fSAndrew Geissler // Match signals added on software path 47cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 4859d494eeSPatrick Williams static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateMatcher; 49cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 5059d494eeSPatrick Williams static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateErrorMatcher; 510e7de46fSAndrew Geissler // Only allow one update at a time 52cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 530e7de46fSAndrew Geissler static bool fwUpdateInProgress = false; 5486adcd6dSAndrew Geissler // Timer for software available 55cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 56271584abSEd Tanous static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer; 5786adcd6dSAndrew Geissler 58df254f2cSEd Tanous inline void cleanUp() 5986adcd6dSAndrew Geissler { 6086adcd6dSAndrew Geissler fwUpdateInProgress = false; 6186adcd6dSAndrew Geissler fwUpdateMatcher = nullptr; 624cde5d90SJames Feist fwUpdateErrorMatcher = nullptr; 6386adcd6dSAndrew Geissler } 64df254f2cSEd Tanous 65df254f2cSEd Tanous inline void activateImage(const std::string& objPath, 6686adcd6dSAndrew Geissler const std::string& service) 6786adcd6dSAndrew Geissler { 6862598e31SEd Tanous BMCWEB_LOG_DEBUG("Activate image for {} {}", objPath, service); 699ae226faSGeorge Liu sdbusplus::asio::setProperty( 709ae226faSGeorge Liu *crow::connections::systemBus, service, objPath, 719ae226faSGeorge Liu "xyz.openbmc_project.Software.Activation", "RequestedActivation", 729ae226faSGeorge Liu "xyz.openbmc_project.Software.Activation.RequestedActivations.Active", 738b24275dSEd Tanous [](const boost::system::error_code& ec) { 748b24275dSEd Tanous if (ec) 7586adcd6dSAndrew Geissler { 7662598e31SEd Tanous BMCWEB_LOG_DEBUG("error_code = {}", ec); 7762598e31SEd Tanous BMCWEB_LOG_DEBUG("error msg = {}", ec.message()); 7886adcd6dSAndrew Geissler } 799ae226faSGeorge Liu }); 8086adcd6dSAndrew Geissler } 810554c984SAndrew Geissler 820554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr 830554c984SAndrew Geissler // then no asyncResp updates will occur 848d1b46d7Szhanghch05 static void 858d1b46d7Szhanghch05 softwareInterfaceAdded(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 8659d494eeSPatrick Williams sdbusplus::message_t& m, task::Payload&& payload) 8786adcd6dSAndrew Geissler { 8880f79a40SMichael Shen dbus::utility::DBusInterfacesMap interfacesProperties; 8986adcd6dSAndrew Geissler 9086adcd6dSAndrew Geissler sdbusplus::message::object_path objPath; 9186adcd6dSAndrew Geissler 9286adcd6dSAndrew Geissler m.read(objPath, interfacesProperties); 9386adcd6dSAndrew Geissler 9462598e31SEd Tanous BMCWEB_LOG_DEBUG("obj path = {}", objPath.str); 95e3eb3d63SEd Tanous for (const auto& interface : interfacesProperties) 9686adcd6dSAndrew Geissler { 9762598e31SEd Tanous BMCWEB_LOG_DEBUG("interface = {}", interface.first); 9886adcd6dSAndrew Geissler 9986adcd6dSAndrew Geissler if (interface.first == "xyz.openbmc_project.Software.Activation") 10086adcd6dSAndrew Geissler { 10186adcd6dSAndrew Geissler // Retrieve service and activate 1022b73119cSGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 1032b73119cSGeorge Liu "xyz.openbmc_project.Software.Activation"}; 1042b73119cSGeorge Liu dbus::utility::getDbusObject( 1052b73119cSGeorge Liu objPath.str, interfaces, 106a3e65892SEd Tanous [objPath, asyncResp, payload(std::move(payload))]( 1078b24275dSEd Tanous const boost::system::error_code& ec, 108a3e65892SEd Tanous const std::vector< 109a3e65892SEd Tanous std::pair<std::string, std::vector<std::string>>>& 110a3e65892SEd Tanous objInfo) mutable { 1118b24275dSEd Tanous if (ec) 11286adcd6dSAndrew Geissler { 11362598e31SEd Tanous BMCWEB_LOG_DEBUG("error_code = {}", ec); 11462598e31SEd Tanous BMCWEB_LOG_DEBUG("error msg = {}", ec.message()); 1150554c984SAndrew Geissler if (asyncResp) 1160554c984SAndrew Geissler { 11786adcd6dSAndrew Geissler messages::internalError(asyncResp->res); 1180554c984SAndrew Geissler } 11986adcd6dSAndrew Geissler cleanUp(); 12086adcd6dSAndrew Geissler return; 12186adcd6dSAndrew Geissler } 12286adcd6dSAndrew Geissler // Ensure we only got one service back 12386adcd6dSAndrew Geissler if (objInfo.size() != 1) 12486adcd6dSAndrew Geissler { 12562598e31SEd Tanous BMCWEB_LOG_ERROR("Invalid Object Size {}", objInfo.size()); 1260554c984SAndrew Geissler if (asyncResp) 1270554c984SAndrew Geissler { 12886adcd6dSAndrew Geissler messages::internalError(asyncResp->res); 1290554c984SAndrew Geissler } 13086adcd6dSAndrew Geissler cleanUp(); 13186adcd6dSAndrew Geissler return; 13286adcd6dSAndrew Geissler } 13386adcd6dSAndrew Geissler // cancel timer only when 13486adcd6dSAndrew Geissler // xyz.openbmc_project.Software.Activation interface 13586adcd6dSAndrew Geissler // is added 13686adcd6dSAndrew Geissler fwAvailableTimer = nullptr; 13786adcd6dSAndrew Geissler 13886adcd6dSAndrew Geissler activateImage(objPath.str, objInfo[0].first); 1390554c984SAndrew Geissler if (asyncResp) 1400554c984SAndrew Geissler { 14132898ceaSJames Feist std::shared_ptr<task::TaskData> task = 14232898ceaSJames Feist task::TaskData::createTask( 1438b24275dSEd Tanous [](const boost::system::error_code& ec2, 14459d494eeSPatrick Williams sdbusplus::message_t& msg, 1451214b7e7SGunnar Mills const std::shared_ptr<task::TaskData>& 1461214b7e7SGunnar Mills taskData) { 1478b24275dSEd Tanous if (ec2) 14832898ceaSJames Feist { 14932898ceaSJames Feist return task::completed; 15032898ceaSJames Feist } 15132898ceaSJames Feist 15232898ceaSJames Feist std::string iface; 153b9d36b47SEd Tanous dbus::utility::DBusPropertiesMap values; 154fd9ab9e1SJames Feist 155002d39b4SEd Tanous std::string index = std::to_string(taskData->index); 15632898ceaSJames Feist msg.read(iface, values); 157fd9ab9e1SJames Feist 158002d39b4SEd Tanous if (iface == "xyz.openbmc_project.Software.Activation") 159fd9ab9e1SJames Feist { 1600fb5b505SGayathri Leburu const std::string* state = nullptr; 161b9d36b47SEd Tanous for (const auto& property : values) 16232898ceaSJames Feist { 163b9d36b47SEd Tanous if (property.first == "Activation") 164b9d36b47SEd Tanous { 1650fb5b505SGayathri Leburu state = std::get_if<std::string>( 166b9d36b47SEd Tanous &property.second); 1670fb5b505SGayathri Leburu if (state == nullptr) 168b9d36b47SEd Tanous { 169002d39b4SEd Tanous taskData->messages.emplace_back( 170002d39b4SEd Tanous messages::internalError()); 171b9d36b47SEd Tanous return task::completed; 172b9d36b47SEd Tanous } 173b9d36b47SEd Tanous } 174b9d36b47SEd Tanous } 17532898ceaSJames Feist 17632898ceaSJames Feist if (state == nullptr) 17732898ceaSJames Feist { 178b9d36b47SEd Tanous return !task::completed; 17932898ceaSJames Feist } 18032898ceaSJames Feist 18111ba3979SEd Tanous if (state->ends_with("Invalid") || 18211ba3979SEd Tanous state->ends_with("Failed")) 18332898ceaSJames Feist { 18432898ceaSJames Feist taskData->state = "Exception"; 18532898ceaSJames Feist taskData->status = "Warning"; 18632898ceaSJames Feist taskData->messages.emplace_back( 187e5d5006bSJames Feist messages::taskAborted(index)); 18832898ceaSJames Feist return task::completed; 18932898ceaSJames Feist } 19032898ceaSJames Feist 19111ba3979SEd Tanous if (state->ends_with("Staged")) 19232898ceaSJames Feist { 193fd9ab9e1SJames Feist taskData->state = "Stopping"; 194fd9ab9e1SJames Feist taskData->messages.emplace_back( 195fd9ab9e1SJames Feist messages::taskPaused(index)); 196fd9ab9e1SJames Feist 197fd9ab9e1SJames Feist // its staged, set a long timer to 198fd9ab9e1SJames Feist // allow them time to complete the 199fd9ab9e1SJames Feist // update (probably cycle the 200fd9ab9e1SJames Feist // system) if this expires then 2018ece0e45SEd Tanous // task will be canceled 202002d39b4SEd Tanous taskData->extendTimer(std::chrono::hours(5)); 20332898ceaSJames Feist return !task::completed; 20432898ceaSJames Feist } 20532898ceaSJames Feist 20611ba3979SEd Tanous if (state->ends_with("Active")) 20732898ceaSJames Feist { 20832898ceaSJames Feist taskData->messages.emplace_back( 209002d39b4SEd Tanous messages::taskCompletedOK(index)); 21032898ceaSJames Feist taskData->state = "Completed"; 21132898ceaSJames Feist return task::completed; 21232898ceaSJames Feist } 213fd9ab9e1SJames Feist } 2140fda0f12SGeorge Liu else if ( 2150fda0f12SGeorge Liu iface == 2160fda0f12SGeorge Liu "xyz.openbmc_project.Software.ActivationProgress") 217fd9ab9e1SJames Feist { 218b9d36b47SEd Tanous const uint8_t* progress = nullptr; 219b9d36b47SEd Tanous for (const auto& property : values) 220fd9ab9e1SJames Feist { 221b9d36b47SEd Tanous if (property.first == "Progress") 222b9d36b47SEd Tanous { 2230fb5b505SGayathri Leburu progress = 2240fb5b505SGayathri Leburu std::get_if<uint8_t>(&property.second); 2250fb5b505SGayathri Leburu if (progress == nullptr) 226b9d36b47SEd Tanous { 227002d39b4SEd Tanous taskData->messages.emplace_back( 228002d39b4SEd Tanous messages::internalError()); 229b9d36b47SEd Tanous return task::completed; 230fd9ab9e1SJames Feist } 231b9d36b47SEd Tanous } 232b9d36b47SEd Tanous } 233fd9ab9e1SJames Feist 234fd9ab9e1SJames Feist if (progress == nullptr) 235fd9ab9e1SJames Feist { 236b9d36b47SEd Tanous return !task::completed; 237fd9ab9e1SJames Feist } 2380fb5b505SGayathri Leburu taskData->percentComplete = *progress; 239fd9ab9e1SJames Feist taskData->messages.emplace_back( 2400fb5b505SGayathri Leburu messages::taskProgressChanged(index, 2410fb5b505SGayathri Leburu *progress)); 242fd9ab9e1SJames Feist 243fd9ab9e1SJames Feist // if we're getting status updates it's 244fd9ab9e1SJames Feist // still alive, update timer 245002d39b4SEd Tanous taskData->extendTimer(std::chrono::minutes(5)); 246fd9ab9e1SJames Feist } 24732898ceaSJames Feist 24832898ceaSJames Feist // as firmware update often results in a 24932898ceaSJames Feist // reboot, the task may never "complete" 25032898ceaSJames Feist // unless it is an error 25132898ceaSJames Feist 25232898ceaSJames Feist return !task::completed; 25332898ceaSJames Feist }, 2540fda0f12SGeorge Liu "type='signal',interface='org.freedesktop.DBus.Properties'," 255fd9ab9e1SJames Feist "member='PropertiesChanged',path='" + 25632898ceaSJames Feist objPath.str + "'"); 25732898ceaSJames Feist task->startTimer(std::chrono::minutes(5)); 25832898ceaSJames Feist task->populateResp(asyncResp->res); 259a3e65892SEd Tanous task->payload.emplace(std::move(payload)); 2600554c984SAndrew Geissler } 26186adcd6dSAndrew Geissler fwUpdateInProgress = false; 2622b73119cSGeorge Liu }); 26362bafc01SPatrick Williams 26462bafc01SPatrick Williams break; 26586adcd6dSAndrew Geissler } 26686adcd6dSAndrew Geissler } 26786adcd6dSAndrew Geissler } 26886adcd6dSAndrew Geissler 2698549b951SMyung Bae inline void afterAvailbleTimerAsyncWait( 2708549b951SMyung Bae const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2718549b951SMyung Bae const boost::system::error_code& ec) 2728549b951SMyung Bae { 2738549b951SMyung Bae cleanUp(); 2748549b951SMyung Bae if (ec == boost::asio::error::operation_aborted) 2758549b951SMyung Bae { 2768549b951SMyung Bae // expected, we were canceled before the timer completed. 2778549b951SMyung Bae return; 2788549b951SMyung Bae } 2798549b951SMyung Bae BMCWEB_LOG_ERROR("Timed out waiting for firmware object being created"); 2808549b951SMyung Bae BMCWEB_LOG_ERROR("FW image may has already been uploaded to server"); 2818549b951SMyung Bae if (ec) 2828549b951SMyung Bae { 2838549b951SMyung Bae BMCWEB_LOG_ERROR("Async_wait failed{}", ec); 2848549b951SMyung Bae return; 2858549b951SMyung Bae } 2868549b951SMyung Bae if (asyncResp) 2878549b951SMyung Bae { 2888549b951SMyung Bae redfish::messages::internalError(asyncResp->res); 2898549b951SMyung Bae } 2908549b951SMyung Bae } 2918549b951SMyung Bae 2928549b951SMyung Bae inline void 2938549b951SMyung Bae handleUpdateErrorType(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2948549b951SMyung Bae const std::string& url, const std::string& type) 2958549b951SMyung Bae { 2968549b951SMyung Bae if (type == "xyz.openbmc_project.Software.Image.Error.UnTarFailure") 2978549b951SMyung Bae { 2988549b951SMyung Bae redfish::messages::invalidUpload(asyncResp->res, url, 2998549b951SMyung Bae "Invalid archive"); 3008549b951SMyung Bae } 3018549b951SMyung Bae else if (type == 3028549b951SMyung Bae "xyz.openbmc_project.Software.Image.Error.ManifestFileFailure") 3038549b951SMyung Bae { 3048549b951SMyung Bae redfish::messages::invalidUpload(asyncResp->res, url, 3058549b951SMyung Bae "Invalid manifest"); 3068549b951SMyung Bae } 3078549b951SMyung Bae else if (type == "xyz.openbmc_project.Software.Image.Error.ImageFailure") 3088549b951SMyung Bae { 3098549b951SMyung Bae redfish::messages::invalidUpload(asyncResp->res, url, 3108549b951SMyung Bae "Invalid image format"); 3118549b951SMyung Bae } 3128549b951SMyung Bae else if (type == "xyz.openbmc_project.Software.Version.Error.AlreadyExists") 3138549b951SMyung Bae { 3148549b951SMyung Bae redfish::messages::invalidUpload(asyncResp->res, url, 3158549b951SMyung Bae "Image version already exists"); 3168549b951SMyung Bae 3178549b951SMyung Bae redfish::messages::resourceAlreadyExists( 3188549b951SMyung Bae asyncResp->res, "UpdateService", "Version", "uploaded version"); 3198549b951SMyung Bae } 3208549b951SMyung Bae else if (type == "xyz.openbmc_project.Software.Image.Error.BusyFailure") 3218549b951SMyung Bae { 3228549b951SMyung Bae redfish::messages::resourceExhaustion(asyncResp->res, url); 3238549b951SMyung Bae } 3244034a652SMyung Bae else if (type == "xyz.openbmc_project.Software.Version.Error.Incompatible") 3258549b951SMyung Bae { 3264034a652SMyung Bae redfish::messages::invalidUpload(asyncResp->res, url, 3274034a652SMyung Bae "Incompatible image version"); 3284034a652SMyung Bae } 3294034a652SMyung Bae else if (type == 3304034a652SMyung Bae "xyz.openbmc_project.Software.Version.Error.ExpiredAccessKey") 3314034a652SMyung Bae { 3324034a652SMyung Bae redfish::messages::invalidUpload(asyncResp->res, url, 3334034a652SMyung Bae "Update Access Key Expired"); 3344034a652SMyung Bae } 3354034a652SMyung Bae else if (type == 3364034a652SMyung Bae "xyz.openbmc_project.Software.Version.Error.InvalidSignature") 3374034a652SMyung Bae { 3384034a652SMyung Bae redfish::messages::invalidUpload(asyncResp->res, url, 3394034a652SMyung Bae "Invalid image signature"); 3404034a652SMyung Bae } 3414034a652SMyung Bae else if (type == 3424034a652SMyung Bae "xyz.openbmc_project.Software.Image.Error.InternalFailure" || 3434034a652SMyung Bae type == "xyz.openbmc_project.Software.Version.Error.HostFile") 3444034a652SMyung Bae { 3454034a652SMyung Bae BMCWEB_LOG_ERROR("Software Image Error type={}", type); 3468549b951SMyung Bae redfish::messages::internalError(asyncResp->res); 3478549b951SMyung Bae } 3484034a652SMyung Bae else 3494034a652SMyung Bae { 3504034a652SMyung Bae // Unrelated error types. Ignored 3514034a652SMyung Bae BMCWEB_LOG_INFO("Non-Software-related Error type={}. Ignored", type); 3524034a652SMyung Bae return; 3534034a652SMyung Bae } 3544034a652SMyung Bae // Clear the timer 3554034a652SMyung Bae fwAvailableTimer = nullptr; 3568549b951SMyung Bae } 3578549b951SMyung Bae 3588549b951SMyung Bae inline void 3598549b951SMyung Bae afterUpdateErrorMatcher(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3608549b951SMyung Bae const std::string& url, sdbusplus::message_t& m) 3618549b951SMyung Bae { 36280f79a40SMichael Shen dbus::utility::DBusInterfacesMap interfacesProperties; 3638549b951SMyung Bae sdbusplus::message::object_path objPath; 3648549b951SMyung Bae m.read(objPath, interfacesProperties); 3658549b951SMyung Bae BMCWEB_LOG_DEBUG("obj path = {}", objPath.str); 3668549b951SMyung Bae for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>& 3678549b951SMyung Bae interface : interfacesProperties) 3688549b951SMyung Bae { 3698549b951SMyung Bae if (interface.first == "xyz.openbmc_project.Logging.Entry") 3708549b951SMyung Bae { 3718549b951SMyung Bae for (const std::pair<std::string, dbus::utility::DbusVariantType>& 3728549b951SMyung Bae value : interface.second) 3738549b951SMyung Bae { 3748549b951SMyung Bae if (value.first != "Message") 3758549b951SMyung Bae { 3768549b951SMyung Bae continue; 3778549b951SMyung Bae } 3788549b951SMyung Bae const std::string* type = 3798549b951SMyung Bae std::get_if<std::string>(&value.second); 3808549b951SMyung Bae if (type == nullptr) 3818549b951SMyung Bae { 3828549b951SMyung Bae // if this was our message, timeout will cover it 3838549b951SMyung Bae return; 3848549b951SMyung Bae } 3858549b951SMyung Bae handleUpdateErrorType(asyncResp, url, *type); 3868549b951SMyung Bae } 3878549b951SMyung Bae } 3888549b951SMyung Bae } 3898549b951SMyung Bae } 3908549b951SMyung Bae 3910554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr 3920554c984SAndrew Geissler // then no asyncResp updates will occur 393b5a76932SEd Tanous static void monitorForSoftwareAvailable( 3948d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3958d1b46d7Szhanghch05 const crow::Request& req, const std::string& url, 3965d138943SGunnar Mills int timeoutTimeSeconds = 25) 39786adcd6dSAndrew Geissler { 39886adcd6dSAndrew Geissler // Only allow one FW update at a time 399e05aec50SEd Tanous if (fwUpdateInProgress) 40086adcd6dSAndrew Geissler { 4010554c984SAndrew Geissler if (asyncResp) 4020554c984SAndrew Geissler { 40386adcd6dSAndrew Geissler messages::serviceTemporarilyUnavailable(asyncResp->res, "30"); 4040554c984SAndrew Geissler } 40586adcd6dSAndrew Geissler return; 40686adcd6dSAndrew Geissler } 40786adcd6dSAndrew Geissler 408*8e8245dbSEd Tanous if (req.ioService == nullptr) 409*8e8245dbSEd Tanous { 410*8e8245dbSEd Tanous messages::internalError(asyncResp->res); 411*8e8245dbSEd Tanous return; 412*8e8245dbSEd Tanous } 413*8e8245dbSEd Tanous 4140554c984SAndrew Geissler fwAvailableTimer = 415271584abSEd Tanous std::make_unique<boost::asio::steady_timer>(*req.ioService); 41686adcd6dSAndrew Geissler 417271584abSEd Tanous fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds)); 41886adcd6dSAndrew Geissler 41986adcd6dSAndrew Geissler fwAvailableTimer->async_wait( 4208549b951SMyung Bae std::bind_front(afterAvailbleTimerAsyncWait, asyncResp)); 4218549b951SMyung Bae 422a3e65892SEd Tanous task::Payload payload(req); 42359d494eeSPatrick Williams auto callback = [asyncResp, payload](sdbusplus::message_t& m) mutable { 42462598e31SEd Tanous BMCWEB_LOG_DEBUG("Match fired"); 425a3e65892SEd Tanous softwareInterfaceAdded(asyncResp, m, std::move(payload)); 42686adcd6dSAndrew Geissler }; 42786adcd6dSAndrew Geissler 42886adcd6dSAndrew Geissler fwUpdateInProgress = true; 42986adcd6dSAndrew Geissler 43059d494eeSPatrick Williams fwUpdateMatcher = std::make_unique<sdbusplus::bus::match_t>( 43186adcd6dSAndrew Geissler *crow::connections::systemBus, 43286adcd6dSAndrew Geissler "interface='org.freedesktop.DBus.ObjectManager',type='signal'," 43386adcd6dSAndrew Geissler "member='InterfacesAdded',path='/xyz/openbmc_project/software'", 43486adcd6dSAndrew Geissler callback); 4354cde5d90SJames Feist 43659d494eeSPatrick Williams fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match_t>( 4374cde5d90SJames Feist *crow::connections::systemBus, 438e1cc4828SBrian Ma "interface='org.freedesktop.DBus.ObjectManager',type='signal'," 439e1cc4828SBrian Ma "member='InterfacesAdded'," 440e1cc4828SBrian Ma "path='/xyz/openbmc_project/logging'", 4418549b951SMyung Bae std::bind_front(afterUpdateErrorMatcher, asyncResp, url)); 44286adcd6dSAndrew Geissler } 443729dae72SJennifer Lee 444f86bcc87SEd Tanous struct TftpUrl 445f86bcc87SEd Tanous { 446f86bcc87SEd Tanous std::string fwFile; 447f86bcc87SEd Tanous std::string tftpServer; 448f86bcc87SEd Tanous }; 449f86bcc87SEd Tanous 450f86bcc87SEd Tanous inline std::optional<TftpUrl> 451f86bcc87SEd Tanous parseTftpUrl(std::string imageURI, 452f86bcc87SEd Tanous std::optional<std::string> transferProtocol, 453f86bcc87SEd Tanous crow::Response& res) 454f86bcc87SEd Tanous { 455f86bcc87SEd Tanous if (imageURI.find("://") == std::string::npos) 456f86bcc87SEd Tanous { 457f86bcc87SEd Tanous if (imageURI.starts_with("/")) 458f86bcc87SEd Tanous { 459f86bcc87SEd Tanous messages::actionParameterValueTypeError( 460f86bcc87SEd Tanous res, imageURI, "ImageURI", "UpdateService.SimpleUpdate"); 461f86bcc87SEd Tanous return std::nullopt; 462f86bcc87SEd Tanous } 463f86bcc87SEd Tanous if (!transferProtocol) 464f86bcc87SEd Tanous { 465f86bcc87SEd Tanous messages::actionParameterValueTypeError( 466f86bcc87SEd Tanous res, imageURI, "ImageURI", "UpdateService.SimpleUpdate"); 467f86bcc87SEd Tanous return std::nullopt; 468f86bcc87SEd Tanous } 469f86bcc87SEd Tanous // OpenBMC currently only supports TFTP 470f86bcc87SEd Tanous if (*transferProtocol != "TFTP") 471f86bcc87SEd Tanous { 472f86bcc87SEd Tanous messages::actionParameterNotSupported(res, "TransferProtocol", 473f86bcc87SEd Tanous *transferProtocol); 474f86bcc87SEd Tanous BMCWEB_LOG_ERROR("Request incorrect protocol parameter: {}", 475f86bcc87SEd Tanous *transferProtocol); 476f86bcc87SEd Tanous return std::nullopt; 477f86bcc87SEd Tanous } 478f86bcc87SEd Tanous imageURI = "tftp://" + imageURI; 479f86bcc87SEd Tanous } 480f86bcc87SEd Tanous 481f86bcc87SEd Tanous boost::system::result<boost::urls::url> url = 482f86bcc87SEd Tanous boost::urls::parse_absolute_uri(imageURI); 483f86bcc87SEd Tanous if (!url) 484f86bcc87SEd Tanous { 485f86bcc87SEd Tanous messages::actionParameterValueTypeError(res, imageURI, "ImageURI", 486f86bcc87SEd Tanous "UpdateService.SimpleUpdate"); 487f86bcc87SEd Tanous 488f86bcc87SEd Tanous return std::nullopt; 489f86bcc87SEd Tanous } 490f86bcc87SEd Tanous url->normalize(); 491f86bcc87SEd Tanous 492f86bcc87SEd Tanous if (url->scheme() != "tftp") 493f86bcc87SEd Tanous { 494f86bcc87SEd Tanous messages::actionParameterNotSupported(res, "ImageURI", imageURI); 495f86bcc87SEd Tanous return std::nullopt; 496f86bcc87SEd Tanous } 497f86bcc87SEd Tanous std::string path(url->encoded_path()); 498f86bcc87SEd Tanous if (path.size() < 2) 499f86bcc87SEd Tanous { 500f86bcc87SEd Tanous messages::actionParameterNotSupported(res, "ImageURI", imageURI); 501f86bcc87SEd Tanous return std::nullopt; 502f86bcc87SEd Tanous } 503f86bcc87SEd Tanous path.erase(0, 1); 504f86bcc87SEd Tanous std::string host(url->encoded_host_and_port()); 505f86bcc87SEd Tanous return TftpUrl{path, host}; 506f86bcc87SEd Tanous } 507f86bcc87SEd Tanous 5080554c984SAndrew Geissler /** 5090554c984SAndrew Geissler * UpdateServiceActionsSimpleUpdate class supports handle POST method for 5100554c984SAndrew Geissler * SimpleUpdate action. 5110554c984SAndrew Geissler */ 5127e860f15SJohn Edward Broadbent inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app) 5130554c984SAndrew Geissler { 5147e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 5157e860f15SJohn Edward Broadbent app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/") 516ed398213SEd Tanous .privileges(redfish::privileges::postUpdateService) 517002d39b4SEd Tanous .methods(boost::beast::http::verb::post)( 518002d39b4SEd Tanous [&app](const crow::Request& req, 5197e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 5203ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 52145ca1b86SEd Tanous { 52245ca1b86SEd Tanous return; 52345ca1b86SEd Tanous } 52445ca1b86SEd Tanous 5250554c984SAndrew Geissler std::optional<std::string> transferProtocol; 5260554c984SAndrew Geissler std::string imageURI; 5270554c984SAndrew Geissler 52862598e31SEd Tanous BMCWEB_LOG_DEBUG("Enter UpdateService.SimpleUpdate doPost"); 5290554c984SAndrew Geissler 5300554c984SAndrew Geissler // User can pass in both TransferProtocol and ImageURI parameters or 5314e0453b1SGunnar Mills // they can pass in just the ImageURI with the transfer protocol 5324e0453b1SGunnar Mills // embedded within it. 5330554c984SAndrew Geissler // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin 5340554c984SAndrew Geissler // 2) ImageURI:tftp://1.1.1.1/myfile.bin 5350554c984SAndrew Geissler 536002d39b4SEd Tanous if (!json_util::readJsonAction(req, asyncResp->res, "TransferProtocol", 537002d39b4SEd Tanous transferProtocol, "ImageURI", imageURI)) 5380554c984SAndrew Geissler { 53962598e31SEd Tanous BMCWEB_LOG_DEBUG("Missing TransferProtocol or ImageURI parameter"); 5400554c984SAndrew Geissler return; 5410554c984SAndrew Geissler } 542f86bcc87SEd Tanous std::optional<TftpUrl> ret = parseTftpUrl(imageURI, transferProtocol, 543f86bcc87SEd Tanous asyncResp->res); 544f86bcc87SEd Tanous if (!ret) 5450554c984SAndrew Geissler { 5460554c984SAndrew Geissler return; 5470554c984SAndrew Geissler } 5480554c984SAndrew Geissler 549f86bcc87SEd Tanous BMCWEB_LOG_DEBUG("Server: {} File: {}", ret->tftpServer, ret->fwFile); 5500554c984SAndrew Geissler 5510554c984SAndrew Geissler // Setup callback for when new software detected 5522618d5e3SGunnar Mills // Give TFTP 10 minutes to complete 5534cde5d90SJames Feist monitorForSoftwareAvailable( 554d7a596bdSAlbert Zhang asyncResp, req, 5554cde5d90SJames Feist "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate", 5562618d5e3SGunnar Mills 600); 5570554c984SAndrew Geissler 5582618d5e3SGunnar Mills // TFTP can take up to 10 minutes depending on image size and 5590554c984SAndrew Geissler // connection speed. Return to caller as soon as the TFTP operation 5600554c984SAndrew Geissler // has been started. The callback above will ensure the activate 5610554c984SAndrew Geissler // is started once the download has completed 5620554c984SAndrew Geissler redfish::messages::success(asyncResp->res); 5630554c984SAndrew Geissler 5640554c984SAndrew Geissler // Call TFTP service 5650554c984SAndrew Geissler crow::connections::systemBus->async_method_call( 5665e7e2dc5SEd Tanous [](const boost::system::error_code& ec) { 5670554c984SAndrew Geissler if (ec) 5680554c984SAndrew Geissler { 5690554c984SAndrew Geissler // messages::internalError(asyncResp->res); 5700554c984SAndrew Geissler cleanUp(); 57162598e31SEd Tanous BMCWEB_LOG_DEBUG("error_code = {}", ec); 57262598e31SEd Tanous BMCWEB_LOG_DEBUG("error msg = {}", ec.message()); 5730554c984SAndrew Geissler } 5740554c984SAndrew Geissler else 5750554c984SAndrew Geissler { 57662598e31SEd Tanous BMCWEB_LOG_DEBUG("Call to DownloaViaTFTP Success"); 5770554c984SAndrew Geissler } 5780554c984SAndrew Geissler }, 5790554c984SAndrew Geissler "xyz.openbmc_project.Software.Download", 580002d39b4SEd Tanous "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP", 581f86bcc87SEd Tanous "DownloadViaTFTP", ret->fwFile, ret->tftpServer); 5820554c984SAndrew Geissler 58362598e31SEd Tanous BMCWEB_LOG_DEBUG("Exit UpdateService.SimpleUpdate doPost"); 5847e860f15SJohn Edward Broadbent }); 585729dae72SJennifer Lee } 586729dae72SJennifer Lee 5870ed80c8cSGeorge Liu inline void uploadImageFile(crow::Response& res, std::string_view body) 5880ed80c8cSGeorge Liu { 5892c6ffdb0SEd Tanous std::filesystem::path filepath("/tmp/images/" + bmcweb::getRandomUUID()); 5902c6ffdb0SEd Tanous 59162598e31SEd Tanous BMCWEB_LOG_DEBUG("Writing file to {}", filepath.string()); 5920ed80c8cSGeorge Liu std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary | 5930ed80c8cSGeorge Liu std::ofstream::trunc); 5940ed80c8cSGeorge Liu // set the permission of the file to 640 59589492a15SPatrick Williams std::filesystem::perms permission = std::filesystem::perms::owner_read | 59689492a15SPatrick Williams std::filesystem::perms::group_read; 5970ed80c8cSGeorge Liu std::filesystem::permissions(filepath, permission); 5980ed80c8cSGeorge Liu out << body; 5990ed80c8cSGeorge Liu 6000ed80c8cSGeorge Liu if (out.bad()) 6010ed80c8cSGeorge Liu { 6020ed80c8cSGeorge Liu messages::internalError(res); 6030ed80c8cSGeorge Liu cleanUp(); 6040ed80c8cSGeorge Liu } 6050ed80c8cSGeorge Liu } 6060ed80c8cSGeorge Liu 6070ed80c8cSGeorge Liu inline void setApplyTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 6080ed80c8cSGeorge Liu const std::string& applyTime) 6090ed80c8cSGeorge Liu { 6100ed80c8cSGeorge Liu std::string applyTimeNewVal; 6110ed80c8cSGeorge Liu if (applyTime == "Immediate") 6120ed80c8cSGeorge Liu { 6130ed80c8cSGeorge Liu applyTimeNewVal = 6140ed80c8cSGeorge Liu "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate"; 6150ed80c8cSGeorge Liu } 6160ed80c8cSGeorge Liu else if (applyTime == "OnReset") 6170ed80c8cSGeorge Liu { 6180ed80c8cSGeorge Liu applyTimeNewVal = 6190ed80c8cSGeorge Liu "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset"; 6200ed80c8cSGeorge Liu } 6210ed80c8cSGeorge Liu else 6220ed80c8cSGeorge Liu { 62362598e31SEd Tanous BMCWEB_LOG_INFO( 62462598e31SEd Tanous "ApplyTime value is not in the list of acceptable values"); 6250ed80c8cSGeorge Liu messages::propertyValueNotInList(asyncResp->res, applyTime, 6260ed80c8cSGeorge Liu "ApplyTime"); 6270ed80c8cSGeorge Liu return; 6280ed80c8cSGeorge Liu } 6290ed80c8cSGeorge Liu 630d02aad39SEd Tanous setDbusProperty(asyncResp, "xyz.openbmc_project.Settings", 631d02aad39SEd Tanous sdbusplus::message::object_path( 632d02aad39SEd Tanous "/xyz/openbmc_project/software/apply_time"), 633d02aad39SEd Tanous "xyz.openbmc_project.Software.ApplyTime", 634d02aad39SEd Tanous "RequestedApplyTime", "ApplyTime", applyTimeNewVal); 6350ed80c8cSGeorge Liu } 6360ed80c8cSGeorge Liu 6370ed80c8cSGeorge Liu inline void 6380ed80c8cSGeorge Liu updateMultipartContext(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 6390ed80c8cSGeorge Liu const MultipartParser& parser) 6400ed80c8cSGeorge Liu { 6410ed80c8cSGeorge Liu const std::string* uploadData = nullptr; 6420ed80c8cSGeorge Liu std::optional<std::string> applyTime = "OnReset"; 6430ed80c8cSGeorge Liu bool targetFound = false; 6440ed80c8cSGeorge Liu for (const FormPart& formpart : parser.mime_fields) 6450ed80c8cSGeorge Liu { 6460ed80c8cSGeorge Liu boost::beast::http::fields::const_iterator it = 6470ed80c8cSGeorge Liu formpart.fields.find("Content-Disposition"); 6480ed80c8cSGeorge Liu if (it == formpart.fields.end()) 6490ed80c8cSGeorge Liu { 65062598e31SEd Tanous BMCWEB_LOG_ERROR("Couldn't find Content-Disposition"); 6510ed80c8cSGeorge Liu return; 6520ed80c8cSGeorge Liu } 65362598e31SEd Tanous BMCWEB_LOG_INFO("Parsing value {}", it->value()); 6540ed80c8cSGeorge Liu 6550ed80c8cSGeorge Liu // The construction parameters of param_list must start with `;` 6560ed80c8cSGeorge Liu size_t index = it->value().find(';'); 6570ed80c8cSGeorge Liu if (index == std::string::npos) 6580ed80c8cSGeorge Liu { 6590ed80c8cSGeorge Liu continue; 6600ed80c8cSGeorge Liu } 6610ed80c8cSGeorge Liu 66289492a15SPatrick Williams for (const auto& param : 6630ed80c8cSGeorge Liu boost::beast::http::param_list{it->value().substr(index)}) 6640ed80c8cSGeorge Liu { 6650ed80c8cSGeorge Liu if (param.first != "name" || param.second.empty()) 6660ed80c8cSGeorge Liu { 6670ed80c8cSGeorge Liu continue; 6680ed80c8cSGeorge Liu } 6690ed80c8cSGeorge Liu 6700ed80c8cSGeorge Liu if (param.second == "UpdateParameters") 6710ed80c8cSGeorge Liu { 6720ed80c8cSGeorge Liu std::vector<std::string> targets; 6730ed80c8cSGeorge Liu nlohmann::json content = 6740ed80c8cSGeorge Liu nlohmann::json::parse(formpart.content); 6757cb59f65SEd Tanous nlohmann::json::object_t* obj = 6767cb59f65SEd Tanous content.get_ptr<nlohmann::json::object_t*>(); 6777cb59f65SEd Tanous if (obj == nullptr) 6787cb59f65SEd Tanous { 6797cb59f65SEd Tanous messages::propertyValueFormatError(asyncResp->res, targets, 6807cb59f65SEd Tanous "UpdateParameters"); 6817cb59f65SEd Tanous return; 6827cb59f65SEd Tanous } 6837cb59f65SEd Tanous 6847cb59f65SEd Tanous if (!json_util::readJsonObject( 6857cb59f65SEd Tanous *obj, asyncResp->res, "Targets", targets, 6867cb59f65SEd Tanous "@Redfish.OperationApplyTime", applyTime)) 6870ed80c8cSGeorge Liu { 6880ed80c8cSGeorge Liu return; 6890ed80c8cSGeorge Liu } 6900ed80c8cSGeorge Liu if (targets.size() != 1) 6910ed80c8cSGeorge Liu { 6927cb59f65SEd Tanous messages::propertyValueFormatError(asyncResp->res, targets, 6937cb59f65SEd Tanous "Targets"); 6940ed80c8cSGeorge Liu return; 6950ed80c8cSGeorge Liu } 6960ed80c8cSGeorge Liu if (targets[0] != "/redfish/v1/Managers/bmc") 6970ed80c8cSGeorge Liu { 6987cb59f65SEd Tanous messages::propertyValueNotInList(asyncResp->res, targets[0], 6997cb59f65SEd Tanous "Targets/0"); 7000ed80c8cSGeorge Liu return; 7010ed80c8cSGeorge Liu } 7020ed80c8cSGeorge Liu targetFound = true; 7030ed80c8cSGeorge Liu } 7040ed80c8cSGeorge Liu else if (param.second == "UpdateFile") 7050ed80c8cSGeorge Liu { 7060ed80c8cSGeorge Liu uploadData = &(formpart.content); 7070ed80c8cSGeorge Liu } 7080ed80c8cSGeorge Liu } 7090ed80c8cSGeorge Liu } 7100ed80c8cSGeorge Liu 7110ed80c8cSGeorge Liu if (uploadData == nullptr) 7120ed80c8cSGeorge Liu { 71362598e31SEd Tanous BMCWEB_LOG_ERROR("Upload data is NULL"); 7140ed80c8cSGeorge Liu messages::propertyMissing(asyncResp->res, "UpdateFile"); 7150ed80c8cSGeorge Liu return; 7160ed80c8cSGeorge Liu } 7170ed80c8cSGeorge Liu if (!targetFound) 7180ed80c8cSGeorge Liu { 7190ed80c8cSGeorge Liu messages::propertyMissing(asyncResp->res, "targets"); 7200ed80c8cSGeorge Liu return; 7210ed80c8cSGeorge Liu } 7220ed80c8cSGeorge Liu 7230ed80c8cSGeorge Liu setApplyTime(asyncResp, *applyTime); 7240ed80c8cSGeorge Liu 7250ed80c8cSGeorge Liu uploadImageFile(asyncResp->res, *uploadData); 7260ed80c8cSGeorge Liu } 7270ed80c8cSGeorge Liu 728c2051d11SEd Tanous inline void 729c2051d11SEd Tanous handleUpdateServicePost(App& app, const crow::Request& req, 730c2051d11SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 731c2051d11SEd Tanous { 7323ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 733c2051d11SEd Tanous { 734c2051d11SEd Tanous return; 735c2051d11SEd Tanous } 736b33a4327SNinad Palsule std::string_view contentType = req.getHeaderValue("Content-Type"); 737b33a4327SNinad Palsule 73862598e31SEd Tanous BMCWEB_LOG_DEBUG("doPost: contentType={}", contentType); 739b33a4327SNinad Palsule 740b33a4327SNinad Palsule // Make sure that content type is application/octet-stream or 741b33a4327SNinad Palsule // multipart/form-data 74218f8f608SEd Tanous if (bmcweb::asciiIEquals(contentType, "application/octet-stream")) 743b33a4327SNinad Palsule { 744b33a4327SNinad Palsule // Setup callback for when new software detected 745b33a4327SNinad Palsule monitorForSoftwareAvailable(asyncResp, req, 746b33a4327SNinad Palsule "/redfish/v1/UpdateService"); 747b33a4327SNinad Palsule 748b33a4327SNinad Palsule uploadImageFile(asyncResp->res, req.body()); 749b33a4327SNinad Palsule } 750b33a4327SNinad Palsule else if (contentType.starts_with("multipart/form-data")) 751b33a4327SNinad Palsule { 752b33a4327SNinad Palsule MultipartParser parser; 753c2051d11SEd Tanous 754c2051d11SEd Tanous // Setup callback for when new software detected 755b33a4327SNinad Palsule monitorForSoftwareAvailable(asyncResp, req, 756b33a4327SNinad Palsule "/redfish/v1/UpdateService"); 757c2051d11SEd Tanous 7580ed80c8cSGeorge Liu ParserError ec = parser.parse(req); 7590ed80c8cSGeorge Liu if (ec != ParserError::PARSER_SUCCESS) 7600ed80c8cSGeorge Liu { 7610ed80c8cSGeorge Liu // handle error 76262598e31SEd Tanous BMCWEB_LOG_ERROR("MIME parse failed, ec : {}", 76362598e31SEd Tanous static_cast<int>(ec)); 7640ed80c8cSGeorge Liu messages::internalError(asyncResp->res); 7650ed80c8cSGeorge Liu return; 7660ed80c8cSGeorge Liu } 7670ed80c8cSGeorge Liu updateMultipartContext(asyncResp, parser); 768c2051d11SEd Tanous } 769b33a4327SNinad Palsule else 770b33a4327SNinad Palsule { 77162598e31SEd Tanous BMCWEB_LOG_DEBUG("Bad content type specified:{}", contentType); 772b33a4327SNinad Palsule asyncResp->res.result(boost::beast::http::status::bad_request); 773b33a4327SNinad Palsule } 774b33a4327SNinad Palsule } 775c2051d11SEd Tanous 7767e860f15SJohn Edward Broadbent inline void requestRoutesUpdateService(App& app) 7771abe55efSEd Tanous { 7787e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") 779ed398213SEd Tanous .privileges(redfish::privileges::getUpdateService) 780002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 781002d39b4SEd Tanous [&app](const crow::Request& req, 782002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 7833ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 78445ca1b86SEd Tanous { 78545ca1b86SEd Tanous return; 78645ca1b86SEd Tanous } 7878d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 7880ed80c8cSGeorge Liu "#UpdateService.v1_11_1.UpdateService"; 7898d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService"; 7908d1b46d7Szhanghch05 asyncResp->res.jsonValue["Id"] = "UpdateService"; 791002d39b4SEd Tanous asyncResp->res.jsonValue["Description"] = "Service for Software Update"; 7928d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "Update Service"; 7934dc23f3fSEd Tanous 7947e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["HttpPushUri"] = 7954dc23f3fSEd Tanous "/redfish/v1/UpdateService/update"; 7960ed80c8cSGeorge Liu asyncResp->res.jsonValue["MultipartHttpPushUri"] = 7970ed80c8cSGeorge Liu "/redfish/v1/UpdateService/update"; 7984dc23f3fSEd Tanous 7990f74e643SEd Tanous // UpdateService cannot be disabled 8008d1b46d7Szhanghch05 asyncResp->res.jsonValue["ServiceEnabled"] = true; 8011476687dSEd Tanous asyncResp->res.jsonValue["FirmwareInventory"]["@odata.id"] = 8021476687dSEd Tanous "/redfish/v1/UpdateService/FirmwareInventory"; 803d61e5194STejas Patil // Get the MaxImageSizeBytes 804d61e5194STejas Patil asyncResp->res.jsonValue["MaxImageSizeBytes"] = 805d61e5194STejas Patil bmcwebHttpReqBodyLimitMb * 1024 * 1024; 806d61e5194STejas Patil 8070554c984SAndrew Geissler #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE 8080554c984SAndrew Geissler // Update Actions object. 8090554c984SAndrew Geissler nlohmann::json& updateSvcSimpleUpdate = 810002d39b4SEd Tanous asyncResp->res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"]; 8110554c984SAndrew Geissler updateSvcSimpleUpdate["target"] = 8120554c984SAndrew Geissler "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate"; 813002d39b4SEd Tanous updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = { 814002d39b4SEd Tanous "TFTP"}; 8150554c984SAndrew Geissler #endif 816274dfe62SJayashankar Padath // Get the current ApplyTime value 8171e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::string>( 8181e1e598dSJonathan Doman *crow::connections::systemBus, "xyz.openbmc_project.Settings", 8191e1e598dSJonathan Doman "/xyz/openbmc_project/software/apply_time", 8201e1e598dSJonathan Doman "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime", 8215e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec, 8221e1e598dSJonathan Doman const std::string& applyTime) { 823274dfe62SJayashankar Padath if (ec) 824274dfe62SJayashankar Padath { 82562598e31SEd Tanous BMCWEB_LOG_DEBUG("DBUS response error {}", ec); 8268d1b46d7Szhanghch05 messages::internalError(asyncResp->res); 827274dfe62SJayashankar Padath return; 828274dfe62SJayashankar Padath } 829274dfe62SJayashankar Padath 830274dfe62SJayashankar Padath // Store the ApplyTime Value 8311e1e598dSJonathan Doman if (applyTime == "xyz.openbmc_project.Software.ApplyTime." 8321e1e598dSJonathan Doman "RequestedApplyTimes.Immediate") 833274dfe62SJayashankar Padath { 834002d39b4SEd Tanous asyncResp->res.jsonValue["HttpPushUriOptions"] 8357e860f15SJohn Edward Broadbent ["HttpPushUriApplyTime"]["ApplyTime"] = 8367e860f15SJohn Edward Broadbent "Immediate"; 837274dfe62SJayashankar Padath } 838002d39b4SEd Tanous else if (applyTime == "xyz.openbmc_project.Software.ApplyTime." 8391e1e598dSJonathan Doman "RequestedApplyTimes.OnReset") 840274dfe62SJayashankar Padath { 841002d39b4SEd Tanous asyncResp->res.jsonValue["HttpPushUriOptions"] 8427e860f15SJohn Edward Broadbent ["HttpPushUriApplyTime"]["ApplyTime"] = 8437e860f15SJohn Edward Broadbent "OnReset"; 844274dfe62SJayashankar Padath } 8451e1e598dSJonathan Doman }); 8467e860f15SJohn Edward Broadbent }); 8477e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") 848ed398213SEd Tanous .privileges(redfish::privileges::patchUpdateService) 849002d39b4SEd Tanous .methods(boost::beast::http::verb::patch)( 850002d39b4SEd Tanous [&app](const crow::Request& req, 851002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 8523ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 85345ca1b86SEd Tanous { 85445ca1b86SEd Tanous return; 85545ca1b86SEd Tanous } 85662598e31SEd Tanous BMCWEB_LOG_DEBUG("doPatch..."); 857fa1a5a38SJayashankar Padath 858274dfe62SJayashankar Padath std::optional<std::string> applyTime; 8597cb59f65SEd Tanous if (!json_util::readJsonPatch( 8607cb59f65SEd Tanous req, asyncResp->res, 8617cb59f65SEd Tanous "HttpPushUriOptions/HttpPushUriApplyTime/ApplyTime", applyTime)) 862274dfe62SJayashankar Padath { 863274dfe62SJayashankar Padath return; 864274dfe62SJayashankar Padath } 865274dfe62SJayashankar Padath 866274dfe62SJayashankar Padath if (applyTime) 867fa1a5a38SJayashankar Padath { 8680ed80c8cSGeorge Liu setApplyTime(asyncResp, *applyTime); 869fa1a5a38SJayashankar Padath } 8707e860f15SJohn Edward Broadbent }); 871c2051d11SEd Tanous 8724dc23f3fSEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/update/") 8734dc23f3fSEd Tanous .privileges(redfish::privileges::postUpdateService) 8747e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 875c2051d11SEd Tanous std::bind_front(handleUpdateServicePost, std::ref(app))); 876729dae72SJennifer Lee } 877729dae72SJennifer Lee 8787e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventoryCollection(App& app) 8791abe55efSEd Tanous { 8807e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/") 881ed398213SEd Tanous .privileges(redfish::privileges::getSoftwareInventoryCollection) 8821476687dSEd Tanous .methods(boost::beast::http::verb::get)( 8831476687dSEd Tanous [&app](const crow::Request& req, 8841476687dSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 8853ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 88645ca1b86SEd Tanous { 88745ca1b86SEd Tanous return; 88845ca1b86SEd Tanous } 8898d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 8900f74e643SEd Tanous "#SoftwareInventoryCollection.SoftwareInventoryCollection"; 8918d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.id"] = 8920f74e643SEd Tanous "/redfish/v1/UpdateService/FirmwareInventory"; 893002d39b4SEd Tanous asyncResp->res.jsonValue["Name"] = "Software Inventory Collection"; 89408d81adaSJohn Edward Broadbent const std::array<const std::string_view, 1> iface = { 895e99073f5SGeorge Liu "xyz.openbmc_project.Software.Version"}; 8966c4eb9deSJennifer Lee 89708d81adaSJohn Edward Broadbent redfish::collection_util::getCollectionMembers( 89808d81adaSJohn Edward Broadbent asyncResp, 89908d81adaSJohn Edward Broadbent boost::urls::url("/redfish/v1/UpdateService/FirmwareInventory"), 90008d81adaSJohn Edward Broadbent iface, "/xyz/openbmc_project/software"); 9017e860f15SJohn Edward Broadbent }); 902729dae72SJennifer Lee } 90387d84729SAndrew Geissler /* Fill related item links (i.e. bmc, bios) in for inventory */ 9047e860f15SJohn Edward Broadbent inline static void 905ac106bf6SEd Tanous getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 90687d84729SAndrew Geissler const std::string& purpose) 90787d84729SAndrew Geissler { 908eee0013eSWilly Tu if (purpose == sw_util::bmcPurpose) 90987d84729SAndrew Geissler { 910ac106bf6SEd Tanous nlohmann::json& relatedItem = asyncResp->res.jsonValue["RelatedItem"]; 9111476687dSEd Tanous nlohmann::json::object_t item; 9121476687dSEd Tanous item["@odata.id"] = "/redfish/v1/Managers/bmc"; 913b2ba3072SPatrick Williams relatedItem.emplace_back(std::move(item)); 914ac106bf6SEd Tanous asyncResp->res.jsonValue["RelatedItem@odata.count"] = 915ac106bf6SEd Tanous relatedItem.size(); 91687d84729SAndrew Geissler } 917eee0013eSWilly Tu else if (purpose == sw_util::biosPurpose) 91887d84729SAndrew Geissler { 919ac106bf6SEd Tanous nlohmann::json& relatedItem = asyncResp->res.jsonValue["RelatedItem"]; 9201476687dSEd Tanous nlohmann::json::object_t item; 9211476687dSEd Tanous item["@odata.id"] = "/redfish/v1/Systems/system/Bios"; 922b2ba3072SPatrick Williams relatedItem.emplace_back(std::move(item)); 923ac106bf6SEd Tanous asyncResp->res.jsonValue["RelatedItem@odata.count"] = 924ac106bf6SEd Tanous relatedItem.size(); 92587d84729SAndrew Geissler } 92687d84729SAndrew Geissler else 92787d84729SAndrew Geissler { 928bf2ddedeSCarson Labrado BMCWEB_LOG_DEBUG("Unknown software purpose {}", purpose); 92987d84729SAndrew Geissler } 93087d84729SAndrew Geissler } 93187d84729SAndrew Geissler 932af24660dSWilly Tu inline void 933af24660dSWilly Tu getSoftwareVersion(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 934af24660dSWilly Tu const std::string& service, const std::string& path, 935af24660dSWilly Tu const std::string& swId) 936af24660dSWilly Tu { 937d1bde9e5SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 938d1bde9e5SKrzysztof Grobelny *crow::connections::systemBus, service, path, 939d1bde9e5SKrzysztof Grobelny "xyz.openbmc_project.Software.Version", 940af24660dSWilly Tu [asyncResp, 9418b24275dSEd Tanous swId](const boost::system::error_code& ec, 942af24660dSWilly Tu const dbus::utility::DBusPropertiesMap& propertiesList) { 9438b24275dSEd Tanous if (ec) 944af24660dSWilly Tu { 945af24660dSWilly Tu messages::internalError(asyncResp->res); 946af24660dSWilly Tu return; 947af24660dSWilly Tu } 948d1bde9e5SKrzysztof Grobelny 949af24660dSWilly Tu const std::string* swInvPurpose = nullptr; 950af24660dSWilly Tu const std::string* version = nullptr; 951d1bde9e5SKrzysztof Grobelny 952d1bde9e5SKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 953d1bde9e5SKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), propertiesList, "Purpose", 954d1bde9e5SKrzysztof Grobelny swInvPurpose, "Version", version); 955d1bde9e5SKrzysztof Grobelny 956d1bde9e5SKrzysztof Grobelny if (!success) 957af24660dSWilly Tu { 958d1bde9e5SKrzysztof Grobelny messages::internalError(asyncResp->res); 959d1bde9e5SKrzysztof Grobelny return; 960af24660dSWilly Tu } 961af24660dSWilly Tu 962af24660dSWilly Tu if (swInvPurpose == nullptr) 963af24660dSWilly Tu { 96462598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't find property \"Purpose\"!"); 965af24660dSWilly Tu messages::internalError(asyncResp->res); 966af24660dSWilly Tu return; 967af24660dSWilly Tu } 968af24660dSWilly Tu 96962598e31SEd Tanous BMCWEB_LOG_DEBUG("swInvPurpose = {}", *swInvPurpose); 970af24660dSWilly Tu 971af24660dSWilly Tu if (version == nullptr) 972af24660dSWilly Tu { 97362598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't find property \"Version\"!"); 974af24660dSWilly Tu 975af24660dSWilly Tu messages::internalError(asyncResp->res); 976af24660dSWilly Tu 977af24660dSWilly Tu return; 978af24660dSWilly Tu } 979af24660dSWilly Tu asyncResp->res.jsonValue["Version"] = *version; 980af24660dSWilly Tu asyncResp->res.jsonValue["Id"] = swId; 981af24660dSWilly Tu 982af24660dSWilly Tu // swInvPurpose is of format: 983af24660dSWilly Tu // xyz.openbmc_project.Software.Version.VersionPurpose.ABC 984af24660dSWilly Tu // Translate this to "ABC image" 985af24660dSWilly Tu size_t endDesc = swInvPurpose->rfind('.'); 986af24660dSWilly Tu if (endDesc == std::string::npos) 987af24660dSWilly Tu { 988af24660dSWilly Tu messages::internalError(asyncResp->res); 989af24660dSWilly Tu return; 990af24660dSWilly Tu } 991af24660dSWilly Tu endDesc++; 992af24660dSWilly Tu if (endDesc >= swInvPurpose->size()) 993af24660dSWilly Tu { 994af24660dSWilly Tu messages::internalError(asyncResp->res); 995af24660dSWilly Tu return; 996af24660dSWilly Tu } 997af24660dSWilly Tu 998af24660dSWilly Tu std::string formatDesc = swInvPurpose->substr(endDesc); 999af24660dSWilly Tu asyncResp->res.jsonValue["Description"] = formatDesc + " image"; 1000af24660dSWilly Tu getRelatedItems(asyncResp, *swInvPurpose); 1001d1bde9e5SKrzysztof Grobelny }); 1002af24660dSWilly Tu } 1003af24660dSWilly Tu 10047e860f15SJohn Edward Broadbent inline void requestRoutesSoftwareInventory(App& app) 10051abe55efSEd Tanous { 10067e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/") 1007ed398213SEd Tanous .privileges(redfish::privileges::getSoftwareInventory) 1008002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 1009002d39b4SEd Tanous [&app](const crow::Request& req, 101045ca1b86SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 10117e860f15SJohn Edward Broadbent const std::string& param) { 10123ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 101345ca1b86SEd Tanous { 101445ca1b86SEd Tanous return; 101545ca1b86SEd Tanous } 10163ae837c9SEd Tanous std::shared_ptr<std::string> swId = 10177e860f15SJohn Edward Broadbent std::make_shared<std::string>(param); 1018c711bf86SEd Tanous 1019ef4c65b7SEd Tanous asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( 1020ef4c65b7SEd Tanous "/redfish/v1/UpdateService/FirmwareInventory/{}", *swId); 1021c711bf86SEd Tanous 1022e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 1023e99073f5SGeorge Liu "xyz.openbmc_project.Software.Version"}; 1024e99073f5SGeorge Liu dbus::utility::getSubTree( 1025e99073f5SGeorge Liu "/", 0, interfaces, 1026b9d36b47SEd Tanous [asyncResp, 1027e99073f5SGeorge Liu swId](const boost::system::error_code& ec, 1028b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 102962598e31SEd Tanous BMCWEB_LOG_DEBUG("doGet callback..."); 10301abe55efSEd Tanous if (ec) 10311abe55efSEd Tanous { 1032f12894f8SJason M. Bills messages::internalError(asyncResp->res); 10336c4eb9deSJennifer Lee return; 10346c4eb9deSJennifer Lee } 10356c4eb9deSJennifer Lee 10366913228dSAndrew Geissler // Ensure we find our input swId, otherwise return an error 10376913228dSAndrew Geissler bool found = false; 1038002d39b4SEd Tanous for (const std::pair<std::string, 10397e860f15SJohn Edward Broadbent std::vector<std::pair< 1040002d39b4SEd Tanous std::string, std::vector<std::string>>>>& 1041002d39b4SEd Tanous obj : subtree) 10421abe55efSEd Tanous { 104311ba3979SEd Tanous if (!obj.first.ends_with(*swId)) 10441abe55efSEd Tanous { 1045acb7cfb4SJennifer Lee continue; 1046acb7cfb4SJennifer Lee } 1047acb7cfb4SJennifer Lee 104826f6976fSEd Tanous if (obj.second.empty()) 10491abe55efSEd Tanous { 1050acb7cfb4SJennifer Lee continue; 1051acb7cfb4SJennifer Lee } 10526c4eb9deSJennifer Lee 10536913228dSAndrew Geissler found = true; 1054eee0013eSWilly Tu sw_util::getSwStatus(asyncResp, swId, obj.second[0].first); 1055af24660dSWilly Tu getSoftwareVersion(asyncResp, obj.second[0].first, obj.first, 1056af24660dSWilly Tu *swId); 10576c4eb9deSJennifer Lee } 10586913228dSAndrew Geissler if (!found) 10596913228dSAndrew Geissler { 106062598e31SEd Tanous BMCWEB_LOG_WARNING("Input swID {} not found!", *swId); 10616913228dSAndrew Geissler messages::resourceMissingAtURI( 1062ef4c65b7SEd Tanous asyncResp->res, 1063ef4c65b7SEd Tanous boost::urls::format( 1064ef4c65b7SEd Tanous "/redfish/v1/UpdateService/FirmwareInventory/{}", 1065ef4c65b7SEd Tanous *swId)); 10666913228dSAndrew Geissler return; 10676913228dSAndrew Geissler } 10684e68c45bSAyushi Smriti asyncResp->res.jsonValue["@odata.type"] = 10694e68c45bSAyushi Smriti "#SoftwareInventory.v1_1_0.SoftwareInventory"; 10704e68c45bSAyushi Smriti asyncResp->res.jsonValue["Name"] = "Software Inventory"; 10714e68c45bSAyushi Smriti asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK"; 10723f8a743aSAppaRao Puli 10733f8a743aSAppaRao Puli asyncResp->res.jsonValue["Updateable"] = false; 1074eee0013eSWilly Tu sw_util::getSwUpdatableStatus(asyncResp, swId); 1075e99073f5SGeorge Liu }); 10767e860f15SJohn Edward Broadbent }); 10776c4eb9deSJennifer Lee } 1078729dae72SJennifer Lee 1079729dae72SJennifer Lee } // namespace redfish 1080