| Jennifer Lee | 729dae7 | 2018-04-24 15:59:34 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | // Copyright (c) 2018 Intel Corporation | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | */ | 
|  | 16 | #pragma once | 
|  | 17 |  | 
| Tejas Patil | d61e519 | 2021-06-04 15:49:35 +0530 | [diff] [blame] | 18 | #include "bmcweb_config.h" | 
|  | 19 |  | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 20 | #include <app.hpp> | 
| Jennifer Lee | 729dae7 | 2018-04-24 15:59:34 -0700 | [diff] [blame] | 21 | #include <boost/container/flat_map.hpp> | 
| Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 22 | #include <dbus_utility.hpp> | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 23 | #include <registries/privilege_registry.hpp> | 
| Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 24 | #include <sdbusplus/asio/property.hpp> | 
| Andrew Geissler | 87d8472 | 2019-02-28 14:28:39 -0600 | [diff] [blame] | 25 | #include <utils/fw_utils.hpp> | 
| Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 26 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | namespace redfish | 
|  | 28 | { | 
| Ed Tanous | 27826b5 | 2018-10-29 11:40:58 -0700 | [diff] [blame] | 29 |  | 
| Andrew Geissler | 0e7de46 | 2019-03-04 19:11:54 -0600 | [diff] [blame] | 30 | // Match signals added on software path | 
| Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 31 | static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher; | 
| James Feist | 4cde5d9 | 2020-06-11 10:39:55 -0700 | [diff] [blame] | 32 | static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateErrorMatcher; | 
| Andrew Geissler | 0e7de46 | 2019-03-04 19:11:54 -0600 | [diff] [blame] | 33 | // Only allow one update at a time | 
|  | 34 | static bool fwUpdateInProgress = false; | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 35 | // Timer for software available | 
| Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 36 | static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer; | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 37 |  | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 38 | inline static void cleanUp() | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 39 | { | 
|  | 40 | fwUpdateInProgress = false; | 
|  | 41 | fwUpdateMatcher = nullptr; | 
| James Feist | 4cde5d9 | 2020-06-11 10:39:55 -0700 | [diff] [blame] | 42 | fwUpdateErrorMatcher = nullptr; | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 43 | } | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 44 | inline static void activateImage(const std::string& objPath, | 
|  | 45 | const std::string& service) | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 46 | { | 
|  | 47 | BMCWEB_LOG_DEBUG << "Activate image for " << objPath << " " << service; | 
|  | 48 | crow::connections::systemBus->async_method_call( | 
| Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 49 | [](const boost::system::error_code errorCode) { | 
|  | 50 | if (errorCode) | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 51 | { | 
| Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 52 | BMCWEB_LOG_DEBUG << "error_code = " << errorCode; | 
|  | 53 | BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message(); | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 54 | } | 
|  | 55 | }, | 
|  | 56 | service, objPath, "org.freedesktop.DBus.Properties", "Set", | 
|  | 57 | "xyz.openbmc_project.Software.Activation", "RequestedActivation", | 
| Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 58 | dbus::utility::DbusVariantType( | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 59 | "xyz.openbmc_project.Software.Activation.RequestedActivations.Active")); | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 60 | } | 
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 61 |  | 
|  | 62 | // Note that asyncResp can be either a valid pointer or nullptr. If nullptr | 
|  | 63 | // then no asyncResp updates will occur | 
| zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 64 | static void | 
|  | 65 | softwareInterfaceAdded(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, | 
|  | 66 | sdbusplus::message::message& m, | 
| Ed Tanous | a3e6589 | 2021-09-16 14:13:20 -0700 | [diff] [blame] | 67 | task::Payload&& payload) | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 68 | { | 
|  | 69 | std::vector<std::pair< | 
|  | 70 | std::string, | 
| Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 71 | std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>>> | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 72 | interfacesProperties; | 
|  | 73 |  | 
|  | 74 | sdbusplus::message::object_path objPath; | 
|  | 75 |  | 
|  | 76 | m.read(objPath, interfacesProperties); | 
|  | 77 |  | 
|  | 78 | BMCWEB_LOG_DEBUG << "obj path = " << objPath.str; | 
| Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 79 | for (auto& interface : interfacesProperties) | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 80 | { | 
|  | 81 | BMCWEB_LOG_DEBUG << "interface = " << interface.first; | 
|  | 82 |  | 
|  | 83 | if (interface.first == "xyz.openbmc_project.Software.Activation") | 
|  | 84 | { | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 85 | // Retrieve service and activate | 
|  | 86 | crow::connections::systemBus->async_method_call( | 
| Ed Tanous | a3e6589 | 2021-09-16 14:13:20 -0700 | [diff] [blame] | 87 | [objPath, asyncResp, payload(std::move(payload))]( | 
|  | 88 | const boost::system::error_code errorCode, | 
|  | 89 | const std::vector< | 
|  | 90 | std::pair<std::string, std::vector<std::string>>>& | 
|  | 91 | objInfo) mutable { | 
| Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 92 | if (errorCode) | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 93 | { | 
| Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 94 | BMCWEB_LOG_DEBUG << "error_code = " << errorCode; | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 95 | BMCWEB_LOG_DEBUG << "error msg = " | 
| Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 96 | << errorCode.message(); | 
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 97 | if (asyncResp) | 
|  | 98 | { | 
|  | 99 | messages::internalError(asyncResp->res); | 
|  | 100 | } | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 101 | cleanUp(); | 
|  | 102 | return; | 
|  | 103 | } | 
|  | 104 | // Ensure we only got one service back | 
|  | 105 | if (objInfo.size() != 1) | 
|  | 106 | { | 
|  | 107 | BMCWEB_LOG_ERROR << "Invalid Object Size " | 
|  | 108 | << objInfo.size(); | 
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 109 | if (asyncResp) | 
|  | 110 | { | 
|  | 111 | messages::internalError(asyncResp->res); | 
|  | 112 | } | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 113 | cleanUp(); | 
|  | 114 | return; | 
|  | 115 | } | 
|  | 116 | // cancel timer only when | 
|  | 117 | // xyz.openbmc_project.Software.Activation interface | 
|  | 118 | // is added | 
|  | 119 | fwAvailableTimer = nullptr; | 
|  | 120 |  | 
|  | 121 | activateImage(objPath.str, objInfo[0].first); | 
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 122 | if (asyncResp) | 
|  | 123 | { | 
| James Feist | 32898ce | 2020-03-10 16:16:52 -0700 | [diff] [blame] | 124 | std::shared_ptr<task::TaskData> task = | 
|  | 125 | task::TaskData::createTask( | 
|  | 126 | [](boost::system::error_code ec, | 
| Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 127 | sdbusplus::message::message& msg, | 
|  | 128 | const std::shared_ptr<task::TaskData>& | 
|  | 129 | taskData) { | 
| James Feist | 32898ce | 2020-03-10 16:16:52 -0700 | [diff] [blame] | 130 | if (ec) | 
|  | 131 | { | 
|  | 132 | return task::completed; | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | std::string iface; | 
|  | 136 | boost::container::flat_map< | 
| James Feist | fd9ab9e | 2020-05-19 13:48:07 -0700 | [diff] [blame] | 137 | std::string, | 
| Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 138 | dbus::utility::DbusVariantType> | 
| James Feist | 32898ce | 2020-03-10 16:16:52 -0700 | [diff] [blame] | 139 | values; | 
| James Feist | 32898ce | 2020-03-10 16:16:52 -0700 | [diff] [blame] | 140 |  | 
| James Feist | e5d5006 | 2020-05-11 17:29:00 -0700 | [diff] [blame] | 141 | std::string index = | 
|  | 142 | std::to_string(taskData->index); | 
| James Feist | fd9ab9e | 2020-05-19 13:48:07 -0700 | [diff] [blame] | 143 | msg.read(iface, values); | 
| James Feist | e5d5006 | 2020-05-11 17:29:00 -0700 | [diff] [blame] | 144 |  | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 145 | if (iface == | 
|  | 146 | "xyz.openbmc_project.Software.Activation") | 
| James Feist | 32898ce | 2020-03-10 16:16:52 -0700 | [diff] [blame] | 147 | { | 
| James Feist | fd9ab9e | 2020-05-19 13:48:07 -0700 | [diff] [blame] | 148 | auto findActivation = | 
|  | 149 | values.find("Activation"); | 
|  | 150 | if (findActivation == values.end()) | 
|  | 151 | { | 
|  | 152 | return !task::completed; | 
|  | 153 | } | 
| Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 154 | std::string* state = | 
| James Feist | fd9ab9e | 2020-05-19 13:48:07 -0700 | [diff] [blame] | 155 | std::get_if<std::string>( | 
|  | 156 | &(findActivation->second)); | 
|  | 157 |  | 
|  | 158 | if (state == nullptr) | 
|  | 159 | { | 
|  | 160 | taskData->messages.emplace_back( | 
|  | 161 | messages::internalError()); | 
|  | 162 | return task::completed; | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | if (boost::ends_with(*state, | 
|  | 166 | "Invalid") || | 
|  | 167 | boost::ends_with(*state, "Failed")) | 
|  | 168 | { | 
|  | 169 | taskData->state = "Exception"; | 
|  | 170 | taskData->status = "Warning"; | 
|  | 171 | taskData->messages.emplace_back( | 
|  | 172 | messages::taskAborted(index)); | 
|  | 173 | return task::completed; | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | if (boost::ends_with(*state, "Staged")) | 
|  | 177 | { | 
|  | 178 | taskData->state = "Stopping"; | 
|  | 179 | taskData->messages.emplace_back( | 
|  | 180 | messages::taskPaused(index)); | 
|  | 181 |  | 
|  | 182 | // its staged, set a long timer to | 
|  | 183 | // allow them time to complete the | 
|  | 184 | // update (probably cycle the | 
|  | 185 | // system) if this expires then | 
|  | 186 | // task will be cancelled | 
|  | 187 | taskData->extendTimer( | 
|  | 188 | std::chrono::hours(5)); | 
|  | 189 | return !task::completed; | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | if (boost::ends_with(*state, "Active")) | 
|  | 193 | { | 
|  | 194 | taskData->messages.emplace_back( | 
|  | 195 | messages::taskCompletedOK( | 
|  | 196 | index)); | 
|  | 197 | taskData->state = "Completed"; | 
|  | 198 | return task::completed; | 
|  | 199 | } | 
| James Feist | 32898ce | 2020-03-10 16:16:52 -0700 | [diff] [blame] | 200 | } | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 201 | else if ( | 
|  | 202 | iface == | 
|  | 203 | "xyz.openbmc_project.Software.ActivationProgress") | 
| James Feist | 32898ce | 2020-03-10 16:16:52 -0700 | [diff] [blame] | 204 | { | 
| James Feist | fd9ab9e | 2020-05-19 13:48:07 -0700 | [diff] [blame] | 205 | auto findProgress = | 
|  | 206 | values.find("Progress"); | 
|  | 207 | if (findProgress == values.end()) | 
|  | 208 | { | 
|  | 209 | return !task::completed; | 
|  | 210 | } | 
| Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 211 | uint8_t* progress = | 
| James Feist | fd9ab9e | 2020-05-19 13:48:07 -0700 | [diff] [blame] | 212 | std::get_if<uint8_t>( | 
|  | 213 | &(findProgress->second)); | 
| James Feist | 32898ce | 2020-03-10 16:16:52 -0700 | [diff] [blame] | 214 |  | 
| James Feist | fd9ab9e | 2020-05-19 13:48:07 -0700 | [diff] [blame] | 215 | if (progress == nullptr) | 
|  | 216 | { | 
|  | 217 | taskData->messages.emplace_back( | 
|  | 218 | messages::internalError()); | 
|  | 219 | return task::completed; | 
|  | 220 | } | 
| George Liu | 6868ff5 | 2021-01-02 11:37:41 +0800 | [diff] [blame] | 221 | taskData->percentComplete = | 
|  | 222 | static_cast<int>(*progress); | 
| James Feist | 32898ce | 2020-03-10 16:16:52 -0700 | [diff] [blame] | 223 | taskData->messages.emplace_back( | 
| James Feist | fd9ab9e | 2020-05-19 13:48:07 -0700 | [diff] [blame] | 224 | messages::taskProgressChanged( | 
|  | 225 | index, static_cast<size_t>( | 
|  | 226 | *progress))); | 
|  | 227 |  | 
|  | 228 | // if we're getting status updates it's | 
|  | 229 | // still alive, update timer | 
|  | 230 | taskData->extendTimer( | 
|  | 231 | std::chrono::minutes(5)); | 
| James Feist | 32898ce | 2020-03-10 16:16:52 -0700 | [diff] [blame] | 232 | } | 
|  | 233 |  | 
|  | 234 | // as firmware update often results in a | 
|  | 235 | // reboot, the task  may never "complete" | 
|  | 236 | // unless it is an error | 
|  | 237 |  | 
|  | 238 | return !task::completed; | 
|  | 239 | }, | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 240 | "type='signal',interface='org.freedesktop.DBus.Properties'," | 
| James Feist | fd9ab9e | 2020-05-19 13:48:07 -0700 | [diff] [blame] | 241 | "member='PropertiesChanged',path='" + | 
| James Feist | 32898ce | 2020-03-10 16:16:52 -0700 | [diff] [blame] | 242 | objPath.str + "'"); | 
|  | 243 | task->startTimer(std::chrono::minutes(5)); | 
|  | 244 | task->populateResp(asyncResp->res); | 
| Ed Tanous | a3e6589 | 2021-09-16 14:13:20 -0700 | [diff] [blame] | 245 | task->payload.emplace(std::move(payload)); | 
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 246 | } | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 247 | fwUpdateInProgress = false; | 
|  | 248 | }, | 
|  | 249 | "xyz.openbmc_project.ObjectMapper", | 
|  | 250 | "/xyz/openbmc_project/object_mapper", | 
|  | 251 | "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str, | 
| Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 252 | std::array<const char*, 1>{ | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 253 | "xyz.openbmc_project.Software.Activation"}); | 
|  | 254 | } | 
|  | 255 | } | 
|  | 256 | } | 
|  | 257 |  | 
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 258 | // Note that asyncResp can be either a valid pointer or nullptr. If nullptr | 
|  | 259 | // then no asyncResp updates will occur | 
| Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 260 | static void monitorForSoftwareAvailable( | 
| zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 261 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, | 
|  | 262 | const crow::Request& req, const std::string& url, | 
|  | 263 | int timeoutTimeSeconds = 10) | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 264 | { | 
|  | 265 | // Only allow one FW update at a time | 
|  | 266 | if (fwUpdateInProgress != false) | 
|  | 267 | { | 
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 268 | if (asyncResp) | 
|  | 269 | { | 
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 270 | messages::serviceTemporarilyUnavailable(asyncResp->res, "30"); | 
|  | 271 | } | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 272 | return; | 
|  | 273 | } | 
|  | 274 |  | 
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 275 | fwAvailableTimer = | 
| Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 276 | std::make_unique<boost::asio::steady_timer>(*req.ioService); | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 277 |  | 
| Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 278 | fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds)); | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 279 |  | 
|  | 280 | fwAvailableTimer->async_wait( | 
| Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 281 | [asyncResp](const boost::system::error_code& ec) { | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 282 | cleanUp(); | 
|  | 283 | if (ec == boost::asio::error::operation_aborted) | 
|  | 284 | { | 
|  | 285 | // expected, we were canceled before the timer completed. | 
|  | 286 | return; | 
|  | 287 | } | 
|  | 288 | BMCWEB_LOG_ERROR | 
|  | 289 | << "Timed out waiting for firmware object being created"; | 
|  | 290 | BMCWEB_LOG_ERROR | 
|  | 291 | << "FW image may has already been uploaded to server"; | 
|  | 292 | if (ec) | 
|  | 293 | { | 
|  | 294 | BMCWEB_LOG_ERROR << "Async_wait failed" << ec; | 
|  | 295 | return; | 
|  | 296 | } | 
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 297 | if (asyncResp) | 
|  | 298 | { | 
|  | 299 | redfish::messages::internalError(asyncResp->res); | 
|  | 300 | } | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 301 | }); | 
| Ed Tanous | a3e6589 | 2021-09-16 14:13:20 -0700 | [diff] [blame] | 302 | task::Payload payload(req); | 
|  | 303 | auto callback = [asyncResp, | 
|  | 304 | payload](sdbusplus::message::message& m) mutable { | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 305 | BMCWEB_LOG_DEBUG << "Match fired"; | 
| Ed Tanous | a3e6589 | 2021-09-16 14:13:20 -0700 | [diff] [blame] | 306 | softwareInterfaceAdded(asyncResp, m, std::move(payload)); | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 307 | }; | 
|  | 308 |  | 
|  | 309 | fwUpdateInProgress = true; | 
|  | 310 |  | 
|  | 311 | fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>( | 
|  | 312 | *crow::connections::systemBus, | 
|  | 313 | "interface='org.freedesktop.DBus.ObjectManager',type='signal'," | 
|  | 314 | "member='InterfacesAdded',path='/xyz/openbmc_project/software'", | 
|  | 315 | callback); | 
| James Feist | 4cde5d9 | 2020-06-11 10:39:55 -0700 | [diff] [blame] | 316 |  | 
|  | 317 | fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match::match>( | 
|  | 318 | *crow::connections::systemBus, | 
| Brian Ma | e1cc482 | 2021-12-01 17:05:54 +0800 | [diff] [blame] | 319 | "interface='org.freedesktop.DBus.ObjectManager',type='signal'," | 
|  | 320 | "member='InterfacesAdded'," | 
|  | 321 | "path='/xyz/openbmc_project/logging'", | 
| James Feist | 4cde5d9 | 2020-06-11 10:39:55 -0700 | [diff] [blame] | 322 | [asyncResp, url](sdbusplus::message::message& m) { | 
| Brian Ma | e1cc482 | 2021-12-01 17:05:54 +0800 | [diff] [blame] | 323 | std::vector< | 
|  | 324 | std::pair<std::string, dbus::utility::DBusPropertiesMap>> | 
|  | 325 | interfacesProperties; | 
|  | 326 | sdbusplus::message::object_path objPath; | 
|  | 327 | m.read(objPath, interfacesProperties); | 
|  | 328 | BMCWEB_LOG_DEBUG << "obj path = " << objPath.str; | 
|  | 329 | for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>& | 
|  | 330 | interface : interfacesProperties) | 
| James Feist | 4cde5d9 | 2020-06-11 10:39:55 -0700 | [diff] [blame] | 331 | { | 
| Brian Ma | e1cc482 | 2021-12-01 17:05:54 +0800 | [diff] [blame] | 332 | if (interface.first == "xyz.openbmc_project.Logging.Entry") | 
|  | 333 | { | 
|  | 334 | BMCWEB_LOG_DEBUG << "Error Match fired"; | 
|  | 335 | const dbus::utility::DBusPropertiesMap& values = | 
|  | 336 | interface.second; | 
|  | 337 | auto find = values.find("Message"); | 
|  | 338 | if (find == values.end()) | 
|  | 339 | { | 
|  | 340 | return; | 
|  | 341 | } | 
|  | 342 | const std::string* type = | 
|  | 343 | std::get_if<std::string>(&(find->second)); | 
|  | 344 | if (type == nullptr) | 
|  | 345 | { | 
|  | 346 | // if this was our message, timeout will cover it | 
|  | 347 | return; | 
|  | 348 | } | 
|  | 349 | fwAvailableTimer = nullptr; | 
|  | 350 | if (*type == | 
|  | 351 | "xyz.openbmc_project.Software.Image.Error.UnTarFailure") | 
|  | 352 | { | 
|  | 353 | redfish::messages::invalidUpload(asyncResp->res, url, | 
|  | 354 | "Invalid archive"); | 
|  | 355 | } | 
|  | 356 | else if (*type == | 
|  | 357 | "xyz.openbmc_project.Software.Image.Error." | 
|  | 358 | "ManifestFileFailure") | 
|  | 359 | { | 
|  | 360 | redfish::messages::invalidUpload(asyncResp->res, url, | 
|  | 361 | "Invalid manifest"); | 
|  | 362 | } | 
|  | 363 | else if ( | 
|  | 364 | *type == | 
|  | 365 | "xyz.openbmc_project.Software.Image.Error.ImageFailure") | 
|  | 366 | { | 
|  | 367 | redfish::messages::invalidUpload( | 
|  | 368 | asyncResp->res, url, "Invalid image format"); | 
|  | 369 | } | 
|  | 370 | else if ( | 
|  | 371 | *type == | 
|  | 372 | "xyz.openbmc_project.Software.Version.Error.AlreadyExists") | 
|  | 373 | { | 
|  | 374 | redfish::messages::invalidUpload( | 
|  | 375 | asyncResp->res, url, | 
|  | 376 | "Image version already exists"); | 
| Gunnar Mills | 88b3dd1 | 2020-11-20 14:26:04 -0600 | [diff] [blame] | 377 |  | 
| Brian Ma | e1cc482 | 2021-12-01 17:05:54 +0800 | [diff] [blame] | 378 | redfish::messages::resourceAlreadyExists( | 
|  | 379 | asyncResp->res, | 
|  | 380 | "UpdateService.v1_5_0.UpdateService", "Version", | 
|  | 381 | "uploaded version"); | 
|  | 382 | } | 
|  | 383 | else if ( | 
|  | 384 | *type == | 
|  | 385 | "xyz.openbmc_project.Software.Image.Error.BusyFailure") | 
|  | 386 | { | 
|  | 387 | redfish::messages::resourceExhaustion(asyncResp->res, | 
|  | 388 | url); | 
|  | 389 | } | 
|  | 390 | else | 
|  | 391 | { | 
|  | 392 | redfish::messages::internalError(asyncResp->res); | 
|  | 393 | } | 
|  | 394 | } | 
| Gunnar Mills | 88b3dd1 | 2020-11-20 14:26:04 -0600 | [diff] [blame] | 395 | } | 
| James Feist | 4cde5d9 | 2020-06-11 10:39:55 -0700 | [diff] [blame] | 396 | }); | 
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 397 | } | 
| Jennifer Lee | 729dae7 | 2018-04-24 15:59:34 -0700 | [diff] [blame] | 398 |  | 
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 399 | /** | 
|  | 400 | * UpdateServiceActionsSimpleUpdate class supports handle POST method for | 
|  | 401 | * SimpleUpdate action. | 
|  | 402 | */ | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 403 | inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app) | 
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 404 | { | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 405 | BMCWEB_ROUTE( | 
|  | 406 | app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/") | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 407 | .privileges(redfish::privileges::postUpdateService) | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 408 | .methods( | 
|  | 409 | boost::beast::http::verb:: | 
|  | 410 | post)([](const crow::Request& req, | 
|  | 411 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { | 
|  | 412 | std::optional<std::string> transferProtocol; | 
|  | 413 | std::string imageURI; | 
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 414 |  | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 415 | BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost"; | 
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 416 |  | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 417 | // User can pass in both TransferProtocol and ImageURI parameters or | 
|  | 418 | // they can pass in just the ImageURI with the transfer protocol | 
|  | 419 | // embedded within it. | 
|  | 420 | // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin | 
|  | 421 | // 2) ImageURI:tftp://1.1.1.1/myfile.bin | 
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 422 |  | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 423 | if (!json_util::readJson(req, asyncResp->res, "TransferProtocol", | 
|  | 424 | transferProtocol, "ImageURI", imageURI)) | 
|  | 425 | { | 
|  | 426 | BMCWEB_LOG_DEBUG | 
|  | 427 | << "Missing TransferProtocol or ImageURI parameter"; | 
|  | 428 | return; | 
|  | 429 | } | 
|  | 430 | if (!transferProtocol) | 
|  | 431 | { | 
|  | 432 | // Must be option 2 | 
|  | 433 | // Verify ImageURI has transfer protocol in it | 
|  | 434 | size_t separator = imageURI.find(':'); | 
|  | 435 | if ((separator == std::string::npos) || | 
|  | 436 | ((separator + 1) > imageURI.size())) | 
|  | 437 | { | 
|  | 438 | messages::actionParameterValueTypeError( | 
|  | 439 | asyncResp->res, imageURI, "ImageURI", | 
|  | 440 | "UpdateService.SimpleUpdate"); | 
|  | 441 | BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: " | 
|  | 442 | << imageURI; | 
|  | 443 | return; | 
|  | 444 | } | 
|  | 445 | transferProtocol = imageURI.substr(0, separator); | 
|  | 446 | // Ensure protocol is upper case for a common comparison path | 
|  | 447 | // below | 
|  | 448 | boost::to_upper(*transferProtocol); | 
|  | 449 | BMCWEB_LOG_DEBUG << "Encoded transfer protocol " | 
|  | 450 | << *transferProtocol; | 
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 451 |  | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 452 | // Adjust imageURI to not have the protocol on it for parsing | 
|  | 453 | // below | 
|  | 454 | // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin | 
|  | 455 | imageURI = imageURI.substr(separator + 3); | 
|  | 456 | BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI; | 
|  | 457 | } | 
|  | 458 |  | 
|  | 459 | // OpenBMC currently only supports TFTP | 
|  | 460 | if (*transferProtocol != "TFTP") | 
|  | 461 | { | 
|  | 462 | messages::actionParameterNotSupported( | 
|  | 463 | asyncResp->res, "TransferProtocol", | 
|  | 464 | "UpdateService.SimpleUpdate"); | 
|  | 465 | BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: " | 
|  | 466 | << *transferProtocol; | 
|  | 467 | return; | 
|  | 468 | } | 
|  | 469 |  | 
|  | 470 | // Format should be <IP or Hostname>/<file> for imageURI | 
|  | 471 | size_t separator = imageURI.find('/'); | 
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 472 | if ((separator == std::string::npos) || | 
|  | 473 | ((separator + 1) > imageURI.size())) | 
|  | 474 | { | 
|  | 475 | messages::actionParameterValueTypeError( | 
|  | 476 | asyncResp->res, imageURI, "ImageURI", | 
|  | 477 | "UpdateService.SimpleUpdate"); | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 478 | BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI; | 
| Jayashankar Padath | 274dfe6 | 2019-08-23 12:30:57 +0530 | [diff] [blame] | 479 | return; | 
| Jayashankar Padath | fa1a5a3 | 2019-05-28 23:54:37 +0530 | [diff] [blame] | 480 | } | 
|  | 481 |  | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 482 | std::string tftpServer = imageURI.substr(0, separator); | 
|  | 483 | std::string fwFile = imageURI.substr(separator + 1); | 
|  | 484 | BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile; | 
| Jayashankar Padath | 274dfe6 | 2019-08-23 12:30:57 +0530 | [diff] [blame] | 485 |  | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 486 | // Setup callback for when new software detected | 
|  | 487 | // Give TFTP 10 minutes to complete | 
|  | 488 | monitorForSoftwareAvailable( | 
|  | 489 | asyncResp, req, | 
|  | 490 | "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate", | 
|  | 491 | 600); | 
|  | 492 |  | 
|  | 493 | // TFTP can take up to 10 minutes depending on image size and | 
|  | 494 | // connection speed. Return to caller as soon as the TFTP operation | 
|  | 495 | // has been started. The callback above will ensure the activate | 
|  | 496 | // is started once the download has completed | 
|  | 497 | redfish::messages::success(asyncResp->res); | 
|  | 498 |  | 
|  | 499 | // Call TFTP service | 
|  | 500 | crow::connections::systemBus->async_method_call( | 
|  | 501 | [](const boost::system::error_code ec) { | 
|  | 502 | if (ec) | 
| Jayashankar Padath | fa1a5a3 | 2019-05-28 23:54:37 +0530 | [diff] [blame] | 503 | { | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 504 | // messages::internalError(asyncResp->res); | 
|  | 505 | cleanUp(); | 
|  | 506 | BMCWEB_LOG_DEBUG << "error_code = " << ec; | 
|  | 507 | BMCWEB_LOG_DEBUG << "error msg = " << ec.message(); | 
| Jayashankar Padath | 274dfe6 | 2019-08-23 12:30:57 +0530 | [diff] [blame] | 508 | } | 
|  | 509 | else | 
|  | 510 | { | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 511 | BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success"; | 
|  | 512 | } | 
|  | 513 | }, | 
|  | 514 | "xyz.openbmc_project.Software.Download", | 
|  | 515 | "/xyz/openbmc_project/software", | 
|  | 516 | "xyz.openbmc_project.Common.TFTP", "DownloadViaTFTP", fwFile, | 
|  | 517 | tftpServer); | 
|  | 518 |  | 
|  | 519 | BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost"; | 
|  | 520 | }); | 
|  | 521 | } | 
|  | 522 |  | 
|  | 523 | inline void requestRoutesUpdateService(App& app) | 
|  | 524 | { | 
|  | 525 | BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 526 | .privileges(redfish::privileges::getUpdateService) | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 527 | .methods( | 
|  | 528 | boost::beast::http::verb:: | 
|  | 529 | get)([](const crow::Request&, | 
|  | 530 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { | 
|  | 531 | asyncResp->res.jsonValue["@odata.type"] = | 
| Chicago Duan | 0588a3b | 2021-06-10 18:20:36 +0800 | [diff] [blame] | 532 | "#UpdateService.v1_5_0.UpdateService"; | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 533 | asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService"; | 
|  | 534 | asyncResp->res.jsonValue["Id"] = "UpdateService"; | 
|  | 535 | asyncResp->res.jsonValue["Description"] = | 
|  | 536 | "Service for Software Update"; | 
|  | 537 | asyncResp->res.jsonValue["Name"] = "Update Service"; | 
|  | 538 | asyncResp->res.jsonValue["HttpPushUri"] = | 
|  | 539 | "/redfish/v1/UpdateService"; | 
|  | 540 | // UpdateService cannot be disabled | 
|  | 541 | asyncResp->res.jsonValue["ServiceEnabled"] = true; | 
|  | 542 | asyncResp->res.jsonValue["FirmwareInventory"] = { | 
|  | 543 | {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}}; | 
| Tejas Patil | d61e519 | 2021-06-04 15:49:35 +0530 | [diff] [blame] | 544 | // Get the MaxImageSizeBytes | 
|  | 545 | asyncResp->res.jsonValue["MaxImageSizeBytes"] = | 
|  | 546 | bmcwebHttpReqBodyLimitMb * 1024 * 1024; | 
|  | 547 |  | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 548 | #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE | 
|  | 549 | // Update Actions object. | 
|  | 550 | nlohmann::json& updateSvcSimpleUpdate = | 
|  | 551 | asyncResp->res | 
|  | 552 | .jsonValue["Actions"]["#UpdateService.SimpleUpdate"]; | 
|  | 553 | updateSvcSimpleUpdate["target"] = | 
|  | 554 | "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate"; | 
|  | 555 | updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = | 
|  | 556 | {"TFTP"}; | 
|  | 557 | #endif | 
|  | 558 | // Get the current ApplyTime value | 
| Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 559 | sdbusplus::asio::getProperty<std::string>( | 
|  | 560 | *crow::connections::systemBus, "xyz.openbmc_project.Settings", | 
|  | 561 | "/xyz/openbmc_project/software/apply_time", | 
|  | 562 | "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime", | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 563 | [asyncResp](const boost::system::error_code ec, | 
| Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 564 | const std::string& applyTime) { | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 565 | if (ec) | 
|  | 566 | { | 
|  | 567 | BMCWEB_LOG_DEBUG << "DBUS response error " << ec; | 
|  | 568 | messages::internalError(asyncResp->res); | 
| Jayashankar Padath | fa1a5a3 | 2019-05-28 23:54:37 +0530 | [diff] [blame] | 569 | return; | 
|  | 570 | } | 
| Jayashankar Padath | 274dfe6 | 2019-08-23 12:30:57 +0530 | [diff] [blame] | 571 |  | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 572 | // Store the ApplyTime Value | 
| Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 573 | if (applyTime == "xyz.openbmc_project.Software.ApplyTime." | 
|  | 574 | "RequestedApplyTimes.Immediate") | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 575 | { | 
|  | 576 | asyncResp->res | 
|  | 577 | .jsonValue["HttpPushUriOptions"] | 
|  | 578 | ["HttpPushUriApplyTime"]["ApplyTime"] = | 
|  | 579 | "Immediate"; | 
|  | 580 | } | 
| Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 581 | else if (applyTime == | 
|  | 582 | "xyz.openbmc_project.Software.ApplyTime." | 
|  | 583 | "RequestedApplyTimes.OnReset") | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 584 | { | 
|  | 585 | asyncResp->res | 
|  | 586 | .jsonValue["HttpPushUriOptions"] | 
|  | 587 | ["HttpPushUriApplyTime"]["ApplyTime"] = | 
|  | 588 | "OnReset"; | 
|  | 589 | } | 
| Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 590 | }); | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 591 | }); | 
|  | 592 | BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 593 | .privileges(redfish::privileges::patchUpdateService) | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 594 | .methods( | 
|  | 595 | boost::beast::http::verb:: | 
|  | 596 | patch)([](const crow::Request& req, | 
|  | 597 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { | 
|  | 598 | BMCWEB_LOG_DEBUG << "doPatch..."; | 
| Jayashankar Padath | fa1a5a3 | 2019-05-28 23:54:37 +0530 | [diff] [blame] | 599 |  | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 600 | std::optional<nlohmann::json> pushUriOptions; | 
|  | 601 | if (!json_util::readJson(req, asyncResp->res, "HttpPushUriOptions", | 
|  | 602 | pushUriOptions)) | 
|  | 603 | { | 
|  | 604 | return; | 
|  | 605 | } | 
|  | 606 |  | 
|  | 607 | if (pushUriOptions) | 
|  | 608 | { | 
|  | 609 | std::optional<nlohmann::json> pushUriApplyTime; | 
|  | 610 | if (!json_util::readJson(*pushUriOptions, asyncResp->res, | 
|  | 611 | "HttpPushUriApplyTime", | 
|  | 612 | pushUriApplyTime)) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 613 | { | 
| Ed Tanous | c711bf8 | 2018-07-30 16:31:33 -0700 | [diff] [blame] | 614 | return; | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 615 | } | 
| Jennifer Lee | 6c4eb9d | 2018-05-22 10:58:31 -0700 | [diff] [blame] | 616 |  | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 617 | if (pushUriApplyTime) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 618 | { | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 619 | std::optional<std::string> applyTime; | 
|  | 620 | if (!json_util::readJson(*pushUriApplyTime, asyncResp->res, | 
|  | 621 | "ApplyTime", applyTime)) | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 622 | { | 
|  | 623 | return; | 
|  | 624 | } | 
|  | 625 |  | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 626 | if (applyTime) | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 627 | { | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 628 | std::string applyTimeNewVal; | 
|  | 629 | if (applyTime == "Immediate") | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 630 | { | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 631 | applyTimeNewVal = | 
|  | 632 | "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate"; | 
|  | 633 | } | 
|  | 634 | else if (applyTime == "OnReset") | 
|  | 635 | { | 
|  | 636 | applyTimeNewVal = | 
|  | 637 | "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset"; | 
|  | 638 | } | 
|  | 639 | else | 
|  | 640 | { | 
|  | 641 | BMCWEB_LOG_INFO | 
|  | 642 | << "ApplyTime value is not in the list of acceptable values"; | 
|  | 643 | messages::propertyValueNotInList( | 
|  | 644 | asyncResp->res, *applyTime, "ApplyTime"); | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 645 | return; | 
|  | 646 | } | 
|  | 647 |  | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 648 | // Set the requested image apply time value | 
|  | 649 | crow::connections::systemBus->async_method_call( | 
|  | 650 | [asyncResp](const boost::system::error_code ec) { | 
|  | 651 | if (ec) | 
|  | 652 | { | 
|  | 653 | BMCWEB_LOG_ERROR | 
|  | 654 | << "D-Bus responses error: " << ec; | 
|  | 655 | messages::internalError(asyncResp->res); | 
|  | 656 | return; | 
|  | 657 | } | 
|  | 658 | messages::success(asyncResp->res); | 
|  | 659 | }, | 
|  | 660 | "xyz.openbmc_project.Settings", | 
|  | 661 | "/xyz/openbmc_project/software/apply_time", | 
|  | 662 | "org.freedesktop.DBus.Properties", "Set", | 
|  | 663 | "xyz.openbmc_project.Software.ApplyTime", | 
|  | 664 | "RequestedApplyTime", | 
| Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 665 | dbus::utility::DbusVariantType{applyTimeNewVal}); | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 666 | } | 
|  | 667 | } | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 668 | } | 
|  | 669 | }); | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 670 | BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 671 | .privileges(redfish::privileges::postUpdateService) | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 672 | .methods(boost::beast::http::verb::post)( | 
|  | 673 | [](const crow::Request& req, | 
|  | 674 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { | 
|  | 675 | BMCWEB_LOG_DEBUG << "doPost..."; | 
|  | 676 |  | 
|  | 677 | // Setup callback for when new software detected | 
|  | 678 | monitorForSoftwareAvailable(asyncResp, req, | 
|  | 679 | "/redfish/v1/UpdateService"); | 
|  | 680 |  | 
|  | 681 | std::string filepath("/tmp/images/" + | 
|  | 682 | boost::uuids::to_string( | 
|  | 683 | boost::uuids::random_generator()())); | 
|  | 684 | BMCWEB_LOG_DEBUG << "Writing file to " << filepath; | 
|  | 685 | std::ofstream out(filepath, std::ofstream::out | | 
|  | 686 | std::ofstream::binary | | 
|  | 687 | std::ofstream::trunc); | 
|  | 688 | out << req.body; | 
|  | 689 | out.close(); | 
|  | 690 | BMCWEB_LOG_DEBUG << "file upload complete!!"; | 
|  | 691 | }); | 
|  | 692 | } | 
|  | 693 |  | 
|  | 694 | inline void requestRoutesSoftwareInventoryCollection(App& app) | 
|  | 695 | { | 
|  | 696 | BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/") | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 697 | .privileges(redfish::privileges::getSoftwareInventoryCollection) | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 698 | .methods( | 
|  | 699 | boost::beast::http::verb:: | 
|  | 700 | get)([](const crow::Request&, | 
|  | 701 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { | 
|  | 702 | asyncResp->res.jsonValue["@odata.type"] = | 
|  | 703 | "#SoftwareInventoryCollection.SoftwareInventoryCollection"; | 
|  | 704 | asyncResp->res.jsonValue["@odata.id"] = | 
|  | 705 | "/redfish/v1/UpdateService/FirmwareInventory"; | 
|  | 706 | asyncResp->res.jsonValue["Name"] = "Software Inventory Collection"; | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 707 |  | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 708 | crow::connections::systemBus->async_method_call( | 
|  | 709 | [asyncResp]( | 
|  | 710 | const boost::system::error_code ec, | 
|  | 711 | const std::vector< | 
|  | 712 | std::pair<std::string, | 
|  | 713 | std::vector<std::pair< | 
|  | 714 | std::string, std::vector<std::string>>>>>& | 
|  | 715 | subtree) { | 
|  | 716 | if (ec) | 
|  | 717 | { | 
|  | 718 | messages::internalError(asyncResp->res); | 
|  | 719 | return; | 
|  | 720 | } | 
|  | 721 | asyncResp->res.jsonValue["Members"] = | 
|  | 722 | nlohmann::json::array(); | 
|  | 723 | asyncResp->res.jsonValue["Members@odata.count"] = 0; | 
|  | 724 |  | 
|  | 725 | for (auto& obj : subtree) | 
|  | 726 | { | 
|  | 727 | sdbusplus::message::object_path path(obj.first); | 
|  | 728 | std::string swId = path.filename(); | 
|  | 729 | if (swId.empty()) | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 730 | { | 
|  | 731 | messages::internalError(asyncResp->res); | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 732 | BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!"; | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 733 | return; | 
|  | 734 | } | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 735 |  | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 736 | nlohmann::json& members = | 
|  | 737 | asyncResp->res.jsonValue["Members"]; | 
|  | 738 | members.push_back( | 
|  | 739 | {{"@odata.id", | 
|  | 740 | "/redfish/v1/UpdateService/FirmwareInventory/" + | 
|  | 741 | swId}}); | 
|  | 742 | asyncResp->res.jsonValue["Members@odata.count"] = | 
|  | 743 | members.size(); | 
|  | 744 | } | 
|  | 745 | }, | 
|  | 746 | // Note that only firmware levels associated with a device | 
|  | 747 | // are stored under /xyz/openbmc_project/software therefore | 
|  | 748 | // to ensure only real FirmwareInventory items are returned, | 
|  | 749 | // this full object path must be used here as input to | 
|  | 750 | // mapper | 
|  | 751 | "xyz.openbmc_project.ObjectMapper", | 
|  | 752 | "/xyz/openbmc_project/object_mapper", | 
|  | 753 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", | 
|  | 754 | "/xyz/openbmc_project/software", static_cast<int32_t>(0), | 
|  | 755 | std::array<const char*, 1>{ | 
|  | 756 | "xyz.openbmc_project.Software.Version"}); | 
|  | 757 | }); | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 758 | } | 
|  | 759 | /* Fill related item links (i.e. bmc, bios) in for inventory */ | 
|  | 760 | inline static void | 
|  | 761 | getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& aResp, | 
|  | 762 | const std::string& purpose) | 
|  | 763 | { | 
|  | 764 | if (purpose == fw_util::bmcPurpose) | 
|  | 765 | { | 
|  | 766 | nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"]; | 
|  | 767 | relatedItem.push_back({{"@odata.id", "/redfish/v1/Managers/bmc"}}); | 
|  | 768 | aResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size(); | 
|  | 769 | } | 
|  | 770 | else if (purpose == fw_util::biosPurpose) | 
|  | 771 | { | 
|  | 772 | nlohmann::json& relatedItem = aResp->res.jsonValue["RelatedItem"]; | 
|  | 773 | relatedItem.push_back( | 
|  | 774 | {{"@odata.id", "/redfish/v1/Systems/system/Bios"}}); | 
|  | 775 | aResp->res.jsonValue["Members@odata.count"] = relatedItem.size(); | 
|  | 776 | } | 
|  | 777 | else | 
|  | 778 | { | 
|  | 779 | BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose; | 
|  | 780 | } | 
|  | 781 | } | 
|  | 782 |  | 
|  | 783 | inline void requestRoutesSoftwareInventory(App& app) | 
|  | 784 | { | 
|  | 785 | BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/") | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 786 | .privileges(redfish::privileges::getSoftwareInventory) | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 787 | .methods( | 
|  | 788 | boost::beast::http::verb::get)([](const crow::Request&, | 
|  | 789 | const std::shared_ptr< | 
|  | 790 | bmcweb::AsyncResp>& asyncResp, | 
|  | 791 | const std::string& param) { | 
|  | 792 | std::shared_ptr<std::string> swId = | 
|  | 793 | std::make_shared<std::string>(param); | 
|  | 794 |  | 
|  | 795 | asyncResp->res.jsonValue["@odata.id"] = | 
|  | 796 | "/redfish/v1/UpdateService/FirmwareInventory/" + *swId; | 
|  | 797 |  | 
|  | 798 | crow::connections::systemBus->async_method_call( | 
|  | 799 | [asyncResp, swId]( | 
|  | 800 | const boost::system::error_code ec, | 
|  | 801 | const std::vector< | 
|  | 802 | std::pair<std::string, | 
|  | 803 | std::vector<std::pair< | 
|  | 804 | std::string, std::vector<std::string>>>>>& | 
|  | 805 | subtree) { | 
|  | 806 | BMCWEB_LOG_DEBUG << "doGet callback..."; | 
|  | 807 | if (ec) | 
| Jennifer Lee | f4b65ab | 2018-09-18 12:00:13 -0700 | [diff] [blame] | 808 | { | 
| Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 809 | messages::internalError(asyncResp->res); | 
| Jennifer Lee | f4b65ab | 2018-09-18 12:00:13 -0700 | [diff] [blame] | 810 | return; | 
|  | 811 | } | 
| Jennifer Lee | f4b65ab | 2018-09-18 12:00:13 -0700 | [diff] [blame] | 812 |  | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 813 | // Ensure we find our input swId, otherwise return an error | 
|  | 814 | bool found = false; | 
|  | 815 | for (const std::pair< | 
|  | 816 | std::string, | 
|  | 817 | std::vector<std::pair< | 
|  | 818 | std::string, std::vector<std::string>>>>& obj : | 
|  | 819 | subtree) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 820 | { | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 821 | if (boost::ends_with(obj.first, *swId) != true) | 
|  | 822 | { | 
|  | 823 | continue; | 
|  | 824 | } | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 825 |  | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 826 | if (obj.second.size() < 1) | 
|  | 827 | { | 
|  | 828 | continue; | 
|  | 829 | } | 
|  | 830 |  | 
|  | 831 | found = true; | 
|  | 832 | fw_util::getFwStatus(asyncResp, swId, | 
|  | 833 | obj.second[0].first); | 
|  | 834 |  | 
|  | 835 | crow::connections::systemBus->async_method_call( | 
| Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 836 | [asyncResp, | 
|  | 837 | swId](const boost::system::error_code errorCode, | 
|  | 838 | const boost::container::flat_map< | 
|  | 839 | std::string, | 
|  | 840 | dbus::utility::DbusVariantType>& | 
|  | 841 | propertiesList) { | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 842 | if (errorCode) | 
|  | 843 | { | 
|  | 844 | messages::internalError(asyncResp->res); | 
|  | 845 | return; | 
|  | 846 | } | 
|  | 847 | boost::container::flat_map< | 
| Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 848 | std::string, | 
|  | 849 | dbus::utility::DbusVariantType>:: | 
|  | 850 | const_iterator it = | 
|  | 851 | propertiesList.find("Purpose"); | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 852 | if (it == propertiesList.end()) | 
|  | 853 | { | 
|  | 854 | BMCWEB_LOG_DEBUG | 
|  | 855 | << "Can't find property \"Purpose\"!"; | 
|  | 856 | messages::propertyMissing(asyncResp->res, | 
|  | 857 | "Purpose"); | 
|  | 858 | return; | 
|  | 859 | } | 
|  | 860 | const std::string* swInvPurpose = | 
|  | 861 | std::get_if<std::string>(&it->second); | 
|  | 862 | if (swInvPurpose == nullptr) | 
|  | 863 | { | 
| George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 864 | BMCWEB_LOG_DEBUG | 
|  | 865 | << "wrong types for property\"Purpose\"!"; | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 866 | messages::propertyValueTypeError( | 
|  | 867 | asyncResp->res, "", "Purpose"); | 
|  | 868 | return; | 
|  | 869 | } | 
|  | 870 |  | 
|  | 871 | BMCWEB_LOG_DEBUG << "swInvPurpose = " | 
|  | 872 | << *swInvPurpose; | 
|  | 873 | it = propertiesList.find("Version"); | 
|  | 874 | if (it == propertiesList.end()) | 
|  | 875 | { | 
|  | 876 | BMCWEB_LOG_DEBUG | 
|  | 877 | << "Can't find property \"Version\"!"; | 
|  | 878 | messages::propertyMissing(asyncResp->res, | 
|  | 879 | "Version"); | 
|  | 880 | return; | 
|  | 881 | } | 
|  | 882 |  | 
|  | 883 | BMCWEB_LOG_DEBUG << "Version found!"; | 
|  | 884 |  | 
|  | 885 | const std::string* version = | 
|  | 886 | std::get_if<std::string>(&it->second); | 
|  | 887 |  | 
|  | 888 | if (version == nullptr) | 
|  | 889 | { | 
|  | 890 | BMCWEB_LOG_DEBUG | 
|  | 891 | << "Can't find property \"Version\"!"; | 
|  | 892 |  | 
|  | 893 | messages::propertyValueTypeError( | 
|  | 894 | asyncResp->res, "", "Version"); | 
|  | 895 | return; | 
|  | 896 | } | 
|  | 897 | asyncResp->res.jsonValue["Version"] = *version; | 
|  | 898 | asyncResp->res.jsonValue["Id"] = *swId; | 
|  | 899 |  | 
|  | 900 | // swInvPurpose is of format: | 
|  | 901 | // xyz.openbmc_project.Software.Version.VersionPurpose.ABC | 
|  | 902 | // Translate this to "ABC image" | 
|  | 903 | size_t endDesc = swInvPurpose->rfind('.'); | 
|  | 904 | if (endDesc == std::string::npos) | 
|  | 905 | { | 
|  | 906 | messages::internalError(asyncResp->res); | 
|  | 907 | return; | 
|  | 908 | } | 
|  | 909 | endDesc++; | 
|  | 910 | if (endDesc >= swInvPurpose->size()) | 
|  | 911 | { | 
|  | 912 | messages::internalError(asyncResp->res); | 
|  | 913 | return; | 
|  | 914 | } | 
|  | 915 |  | 
|  | 916 | std::string formatDesc = | 
|  | 917 | swInvPurpose->substr(endDesc); | 
|  | 918 | asyncResp->res.jsonValue["Description"] = | 
|  | 919 | formatDesc + " image"; | 
|  | 920 | getRelatedItems(asyncResp, *swInvPurpose); | 
|  | 921 | }, | 
|  | 922 | obj.second[0].first, obj.first, | 
|  | 923 | "org.freedesktop.DBus.Properties", "GetAll", | 
|  | 924 | "xyz.openbmc_project.Software.Version"); | 
|  | 925 | } | 
|  | 926 | if (!found) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 927 | { | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 928 | BMCWEB_LOG_ERROR | 
|  | 929 | << "Input swID " + *swId + " not found!"; | 
|  | 930 | messages::resourceMissingAtURI( | 
|  | 931 | asyncResp->res, | 
|  | 932 | "/redfish/v1/UpdateService/FirmwareInventory/" + | 
|  | 933 | *swId); | 
|  | 934 | return; | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 935 | } | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 936 | asyncResp->res.jsonValue["@odata.type"] = | 
|  | 937 | "#SoftwareInventory.v1_1_0.SoftwareInventory"; | 
|  | 938 | asyncResp->res.jsonValue["Name"] = "Software Inventory"; | 
|  | 939 | asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK"; | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 940 |  | 
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 941 | asyncResp->res.jsonValue["Updateable"] = false; | 
|  | 942 | fw_util::getFwUpdateableStatus(asyncResp, swId); | 
|  | 943 | }, | 
|  | 944 | "xyz.openbmc_project.ObjectMapper", | 
|  | 945 | "/xyz/openbmc_project/object_mapper", | 
|  | 946 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", | 
|  | 947 | static_cast<int32_t>(0), | 
|  | 948 | std::array<const char*, 1>{ | 
|  | 949 | "xyz.openbmc_project.Software.Version"}); | 
|  | 950 | }); | 
|  | 951 | } | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 952 |  | 
|  | 953 | } // namespace redfish |