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*757178a5SEd Tanous #include "generated/enums/update_service.hpp" 230ed80c8cSGeorge Liu #include "multipart_parser.hpp" 242c6ffdb0SEd Tanous #include "ossl_random.hpp" 253ccb3adbSEd Tanous #include "query.hpp" 263ccb3adbSEd Tanous #include "registries/privilege_registry.hpp" 27a8e884fcSEd Tanous #include "task.hpp" 2808d81adaSJohn Edward Broadbent #include "utils/collection.hpp" 293ccb3adbSEd Tanous #include "utils/dbus_utils.hpp" 303ccb3adbSEd Tanous #include "utils/sw_utils.hpp" 313ccb3adbSEd Tanous 32e99073f5SGeorge Liu #include <boost/system/error_code.hpp> 33ef4c65b7SEd Tanous #include <boost/url/format.hpp> 341e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp> 353ccb3adbSEd Tanous #include <sdbusplus/bus/match.hpp> 36d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp> 371214b7e7SGunnar Mills 382b73119cSGeorge Liu #include <array> 390ed80c8cSGeorge Liu #include <filesystem> 407cb59f65SEd Tanous #include <optional> 417cb59f65SEd Tanous #include <string> 422b73119cSGeorge Liu #include <string_view> 432b73119cSGeorge Liu 441abe55efSEd Tanous namespace redfish 451abe55efSEd Tanous { 4627826b5fSEd Tanous 470e7de46fSAndrew Geissler // Match signals added on software path 48cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 4959d494eeSPatrick Williams static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateMatcher; 50cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 5159d494eeSPatrick Williams static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateErrorMatcher; 520e7de46fSAndrew Geissler // Only allow one update at a time 53cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 540e7de46fSAndrew Geissler static bool fwUpdateInProgress = false; 5586adcd6dSAndrew Geissler // Timer for software available 56cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 57271584abSEd Tanous static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer; 5886adcd6dSAndrew Geissler 59df254f2cSEd Tanous inline void cleanUp() 6086adcd6dSAndrew Geissler { 6186adcd6dSAndrew Geissler fwUpdateInProgress = false; 6286adcd6dSAndrew Geissler fwUpdateMatcher = nullptr; 634cde5d90SJames Feist fwUpdateErrorMatcher = nullptr; 6486adcd6dSAndrew Geissler } 65df254f2cSEd Tanous 66df254f2cSEd Tanous inline void activateImage(const std::string& objPath, 6786adcd6dSAndrew Geissler const std::string& service) 6886adcd6dSAndrew Geissler { 6962598e31SEd Tanous BMCWEB_LOG_DEBUG("Activate image for {} {}", objPath, service); 709ae226faSGeorge Liu sdbusplus::asio::setProperty( 719ae226faSGeorge Liu *crow::connections::systemBus, service, objPath, 729ae226faSGeorge Liu "xyz.openbmc_project.Software.Activation", "RequestedActivation", 739ae226faSGeorge Liu "xyz.openbmc_project.Software.Activation.RequestedActivations.Active", 748b24275dSEd Tanous [](const boost::system::error_code& ec) { 758b24275dSEd Tanous if (ec) 7686adcd6dSAndrew Geissler { 7762598e31SEd Tanous BMCWEB_LOG_DEBUG("error_code = {}", ec); 7862598e31SEd Tanous BMCWEB_LOG_DEBUG("error msg = {}", ec.message()); 7986adcd6dSAndrew Geissler } 809ae226faSGeorge Liu }); 8186adcd6dSAndrew Geissler } 820554c984SAndrew Geissler 830554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr 840554c984SAndrew Geissler // then no asyncResp updates will occur 858d1b46d7Szhanghch05 static void 868d1b46d7Szhanghch05 softwareInterfaceAdded(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 8759d494eeSPatrick Williams sdbusplus::message_t& m, task::Payload&& payload) 8886adcd6dSAndrew Geissler { 8980f79a40SMichael Shen dbus::utility::DBusInterfacesMap interfacesProperties; 9086adcd6dSAndrew Geissler 9186adcd6dSAndrew Geissler sdbusplus::message::object_path objPath; 9286adcd6dSAndrew Geissler 9386adcd6dSAndrew Geissler m.read(objPath, interfacesProperties); 9486adcd6dSAndrew Geissler 9562598e31SEd Tanous BMCWEB_LOG_DEBUG("obj path = {}", objPath.str); 96e3eb3d63SEd Tanous for (const auto& interface : interfacesProperties) 9786adcd6dSAndrew Geissler { 9862598e31SEd Tanous BMCWEB_LOG_DEBUG("interface = {}", interface.first); 9986adcd6dSAndrew Geissler 10086adcd6dSAndrew Geissler if (interface.first == "xyz.openbmc_project.Software.Activation") 10186adcd6dSAndrew Geissler { 10286adcd6dSAndrew Geissler // Retrieve service and activate 1032b73119cSGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 1042b73119cSGeorge Liu "xyz.openbmc_project.Software.Activation"}; 1052b73119cSGeorge Liu dbus::utility::getDbusObject( 1062b73119cSGeorge Liu objPath.str, interfaces, 107a3e65892SEd Tanous [objPath, asyncResp, payload(std::move(payload))]( 1088b24275dSEd Tanous const boost::system::error_code& ec, 109a3e65892SEd Tanous const std::vector< 110a3e65892SEd Tanous std::pair<std::string, std::vector<std::string>>>& 111a3e65892SEd Tanous objInfo) mutable { 1128b24275dSEd Tanous if (ec) 11386adcd6dSAndrew Geissler { 11462598e31SEd Tanous BMCWEB_LOG_DEBUG("error_code = {}", ec); 11562598e31SEd Tanous BMCWEB_LOG_DEBUG("error msg = {}", ec.message()); 1160554c984SAndrew Geissler if (asyncResp) 1170554c984SAndrew Geissler { 11886adcd6dSAndrew Geissler messages::internalError(asyncResp->res); 1190554c984SAndrew Geissler } 12086adcd6dSAndrew Geissler cleanUp(); 12186adcd6dSAndrew Geissler return; 12286adcd6dSAndrew Geissler } 12386adcd6dSAndrew Geissler // Ensure we only got one service back 12486adcd6dSAndrew Geissler if (objInfo.size() != 1) 12586adcd6dSAndrew Geissler { 12662598e31SEd Tanous BMCWEB_LOG_ERROR("Invalid Object Size {}", objInfo.size()); 1270554c984SAndrew Geissler if (asyncResp) 1280554c984SAndrew Geissler { 12986adcd6dSAndrew Geissler messages::internalError(asyncResp->res); 1300554c984SAndrew Geissler } 13186adcd6dSAndrew Geissler cleanUp(); 13286adcd6dSAndrew Geissler return; 13386adcd6dSAndrew Geissler } 13486adcd6dSAndrew Geissler // cancel timer only when 13586adcd6dSAndrew Geissler // xyz.openbmc_project.Software.Activation interface 13686adcd6dSAndrew Geissler // is added 13786adcd6dSAndrew Geissler fwAvailableTimer = nullptr; 13886adcd6dSAndrew Geissler 13986adcd6dSAndrew Geissler activateImage(objPath.str, objInfo[0].first); 1400554c984SAndrew Geissler if (asyncResp) 1410554c984SAndrew Geissler { 14232898ceaSJames Feist std::shared_ptr<task::TaskData> task = 14332898ceaSJames Feist task::TaskData::createTask( 1448b24275dSEd Tanous [](const boost::system::error_code& ec2, 14559d494eeSPatrick Williams sdbusplus::message_t& msg, 1461214b7e7SGunnar Mills const std::shared_ptr<task::TaskData>& 1471214b7e7SGunnar Mills taskData) { 1488b24275dSEd Tanous if (ec2) 14932898ceaSJames Feist { 15032898ceaSJames Feist return task::completed; 15132898ceaSJames Feist } 15232898ceaSJames Feist 15332898ceaSJames Feist std::string iface; 154b9d36b47SEd Tanous dbus::utility::DBusPropertiesMap values; 155fd9ab9e1SJames Feist 156002d39b4SEd Tanous std::string index = std::to_string(taskData->index); 15732898ceaSJames Feist msg.read(iface, values); 158fd9ab9e1SJames Feist 159002d39b4SEd Tanous if (iface == "xyz.openbmc_project.Software.Activation") 160fd9ab9e1SJames Feist { 1610fb5b505SGayathri Leburu const std::string* state = nullptr; 162b9d36b47SEd Tanous for (const auto& property : values) 16332898ceaSJames Feist { 164b9d36b47SEd Tanous if (property.first == "Activation") 165b9d36b47SEd Tanous { 1660fb5b505SGayathri Leburu state = std::get_if<std::string>( 167b9d36b47SEd Tanous &property.second); 1680fb5b505SGayathri Leburu if (state == nullptr) 169b9d36b47SEd Tanous { 170002d39b4SEd Tanous taskData->messages.emplace_back( 171002d39b4SEd Tanous messages::internalError()); 172b9d36b47SEd Tanous return task::completed; 173b9d36b47SEd Tanous } 174b9d36b47SEd Tanous } 175b9d36b47SEd Tanous } 17632898ceaSJames Feist 17732898ceaSJames Feist if (state == nullptr) 17832898ceaSJames Feist { 179b9d36b47SEd Tanous return !task::completed; 18032898ceaSJames Feist } 18132898ceaSJames Feist 18211ba3979SEd Tanous if (state->ends_with("Invalid") || 18311ba3979SEd Tanous state->ends_with("Failed")) 18432898ceaSJames Feist { 18532898ceaSJames Feist taskData->state = "Exception"; 18632898ceaSJames Feist taskData->status = "Warning"; 18732898ceaSJames Feist taskData->messages.emplace_back( 188e5d5006bSJames Feist messages::taskAborted(index)); 18932898ceaSJames Feist return task::completed; 19032898ceaSJames Feist } 19132898ceaSJames Feist 19211ba3979SEd Tanous if (state->ends_with("Staged")) 19332898ceaSJames Feist { 194fd9ab9e1SJames Feist taskData->state = "Stopping"; 195fd9ab9e1SJames Feist taskData->messages.emplace_back( 196fd9ab9e1SJames Feist messages::taskPaused(index)); 197fd9ab9e1SJames Feist 198fd9ab9e1SJames Feist // its staged, set a long timer to 199fd9ab9e1SJames Feist // allow them time to complete the 200fd9ab9e1SJames Feist // update (probably cycle the 201fd9ab9e1SJames Feist // system) if this expires then 2028ece0e45SEd Tanous // task will be canceled 203002d39b4SEd Tanous taskData->extendTimer(std::chrono::hours(5)); 20432898ceaSJames Feist return !task::completed; 20532898ceaSJames Feist } 20632898ceaSJames Feist 20711ba3979SEd Tanous if (state->ends_with("Active")) 20832898ceaSJames Feist { 20932898ceaSJames Feist taskData->messages.emplace_back( 210002d39b4SEd Tanous messages::taskCompletedOK(index)); 21132898ceaSJames Feist taskData->state = "Completed"; 21232898ceaSJames Feist return task::completed; 21332898ceaSJames Feist } 214fd9ab9e1SJames Feist } 2150fda0f12SGeorge Liu else if ( 2160fda0f12SGeorge Liu iface == 2170fda0f12SGeorge Liu "xyz.openbmc_project.Software.ActivationProgress") 218fd9ab9e1SJames Feist { 219b9d36b47SEd Tanous const uint8_t* progress = nullptr; 220b9d36b47SEd Tanous for (const auto& property : values) 221fd9ab9e1SJames Feist { 222b9d36b47SEd Tanous if (property.first == "Progress") 223b9d36b47SEd Tanous { 2240fb5b505SGayathri Leburu progress = 2250fb5b505SGayathri Leburu std::get_if<uint8_t>(&property.second); 2260fb5b505SGayathri Leburu if (progress == nullptr) 227b9d36b47SEd Tanous { 228002d39b4SEd Tanous taskData->messages.emplace_back( 229002d39b4SEd Tanous messages::internalError()); 230b9d36b47SEd Tanous return task::completed; 231fd9ab9e1SJames Feist } 232b9d36b47SEd Tanous } 233b9d36b47SEd Tanous } 234fd9ab9e1SJames Feist 235fd9ab9e1SJames Feist if (progress == nullptr) 236fd9ab9e1SJames Feist { 237b9d36b47SEd Tanous return !task::completed; 238fd9ab9e1SJames Feist } 2390fb5b505SGayathri Leburu taskData->percentComplete = *progress; 240fd9ab9e1SJames Feist taskData->messages.emplace_back( 2410fb5b505SGayathri Leburu messages::taskProgressChanged(index, 2420fb5b505SGayathri Leburu *progress)); 243fd9ab9e1SJames Feist 244fd9ab9e1SJames Feist // if we're getting status updates it's 245fd9ab9e1SJames Feist // still alive, update timer 246002d39b4SEd Tanous taskData->extendTimer(std::chrono::minutes(5)); 247fd9ab9e1SJames Feist } 24832898ceaSJames Feist 24932898ceaSJames Feist // as firmware update often results in a 25032898ceaSJames Feist // reboot, the task may never "complete" 25132898ceaSJames Feist // unless it is an error 25232898ceaSJames Feist 25332898ceaSJames Feist return !task::completed; 25432898ceaSJames Feist }, 2550fda0f12SGeorge Liu "type='signal',interface='org.freedesktop.DBus.Properties'," 256fd9ab9e1SJames Feist "member='PropertiesChanged',path='" + 25732898ceaSJames Feist objPath.str + "'"); 25832898ceaSJames Feist task->startTimer(std::chrono::minutes(5)); 25932898ceaSJames Feist task->populateResp(asyncResp->res); 260a3e65892SEd Tanous task->payload.emplace(std::move(payload)); 2610554c984SAndrew Geissler } 26286adcd6dSAndrew Geissler fwUpdateInProgress = false; 2632b73119cSGeorge Liu }); 26462bafc01SPatrick Williams 26562bafc01SPatrick Williams break; 26686adcd6dSAndrew Geissler } 26786adcd6dSAndrew Geissler } 26886adcd6dSAndrew Geissler } 26986adcd6dSAndrew Geissler 2708549b951SMyung Bae inline void afterAvailbleTimerAsyncWait( 2718549b951SMyung Bae const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2728549b951SMyung Bae const boost::system::error_code& ec) 2738549b951SMyung Bae { 2748549b951SMyung Bae cleanUp(); 2758549b951SMyung Bae if (ec == boost::asio::error::operation_aborted) 2768549b951SMyung Bae { 2778549b951SMyung Bae // expected, we were canceled before the timer completed. 2788549b951SMyung Bae return; 2798549b951SMyung Bae } 2808549b951SMyung Bae BMCWEB_LOG_ERROR("Timed out waiting for firmware object being created"); 2818549b951SMyung Bae BMCWEB_LOG_ERROR("FW image may has already been uploaded to server"); 2828549b951SMyung Bae if (ec) 2838549b951SMyung Bae { 2848549b951SMyung Bae BMCWEB_LOG_ERROR("Async_wait failed{}", ec); 2858549b951SMyung Bae return; 2868549b951SMyung Bae } 2878549b951SMyung Bae if (asyncResp) 2888549b951SMyung Bae { 2898549b951SMyung Bae redfish::messages::internalError(asyncResp->res); 2908549b951SMyung Bae } 2918549b951SMyung Bae } 2928549b951SMyung Bae 2938549b951SMyung Bae inline void 2948549b951SMyung Bae handleUpdateErrorType(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2958549b951SMyung Bae const std::string& url, const std::string& type) 2968549b951SMyung Bae { 2978549b951SMyung Bae if (type == "xyz.openbmc_project.Software.Image.Error.UnTarFailure") 2988549b951SMyung Bae { 2998549b951SMyung Bae redfish::messages::invalidUpload(asyncResp->res, url, 3008549b951SMyung Bae "Invalid archive"); 3018549b951SMyung Bae } 3028549b951SMyung Bae else if (type == 3038549b951SMyung Bae "xyz.openbmc_project.Software.Image.Error.ManifestFileFailure") 3048549b951SMyung Bae { 3058549b951SMyung Bae redfish::messages::invalidUpload(asyncResp->res, url, 3068549b951SMyung Bae "Invalid manifest"); 3078549b951SMyung Bae } 3088549b951SMyung Bae else if (type == "xyz.openbmc_project.Software.Image.Error.ImageFailure") 3098549b951SMyung Bae { 3108549b951SMyung Bae redfish::messages::invalidUpload(asyncResp->res, url, 3118549b951SMyung Bae "Invalid image format"); 3128549b951SMyung Bae } 3138549b951SMyung Bae else if (type == "xyz.openbmc_project.Software.Version.Error.AlreadyExists") 3148549b951SMyung Bae { 3158549b951SMyung Bae redfish::messages::invalidUpload(asyncResp->res, url, 3168549b951SMyung Bae "Image version already exists"); 3178549b951SMyung Bae 3188549b951SMyung Bae redfish::messages::resourceAlreadyExists( 3198549b951SMyung Bae asyncResp->res, "UpdateService", "Version", "uploaded version"); 3208549b951SMyung Bae } 3218549b951SMyung Bae else if (type == "xyz.openbmc_project.Software.Image.Error.BusyFailure") 3228549b951SMyung Bae { 3238549b951SMyung Bae redfish::messages::resourceExhaustion(asyncResp->res, url); 3248549b951SMyung Bae } 3254034a652SMyung Bae else if (type == "xyz.openbmc_project.Software.Version.Error.Incompatible") 3268549b951SMyung Bae { 3274034a652SMyung Bae redfish::messages::invalidUpload(asyncResp->res, url, 3284034a652SMyung Bae "Incompatible image version"); 3294034a652SMyung Bae } 3304034a652SMyung Bae else if (type == 3314034a652SMyung Bae "xyz.openbmc_project.Software.Version.Error.ExpiredAccessKey") 3324034a652SMyung Bae { 3334034a652SMyung Bae redfish::messages::invalidUpload(asyncResp->res, url, 3344034a652SMyung Bae "Update Access Key Expired"); 3354034a652SMyung Bae } 3364034a652SMyung Bae else if (type == 3374034a652SMyung Bae "xyz.openbmc_project.Software.Version.Error.InvalidSignature") 3384034a652SMyung Bae { 3394034a652SMyung Bae redfish::messages::invalidUpload(asyncResp->res, url, 3404034a652SMyung Bae "Invalid image signature"); 3414034a652SMyung Bae } 3424034a652SMyung Bae else if (type == 3434034a652SMyung Bae "xyz.openbmc_project.Software.Image.Error.InternalFailure" || 3444034a652SMyung Bae type == "xyz.openbmc_project.Software.Version.Error.HostFile") 3454034a652SMyung Bae { 3464034a652SMyung Bae BMCWEB_LOG_ERROR("Software Image Error type={}", type); 3478549b951SMyung Bae redfish::messages::internalError(asyncResp->res); 3488549b951SMyung Bae } 3494034a652SMyung Bae else 3504034a652SMyung Bae { 3514034a652SMyung Bae // Unrelated error types. Ignored 3524034a652SMyung Bae BMCWEB_LOG_INFO("Non-Software-related Error type={}. Ignored", type); 3534034a652SMyung Bae return; 3544034a652SMyung Bae } 3554034a652SMyung Bae // Clear the timer 3564034a652SMyung Bae fwAvailableTimer = nullptr; 3578549b951SMyung Bae } 3588549b951SMyung Bae 3598549b951SMyung Bae inline void 3608549b951SMyung Bae afterUpdateErrorMatcher(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3618549b951SMyung Bae const std::string& url, sdbusplus::message_t& m) 3628549b951SMyung Bae { 36380f79a40SMichael Shen dbus::utility::DBusInterfacesMap interfacesProperties; 3648549b951SMyung Bae sdbusplus::message::object_path objPath; 3658549b951SMyung Bae m.read(objPath, interfacesProperties); 3668549b951SMyung Bae BMCWEB_LOG_DEBUG("obj path = {}", objPath.str); 3678549b951SMyung Bae for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>& 3688549b951SMyung Bae interface : interfacesProperties) 3698549b951SMyung Bae { 3708549b951SMyung Bae if (interface.first == "xyz.openbmc_project.Logging.Entry") 3718549b951SMyung Bae { 3728549b951SMyung Bae for (const std::pair<std::string, dbus::utility::DbusVariantType>& 3738549b951SMyung Bae value : interface.second) 3748549b951SMyung Bae { 3758549b951SMyung Bae if (value.first != "Message") 3768549b951SMyung Bae { 3778549b951SMyung Bae continue; 3788549b951SMyung Bae } 3798549b951SMyung Bae const std::string* type = 3808549b951SMyung Bae std::get_if<std::string>(&value.second); 3818549b951SMyung Bae if (type == nullptr) 3828549b951SMyung Bae { 3838549b951SMyung Bae // if this was our message, timeout will cover it 3848549b951SMyung Bae return; 3858549b951SMyung Bae } 3868549b951SMyung Bae handleUpdateErrorType(asyncResp, url, *type); 3878549b951SMyung Bae } 3888549b951SMyung Bae } 3898549b951SMyung Bae } 3908549b951SMyung Bae } 3918549b951SMyung Bae 3920554c984SAndrew Geissler // Note that asyncResp can be either a valid pointer or nullptr. If nullptr 3930554c984SAndrew Geissler // then no asyncResp updates will occur 394f5139334SEd Tanous inline void monitorForSoftwareAvailable( 3958d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3968d1b46d7Szhanghch05 const crow::Request& req, const std::string& url, 3975d138943SGunnar Mills int timeoutTimeSeconds = 25) 39886adcd6dSAndrew Geissler { 39986adcd6dSAndrew Geissler // Only allow one FW update at a time 400e05aec50SEd Tanous if (fwUpdateInProgress) 40186adcd6dSAndrew Geissler { 4020554c984SAndrew Geissler if (asyncResp) 4030554c984SAndrew Geissler { 40486adcd6dSAndrew Geissler messages::serviceTemporarilyUnavailable(asyncResp->res, "30"); 4050554c984SAndrew Geissler } 40686adcd6dSAndrew Geissler return; 40786adcd6dSAndrew Geissler } 40886adcd6dSAndrew Geissler 4098e8245dbSEd Tanous if (req.ioService == nullptr) 4108e8245dbSEd Tanous { 4118e8245dbSEd Tanous messages::internalError(asyncResp->res); 4128e8245dbSEd Tanous return; 4138e8245dbSEd Tanous } 4148e8245dbSEd Tanous 4150554c984SAndrew Geissler fwAvailableTimer = 416271584abSEd Tanous std::make_unique<boost::asio::steady_timer>(*req.ioService); 41786adcd6dSAndrew Geissler 418271584abSEd Tanous fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds)); 41986adcd6dSAndrew Geissler 42086adcd6dSAndrew Geissler fwAvailableTimer->async_wait( 4218549b951SMyung Bae std::bind_front(afterAvailbleTimerAsyncWait, asyncResp)); 4228549b951SMyung Bae 423a3e65892SEd Tanous task::Payload payload(req); 42459d494eeSPatrick Williams auto callback = [asyncResp, payload](sdbusplus::message_t& m) mutable { 42562598e31SEd Tanous BMCWEB_LOG_DEBUG("Match fired"); 426a3e65892SEd Tanous softwareInterfaceAdded(asyncResp, m, std::move(payload)); 42786adcd6dSAndrew Geissler }; 42886adcd6dSAndrew Geissler 42986adcd6dSAndrew Geissler fwUpdateInProgress = true; 43086adcd6dSAndrew Geissler 43159d494eeSPatrick Williams fwUpdateMatcher = std::make_unique<sdbusplus::bus::match_t>( 43286adcd6dSAndrew Geissler *crow::connections::systemBus, 43386adcd6dSAndrew Geissler "interface='org.freedesktop.DBus.ObjectManager',type='signal'," 43486adcd6dSAndrew Geissler "member='InterfacesAdded',path='/xyz/openbmc_project/software'", 43586adcd6dSAndrew Geissler callback); 4364cde5d90SJames Feist 43759d494eeSPatrick Williams fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match_t>( 4384cde5d90SJames Feist *crow::connections::systemBus, 439e1cc4828SBrian Ma "interface='org.freedesktop.DBus.ObjectManager',type='signal'," 440e1cc4828SBrian Ma "member='InterfacesAdded'," 441e1cc4828SBrian Ma "path='/xyz/openbmc_project/logging'", 4428549b951SMyung Bae std::bind_front(afterUpdateErrorMatcher, asyncResp, url)); 44386adcd6dSAndrew Geissler } 444729dae72SJennifer Lee 445*757178a5SEd Tanous inline std::optional<boost::urls::url> 446*757178a5SEd Tanous parseSimpleUpdateUrl(std::string imageURI, 447f86bcc87SEd Tanous std::optional<std::string> transferProtocol, 448f86bcc87SEd Tanous crow::Response& res) 449f86bcc87SEd Tanous { 450f86bcc87SEd Tanous if (imageURI.find("://") == std::string::npos) 451f86bcc87SEd Tanous { 452f86bcc87SEd Tanous if (imageURI.starts_with("/")) 453f86bcc87SEd Tanous { 454f86bcc87SEd Tanous messages::actionParameterValueTypeError( 455f86bcc87SEd Tanous res, imageURI, "ImageURI", "UpdateService.SimpleUpdate"); 456f86bcc87SEd Tanous return std::nullopt; 457f86bcc87SEd Tanous } 458f86bcc87SEd Tanous if (!transferProtocol) 459f86bcc87SEd Tanous { 460f86bcc87SEd Tanous messages::actionParameterValueTypeError( 461f86bcc87SEd Tanous res, imageURI, "ImageURI", "UpdateService.SimpleUpdate"); 462f86bcc87SEd Tanous return std::nullopt; 463f86bcc87SEd Tanous } 464f86bcc87SEd Tanous // OpenBMC currently only supports TFTP 465*757178a5SEd Tanous if (*transferProtocol == "TFTP") 466*757178a5SEd Tanous { 467*757178a5SEd Tanous imageURI = "tftp://" + imageURI; 468*757178a5SEd Tanous } 469*757178a5SEd Tanous else 470f86bcc87SEd Tanous { 471f86bcc87SEd Tanous messages::actionParameterNotSupported(res, "TransferProtocol", 472f86bcc87SEd Tanous *transferProtocol); 473f86bcc87SEd Tanous BMCWEB_LOG_ERROR("Request incorrect protocol parameter: {}", 474f86bcc87SEd Tanous *transferProtocol); 475f86bcc87SEd Tanous return std::nullopt; 476f86bcc87SEd Tanous } 477f86bcc87SEd Tanous } 478f86bcc87SEd Tanous 479f86bcc87SEd Tanous boost::system::result<boost::urls::url> url = 480f86bcc87SEd Tanous boost::urls::parse_absolute_uri(imageURI); 481f86bcc87SEd Tanous if (!url) 482f86bcc87SEd Tanous { 483f86bcc87SEd Tanous messages::actionParameterValueTypeError(res, imageURI, "ImageURI", 484f86bcc87SEd Tanous "UpdateService.SimpleUpdate"); 485f86bcc87SEd Tanous 486f86bcc87SEd Tanous return std::nullopt; 487f86bcc87SEd Tanous } 488f86bcc87SEd Tanous url->normalize(); 489f86bcc87SEd Tanous 490*757178a5SEd Tanous if (url->scheme() == "tftp") 491*757178a5SEd Tanous { 492*757178a5SEd Tanous if (url->encoded_path().size() < 2) 493*757178a5SEd Tanous { 494*757178a5SEd Tanous messages::actionParameterNotSupported(res, "ImageURI", 495*757178a5SEd Tanous url->buffer()); 496*757178a5SEd Tanous return std::nullopt; 497*757178a5SEd Tanous } 498*757178a5SEd Tanous } 499*757178a5SEd Tanous else 500f86bcc87SEd Tanous { 501f86bcc87SEd Tanous messages::actionParameterNotSupported(res, "ImageURI", imageURI); 502f86bcc87SEd Tanous return std::nullopt; 503f86bcc87SEd Tanous } 504*757178a5SEd Tanous 505*757178a5SEd Tanous if (url->encoded_path().empty()) 506f86bcc87SEd Tanous { 507*757178a5SEd Tanous messages::actionParameterValueTypeError(res, imageURI, "ImageURI", 508*757178a5SEd Tanous "UpdateService.SimpleUpdate"); 509f86bcc87SEd Tanous return std::nullopt; 510f86bcc87SEd Tanous } 511*757178a5SEd Tanous 512*757178a5SEd Tanous return *url; 513f86bcc87SEd Tanous } 514f86bcc87SEd Tanous 5156b0f66bdSEd Tanous inline void doTftpUpdate(const crow::Request& req, 5166b0f66bdSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 517*757178a5SEd Tanous const boost::urls::url_view_base& url) 5186b0f66bdSEd Tanous { 5196b0f66bdSEd Tanous #ifndef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE 5206b0f66bdSEd Tanous messages::actionParameterNotSupported(asyncResp->res, "ImageURI", 521*757178a5SEd Tanous url.buffer()); 5226b0f66bdSEd Tanous return; 5236b0f66bdSEd Tanous #endif 524*757178a5SEd Tanous 525*757178a5SEd Tanous std::string path(url.encoded_path()); 526*757178a5SEd Tanous if (path.size() < 2) 527*757178a5SEd Tanous { 528*757178a5SEd Tanous messages::actionParameterNotSupported(asyncResp->res, "ImageURI", 529*757178a5SEd Tanous url.buffer()); 530*757178a5SEd Tanous return; 531*757178a5SEd Tanous } 532*757178a5SEd Tanous // TFTP expects a path without a / 533*757178a5SEd Tanous path.erase(0, 1); 534*757178a5SEd Tanous std::string host(url.encoded_host_and_port()); 535*757178a5SEd Tanous BMCWEB_LOG_DEBUG("Server: {} File: {}", host, path); 5366b0f66bdSEd Tanous 5376b0f66bdSEd Tanous // Setup callback for when new software detected 5386b0f66bdSEd Tanous // Give TFTP 10 minutes to complete 5396b0f66bdSEd Tanous monitorForSoftwareAvailable( 5406b0f66bdSEd Tanous asyncResp, req, 5416b0f66bdSEd Tanous "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate", 600); 5426b0f66bdSEd Tanous 5436b0f66bdSEd Tanous // TFTP can take up to 10 minutes depending on image size and 5446b0f66bdSEd Tanous // connection speed. Return to caller as soon as the TFTP operation 5456b0f66bdSEd Tanous // has been started. The callback above will ensure the activate 5466b0f66bdSEd Tanous // is started once the download has completed 5476b0f66bdSEd Tanous redfish::messages::success(asyncResp->res); 5486b0f66bdSEd Tanous 5496b0f66bdSEd Tanous // Call TFTP service 5506b0f66bdSEd Tanous crow::connections::systemBus->async_method_call( 5516b0f66bdSEd Tanous [](const boost::system::error_code& ec) { 5526b0f66bdSEd Tanous if (ec) 5536b0f66bdSEd Tanous { 5546b0f66bdSEd Tanous // messages::internalError(asyncResp->res); 5556b0f66bdSEd Tanous cleanUp(); 5566b0f66bdSEd Tanous BMCWEB_LOG_DEBUG("error_code = {}", ec); 5576b0f66bdSEd Tanous BMCWEB_LOG_DEBUG("error msg = {}", ec.message()); 5586b0f66bdSEd Tanous } 5596b0f66bdSEd Tanous else 5606b0f66bdSEd Tanous { 5616b0f66bdSEd Tanous BMCWEB_LOG_DEBUG("Call to DownloaViaTFTP Success"); 5626b0f66bdSEd Tanous } 5636b0f66bdSEd Tanous }, 5646b0f66bdSEd Tanous "xyz.openbmc_project.Software.Download", 5656b0f66bdSEd Tanous "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP", 566*757178a5SEd Tanous "DownloadViaTFTP", path, host); 5676b0f66bdSEd Tanous } 5686b0f66bdSEd Tanous 569f5139334SEd Tanous inline void handleUpdateServiceSimpleUpdateAction( 570f5139334SEd Tanous crow::App& app, const crow::Request& req, 571f5139334SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 5720554c984SAndrew Geissler { 5733ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 57445ca1b86SEd Tanous { 57545ca1b86SEd Tanous return; 57645ca1b86SEd Tanous } 57745ca1b86SEd Tanous 5780554c984SAndrew Geissler std::optional<std::string> transferProtocol; 5790554c984SAndrew Geissler std::string imageURI; 5800554c984SAndrew Geissler 58162598e31SEd Tanous BMCWEB_LOG_DEBUG("Enter UpdateService.SimpleUpdate doPost"); 5820554c984SAndrew Geissler 5830554c984SAndrew Geissler // User can pass in both TransferProtocol and ImageURI parameters or 5844e0453b1SGunnar Mills // they can pass in just the ImageURI with the transfer protocol 5854e0453b1SGunnar Mills // embedded within it. 5860554c984SAndrew Geissler // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin 5870554c984SAndrew Geissler // 2) ImageURI:tftp://1.1.1.1/myfile.bin 5880554c984SAndrew Geissler 589002d39b4SEd Tanous if (!json_util::readJsonAction(req, asyncResp->res, "TransferProtocol", 590002d39b4SEd Tanous transferProtocol, "ImageURI", imageURI)) 5910554c984SAndrew Geissler { 59262598e31SEd Tanous BMCWEB_LOG_DEBUG("Missing TransferProtocol or ImageURI parameter"); 5930554c984SAndrew Geissler return; 5940554c984SAndrew Geissler } 595f5139334SEd Tanous 596*757178a5SEd Tanous std::optional<boost::urls::url> url = 597*757178a5SEd Tanous parseSimpleUpdateUrl(imageURI, transferProtocol, asyncResp->res); 598*757178a5SEd Tanous if (!url) 5990554c984SAndrew Geissler { 6000554c984SAndrew Geissler return; 6010554c984SAndrew Geissler } 602*757178a5SEd Tanous if (url->scheme() == "tftp") 603*757178a5SEd Tanous { 604*757178a5SEd Tanous doTftpUpdate(req, asyncResp, *url); 605*757178a5SEd Tanous } 606*757178a5SEd Tanous else 607*757178a5SEd Tanous { 608*757178a5SEd Tanous messages::actionParameterNotSupported(asyncResp->res, "ImageURI", 609*757178a5SEd Tanous url->buffer()); 610*757178a5SEd Tanous return; 611*757178a5SEd Tanous } 6120554c984SAndrew Geissler 61362598e31SEd Tanous BMCWEB_LOG_DEBUG("Exit UpdateService.SimpleUpdate doPost"); 614729dae72SJennifer Lee } 615729dae72SJennifer Lee 6160ed80c8cSGeorge Liu inline void uploadImageFile(crow::Response& res, std::string_view body) 6170ed80c8cSGeorge Liu { 6182c6ffdb0SEd Tanous std::filesystem::path filepath("/tmp/images/" + bmcweb::getRandomUUID()); 6192c6ffdb0SEd Tanous 62062598e31SEd Tanous BMCWEB_LOG_DEBUG("Writing file to {}", filepath.string()); 6210ed80c8cSGeorge Liu std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary | 6220ed80c8cSGeorge Liu std::ofstream::trunc); 6230ed80c8cSGeorge Liu // set the permission of the file to 640 62489492a15SPatrick Williams std::filesystem::perms permission = std::filesystem::perms::owner_read | 62589492a15SPatrick Williams std::filesystem::perms::group_read; 6260ed80c8cSGeorge Liu std::filesystem::permissions(filepath, permission); 6270ed80c8cSGeorge Liu out << body; 6280ed80c8cSGeorge Liu 6290ed80c8cSGeorge Liu if (out.bad()) 6300ed80c8cSGeorge Liu { 6310ed80c8cSGeorge Liu messages::internalError(res); 6320ed80c8cSGeorge Liu cleanUp(); 6330ed80c8cSGeorge Liu } 6340ed80c8cSGeorge Liu } 6350ed80c8cSGeorge Liu 6360ed80c8cSGeorge Liu inline void setApplyTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 6370ed80c8cSGeorge Liu const std::string& applyTime) 6380ed80c8cSGeorge Liu { 6390ed80c8cSGeorge Liu std::string applyTimeNewVal; 6400ed80c8cSGeorge Liu if (applyTime == "Immediate") 6410ed80c8cSGeorge Liu { 6420ed80c8cSGeorge Liu applyTimeNewVal = 6430ed80c8cSGeorge Liu "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate"; 6440ed80c8cSGeorge Liu } 6450ed80c8cSGeorge Liu else if (applyTime == "OnReset") 6460ed80c8cSGeorge Liu { 6470ed80c8cSGeorge Liu applyTimeNewVal = 6480ed80c8cSGeorge Liu "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset"; 6490ed80c8cSGeorge Liu } 6500ed80c8cSGeorge Liu else 6510ed80c8cSGeorge Liu { 65262598e31SEd Tanous BMCWEB_LOG_INFO( 65362598e31SEd Tanous "ApplyTime value is not in the list of acceptable values"); 6540ed80c8cSGeorge Liu messages::propertyValueNotInList(asyncResp->res, applyTime, 6550ed80c8cSGeorge Liu "ApplyTime"); 6560ed80c8cSGeorge Liu return; 6570ed80c8cSGeorge Liu } 6580ed80c8cSGeorge Liu 659d02aad39SEd Tanous setDbusProperty(asyncResp, "xyz.openbmc_project.Settings", 660d02aad39SEd Tanous sdbusplus::message::object_path( 661d02aad39SEd Tanous "/xyz/openbmc_project/software/apply_time"), 662d02aad39SEd Tanous "xyz.openbmc_project.Software.ApplyTime", 663d02aad39SEd Tanous "RequestedApplyTime", "ApplyTime", applyTimeNewVal); 6640ed80c8cSGeorge Liu } 6650ed80c8cSGeorge Liu 6660ed80c8cSGeorge Liu inline void 6670ed80c8cSGeorge Liu updateMultipartContext(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 6680ed80c8cSGeorge Liu const MultipartParser& parser) 6690ed80c8cSGeorge Liu { 6700ed80c8cSGeorge Liu const std::string* uploadData = nullptr; 6710ed80c8cSGeorge Liu std::optional<std::string> applyTime = "OnReset"; 6720ed80c8cSGeorge Liu bool targetFound = false; 6730ed80c8cSGeorge Liu for (const FormPart& formpart : parser.mime_fields) 6740ed80c8cSGeorge Liu { 6750ed80c8cSGeorge Liu boost::beast::http::fields::const_iterator it = 6760ed80c8cSGeorge Liu formpart.fields.find("Content-Disposition"); 6770ed80c8cSGeorge Liu if (it == formpart.fields.end()) 6780ed80c8cSGeorge Liu { 67962598e31SEd Tanous BMCWEB_LOG_ERROR("Couldn't find Content-Disposition"); 6800ed80c8cSGeorge Liu return; 6810ed80c8cSGeorge Liu } 68262598e31SEd Tanous BMCWEB_LOG_INFO("Parsing value {}", it->value()); 6830ed80c8cSGeorge Liu 6840ed80c8cSGeorge Liu // The construction parameters of param_list must start with `;` 6850ed80c8cSGeorge Liu size_t index = it->value().find(';'); 6860ed80c8cSGeorge Liu if (index == std::string::npos) 6870ed80c8cSGeorge Liu { 6880ed80c8cSGeorge Liu continue; 6890ed80c8cSGeorge Liu } 6900ed80c8cSGeorge Liu 69189492a15SPatrick Williams for (const auto& param : 6920ed80c8cSGeorge Liu boost::beast::http::param_list{it->value().substr(index)}) 6930ed80c8cSGeorge Liu { 6940ed80c8cSGeorge Liu if (param.first != "name" || param.second.empty()) 6950ed80c8cSGeorge Liu { 6960ed80c8cSGeorge Liu continue; 6970ed80c8cSGeorge Liu } 6980ed80c8cSGeorge Liu 6990ed80c8cSGeorge Liu if (param.second == "UpdateParameters") 7000ed80c8cSGeorge Liu { 7010ed80c8cSGeorge Liu std::vector<std::string> targets; 7020ed80c8cSGeorge Liu nlohmann::json content = 7030ed80c8cSGeorge Liu nlohmann::json::parse(formpart.content); 7047cb59f65SEd Tanous nlohmann::json::object_t* obj = 7057cb59f65SEd Tanous content.get_ptr<nlohmann::json::object_t*>(); 7067cb59f65SEd Tanous if (obj == nullptr) 7077cb59f65SEd Tanous { 7087cb59f65SEd Tanous messages::propertyValueFormatError(asyncResp->res, targets, 7097cb59f65SEd Tanous "UpdateParameters"); 7107cb59f65SEd Tanous return; 7117cb59f65SEd Tanous } 7127cb59f65SEd Tanous 7137cb59f65SEd Tanous if (!json_util::readJsonObject( 7147cb59f65SEd Tanous *obj, asyncResp->res, "Targets", targets, 7157cb59f65SEd Tanous "@Redfish.OperationApplyTime", applyTime)) 7160ed80c8cSGeorge Liu { 7170ed80c8cSGeorge Liu return; 7180ed80c8cSGeorge Liu } 7190ed80c8cSGeorge Liu if (targets.size() != 1) 7200ed80c8cSGeorge Liu { 7217cb59f65SEd Tanous messages::propertyValueFormatError(asyncResp->res, targets, 7227cb59f65SEd Tanous "Targets"); 7230ed80c8cSGeorge Liu return; 7240ed80c8cSGeorge Liu } 7250ed80c8cSGeorge Liu if (targets[0] != "/redfish/v1/Managers/bmc") 7260ed80c8cSGeorge Liu { 7277cb59f65SEd Tanous messages::propertyValueNotInList(asyncResp->res, targets[0], 7287cb59f65SEd Tanous "Targets/0"); 7290ed80c8cSGeorge Liu return; 7300ed80c8cSGeorge Liu } 7310ed80c8cSGeorge Liu targetFound = true; 7320ed80c8cSGeorge Liu } 7330ed80c8cSGeorge Liu else if (param.second == "UpdateFile") 7340ed80c8cSGeorge Liu { 7350ed80c8cSGeorge Liu uploadData = &(formpart.content); 7360ed80c8cSGeorge Liu } 7370ed80c8cSGeorge Liu } 7380ed80c8cSGeorge Liu } 7390ed80c8cSGeorge Liu 7400ed80c8cSGeorge Liu if (uploadData == nullptr) 7410ed80c8cSGeorge Liu { 74262598e31SEd Tanous BMCWEB_LOG_ERROR("Upload data is NULL"); 7430ed80c8cSGeorge Liu messages::propertyMissing(asyncResp->res, "UpdateFile"); 7440ed80c8cSGeorge Liu return; 7450ed80c8cSGeorge Liu } 7460ed80c8cSGeorge Liu if (!targetFound) 7470ed80c8cSGeorge Liu { 7480ed80c8cSGeorge Liu messages::propertyMissing(asyncResp->res, "targets"); 7490ed80c8cSGeorge Liu return; 7500ed80c8cSGeorge Liu } 7510ed80c8cSGeorge Liu 7520ed80c8cSGeorge Liu setApplyTime(asyncResp, *applyTime); 7530ed80c8cSGeorge Liu 7540ed80c8cSGeorge Liu uploadImageFile(asyncResp->res, *uploadData); 7550ed80c8cSGeorge Liu } 7560ed80c8cSGeorge Liu 757c2051d11SEd Tanous inline void 758c2051d11SEd Tanous handleUpdateServicePost(App& app, const crow::Request& req, 759c2051d11SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 760c2051d11SEd Tanous { 7613ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 762c2051d11SEd Tanous { 763c2051d11SEd Tanous return; 764c2051d11SEd Tanous } 765b33a4327SNinad Palsule std::string_view contentType = req.getHeaderValue("Content-Type"); 766b33a4327SNinad Palsule 76762598e31SEd Tanous BMCWEB_LOG_DEBUG("doPost: contentType={}", contentType); 768b33a4327SNinad Palsule 769b33a4327SNinad Palsule // Make sure that content type is application/octet-stream or 770b33a4327SNinad Palsule // multipart/form-data 77118f8f608SEd Tanous if (bmcweb::asciiIEquals(contentType, "application/octet-stream")) 772b33a4327SNinad Palsule { 773b33a4327SNinad Palsule // Setup callback for when new software detected 774b33a4327SNinad Palsule monitorForSoftwareAvailable(asyncResp, req, 775b33a4327SNinad Palsule "/redfish/v1/UpdateService"); 776b33a4327SNinad Palsule 777b33a4327SNinad Palsule uploadImageFile(asyncResp->res, req.body()); 778b33a4327SNinad Palsule } 779b33a4327SNinad Palsule else if (contentType.starts_with("multipart/form-data")) 780b33a4327SNinad Palsule { 781b33a4327SNinad Palsule MultipartParser parser; 782c2051d11SEd Tanous 783c2051d11SEd Tanous // Setup callback for when new software detected 784b33a4327SNinad Palsule monitorForSoftwareAvailable(asyncResp, req, 785b33a4327SNinad Palsule "/redfish/v1/UpdateService"); 786c2051d11SEd Tanous 7870ed80c8cSGeorge Liu ParserError ec = parser.parse(req); 7880ed80c8cSGeorge Liu if (ec != ParserError::PARSER_SUCCESS) 7890ed80c8cSGeorge Liu { 7900ed80c8cSGeorge Liu // handle error 79162598e31SEd Tanous BMCWEB_LOG_ERROR("MIME parse failed, ec : {}", 79262598e31SEd Tanous static_cast<int>(ec)); 7930ed80c8cSGeorge Liu messages::internalError(asyncResp->res); 7940ed80c8cSGeorge Liu return; 7950ed80c8cSGeorge Liu } 7960ed80c8cSGeorge Liu updateMultipartContext(asyncResp, parser); 797c2051d11SEd Tanous } 798b33a4327SNinad Palsule else 799b33a4327SNinad Palsule { 80062598e31SEd Tanous BMCWEB_LOG_DEBUG("Bad content type specified:{}", contentType); 801b33a4327SNinad Palsule asyncResp->res.result(boost::beast::http::status::bad_request); 802b33a4327SNinad Palsule } 803b33a4327SNinad Palsule } 804c2051d11SEd Tanous 805f5139334SEd Tanous inline void 806f5139334SEd Tanous handleUpdateServiceGet(App& app, const crow::Request& req, 807f5139334SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 8081abe55efSEd Tanous { 8093ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 81045ca1b86SEd Tanous { 81145ca1b86SEd Tanous return; 81245ca1b86SEd Tanous } 8138d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 8140ed80c8cSGeorge Liu "#UpdateService.v1_11_1.UpdateService"; 8158d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService"; 8168d1b46d7Szhanghch05 asyncResp->res.jsonValue["Id"] = "UpdateService"; 817002d39b4SEd Tanous asyncResp->res.jsonValue["Description"] = "Service for Software Update"; 8188d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "Update Service"; 8194dc23f3fSEd Tanous 8207e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["HttpPushUri"] = 8214dc23f3fSEd Tanous "/redfish/v1/UpdateService/update"; 8220ed80c8cSGeorge Liu asyncResp->res.jsonValue["MultipartHttpPushUri"] = 8230ed80c8cSGeorge Liu "/redfish/v1/UpdateService/update"; 8244dc23f3fSEd Tanous 8250f74e643SEd Tanous // UpdateService cannot be disabled 8268d1b46d7Szhanghch05 asyncResp->res.jsonValue["ServiceEnabled"] = true; 8271476687dSEd Tanous asyncResp->res.jsonValue["FirmwareInventory"]["@odata.id"] = 8281476687dSEd Tanous "/redfish/v1/UpdateService/FirmwareInventory"; 829d61e5194STejas Patil // Get the MaxImageSizeBytes 830f5139334SEd Tanous asyncResp->res.jsonValue["MaxImageSizeBytes"] = bmcwebHttpReqBodyLimitMb * 831f5139334SEd Tanous 1024 * 1024; 832d61e5194STejas Patil 8330554c984SAndrew Geissler // Update Actions object. 8340554c984SAndrew Geissler nlohmann::json& updateSvcSimpleUpdate = 835002d39b4SEd Tanous asyncResp->res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"]; 8360554c984SAndrew Geissler updateSvcSimpleUpdate["target"] = 8370554c984SAndrew Geissler "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate"; 838*757178a5SEd Tanous 839*757178a5SEd Tanous nlohmann::json::array_t allowed; 840*757178a5SEd Tanous 841*757178a5SEd Tanous #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE 842*757178a5SEd Tanous allowed.emplace_back(update_service::TransferProtocolType::TFTP); 8430554c984SAndrew Geissler #endif 844*757178a5SEd Tanous 845*757178a5SEd Tanous updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = 846*757178a5SEd Tanous std::move(allowed); 847*757178a5SEd Tanous 848274dfe62SJayashankar Padath // Get the current ApplyTime value 8491e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::string>( 8501e1e598dSJonathan Doman *crow::connections::systemBus, "xyz.openbmc_project.Settings", 8511e1e598dSJonathan Doman "/xyz/openbmc_project/software/apply_time", 8521e1e598dSJonathan Doman "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime", 8535e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec, 8541e1e598dSJonathan Doman const std::string& applyTime) { 855274dfe62SJayashankar Padath if (ec) 856274dfe62SJayashankar Padath { 85762598e31SEd Tanous BMCWEB_LOG_DEBUG("DBUS response error {}", ec); 8588d1b46d7Szhanghch05 messages::internalError(asyncResp->res); 859274dfe62SJayashankar Padath return; 860274dfe62SJayashankar Padath } 861274dfe62SJayashankar Padath 862274dfe62SJayashankar Padath // Store the ApplyTime Value 8631e1e598dSJonathan Doman if (applyTime == "xyz.openbmc_project.Software.ApplyTime." 8641e1e598dSJonathan Doman "RequestedApplyTimes.Immediate") 865274dfe62SJayashankar Padath { 866002d39b4SEd Tanous asyncResp->res.jsonValue["HttpPushUriOptions"] 8677e860f15SJohn Edward Broadbent ["HttpPushUriApplyTime"]["ApplyTime"] = 8687e860f15SJohn Edward Broadbent "Immediate"; 869274dfe62SJayashankar Padath } 870002d39b4SEd Tanous else if (applyTime == "xyz.openbmc_project.Software.ApplyTime." 8711e1e598dSJonathan Doman "RequestedApplyTimes.OnReset") 872274dfe62SJayashankar Padath { 873002d39b4SEd Tanous asyncResp->res.jsonValue["HttpPushUriOptions"] 8747e860f15SJohn Edward Broadbent ["HttpPushUriApplyTime"]["ApplyTime"] = 8757e860f15SJohn Edward Broadbent "OnReset"; 876274dfe62SJayashankar Padath } 8771e1e598dSJonathan Doman }); 878f5139334SEd Tanous } 879f5139334SEd Tanous 880f5139334SEd Tanous inline void handleUpdateServicePatch( 881f5139334SEd Tanous App& app, const crow::Request& req, 882f5139334SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 883f5139334SEd Tanous { 8843ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 88545ca1b86SEd Tanous { 88645ca1b86SEd Tanous return; 88745ca1b86SEd Tanous } 88862598e31SEd Tanous BMCWEB_LOG_DEBUG("doPatch..."); 889fa1a5a38SJayashankar Padath 890274dfe62SJayashankar Padath std::optional<std::string> applyTime; 8917cb59f65SEd Tanous if (!json_util::readJsonPatch( 8927cb59f65SEd Tanous req, asyncResp->res, 8937cb59f65SEd Tanous "HttpPushUriOptions/HttpPushUriApplyTime/ApplyTime", applyTime)) 894274dfe62SJayashankar Padath { 895274dfe62SJayashankar Padath return; 896274dfe62SJayashankar Padath } 897274dfe62SJayashankar Padath 898274dfe62SJayashankar Padath if (applyTime) 899fa1a5a38SJayashankar Padath { 9000ed80c8cSGeorge Liu setApplyTime(asyncResp, *applyTime); 901fa1a5a38SJayashankar Padath } 902729dae72SJennifer Lee } 903729dae72SJennifer Lee 904f5139334SEd Tanous inline void handleUpdateServiceFirmwareInventoryCollectionGet( 905f5139334SEd Tanous App& app, const crow::Request& req, 906f5139334SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 9071abe55efSEd Tanous { 9083ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 90945ca1b86SEd Tanous { 91045ca1b86SEd Tanous return; 91145ca1b86SEd Tanous } 9128d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 9130f74e643SEd Tanous "#SoftwareInventoryCollection.SoftwareInventoryCollection"; 9148d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.id"] = 9150f74e643SEd Tanous "/redfish/v1/UpdateService/FirmwareInventory"; 916002d39b4SEd Tanous asyncResp->res.jsonValue["Name"] = "Software Inventory Collection"; 91708d81adaSJohn Edward Broadbent const std::array<const std::string_view, 1> iface = { 918e99073f5SGeorge Liu "xyz.openbmc_project.Software.Version"}; 9196c4eb9deSJennifer Lee 92008d81adaSJohn Edward Broadbent redfish::collection_util::getCollectionMembers( 92108d81adaSJohn Edward Broadbent asyncResp, 922f5139334SEd Tanous boost::urls::url("/redfish/v1/UpdateService/FirmwareInventory"), iface, 923f5139334SEd Tanous "/xyz/openbmc_project/software"); 924729dae72SJennifer Lee } 925f5139334SEd Tanous 92687d84729SAndrew Geissler /* Fill related item links (i.e. bmc, bios) in for inventory */ 927f5139334SEd Tanous inline void getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 92887d84729SAndrew Geissler const std::string& purpose) 92987d84729SAndrew Geissler { 930eee0013eSWilly Tu if (purpose == sw_util::bmcPurpose) 93187d84729SAndrew Geissler { 932ac106bf6SEd Tanous nlohmann::json& relatedItem = asyncResp->res.jsonValue["RelatedItem"]; 9331476687dSEd Tanous nlohmann::json::object_t item; 9341476687dSEd Tanous item["@odata.id"] = "/redfish/v1/Managers/bmc"; 935b2ba3072SPatrick Williams relatedItem.emplace_back(std::move(item)); 936ac106bf6SEd Tanous asyncResp->res.jsonValue["RelatedItem@odata.count"] = 937ac106bf6SEd Tanous relatedItem.size(); 93887d84729SAndrew Geissler } 939eee0013eSWilly Tu else if (purpose == sw_util::biosPurpose) 94087d84729SAndrew Geissler { 941ac106bf6SEd Tanous nlohmann::json& relatedItem = asyncResp->res.jsonValue["RelatedItem"]; 9421476687dSEd Tanous nlohmann::json::object_t item; 9431476687dSEd Tanous item["@odata.id"] = "/redfish/v1/Systems/system/Bios"; 944b2ba3072SPatrick Williams relatedItem.emplace_back(std::move(item)); 945ac106bf6SEd Tanous asyncResp->res.jsonValue["RelatedItem@odata.count"] = 946ac106bf6SEd Tanous relatedItem.size(); 94787d84729SAndrew Geissler } 94887d84729SAndrew Geissler else 94987d84729SAndrew Geissler { 950bf2ddedeSCarson Labrado BMCWEB_LOG_DEBUG("Unknown software purpose {}", purpose); 95187d84729SAndrew Geissler } 95287d84729SAndrew Geissler } 95387d84729SAndrew Geissler 954af24660dSWilly Tu inline void 955af24660dSWilly Tu getSoftwareVersion(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 956af24660dSWilly Tu const std::string& service, const std::string& path, 957af24660dSWilly Tu const std::string& swId) 958af24660dSWilly Tu { 959d1bde9e5SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 960d1bde9e5SKrzysztof Grobelny *crow::connections::systemBus, service, path, 961d1bde9e5SKrzysztof Grobelny "xyz.openbmc_project.Software.Version", 962af24660dSWilly Tu [asyncResp, 9638b24275dSEd Tanous swId](const boost::system::error_code& ec, 964af24660dSWilly Tu const dbus::utility::DBusPropertiesMap& propertiesList) { 9658b24275dSEd Tanous if (ec) 966af24660dSWilly Tu { 967af24660dSWilly Tu messages::internalError(asyncResp->res); 968af24660dSWilly Tu return; 969af24660dSWilly Tu } 970d1bde9e5SKrzysztof Grobelny 971af24660dSWilly Tu const std::string* swInvPurpose = nullptr; 972af24660dSWilly Tu const std::string* version = nullptr; 973d1bde9e5SKrzysztof Grobelny 974d1bde9e5SKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 975d1bde9e5SKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), propertiesList, "Purpose", 976d1bde9e5SKrzysztof Grobelny swInvPurpose, "Version", version); 977d1bde9e5SKrzysztof Grobelny 978d1bde9e5SKrzysztof Grobelny if (!success) 979af24660dSWilly Tu { 980d1bde9e5SKrzysztof Grobelny messages::internalError(asyncResp->res); 981d1bde9e5SKrzysztof Grobelny return; 982af24660dSWilly Tu } 983af24660dSWilly Tu 984af24660dSWilly Tu if (swInvPurpose == nullptr) 985af24660dSWilly Tu { 98662598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't find property \"Purpose\"!"); 987af24660dSWilly Tu messages::internalError(asyncResp->res); 988af24660dSWilly Tu return; 989af24660dSWilly Tu } 990af24660dSWilly Tu 99162598e31SEd Tanous BMCWEB_LOG_DEBUG("swInvPurpose = {}", *swInvPurpose); 992af24660dSWilly Tu 993af24660dSWilly Tu if (version == nullptr) 994af24660dSWilly Tu { 99562598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't find property \"Version\"!"); 996af24660dSWilly Tu 997af24660dSWilly Tu messages::internalError(asyncResp->res); 998af24660dSWilly Tu 999af24660dSWilly Tu return; 1000af24660dSWilly Tu } 1001af24660dSWilly Tu asyncResp->res.jsonValue["Version"] = *version; 1002af24660dSWilly Tu asyncResp->res.jsonValue["Id"] = swId; 1003af24660dSWilly Tu 1004af24660dSWilly Tu // swInvPurpose is of format: 1005af24660dSWilly Tu // xyz.openbmc_project.Software.Version.VersionPurpose.ABC 1006af24660dSWilly Tu // Translate this to "ABC image" 1007af24660dSWilly Tu size_t endDesc = swInvPurpose->rfind('.'); 1008af24660dSWilly Tu if (endDesc == std::string::npos) 1009af24660dSWilly Tu { 1010af24660dSWilly Tu messages::internalError(asyncResp->res); 1011af24660dSWilly Tu return; 1012af24660dSWilly Tu } 1013af24660dSWilly Tu endDesc++; 1014af24660dSWilly Tu if (endDesc >= swInvPurpose->size()) 1015af24660dSWilly Tu { 1016af24660dSWilly Tu messages::internalError(asyncResp->res); 1017af24660dSWilly Tu return; 1018af24660dSWilly Tu } 1019af24660dSWilly Tu 1020af24660dSWilly Tu std::string formatDesc = swInvPurpose->substr(endDesc); 1021af24660dSWilly Tu asyncResp->res.jsonValue["Description"] = formatDesc + " image"; 1022af24660dSWilly Tu getRelatedItems(asyncResp, *swInvPurpose); 1023d1bde9e5SKrzysztof Grobelny }); 1024af24660dSWilly Tu } 1025af24660dSWilly Tu 1026f5139334SEd Tanous inline void handleUpdateServiceFirmwareInventoryGet( 1027f5139334SEd Tanous App& app, const crow::Request& req, 102845ca1b86SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1029f5139334SEd Tanous const std::string& param) 1030f5139334SEd Tanous { 10313ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 103245ca1b86SEd Tanous { 103345ca1b86SEd Tanous return; 103445ca1b86SEd Tanous } 1035f5139334SEd Tanous std::shared_ptr<std::string> swId = std::make_shared<std::string>(param); 1036c711bf86SEd Tanous 1037ef4c65b7SEd Tanous asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( 1038ef4c65b7SEd Tanous "/redfish/v1/UpdateService/FirmwareInventory/{}", *swId); 1039c711bf86SEd Tanous 1040e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 1041e99073f5SGeorge Liu "xyz.openbmc_project.Software.Version"}; 1042e99073f5SGeorge Liu dbus::utility::getSubTree( 1043e99073f5SGeorge Liu "/", 0, interfaces, 1044b9d36b47SEd Tanous [asyncResp, 1045e99073f5SGeorge Liu swId](const boost::system::error_code& ec, 1046b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 104762598e31SEd Tanous BMCWEB_LOG_DEBUG("doGet callback..."); 10481abe55efSEd Tanous if (ec) 10491abe55efSEd Tanous { 1050f12894f8SJason M. Bills messages::internalError(asyncResp->res); 10516c4eb9deSJennifer Lee return; 10526c4eb9deSJennifer Lee } 10536c4eb9deSJennifer Lee 10546913228dSAndrew Geissler // Ensure we find our input swId, otherwise return an error 10556913228dSAndrew Geissler bool found = false; 1056f5139334SEd Tanous for (const std::pair< 1057f5139334SEd Tanous std::string, 1058f5139334SEd Tanous std::vector<std::pair<std::string, std::vector<std::string>>>>& 1059002d39b4SEd Tanous obj : subtree) 10601abe55efSEd Tanous { 106111ba3979SEd Tanous if (!obj.first.ends_with(*swId)) 10621abe55efSEd Tanous { 1063acb7cfb4SJennifer Lee continue; 1064acb7cfb4SJennifer Lee } 1065acb7cfb4SJennifer Lee 106626f6976fSEd Tanous if (obj.second.empty()) 10671abe55efSEd Tanous { 1068acb7cfb4SJennifer Lee continue; 1069acb7cfb4SJennifer Lee } 10706c4eb9deSJennifer Lee 10716913228dSAndrew Geissler found = true; 1072eee0013eSWilly Tu sw_util::getSwStatus(asyncResp, swId, obj.second[0].first); 1073af24660dSWilly Tu getSoftwareVersion(asyncResp, obj.second[0].first, obj.first, 1074af24660dSWilly Tu *swId); 10756c4eb9deSJennifer Lee } 10766913228dSAndrew Geissler if (!found) 10776913228dSAndrew Geissler { 107862598e31SEd Tanous BMCWEB_LOG_WARNING("Input swID {} not found!", *swId); 10796913228dSAndrew Geissler messages::resourceMissingAtURI( 1080ef4c65b7SEd Tanous asyncResp->res, 1081ef4c65b7SEd Tanous boost::urls::format( 1082f5139334SEd Tanous "/redfish/v1/UpdateService/FirmwareInventory/{}", *swId)); 10836913228dSAndrew Geissler return; 10846913228dSAndrew Geissler } 10854e68c45bSAyushi Smriti asyncResp->res.jsonValue["@odata.type"] = 10864e68c45bSAyushi Smriti "#SoftwareInventory.v1_1_0.SoftwareInventory"; 10874e68c45bSAyushi Smriti asyncResp->res.jsonValue["Name"] = "Software Inventory"; 10884e68c45bSAyushi Smriti asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK"; 10893f8a743aSAppaRao Puli 10903f8a743aSAppaRao Puli asyncResp->res.jsonValue["Updateable"] = false; 1091eee0013eSWilly Tu sw_util::getSwUpdatableStatus(asyncResp, swId); 1092e99073f5SGeorge Liu }); 1093f5139334SEd Tanous } 1094f5139334SEd Tanous 1095f5139334SEd Tanous inline void requestRoutesUpdateService(App& app) 1096f5139334SEd Tanous { 1097f5139334SEd Tanous BMCWEB_ROUTE( 1098f5139334SEd Tanous app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/") 1099f5139334SEd Tanous .privileges(redfish::privileges::postUpdateService) 1100f5139334SEd Tanous .methods(boost::beast::http::verb::post)(std::bind_front( 1101f5139334SEd Tanous handleUpdateServiceSimpleUpdateAction, std::ref(app))); 1102f5139334SEd Tanous 1103f5139334SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/") 1104f5139334SEd Tanous .privileges(redfish::privileges::getSoftwareInventory) 1105f5139334SEd Tanous .methods(boost::beast::http::verb::get)(std::bind_front( 1106f5139334SEd Tanous handleUpdateServiceFirmwareInventoryGet, std::ref(app))); 1107f5139334SEd Tanous 1108f5139334SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") 1109f5139334SEd Tanous .privileges(redfish::privileges::getUpdateService) 1110f5139334SEd Tanous .methods(boost::beast::http::verb::get)( 1111f5139334SEd Tanous std::bind_front(handleUpdateServiceGet, std::ref(app))); 1112f5139334SEd Tanous 1113f5139334SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") 1114f5139334SEd Tanous .privileges(redfish::privileges::patchUpdateService) 1115f5139334SEd Tanous .methods(boost::beast::http::verb::patch)( 1116f5139334SEd Tanous std::bind_front(handleUpdateServicePatch, std::ref(app))); 1117f5139334SEd Tanous 1118f5139334SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/update/") 1119f5139334SEd Tanous .privileges(redfish::privileges::postUpdateService) 1120f5139334SEd Tanous .methods(boost::beast::http::verb::post)( 1121f5139334SEd Tanous std::bind_front(handleUpdateServicePost, std::ref(app))); 1122f5139334SEd Tanous 1123f5139334SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/") 1124f5139334SEd Tanous .privileges(redfish::privileges::getSoftwareInventoryCollection) 1125f5139334SEd Tanous .methods(boost::beast::http::verb::get)(std::bind_front( 1126f5139334SEd Tanous handleUpdateServiceFirmwareInventoryCollectionGet, std::ref(app))); 11276c4eb9deSJennifer Lee } 1128729dae72SJennifer Lee 1129729dae72SJennifer Lee } // namespace redfish 1130