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