| Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
| 3 | // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation |
| Jennifer Lee | 729dae7 | 2018-04-24 15:59:34 -0700 | [diff] [blame] | 4 | #pragma once |
| 5 | |
| Tejas Patil | d61e519 | 2021-06-04 15:49:35 +0530 | [diff] [blame] | 6 | #include "bmcweb_config.h" |
| 7 | |
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 8 | #include "app.hpp" |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 9 | #include "async_resp.hpp" |
| 10 | #include "dbus_singleton.hpp" |
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 11 | #include "dbus_utility.hpp" |
| Ed Tanous | 5b90429 | 2024-04-16 11:10:17 -0700 | [diff] [blame] | 12 | #include "error_messages.hpp" |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 13 | #include "generated/enums/resource.hpp" |
| Ed Tanous | 757178a | 2024-04-03 14:32:38 -0700 | [diff] [blame] | 14 | #include "generated/enums/update_service.hpp" |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 15 | #include "http_request.hpp" |
| 16 | #include "http_response.hpp" |
| Ed Tanous | d98a2f9 | 2025-02-06 17:36:31 -0800 | [diff] [blame] | 17 | #include "io_context_singleton.hpp" |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 18 | #include "logging.hpp" |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 19 | #include "multipart_parser.hpp" |
| Ed Tanous | 2c6ffdb | 2023-06-28 11:28:38 -0700 | [diff] [blame] | 20 | #include "ossl_random.hpp" |
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 21 | #include "query.hpp" |
| 22 | #include "registries/privilege_registry.hpp" |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 23 | #include "str_utility.hpp" |
| Ed Tanous | a8e884f | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 24 | #include "task.hpp" |
| Ed Tanous | 5b90429 | 2024-04-16 11:10:17 -0700 | [diff] [blame] | 25 | #include "task_messages.hpp" |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 26 | #include "utility.hpp" |
| John Edward Broadbent | 08d81ad | 2022-05-17 20:00:23 -0700 | [diff] [blame] | 27 | #include "utils/collection.hpp" |
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 28 | #include "utils/dbus_utils.hpp" |
| Ed Tanous | 5b90429 | 2024-04-16 11:10:17 -0700 | [diff] [blame] | 29 | #include "utils/json_utils.hpp" |
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 30 | #include "utils/sw_utils.hpp" |
| 31 | |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 32 | #include <sys/mman.h> |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 33 | #include <unistd.h> |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 34 | |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 35 | #include <boost/asio/error.hpp> |
| 36 | #include <boost/asio/steady_timer.hpp> |
| 37 | #include <boost/beast/http/fields.hpp> |
| 38 | #include <boost/beast/http/status.hpp> |
| 39 | #include <boost/beast/http/verb.hpp> |
| George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 40 | #include <boost/system/error_code.hpp> |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 41 | #include <boost/system/result.hpp> |
| Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 42 | #include <boost/url/format.hpp> |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 43 | #include <boost/url/parse.hpp> |
| 44 | #include <boost/url/url.hpp> |
| 45 | #include <boost/url/url_view.hpp> |
| 46 | #include <boost/url/url_view_base.hpp> |
| Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 47 | #include <sdbusplus/asio/property.hpp> |
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 48 | #include <sdbusplus/bus/match.hpp> |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 49 | #include <sdbusplus/message.hpp> |
| 50 | #include <sdbusplus/message/native_types.hpp> |
| Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 51 | #include <sdbusplus/unpack_properties.hpp> |
| Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 52 | |
| George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 53 | #include <array> |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 54 | #include <chrono> |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 55 | #include <cstddef> |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 56 | #include <cstdint> |
| 57 | #include <cstdio> |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 58 | #include <filesystem> |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 59 | #include <format> |
| 60 | #include <fstream> |
| Jagpal Singh Gill | c71b6c9 | 2024-04-29 16:50:53 -0700 | [diff] [blame] | 61 | #include <functional> |
| Jagpal Singh Gill | ef93eab | 2024-04-17 16:06:14 -0700 | [diff] [blame] | 62 | #include <memory> |
| Ed Tanous | 7cb59f6 | 2022-05-05 11:48:31 -0700 | [diff] [blame] | 63 | #include <optional> |
| 64 | #include <string> |
| George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 65 | #include <string_view> |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 66 | #include <unordered_map> |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 67 | #include <utility> |
| 68 | #include <variant> |
| Jagpal Singh Gill | ef93eab | 2024-04-17 16:06:14 -0700 | [diff] [blame] | 69 | #include <vector> |
| George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 70 | |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 71 | namespace redfish |
| 72 | { |
| Ed Tanous | 27826b5 | 2018-10-29 11:40:58 -0700 | [diff] [blame] | 73 | |
| Andrew Geissler | 0e7de46 | 2019-03-04 19:11:54 -0600 | [diff] [blame] | 74 | // Match signals added on software path |
| Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 75 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 76 | static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateMatcher; |
| Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 77 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 78 | static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateErrorMatcher; |
| Andrew Geissler | 0e7de46 | 2019-03-04 19:11:54 -0600 | [diff] [blame] | 79 | // Only allow one update at a time |
| Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 80 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| Andrew Geissler | 0e7de46 | 2019-03-04 19:11:54 -0600 | [diff] [blame] | 81 | static bool fwUpdateInProgress = false; |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 82 | // Timer for software available |
| Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 83 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 84 | static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer; |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 85 | |
| rajeeranjan | 9fbf853 | 2025-07-23 23:25:52 +0530 | [diff] [blame^] | 86 | /* @brief String that indicates the Software Update D-Bus interface */ |
| 87 | constexpr const char* updateInterface = "xyz.openbmc_project.Software.Update"; |
| 88 | |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 89 | struct MemoryFileDescriptor |
| 90 | { |
| 91 | int fd = -1; |
| 92 | |
| 93 | explicit MemoryFileDescriptor(const std::string& filename) : |
| 94 | fd(memfd_create(filename.c_str(), 0)) |
| 95 | {} |
| 96 | |
| 97 | MemoryFileDescriptor(const MemoryFileDescriptor&) = default; |
| 98 | MemoryFileDescriptor(MemoryFileDescriptor&& other) noexcept : fd(other.fd) |
| 99 | { |
| 100 | other.fd = -1; |
| 101 | } |
| 102 | MemoryFileDescriptor& operator=(const MemoryFileDescriptor&) = delete; |
| 103 | MemoryFileDescriptor& operator=(MemoryFileDescriptor&&) = default; |
| 104 | |
| 105 | ~MemoryFileDescriptor() |
| 106 | { |
| 107 | if (fd != -1) |
| 108 | { |
| 109 | close(fd); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | bool rewind() const |
| 114 | { |
| 115 | if (lseek(fd, 0, SEEK_SET) == -1) |
| 116 | { |
| 117 | BMCWEB_LOG_ERROR("Failed to seek to beginning of image memfd"); |
| 118 | return false; |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | }; |
| 123 | |
| Ed Tanous | df254f2 | 2024-04-01 13:25:46 -0700 | [diff] [blame] | 124 | inline void cleanUp() |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 125 | { |
| 126 | fwUpdateInProgress = false; |
| 127 | fwUpdateMatcher = nullptr; |
| James Feist | 4cde5d9 | 2020-06-11 10:39:55 -0700 | [diff] [blame] | 128 | fwUpdateErrorMatcher = nullptr; |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 129 | } |
| Ed Tanous | df254f2 | 2024-04-01 13:25:46 -0700 | [diff] [blame] | 130 | |
| 131 | inline void activateImage(const std::string& objPath, |
| 132 | const std::string& service) |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 133 | { |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 134 | BMCWEB_LOG_DEBUG("Activate image for {} {}", objPath, service); |
| George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 135 | sdbusplus::asio::setProperty( |
| 136 | *crow::connections::systemBus, service, objPath, |
| 137 | "xyz.openbmc_project.Software.Activation", "RequestedActivation", |
| 138 | "xyz.openbmc_project.Software.Activation.RequestedActivations.Active", |
| Ed Tanous | 8b24275 | 2023-06-27 17:17:13 -0700 | [diff] [blame] | 139 | [](const boost::system::error_code& ec) { |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 140 | if (ec) |
| 141 | { |
| 142 | BMCWEB_LOG_DEBUG("error_code = {}", ec); |
| 143 | BMCWEB_LOG_DEBUG("error msg = {}", ec.message()); |
| 144 | } |
| 145 | }); |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 146 | } |
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 147 | |
| Jagpal Singh Gill | c71b6c9 | 2024-04-29 16:50:53 -0700 | [diff] [blame] | 148 | inline bool handleCreateTask(const boost::system::error_code& ec2, |
| 149 | sdbusplus::message_t& msg, |
| 150 | const std::shared_ptr<task::TaskData>& taskData) |
| 151 | { |
| 152 | if (ec2) |
| 153 | { |
| 154 | return task::completed; |
| 155 | } |
| 156 | |
| 157 | std::string iface; |
| 158 | dbus::utility::DBusPropertiesMap values; |
| 159 | |
| 160 | std::string index = std::to_string(taskData->index); |
| 161 | msg.read(iface, values); |
| 162 | |
| 163 | if (iface == "xyz.openbmc_project.Software.Activation") |
| 164 | { |
| 165 | const std::string* state = nullptr; |
| 166 | for (const auto& property : values) |
| 167 | { |
| 168 | if (property.first == "Activation") |
| 169 | { |
| 170 | state = std::get_if<std::string>(&property.second); |
| 171 | if (state == nullptr) |
| 172 | { |
| 173 | taskData->messages.emplace_back(messages::internalError()); |
| 174 | return task::completed; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (state == nullptr) |
| 180 | { |
| 181 | return !task::completed; |
| 182 | } |
| 183 | |
| 184 | if (state->ends_with("Invalid") || state->ends_with("Failed")) |
| 185 | { |
| 186 | taskData->state = "Exception"; |
| 187 | taskData->status = "Warning"; |
| 188 | taskData->messages.emplace_back(messages::taskAborted(index)); |
| 189 | return task::completed; |
| 190 | } |
| 191 | |
| 192 | if (state->ends_with("Staged")) |
| 193 | { |
| 194 | taskData->state = "Stopping"; |
| 195 | taskData->messages.emplace_back(messages::taskPaused(index)); |
| 196 | |
| 197 | // its staged, set a long timer to |
| 198 | // allow them time to complete the |
| 199 | // update (probably cycle the |
| 200 | // system) if this expires then |
| 201 | // task will be canceled |
| 202 | taskData->extendTimer(std::chrono::hours(5)); |
| 203 | return !task::completed; |
| 204 | } |
| 205 | |
| 206 | if (state->ends_with("Active")) |
| 207 | { |
| 208 | taskData->messages.emplace_back(messages::taskCompletedOK(index)); |
| 209 | taskData->state = "Completed"; |
| 210 | return task::completed; |
| 211 | } |
| 212 | } |
| 213 | else if (iface == "xyz.openbmc_project.Software.ActivationProgress") |
| 214 | { |
| 215 | const uint8_t* progress = nullptr; |
| 216 | for (const auto& property : values) |
| 217 | { |
| 218 | if (property.first == "Progress") |
| 219 | { |
| 220 | progress = std::get_if<uint8_t>(&property.second); |
| 221 | if (progress == nullptr) |
| 222 | { |
| 223 | taskData->messages.emplace_back(messages::internalError()); |
| 224 | return task::completed; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if (progress == nullptr) |
| 230 | { |
| 231 | return !task::completed; |
| 232 | } |
| 233 | taskData->percentComplete = *progress; |
| 234 | taskData->messages.emplace_back( |
| 235 | messages::taskProgressChanged(index, *progress)); |
| 236 | |
| 237 | // if we're getting status updates it's |
| 238 | // still alive, update timer |
| 239 | taskData->extendTimer(std::chrono::minutes(5)); |
| 240 | } |
| 241 | |
| 242 | // as firmware update often results in a |
| 243 | // reboot, the task may never "complete" |
| 244 | // unless it is an error |
| 245 | |
| 246 | return !task::completed; |
| 247 | } |
| 248 | |
| 249 | inline void createTask(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 250 | task::Payload&& payload, |
| 251 | const sdbusplus::message::object_path& objPath) |
| 252 | { |
| 253 | std::shared_ptr<task::TaskData> task = task::TaskData::createTask( |
| 254 | std::bind_front(handleCreateTask), |
| 255 | "type='signal',interface='org.freedesktop.DBus.Properties'," |
| 256 | "member='PropertiesChanged',path='" + |
| 257 | objPath.str + "'"); |
| 258 | task->startTimer(std::chrono::minutes(5)); |
| Jagpal Singh Gill | c71b6c9 | 2024-04-29 16:50:53 -0700 | [diff] [blame] | 259 | task->payload.emplace(std::move(payload)); |
| Chinmay Shripad Hegde | 29e2bdd | 2025-06-06 16:23:47 +0530 | [diff] [blame] | 260 | task->populateResp(asyncResp->res); |
| Jagpal Singh Gill | c71b6c9 | 2024-04-29 16:50:53 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 263 | // Note that asyncResp can be either a valid pointer or nullptr. If nullptr |
| 264 | // then no asyncResp updates will occur |
| Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 265 | inline void softwareInterfaceAdded( |
| 266 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 267 | sdbusplus::message_t& m, task::Payload&& payload) |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 268 | { |
| Michael Shen | 80f79a4 | 2023-08-24 13:41:53 +0000 | [diff] [blame] | 269 | dbus::utility::DBusInterfacesMap interfacesProperties; |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 270 | |
| 271 | sdbusplus::message::object_path objPath; |
| 272 | |
| 273 | m.read(objPath, interfacesProperties); |
| 274 | |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 275 | BMCWEB_LOG_DEBUG("obj path = {}", objPath.str); |
| Ed Tanous | e3eb3d6 | 2022-12-21 11:56:07 -0800 | [diff] [blame] | 276 | for (const auto& interface : interfacesProperties) |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 277 | { |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 278 | BMCWEB_LOG_DEBUG("interface = {}", interface.first); |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 279 | |
| 280 | if (interface.first == "xyz.openbmc_project.Software.Activation") |
| 281 | { |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 282 | // Retrieve service and activate |
| George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 283 | constexpr std::array<std::string_view, 1> interfaces = { |
| 284 | "xyz.openbmc_project.Software.Activation"}; |
| 285 | dbus::utility::getDbusObject( |
| 286 | objPath.str, interfaces, |
| Ed Tanous | a3e6589 | 2021-09-16 14:13:20 -0700 | [diff] [blame] | 287 | [objPath, asyncResp, payload(std::move(payload))]( |
| Ed Tanous | 8b24275 | 2023-06-27 17:17:13 -0700 | [diff] [blame] | 288 | const boost::system::error_code& ec, |
| Ed Tanous | a3e6589 | 2021-09-16 14:13:20 -0700 | [diff] [blame] | 289 | const std::vector< |
| 290 | std::pair<std::string, std::vector<std::string>>>& |
| 291 | objInfo) mutable { |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 292 | if (ec) |
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 293 | { |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 294 | BMCWEB_LOG_DEBUG("error_code = {}", ec); |
| 295 | BMCWEB_LOG_DEBUG("error msg = {}", ec.message()); |
| 296 | if (asyncResp) |
| 297 | { |
| 298 | messages::internalError(asyncResp->res); |
| 299 | } |
| 300 | cleanUp(); |
| 301 | return; |
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 302 | } |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 303 | // Ensure we only got one service back |
| 304 | if (objInfo.size() != 1) |
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 305 | { |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 306 | BMCWEB_LOG_ERROR("Invalid Object Size {}", |
| 307 | objInfo.size()); |
| 308 | if (asyncResp) |
| 309 | { |
| 310 | messages::internalError(asyncResp->res); |
| 311 | } |
| 312 | cleanUp(); |
| 313 | return; |
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 314 | } |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 315 | // cancel timer only when |
| 316 | // xyz.openbmc_project.Software.Activation interface |
| 317 | // is added |
| 318 | fwAvailableTimer = nullptr; |
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 319 | |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 320 | activateImage(objPath.str, objInfo[0].first); |
| 321 | if (asyncResp) |
| 322 | { |
| 323 | createTask(asyncResp, std::move(payload), objPath); |
| 324 | } |
| 325 | fwUpdateInProgress = false; |
| 326 | }); |
| Patrick Williams | 62bafc0 | 2022-09-08 17:35:35 -0500 | [diff] [blame] | 327 | |
| 328 | break; |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| Myung Bae | 8549b95 | 2023-08-16 15:18:19 -0400 | [diff] [blame] | 333 | inline void afterAvailbleTimerAsyncWait( |
| 334 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 335 | const boost::system::error_code& ec) |
| 336 | { |
| 337 | cleanUp(); |
| 338 | if (ec == boost::asio::error::operation_aborted) |
| 339 | { |
| 340 | // expected, we were canceled before the timer completed. |
| 341 | return; |
| 342 | } |
| 343 | BMCWEB_LOG_ERROR("Timed out waiting for firmware object being created"); |
| 344 | BMCWEB_LOG_ERROR("FW image may has already been uploaded to server"); |
| 345 | if (ec) |
| 346 | { |
| 347 | BMCWEB_LOG_ERROR("Async_wait failed{}", ec); |
| 348 | return; |
| 349 | } |
| 350 | if (asyncResp) |
| 351 | { |
| 352 | redfish::messages::internalError(asyncResp->res); |
| 353 | } |
| 354 | } |
| 355 | |
| Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 356 | inline void handleUpdateErrorType( |
| 357 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& url, |
| 358 | const std::string& type) |
| Myung Bae | 8549b95 | 2023-08-16 15:18:19 -0400 | [diff] [blame] | 359 | { |
| Ed Tanous | c87294a | 2024-11-16 22:17:12 -0800 | [diff] [blame] | 360 | // NOLINTBEGIN(bugprone-branch-clone) |
| Myung Bae | 8549b95 | 2023-08-16 15:18:19 -0400 | [diff] [blame] | 361 | if (type == "xyz.openbmc_project.Software.Image.Error.UnTarFailure") |
| 362 | { |
| Ed Tanous | 48fb20b | 2024-11-17 11:51:13 -0800 | [diff] [blame] | 363 | messages::missingOrMalformedPart(asyncResp->res); |
| Myung Bae | 8549b95 | 2023-08-16 15:18:19 -0400 | [diff] [blame] | 364 | } |
| 365 | else if (type == |
| 366 | "xyz.openbmc_project.Software.Image.Error.ManifestFileFailure") |
| 367 | { |
| Ed Tanous | 48fb20b | 2024-11-17 11:51:13 -0800 | [diff] [blame] | 368 | messages::missingOrMalformedPart(asyncResp->res); |
| Myung Bae | 8549b95 | 2023-08-16 15:18:19 -0400 | [diff] [blame] | 369 | } |
| 370 | else if (type == "xyz.openbmc_project.Software.Image.Error.ImageFailure") |
| 371 | { |
| Ed Tanous | 48fb20b | 2024-11-17 11:51:13 -0800 | [diff] [blame] | 372 | messages::missingOrMalformedPart(asyncResp->res); |
| Myung Bae | 8549b95 | 2023-08-16 15:18:19 -0400 | [diff] [blame] | 373 | } |
| 374 | else if (type == "xyz.openbmc_project.Software.Version.Error.AlreadyExists") |
| 375 | { |
| Ed Tanous | c87294a | 2024-11-16 22:17:12 -0800 | [diff] [blame] | 376 | messages::resourceAlreadyExists(asyncResp->res, "UpdateService", |
| 377 | "Version", "uploaded version"); |
| Myung Bae | 8549b95 | 2023-08-16 15:18:19 -0400 | [diff] [blame] | 378 | } |
| 379 | else if (type == "xyz.openbmc_project.Software.Image.Error.BusyFailure") |
| 380 | { |
| Ed Tanous | 48fb20b | 2024-11-17 11:51:13 -0800 | [diff] [blame] | 381 | messages::serviceTemporarilyUnavailable(asyncResp->res, url); |
| Myung Bae | 8549b95 | 2023-08-16 15:18:19 -0400 | [diff] [blame] | 382 | } |
| Myung Bae | 4034a65 | 2023-08-17 08:47:35 -0400 | [diff] [blame] | 383 | else if (type == "xyz.openbmc_project.Software.Version.Error.Incompatible") |
| Myung Bae | 8549b95 | 2023-08-16 15:18:19 -0400 | [diff] [blame] | 384 | { |
| Ed Tanous | c87294a | 2024-11-16 22:17:12 -0800 | [diff] [blame] | 385 | messages::internalError(asyncResp->res); |
| Myung Bae | 4034a65 | 2023-08-17 08:47:35 -0400 | [diff] [blame] | 386 | } |
| 387 | else if (type == |
| 388 | "xyz.openbmc_project.Software.Version.Error.ExpiredAccessKey") |
| 389 | { |
| Ed Tanous | c87294a | 2024-11-16 22:17:12 -0800 | [diff] [blame] | 390 | messages::internalError(asyncResp->res); |
| Myung Bae | 4034a65 | 2023-08-17 08:47:35 -0400 | [diff] [blame] | 391 | } |
| 392 | else if (type == |
| 393 | "xyz.openbmc_project.Software.Version.Error.InvalidSignature") |
| 394 | { |
| Ed Tanous | 48fb20b | 2024-11-17 11:51:13 -0800 | [diff] [blame] | 395 | messages::missingOrMalformedPart(asyncResp->res); |
| Myung Bae | 4034a65 | 2023-08-17 08:47:35 -0400 | [diff] [blame] | 396 | } |
| 397 | else if (type == |
| 398 | "xyz.openbmc_project.Software.Image.Error.InternalFailure" || |
| 399 | type == "xyz.openbmc_project.Software.Version.Error.HostFile") |
| 400 | { |
| 401 | BMCWEB_LOG_ERROR("Software Image Error type={}", type); |
| Ed Tanous | 48fb20b | 2024-11-17 11:51:13 -0800 | [diff] [blame] | 402 | messages::internalError(asyncResp->res); |
| Myung Bae | 8549b95 | 2023-08-16 15:18:19 -0400 | [diff] [blame] | 403 | } |
| Myung Bae | 4034a65 | 2023-08-17 08:47:35 -0400 | [diff] [blame] | 404 | else |
| 405 | { |
| 406 | // Unrelated error types. Ignored |
| 407 | BMCWEB_LOG_INFO("Non-Software-related Error type={}. Ignored", type); |
| 408 | return; |
| 409 | } |
| Ed Tanous | c87294a | 2024-11-16 22:17:12 -0800 | [diff] [blame] | 410 | // NOLINTEND(bugprone-branch-clone) |
| Myung Bae | 4034a65 | 2023-08-17 08:47:35 -0400 | [diff] [blame] | 411 | // Clear the timer |
| 412 | fwAvailableTimer = nullptr; |
| Myung Bae | 8549b95 | 2023-08-16 15:18:19 -0400 | [diff] [blame] | 413 | } |
| 414 | |
| Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 415 | inline void afterUpdateErrorMatcher( |
| 416 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& url, |
| 417 | sdbusplus::message_t& m) |
| Myung Bae | 8549b95 | 2023-08-16 15:18:19 -0400 | [diff] [blame] | 418 | { |
| Michael Shen | 80f79a4 | 2023-08-24 13:41:53 +0000 | [diff] [blame] | 419 | dbus::utility::DBusInterfacesMap interfacesProperties; |
| Myung Bae | 8549b95 | 2023-08-16 15:18:19 -0400 | [diff] [blame] | 420 | sdbusplus::message::object_path objPath; |
| 421 | m.read(objPath, interfacesProperties); |
| 422 | BMCWEB_LOG_DEBUG("obj path = {}", objPath.str); |
| 423 | for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>& |
| 424 | interface : interfacesProperties) |
| 425 | { |
| 426 | if (interface.first == "xyz.openbmc_project.Logging.Entry") |
| 427 | { |
| 428 | for (const std::pair<std::string, dbus::utility::DbusVariantType>& |
| 429 | value : interface.second) |
| 430 | { |
| 431 | if (value.first != "Message") |
| 432 | { |
| 433 | continue; |
| 434 | } |
| 435 | const std::string* type = |
| 436 | std::get_if<std::string>(&value.second); |
| 437 | if (type == nullptr) |
| 438 | { |
| 439 | // if this was our message, timeout will cover it |
| 440 | return; |
| 441 | } |
| Myung Bae | 8549b95 | 2023-08-16 15:18:19 -0400 | [diff] [blame] | 442 | handleUpdateErrorType(asyncResp, url, *type); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 448 | // Note that asyncResp can be either a valid pointer or nullptr. If nullptr |
| 449 | // then no asyncResp updates will occur |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 450 | inline void monitorForSoftwareAvailable( |
| zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 451 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 452 | const crow::Request& req, const std::string& url, |
| Gunnar Mills | 1940677 | 2025-03-04 14:42:46 -0600 | [diff] [blame] | 453 | int timeoutTimeSeconds = 50) |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 454 | { |
| 455 | // Only allow one FW update at a time |
| Ed Tanous | e05aec5 | 2022-01-25 10:28:56 -0800 | [diff] [blame] | 456 | if (fwUpdateInProgress) |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 457 | { |
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 458 | if (asyncResp) |
| 459 | { |
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 460 | messages::serviceTemporarilyUnavailable(asyncResp->res, "30"); |
| 461 | } |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 462 | return; |
| 463 | } |
| 464 | |
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 465 | fwAvailableTimer = |
| Ed Tanous | d98a2f9 | 2025-02-06 17:36:31 -0800 | [diff] [blame] | 466 | std::make_unique<boost::asio::steady_timer>(getIoContext()); |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 467 | |
| Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 468 | fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds)); |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 469 | |
| 470 | fwAvailableTimer->async_wait( |
| Myung Bae | 8549b95 | 2023-08-16 15:18:19 -0400 | [diff] [blame] | 471 | std::bind_front(afterAvailbleTimerAsyncWait, asyncResp)); |
| 472 | |
| Ed Tanous | a3e6589 | 2021-09-16 14:13:20 -0700 | [diff] [blame] | 473 | task::Payload payload(req); |
| Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 474 | auto callback = [asyncResp, payload](sdbusplus::message_t& m) mutable { |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 475 | BMCWEB_LOG_DEBUG("Match fired"); |
| Ed Tanous | a3e6589 | 2021-09-16 14:13:20 -0700 | [diff] [blame] | 476 | softwareInterfaceAdded(asyncResp, m, std::move(payload)); |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 477 | }; |
| 478 | |
| 479 | fwUpdateInProgress = true; |
| 480 | |
| Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 481 | fwUpdateMatcher = std::make_unique<sdbusplus::bus::match_t>( |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 482 | *crow::connections::systemBus, |
| 483 | "interface='org.freedesktop.DBus.ObjectManager',type='signal'," |
| 484 | "member='InterfacesAdded',path='/xyz/openbmc_project/software'", |
| 485 | callback); |
| James Feist | 4cde5d9 | 2020-06-11 10:39:55 -0700 | [diff] [blame] | 486 | |
| Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 487 | fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match_t>( |
| James Feist | 4cde5d9 | 2020-06-11 10:39:55 -0700 | [diff] [blame] | 488 | *crow::connections::systemBus, |
| Brian Ma | e1cc482 | 2021-12-01 17:05:54 +0800 | [diff] [blame] | 489 | "interface='org.freedesktop.DBus.ObjectManager',type='signal'," |
| 490 | "member='InterfacesAdded'," |
| 491 | "path='/xyz/openbmc_project/logging'", |
| Myung Bae | 8549b95 | 2023-08-16 15:18:19 -0400 | [diff] [blame] | 492 | std::bind_front(afterUpdateErrorMatcher, asyncResp, url)); |
| Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 493 | } |
| Jennifer Lee | 729dae7 | 2018-04-24 15:59:34 -0700 | [diff] [blame] | 494 | |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 495 | inline std::optional<boost::urls::url> parseSimpleUpdateUrl( |
| 496 | std::string imageURI, std::optional<std::string> transferProtocol, |
| 497 | crow::Response& res) |
| Ed Tanous | f86bcc8 | 2023-08-25 09:34:07 -0700 | [diff] [blame] | 498 | { |
| 499 | if (imageURI.find("://") == std::string::npos) |
| 500 | { |
| 501 | if (imageURI.starts_with("/")) |
| 502 | { |
| 503 | messages::actionParameterValueTypeError( |
| 504 | res, imageURI, "ImageURI", "UpdateService.SimpleUpdate"); |
| 505 | return std::nullopt; |
| 506 | } |
| 507 | if (!transferProtocol) |
| 508 | { |
| 509 | messages::actionParameterValueTypeError( |
| 510 | res, imageURI, "ImageURI", "UpdateService.SimpleUpdate"); |
| 511 | return std::nullopt; |
| 512 | } |
| Ed Tanous | 6a37140 | 2024-12-03 14:01:25 -0800 | [diff] [blame] | 513 | // OpenBMC currently only supports HTTPS |
| 514 | if (*transferProtocol == "HTTPS") |
| Ed Tanous | e5cf777 | 2024-04-03 13:45:31 -0700 | [diff] [blame] | 515 | { |
| 516 | imageURI = "https://" + imageURI; |
| 517 | } |
| Ed Tanous | 757178a | 2024-04-03 14:32:38 -0700 | [diff] [blame] | 518 | else |
| Ed Tanous | f86bcc8 | 2023-08-25 09:34:07 -0700 | [diff] [blame] | 519 | { |
| 520 | messages::actionParameterNotSupported(res, "TransferProtocol", |
| 521 | *transferProtocol); |
| 522 | BMCWEB_LOG_ERROR("Request incorrect protocol parameter: {}", |
| 523 | *transferProtocol); |
| 524 | return std::nullopt; |
| 525 | } |
| Ed Tanous | f86bcc8 | 2023-08-25 09:34:07 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | boost::system::result<boost::urls::url> url = |
| 529 | boost::urls::parse_absolute_uri(imageURI); |
| 530 | if (!url) |
| 531 | { |
| 532 | messages::actionParameterValueTypeError(res, imageURI, "ImageURI", |
| 533 | "UpdateService.SimpleUpdate"); |
| 534 | |
| 535 | return std::nullopt; |
| 536 | } |
| 537 | url->normalize(); |
| 538 | |
| Ed Tanous | 757178a | 2024-04-03 14:32:38 -0700 | [diff] [blame] | 539 | if (url->scheme() == "tftp") |
| 540 | { |
| 541 | if (url->encoded_path().size() < 2) |
| 542 | { |
| 543 | messages::actionParameterNotSupported(res, "ImageURI", |
| 544 | url->buffer()); |
| 545 | return std::nullopt; |
| 546 | } |
| 547 | } |
| Ed Tanous | e5cf777 | 2024-04-03 13:45:31 -0700 | [diff] [blame] | 548 | else if (url->scheme() == "https") |
| 549 | { |
| 550 | // Empty paths default to "/" |
| 551 | if (url->encoded_path().empty()) |
| 552 | { |
| 553 | url->set_encoded_path("/"); |
| 554 | } |
| 555 | } |
| Ed Tanous | 757178a | 2024-04-03 14:32:38 -0700 | [diff] [blame] | 556 | else |
| Ed Tanous | f86bcc8 | 2023-08-25 09:34:07 -0700 | [diff] [blame] | 557 | { |
| 558 | messages::actionParameterNotSupported(res, "ImageURI", imageURI); |
| 559 | return std::nullopt; |
| 560 | } |
| Ed Tanous | 757178a | 2024-04-03 14:32:38 -0700 | [diff] [blame] | 561 | |
| 562 | if (url->encoded_path().empty()) |
| Ed Tanous | f86bcc8 | 2023-08-25 09:34:07 -0700 | [diff] [blame] | 563 | { |
| Ed Tanous | 757178a | 2024-04-03 14:32:38 -0700 | [diff] [blame] | 564 | messages::actionParameterValueTypeError(res, imageURI, "ImageURI", |
| 565 | "UpdateService.SimpleUpdate"); |
| Ed Tanous | f86bcc8 | 2023-08-25 09:34:07 -0700 | [diff] [blame] | 566 | return std::nullopt; |
| 567 | } |
| Ed Tanous | 757178a | 2024-04-03 14:32:38 -0700 | [diff] [blame] | 568 | |
| 569 | return *url; |
| Ed Tanous | f86bcc8 | 2023-08-25 09:34:07 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| Ed Tanous | e5cf777 | 2024-04-03 13:45:31 -0700 | [diff] [blame] | 572 | inline void doHttpsUpdate(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 573 | const boost::urls::url_view_base& url) |
| 574 | { |
| 575 | messages::actionParameterNotSupported(asyncResp->res, "ImageURI", |
| 576 | url.buffer()); |
| 577 | } |
| 578 | |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 579 | inline void handleUpdateServiceSimpleUpdateAction( |
| 580 | crow::App& app, const crow::Request& req, |
| 581 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 582 | { |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 583 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 584 | { |
| 585 | return; |
| 586 | } |
| 587 | |
| 588 | std::optional<std::string> transferProtocol; |
| 589 | std::string imageURI; |
| 590 | |
| 591 | BMCWEB_LOG_DEBUG("Enter UpdateService.SimpleUpdate doPost"); |
| 592 | |
| 593 | // User can pass in both TransferProtocol and ImageURI parameters or |
| 594 | // they can pass in just the ImageURI with the transfer protocol |
| 595 | // embedded within it. |
| 596 | // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin |
| 597 | // 2) ImageURI:tftp://1.1.1.1/myfile.bin |
| 598 | |
| Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 599 | if (!json_util::readJsonAction( // |
| 600 | req, asyncResp->res, // |
| 601 | "ImageURI", imageURI, // |
| Myung Bae | afc474a | 2024-10-09 00:53:29 -0700 | [diff] [blame] | 602 | "TransferProtocol", transferProtocol // |
| 603 | )) |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 604 | { |
| 605 | BMCWEB_LOG_DEBUG("Missing TransferProtocol or ImageURI parameter"); |
| 606 | return; |
| 607 | } |
| 608 | |
| Ed Tanous | 757178a | 2024-04-03 14:32:38 -0700 | [diff] [blame] | 609 | std::optional<boost::urls::url> url = |
| 610 | parseSimpleUpdateUrl(imageURI, transferProtocol, asyncResp->res); |
| 611 | if (!url) |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 612 | { |
| 613 | return; |
| 614 | } |
| Jagpal Singh Gill | 4e338b2 | 2024-06-14 14:24:56 -0700 | [diff] [blame] | 615 | if (url->scheme() == "https") |
| Ed Tanous | e5cf777 | 2024-04-03 13:45:31 -0700 | [diff] [blame] | 616 | { |
| 617 | doHttpsUpdate(asyncResp, *url); |
| 618 | } |
| Ed Tanous | 757178a | 2024-04-03 14:32:38 -0700 | [diff] [blame] | 619 | else |
| 620 | { |
| 621 | messages::actionParameterNotSupported(asyncResp->res, "ImageURI", |
| 622 | url->buffer()); |
| 623 | return; |
| 624 | } |
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 625 | |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 626 | BMCWEB_LOG_DEBUG("Exit UpdateService.SimpleUpdate doPost"); |
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 629 | inline void uploadImageFile(crow::Response& res, std::string_view body) |
| 630 | { |
| Ed Tanous | 2c6ffdb | 2023-06-28 11:28:38 -0700 | [diff] [blame] | 631 | std::filesystem::path filepath("/tmp/images/" + bmcweb::getRandomUUID()); |
| 632 | |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 633 | BMCWEB_LOG_DEBUG("Writing file to {}", filepath.string()); |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 634 | std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary | |
| 635 | std::ofstream::trunc); |
| 636 | // set the permission of the file to 640 |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 637 | std::filesystem::perms permission = |
| 638 | std::filesystem::perms::owner_read | std::filesystem::perms::group_read; |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 639 | std::filesystem::permissions(filepath, permission); |
| 640 | out << body; |
| 641 | |
| 642 | if (out.bad()) |
| 643 | { |
| 644 | messages::internalError(res); |
| 645 | cleanUp(); |
| 646 | } |
| 647 | } |
| 648 | |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 649 | // Convert the Request Apply Time to the D-Bus value |
| 650 | inline bool convertApplyTime(crow::Response& res, const std::string& applyTime, |
| 651 | std::string& applyTimeNewVal) |
| 652 | { |
| 653 | if (applyTime == "Immediate") |
| 654 | { |
| 655 | applyTimeNewVal = |
| Jagpal Singh Gill | 049079f | 2024-06-02 18:11:13 -0700 | [diff] [blame] | 656 | "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate"; |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 657 | } |
| 658 | else if (applyTime == "OnReset") |
| 659 | { |
| 660 | applyTimeNewVal = |
| Jagpal Singh Gill | 049079f | 2024-06-02 18:11:13 -0700 | [diff] [blame] | 661 | "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset"; |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 662 | } |
| 663 | else |
| 664 | { |
| 665 | BMCWEB_LOG_WARNING( |
| 666 | "ApplyTime value {} is not in the list of acceptable values", |
| 667 | applyTime); |
| 668 | messages::propertyValueNotInList(res, applyTime, "ApplyTime"); |
| 669 | return false; |
| 670 | } |
| 671 | return true; |
| 672 | } |
| 673 | |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 674 | inline void setApplyTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 675 | const std::string& applyTime) |
| 676 | { |
| 677 | std::string applyTimeNewVal; |
| Jagpal Singh Gill | 049079f | 2024-06-02 18:11:13 -0700 | [diff] [blame] | 678 | if (!convertApplyTime(asyncResp->res, applyTime, applyTimeNewVal)) |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 679 | { |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 680 | return; |
| 681 | } |
| 682 | |
| Ginu George | e93abac | 2024-06-14 17:35:27 +0530 | [diff] [blame] | 683 | setDbusProperty(asyncResp, "ApplyTime", "xyz.openbmc_project.Settings", |
| Ed Tanous | d02aad3 | 2024-02-13 14:43:34 -0800 | [diff] [blame] | 684 | sdbusplus::message::object_path( |
| 685 | "/xyz/openbmc_project/software/apply_time"), |
| 686 | "xyz.openbmc_project.Software.ApplyTime", |
| Ginu George | e93abac | 2024-06-14 17:35:27 +0530 | [diff] [blame] | 687 | "RequestedApplyTime", applyTimeNewVal); |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 688 | } |
| 689 | |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 690 | struct MultiPartUpdate |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 691 | { |
| Jagpal Singh Gill | ef93eab | 2024-04-17 16:06:14 -0700 | [diff] [blame] | 692 | std::string uploadData; |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 693 | struct UpdateParameters |
| 694 | { |
| 695 | std::optional<std::string> applyTime; |
| rajeeranjan | 9fbf853 | 2025-07-23 23:25:52 +0530 | [diff] [blame^] | 696 | std::optional<std::vector<std::string>> targets; |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 697 | } params; |
| Jagpal Singh Gill | ef93eab | 2024-04-17 16:06:14 -0700 | [diff] [blame] | 698 | }; |
| 699 | |
| Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 700 | inline std::optional<std::string> processUrl( |
| 701 | boost::system::result<boost::urls::url_view>& url) |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 702 | { |
| 703 | if (!url) |
| 704 | { |
| 705 | return std::nullopt; |
| 706 | } |
| 707 | if (crow::utility::readUrlSegments(*url, "redfish", "v1", "Managers", |
| 708 | BMCWEB_REDFISH_MANAGER_URI_NAME)) |
| 709 | { |
| 710 | return std::make_optional(std::string(BMCWEB_REDFISH_MANAGER_URI_NAME)); |
| 711 | } |
| 712 | if constexpr (!BMCWEB_REDFISH_UPDATESERVICE_USE_DBUS) |
| 713 | { |
| 714 | return std::nullopt; |
| 715 | } |
| 716 | std::string firmwareId; |
| 717 | if (!crow::utility::readUrlSegments(*url, "redfish", "v1", "UpdateService", |
| 718 | "FirmwareInventory", |
| 719 | std::ref(firmwareId))) |
| 720 | { |
| 721 | return std::nullopt; |
| 722 | } |
| 723 | |
| 724 | return std::make_optional(firmwareId); |
| 725 | } |
| 726 | |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 727 | inline std::optional<std::string> parseFormPartName( |
| 728 | const boost::beast::http::fields::const_iterator& contentDisposition) |
| Jagpal Singh Gill | ef93eab | 2024-04-17 16:06:14 -0700 | [diff] [blame] | 729 | { |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 730 | size_t semicolonPos = contentDisposition->value().find(';'); |
| 731 | if (semicolonPos == std::string::npos) |
| 732 | { |
| 733 | return std::nullopt; |
| 734 | } |
| 735 | |
| 736 | for (const auto& param : boost::beast::http::param_list{ |
| 737 | contentDisposition->value().substr(semicolonPos)}) |
| 738 | { |
| 739 | if (param.first == "name" && !param.second.empty()) |
| 740 | { |
| 741 | return std::string(param.second); |
| 742 | } |
| 743 | } |
| 744 | return std::nullopt; |
| 745 | } |
| 746 | |
| 747 | inline std::optional<MultiPartUpdate::UpdateParameters> processUpdateParameters( |
| 748 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 749 | std::string_view content) |
| 750 | { |
| 751 | MultiPartUpdate::UpdateParameters multiRet; |
| 752 | nlohmann::json jsonContent = nlohmann::json::parse(content, nullptr, false); |
| 753 | if (jsonContent.is_discarded()) |
| 754 | { |
| 755 | return std::nullopt; |
| 756 | } |
| 757 | nlohmann::json::object_t* obj = |
| 758 | jsonContent.get_ptr<nlohmann::json::object_t*>(); |
| 759 | if (obj == nullptr) |
| 760 | { |
| 761 | messages::propertyValueTypeError(asyncResp->res, content, |
| 762 | "UpdateParameters"); |
| 763 | return std::nullopt; |
| 764 | } |
| 765 | |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 766 | if (!json_util::readJsonObject( // |
| 767 | *obj, asyncResp->res, // |
| 768 | "@Redfish.OperationApplyTime", multiRet.applyTime, // |
| rajeeranjan | 9fbf853 | 2025-07-23 23:25:52 +0530 | [diff] [blame^] | 769 | "Targets", multiRet.targets // |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 770 | )) |
| 771 | { |
| 772 | return std::nullopt; |
| 773 | } |
| 774 | |
| rajeeranjan | 9fbf853 | 2025-07-23 23:25:52 +0530 | [diff] [blame^] | 775 | if (multiRet.targets) |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 776 | { |
| rajeeranjan | 9fbf853 | 2025-07-23 23:25:52 +0530 | [diff] [blame^] | 777 | if (multiRet.targets->size() > 1) |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 778 | { |
| rajeeranjan | 9fbf853 | 2025-07-23 23:25:52 +0530 | [diff] [blame^] | 779 | messages::propertyValueFormatError(asyncResp->res, |
| 780 | *multiRet.targets, "Targets"); |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 781 | return std::nullopt; |
| 782 | } |
| rajeeranjan | 9fbf853 | 2025-07-23 23:25:52 +0530 | [diff] [blame^] | 783 | |
| 784 | for (auto& target : *multiRet.targets) |
| 785 | { |
| 786 | boost::system::result<boost::urls::url_view> url = |
| 787 | boost::urls::parse_origin_form(target); |
| 788 | auto res = processUrl(url); |
| 789 | if (!res.has_value()) |
| 790 | { |
| 791 | messages::propertyValueFormatError(asyncResp->res, target, |
| 792 | "Targets"); |
| 793 | return std::nullopt; |
| 794 | } |
| 795 | target = res.value(); |
| 796 | } |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 797 | } |
| rajeeranjan | 9fbf853 | 2025-07-23 23:25:52 +0530 | [diff] [blame^] | 798 | |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 799 | return multiRet; |
| 800 | } |
| 801 | |
| 802 | inline std::optional<MultiPartUpdate> extractMultipartUpdateParameters( |
| 803 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, MultipartParser parser) |
| 804 | { |
| 805 | MultiPartUpdate multiRet; |
| Jagpal Singh Gill | ef93eab | 2024-04-17 16:06:14 -0700 | [diff] [blame] | 806 | for (FormPart& formpart : parser.mime_fields) |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 807 | { |
| 808 | boost::beast::http::fields::const_iterator it = |
| 809 | formpart.fields.find("Content-Disposition"); |
| 810 | if (it == formpart.fields.end()) |
| 811 | { |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 812 | BMCWEB_LOG_ERROR("Couldn't find Content-Disposition"); |
| Jagpal Singh Gill | ef93eab | 2024-04-17 16:06:14 -0700 | [diff] [blame] | 813 | return std::nullopt; |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 814 | } |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 815 | BMCWEB_LOG_INFO("Parsing value {}", it->value()); |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 816 | |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 817 | auto formFieldNameOpt = parseFormPartName(it); |
| 818 | if (!formFieldNameOpt.has_value()) |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 819 | { |
| 820 | continue; |
| 821 | } |
| 822 | |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 823 | const std::string& formFieldName = formFieldNameOpt.value(); |
| 824 | |
| 825 | if (formFieldName == "UpdateParameters") |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 826 | { |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 827 | std::optional<MultiPartUpdate::UpdateParameters> params = |
| 828 | processUpdateParameters(asyncResp, formpart.content); |
| 829 | if (!params) |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 830 | { |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 831 | return std::nullopt; |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 832 | } |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 833 | multiRet.params = std::move(*params); |
| 834 | } |
| 835 | else if (formFieldName == "UpdateFile") |
| 836 | { |
| 837 | multiRet.uploadData = std::move(formpart.content); |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | |
| Jagpal Singh Gill | ef93eab | 2024-04-17 16:06:14 -0700 | [diff] [blame] | 841 | if (multiRet.uploadData.empty()) |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 842 | { |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 843 | BMCWEB_LOG_ERROR("Upload data is NULL"); |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 844 | messages::propertyMissing(asyncResp->res, "UpdateFile"); |
| Jagpal Singh Gill | ef93eab | 2024-04-17 16:06:14 -0700 | [diff] [blame] | 845 | return std::nullopt; |
| 846 | } |
| rajeeranjan | 9fbf853 | 2025-07-23 23:25:52 +0530 | [diff] [blame^] | 847 | |
| Jagpal Singh Gill | ef93eab | 2024-04-17 16:06:14 -0700 | [diff] [blame] | 848 | return multiRet; |
| 849 | } |
| 850 | |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 851 | inline void handleStartUpdate( |
| 852 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, task::Payload payload, |
| 853 | const std::string& objectPath, const boost::system::error_code& ec, |
| 854 | const sdbusplus::message::object_path& retPath) |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 855 | { |
| 856 | if (ec) |
| 857 | { |
| 858 | BMCWEB_LOG_ERROR("error_code = {}", ec); |
| 859 | BMCWEB_LOG_ERROR("error msg = {}", ec.message()); |
| 860 | messages::internalError(asyncResp->res); |
| 861 | return; |
| 862 | } |
| 863 | |
| Jagpal Singh Gill | 587090c | 2024-08-12 00:24:16 -0700 | [diff] [blame] | 864 | BMCWEB_LOG_INFO("Call to StartUpdate on {} Success, retPath = {}", |
| 865 | objectPath, retPath.str); |
| 866 | createTask(asyncResp, std::move(payload), retPath); |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 867 | } |
| 868 | |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 869 | inline void startUpdate( |
| 870 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, task::Payload payload, |
| 871 | const MemoryFileDescriptor& memfd, const std::string& applyTime, |
| 872 | const std::string& objectPath, const std::string& serviceName) |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 873 | { |
| Ed Tanous | 177612a | 2025-02-14 15:16:09 -0800 | [diff] [blame] | 874 | dbus::utility::async_method_call( |
| 875 | asyncResp, |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 876 | [asyncResp, payload = std::move(payload), |
| 877 | objectPath](const boost::system::error_code& ec1, |
| 878 | const sdbusplus::message::object_path& retPath) mutable { |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 879 | handleStartUpdate(asyncResp, std::move(payload), objectPath, ec1, |
| 880 | retPath); |
| 881 | }, |
| rajeeranjan | 9fbf853 | 2025-07-23 23:25:52 +0530 | [diff] [blame^] | 882 | serviceName, objectPath, updateInterface, "StartUpdate", |
| 883 | sdbusplus::message::unix_fd(memfd.fd), applyTime); |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| Jagpal Singh Gill | 08f61d5 | 2024-07-17 15:17:22 -0700 | [diff] [blame] | 886 | inline void getSwInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 887 | task::Payload payload, const MemoryFileDescriptor& memfd, |
| 888 | const std::string& applyTime, const std::string& target, |
| 889 | const boost::system::error_code& ec, |
| 890 | const dbus::utility::MapperGetSubTreeResponse& subtree) |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 891 | { |
| Jagpal Singh Gill | 08f61d5 | 2024-07-17 15:17:22 -0700 | [diff] [blame] | 892 | using SwInfoMap = std::unordered_map< |
| 893 | std::string, std::pair<sdbusplus::message::object_path, std::string>>; |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 894 | SwInfoMap swInfoMap; |
| 895 | |
| 896 | if (ec) |
| 897 | { |
| 898 | BMCWEB_LOG_ERROR("error_code = {}", ec); |
| 899 | BMCWEB_LOG_ERROR("error msg = {}", ec.message()); |
| 900 | messages::internalError(asyncResp->res); |
| 901 | return; |
| 902 | } |
| 903 | BMCWEB_LOG_DEBUG("Found {} software version paths", subtree.size()); |
| 904 | |
| Jagpal Singh Gill | 08f61d5 | 2024-07-17 15:17:22 -0700 | [diff] [blame] | 905 | for (const auto& entry : subtree) |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 906 | { |
| Jagpal Singh Gill | 08f61d5 | 2024-07-17 15:17:22 -0700 | [diff] [blame] | 907 | sdbusplus::message::object_path path(entry.first); |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 908 | std::string swId = path.filename(); |
| Jagpal Singh Gill | 08f61d5 | 2024-07-17 15:17:22 -0700 | [diff] [blame] | 909 | swInfoMap.emplace(swId, make_pair(path, entry.second[0].first)); |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | auto swEntry = swInfoMap.find(target); |
| 913 | if (swEntry == swInfoMap.end()) |
| 914 | { |
| 915 | BMCWEB_LOG_WARNING("No valid DBus path for Target URI {}", target); |
| 916 | messages::propertyValueFormatError(asyncResp->res, target, "Targets"); |
| 917 | return; |
| 918 | } |
| 919 | |
| Jagpal Singh Gill | 08f61d5 | 2024-07-17 15:17:22 -0700 | [diff] [blame] | 920 | BMCWEB_LOG_DEBUG("Found software version path {} serviceName {}", |
| 921 | swEntry->second.first.str, swEntry->second.second); |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 922 | |
| Jagpal Singh Gill | 08f61d5 | 2024-07-17 15:17:22 -0700 | [diff] [blame] | 923 | startUpdate(asyncResp, std::move(payload), memfd, applyTime, |
| 924 | swEntry->second.first.str, swEntry->second.second); |
| 925 | } |
| 926 | |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 927 | inline void handleBMCUpdate( |
| 928 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, task::Payload payload, |
| 929 | const MemoryFileDescriptor& memfd, const std::string& applyTime, |
| 930 | const boost::system::error_code& ec, |
| 931 | const dbus::utility::MapperEndPoints& functionalSoftware) |
| Jagpal Singh Gill | 08f61d5 | 2024-07-17 15:17:22 -0700 | [diff] [blame] | 932 | { |
| 933 | if (ec) |
| 934 | { |
| 935 | BMCWEB_LOG_ERROR("error_code = {}", ec); |
| 936 | BMCWEB_LOG_ERROR("error msg = {}", ec.message()); |
| 937 | messages::internalError(asyncResp->res); |
| 938 | return; |
| 939 | } |
| 940 | if (functionalSoftware.size() != 1) |
| 941 | { |
| 942 | BMCWEB_LOG_ERROR("Found {} functional software endpoints", |
| 943 | functionalSoftware.size()); |
| 944 | messages::internalError(asyncResp->res); |
| 945 | return; |
| 946 | } |
| 947 | |
| 948 | startUpdate(asyncResp, std::move(payload), memfd, applyTime, |
| 949 | functionalSoftware[0], "xyz.openbmc_project.Software.Manager"); |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 950 | } |
| 951 | |
| rajeeranjan | 9fbf853 | 2025-07-23 23:25:52 +0530 | [diff] [blame^] | 952 | inline void handleMultipartManagerUpdate( |
| 953 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, task::Payload payload, |
| 954 | const MemoryFileDescriptor& memfd, const std::string& applyTime, |
| 955 | const boost::system::error_code& ec, |
| 956 | const dbus::utility::MapperGetSubTreeResponse& subtree) |
| 957 | { |
| 958 | if (ec) |
| 959 | { |
| 960 | BMCWEB_LOG_ERROR("error_code = {}", ec); |
| 961 | BMCWEB_LOG_ERROR("error msg = {}", ec.message()); |
| 962 | messages::internalError(asyncResp->res); |
| 963 | return; |
| 964 | } |
| 965 | if (subtree.size() != 1) |
| 966 | { |
| 967 | BMCWEB_LOG_ERROR("Found {} MultipartUpdate objects, expected exactly 1", |
| 968 | subtree.size()); |
| 969 | messages::internalError(asyncResp->res); |
| 970 | return; |
| 971 | } |
| 972 | |
| 973 | const auto& [objectPath, services] = subtree[0]; |
| 974 | for (const auto& [serviceName, ifaces] : services) |
| 975 | { |
| 976 | if (std::find(ifaces.begin(), ifaces.end(), updateInterface) != |
| 977 | ifaces.end()) |
| 978 | { |
| 979 | startUpdate(asyncResp, std::move(payload), memfd, applyTime, |
| 980 | objectPath, serviceName); |
| 981 | return; |
| 982 | } |
| 983 | } |
| 984 | BMCWEB_LOG_ERROR( |
| 985 | "MultipartUpdate object at {} does not implement {} interface", |
| 986 | objectPath, updateInterface); |
| 987 | messages::internalError(asyncResp->res); |
| 988 | } |
| 989 | |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 990 | inline void processUpdateRequest( |
| 991 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 992 | task::Payload&& payload, std::string_view body, |
| rajeeranjan | 9fbf853 | 2025-07-23 23:25:52 +0530 | [diff] [blame^] | 993 | const std::string& applyTime, const std::vector<std::string>& targets) |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 994 | { |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 995 | MemoryFileDescriptor memfd("update-image"); |
| 996 | if (memfd.fd == -1) |
| 997 | { |
| 998 | BMCWEB_LOG_ERROR("Failed to create image memfd"); |
| 999 | messages::internalError(asyncResp->res); |
| 1000 | return; |
| 1001 | } |
| 1002 | if (write(memfd.fd, body.data(), body.length()) != |
| 1003 | static_cast<ssize_t>(body.length())) |
| 1004 | { |
| 1005 | BMCWEB_LOG_ERROR("Failed to write to image memfd"); |
| 1006 | messages::internalError(asyncResp->res); |
| 1007 | return; |
| 1008 | } |
| 1009 | if (!memfd.rewind()) |
| 1010 | { |
| 1011 | messages::internalError(asyncResp->res); |
| 1012 | return; |
| 1013 | } |
| 1014 | |
| rajeeranjan | 9fbf853 | 2025-07-23 23:25:52 +0530 | [diff] [blame^] | 1015 | if (targets.empty()) |
| 1016 | { |
| 1017 | constexpr std::array<std::string_view, 1> interfaces = { |
| 1018 | "xyz.openbmc_project.Software.MultipartUpdate"}; |
| 1019 | dbus::utility::getSubTree( |
| 1020 | "/xyz/openbmc_project/software", 1, interfaces, |
| 1021 | [asyncResp, payload = std::move(payload), memfd = std::move(memfd), |
| 1022 | applyTime](const boost::system::error_code& ec, |
| 1023 | const dbus::utility::MapperGetSubTreeResponse& |
| 1024 | subtree) mutable { |
| 1025 | handleMultipartManagerUpdate(asyncResp, std::move(payload), |
| 1026 | memfd, applyTime, ec, subtree); |
| 1027 | }); |
| 1028 | return; |
| 1029 | } |
| 1030 | |
| 1031 | if (targets[0] == BMCWEB_REDFISH_MANAGER_URI_NAME) |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 1032 | { |
| Jagpal Singh Gill | 08f61d5 | 2024-07-17 15:17:22 -0700 | [diff] [blame] | 1033 | dbus::utility::getAssociationEndPoints( |
| Jagpal Singh Gill | 89449bb | 2024-08-12 16:17:58 -0700 | [diff] [blame] | 1034 | "/xyz/openbmc_project/software/bmc/updateable", |
| Jagpal Singh Gill | 08f61d5 | 2024-07-17 15:17:22 -0700 | [diff] [blame] | 1035 | [asyncResp, payload = std::move(payload), memfd = std::move(memfd), |
| 1036 | applyTime]( |
| 1037 | const boost::system::error_code& ec, |
| 1038 | const dbus::utility::MapperEndPoints& objectPaths) mutable { |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1039 | handleBMCUpdate(asyncResp, std::move(payload), memfd, applyTime, |
| 1040 | ec, objectPaths); |
| 1041 | }); |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 1042 | } |
| 1043 | else |
| 1044 | { |
| 1045 | constexpr std::array<std::string_view, 1> interfaces = { |
| 1046 | "xyz.openbmc_project.Software.Version"}; |
| Jagpal Singh Gill | 08f61d5 | 2024-07-17 15:17:22 -0700 | [diff] [blame] | 1047 | dbus::utility::getSubTree( |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 1048 | "/xyz/openbmc_project/software", 1, interfaces, |
| 1049 | [asyncResp, payload = std::move(payload), memfd = std::move(memfd), |
| Jagpal Singh Gill | 08f61d5 | 2024-07-17 15:17:22 -0700 | [diff] [blame] | 1050 | applyTime, targets](const boost::system::error_code& ec, |
| 1051 | const dbus::utility::MapperGetSubTreeResponse& |
| 1052 | subtree) mutable { |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1053 | getSwInfo(asyncResp, std::move(payload), memfd, applyTime, |
| 1054 | targets[0], ec, subtree); |
| 1055 | }); |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 1056 | } |
| 1057 | } |
| 1058 | |
| Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 1059 | inline void updateMultipartContext( |
| 1060 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1061 | const crow::Request& req, MultipartParser&& parser) |
| Jagpal Singh Gill | ef93eab | 2024-04-17 16:06:14 -0700 | [diff] [blame] | 1062 | { |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 1063 | std::optional<MultiPartUpdate> multipart = |
| Jagpal Singh Gill | ef93eab | 2024-04-17 16:06:14 -0700 | [diff] [blame] | 1064 | extractMultipartUpdateParameters(asyncResp, std::move(parser)); |
| 1065 | if (!multipart) |
| 1066 | { |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 1067 | return; |
| 1068 | } |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 1069 | if (!multipart->params.applyTime) |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 1070 | { |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 1071 | multipart->params.applyTime = "OnReset"; |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 1072 | } |
| 1073 | |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 1074 | if constexpr (BMCWEB_REDFISH_UPDATESERVICE_USE_DBUS) |
| 1075 | { |
| Jagpal Singh Gill | 9dae4de | 2024-06-02 23:43:56 -0700 | [diff] [blame] | 1076 | std::string applyTimeNewVal; |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 1077 | if (!convertApplyTime(asyncResp->res, *multipart->params.applyTime, |
| Jagpal Singh Gill | 9dae4de | 2024-06-02 23:43:56 -0700 | [diff] [blame] | 1078 | applyTimeNewVal)) |
| 1079 | { |
| 1080 | return; |
| 1081 | } |
| 1082 | task::Payload payload(req); |
| 1083 | |
| rajeeranjan | 9fbf853 | 2025-07-23 23:25:52 +0530 | [diff] [blame^] | 1084 | processUpdateRequest( |
| 1085 | asyncResp, std::move(payload), multipart->uploadData, |
| 1086 | applyTimeNewVal, |
| 1087 | multipart->params.targets.value_or(std::vector<std::string>{})); |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 1088 | } |
| 1089 | else |
| 1090 | { |
| rajeeranjan | 2c6f32a | 2025-07-21 11:34:50 +0530 | [diff] [blame] | 1091 | setApplyTime(asyncResp, *multipart->params.applyTime); |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 1092 | |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 1093 | // Setup callback for when new software detected |
| 1094 | monitorForSoftwareAvailable(asyncResp, req, |
| 1095 | "/redfish/v1/UpdateService"); |
| Ed Tanous | 6b54e4e | 2024-04-10 08:58:48 -0700 | [diff] [blame] | 1096 | |
| Jagpal Singh Gill | de0c960 | 2024-04-29 17:30:21 -0700 | [diff] [blame] | 1097 | uploadImageFile(asyncResp->res, multipart->uploadData); |
| 1098 | } |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 1099 | } |
| 1100 | |
| Jagpal Singh Gill | 9dae4de | 2024-06-02 23:43:56 -0700 | [diff] [blame] | 1101 | inline void doHTTPUpdate(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1102 | const crow::Request& req) |
| 1103 | { |
| 1104 | if constexpr (BMCWEB_REDFISH_UPDATESERVICE_USE_DBUS) |
| 1105 | { |
| 1106 | task::Payload payload(req); |
| 1107 | // HTTP push only supports BMC updates (with ApplyTime as immediate) for |
| 1108 | // backwards compatibility. Specific component updates will be handled |
| 1109 | // through Multipart form HTTP push. |
| 1110 | std::vector<std::string> targets; |
| 1111 | targets.emplace_back(BMCWEB_REDFISH_MANAGER_URI_NAME); |
| 1112 | |
| 1113 | processUpdateRequest( |
| 1114 | asyncResp, std::move(payload), req.body(), |
| 1115 | "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate", |
| 1116 | targets); |
| 1117 | } |
| 1118 | else |
| 1119 | { |
| 1120 | // Setup callback for when new software detected |
| 1121 | monitorForSoftwareAvailable(asyncResp, req, |
| 1122 | "/redfish/v1/UpdateService"); |
| 1123 | |
| 1124 | uploadImageFile(asyncResp->res, req.body()); |
| 1125 | } |
| 1126 | } |
| 1127 | |
| Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 1128 | inline void handleUpdateServicePost( |
| 1129 | App& app, const crow::Request& req, |
| 1130 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| Ed Tanous | c2051d1 | 2022-05-11 12:21:55 -0700 | [diff] [blame] | 1131 | { |
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 1132 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| Ed Tanous | c2051d1 | 2022-05-11 12:21:55 -0700 | [diff] [blame] | 1133 | { |
| 1134 | return; |
| 1135 | } |
| Ninad Palsule | b33a432 | 2023-06-09 09:19:18 -0500 | [diff] [blame] | 1136 | std::string_view contentType = req.getHeaderValue("Content-Type"); |
| Ed Tanous | c2051d1 | 2022-05-11 12:21:55 -0700 | [diff] [blame] | 1137 | |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1138 | BMCWEB_LOG_DEBUG("doPost: contentType={}", contentType); |
| Ed Tanous | c2051d1 | 2022-05-11 12:21:55 -0700 | [diff] [blame] | 1139 | |
| rajeeranjan | 0c814aa | 2025-03-25 13:31:49 +0530 | [diff] [blame] | 1140 | // Make sure that content type is application/octet-stream |
| Ed Tanous | 18f8f60 | 2023-07-18 10:07:23 -0700 | [diff] [blame] | 1141 | if (bmcweb::asciiIEquals(contentType, "application/octet-stream")) |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 1142 | { |
| Jagpal Singh Gill | 9dae4de | 2024-06-02 23:43:56 -0700 | [diff] [blame] | 1143 | doHTTPUpdate(asyncResp, req); |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 1144 | } |
| rajeeranjan | 0c814aa | 2025-03-25 13:31:49 +0530 | [diff] [blame] | 1145 | else |
| 1146 | { |
| 1147 | BMCWEB_LOG_DEBUG("Bad content type specified:{}", contentType); |
| 1148 | asyncResp->res.result(boost::beast::http::status::bad_request); |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | inline void handleUpdateServiceMultipartUpdatePost( |
| 1153 | App& app, const crow::Request& req, |
| 1154 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 1155 | { |
| 1156 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 1157 | { |
| 1158 | return; |
| 1159 | } |
| 1160 | |
| 1161 | std::string_view contentType = req.getHeaderValue("Content-Type"); |
| 1162 | // Make sure that content type is multipart/form-data |
| 1163 | if (contentType.starts_with("multipart/form-data")) |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 1164 | { |
| Ninad Palsule | b33a432 | 2023-06-09 09:19:18 -0500 | [diff] [blame] | 1165 | MultipartParser parser; |
| 1166 | |
| Ninad Palsule | b33a432 | 2023-06-09 09:19:18 -0500 | [diff] [blame] | 1167 | ParserError ec = parser.parse(req); |
| 1168 | if (ec != ParserError::PARSER_SUCCESS) |
| 1169 | { |
| 1170 | // handle error |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1171 | BMCWEB_LOG_ERROR("MIME parse failed, ec : {}", |
| 1172 | static_cast<int>(ec)); |
| Ninad Palsule | b33a432 | 2023-06-09 09:19:18 -0500 | [diff] [blame] | 1173 | messages::internalError(asyncResp->res); |
| 1174 | return; |
| 1175 | } |
| Ed Tanous | 6b54e4e | 2024-04-10 08:58:48 -0700 | [diff] [blame] | 1176 | |
| Jagpal Singh Gill | ef93eab | 2024-04-17 16:06:14 -0700 | [diff] [blame] | 1177 | updateMultipartContext(asyncResp, req, std::move(parser)); |
| George Liu | 0ed80c8 | 2020-05-12 16:06:27 +0800 | [diff] [blame] | 1178 | } |
| Ninad Palsule | b33a432 | 2023-06-09 09:19:18 -0500 | [diff] [blame] | 1179 | else |
| 1180 | { |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1181 | BMCWEB_LOG_DEBUG("Bad content type specified:{}", contentType); |
| Sandeep | de5259d | 2025-09-23 16:30:59 +0530 | [diff] [blame] | 1182 | asyncResp->res.result( |
| 1183 | boost::beast::http::status::unsupported_media_type); |
| Ninad Palsule | b33a432 | 2023-06-09 09:19:18 -0500 | [diff] [blame] | 1184 | } |
| Ed Tanous | c2051d1 | 2022-05-11 12:21:55 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
| Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 1187 | inline void handleUpdateServiceGet( |
| 1188 | App& app, const crow::Request& req, |
| 1189 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1190 | { |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 1191 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 1192 | { |
| 1193 | return; |
| 1194 | } |
| 1195 | asyncResp->res.jsonValue["@odata.type"] = |
| 1196 | "#UpdateService.v1_11_1.UpdateService"; |
| 1197 | asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService"; |
| 1198 | asyncResp->res.jsonValue["Id"] = "UpdateService"; |
| 1199 | asyncResp->res.jsonValue["Description"] = "Service for Software Update"; |
| 1200 | asyncResp->res.jsonValue["Name"] = "Update Service"; |
| Ed Tanous | 4dc23f3 | 2022-05-11 11:32:19 -0700 | [diff] [blame] | 1201 | |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 1202 | asyncResp->res.jsonValue["HttpPushUri"] = |
| 1203 | "/redfish/v1/UpdateService/update"; |
| 1204 | asyncResp->res.jsonValue["MultipartHttpPushUri"] = |
| rajeeranjan | 0c814aa | 2025-03-25 13:31:49 +0530 | [diff] [blame] | 1205 | "/redfish/v1/UpdateService/update-multipart"; |
| Ed Tanous | 4dc23f3 | 2022-05-11 11:32:19 -0700 | [diff] [blame] | 1206 | |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 1207 | // UpdateService cannot be disabled |
| 1208 | asyncResp->res.jsonValue["ServiceEnabled"] = true; |
| 1209 | asyncResp->res.jsonValue["FirmwareInventory"]["@odata.id"] = |
| 1210 | "/redfish/v1/UpdateService/FirmwareInventory"; |
| 1211 | // Get the MaxImageSizeBytes |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1212 | asyncResp->res.jsonValue["MaxImageSizeBytes"] = |
| 1213 | BMCWEB_HTTP_BODY_LIMIT * 1024 * 1024; |
| Tejas Patil | d61e519 | 2021-06-04 15:49:35 +0530 | [diff] [blame] | 1214 | |
| Ed Tanous | 6a37140 | 2024-12-03 14:01:25 -0800 | [diff] [blame] | 1215 | if constexpr (BMCWEB_REDFISH_ALLOW_SIMPLE_UPDATE) |
| Ed Tanous | 25b54db | 2024-04-17 15:40:31 -0700 | [diff] [blame] | 1216 | { |
| Ed Tanous | 6a37140 | 2024-12-03 14:01:25 -0800 | [diff] [blame] | 1217 | // Update Actions object. |
| 1218 | nlohmann::json& updateSvcSimpleUpdate = |
| 1219 | asyncResp->res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"]; |
| 1220 | updateSvcSimpleUpdate["target"] = |
| 1221 | "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate"; |
| Ed Tanous | 757178a | 2024-04-03 14:32:38 -0700 | [diff] [blame] | 1222 | |
| Ed Tanous | 6a37140 | 2024-12-03 14:01:25 -0800 | [diff] [blame] | 1223 | nlohmann::json::array_t allowed; |
| 1224 | allowed.emplace_back(update_service::TransferProtocolType::HTTPS); |
| 1225 | updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = |
| 1226 | std::move(allowed); |
| 1227 | } |
| Ed Tanous | 757178a | 2024-04-03 14:32:38 -0700 | [diff] [blame] | 1228 | |
| Ed Tanous | 539d8c6 | 2024-06-19 14:38:27 -0700 | [diff] [blame] | 1229 | asyncResp->res |
| 1230 | .jsonValue["HttpPushUriOptions"]["HttpPushUriApplyTime"]["ApplyTime"] = |
| 1231 | update_service::ApplyTime::Immediate; |
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1232 | } |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 1233 | |
| 1234 | inline void handleUpdateServiceFirmwareInventoryCollectionGet( |
| 1235 | App& app, const crow::Request& req, |
| 1236 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 1237 | { |
| 1238 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 1239 | { |
| 1240 | return; |
| 1241 | } |
| 1242 | asyncResp->res.jsonValue["@odata.type"] = |
| 1243 | "#SoftwareInventoryCollection.SoftwareInventoryCollection"; |
| 1244 | asyncResp->res.jsonValue["@odata.id"] = |
| 1245 | "/redfish/v1/UpdateService/FirmwareInventory"; |
| 1246 | asyncResp->res.jsonValue["Name"] = "Software Inventory Collection"; |
| 1247 | const std::array<const std::string_view, 1> iface = { |
| 1248 | "xyz.openbmc_project.Software.Version"}; |
| 1249 | |
| 1250 | redfish::collection_util::getCollectionMembers( |
| 1251 | asyncResp, |
| 1252 | boost::urls::url("/redfish/v1/UpdateService/FirmwareInventory"), iface, |
| 1253 | "/xyz/openbmc_project/software"); |
| 1254 | } |
| 1255 | |
| Alexander Hansen | 50ab50d | 2025-08-13 16:34:49 +0200 | [diff] [blame] | 1256 | inline void addRelatedItem(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1257 | const boost::urls::url& url) |
| 1258 | { |
| 1259 | nlohmann::json& relatedItem = asyncResp->res.jsonValue["RelatedItem"]; |
| 1260 | nlohmann::json::object_t item; |
| 1261 | item["@odata.id"] = url; |
| 1262 | relatedItem.emplace_back(std::move(item)); |
| 1263 | asyncResp->res.jsonValue["RelatedItem@odata.count"] = relatedItem.size(); |
| 1264 | } |
| 1265 | |
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1266 | /* Fill related item links (i.e. bmc, bios) in for inventory */ |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 1267 | inline void getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1268 | const std::string& purpose) |
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1269 | { |
| Willy Tu | eee0013 | 2022-06-14 14:53:17 -0700 | [diff] [blame] | 1270 | if (purpose == sw_util::bmcPurpose) |
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1271 | { |
| Alexander Hansen | 50ab50d | 2025-08-13 16:34:49 +0200 | [diff] [blame] | 1272 | auto url = boost::urls::format("/redfish/v1/Managers/{}", |
| 1273 | BMCWEB_REDFISH_MANAGER_URI_NAME); |
| 1274 | addRelatedItem(asyncResp, url); |
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1275 | } |
| Willy Tu | eee0013 | 2022-06-14 14:53:17 -0700 | [diff] [blame] | 1276 | else if (purpose == sw_util::biosPurpose) |
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1277 | { |
| Alexander Hansen | 50ab50d | 2025-08-13 16:34:49 +0200 | [diff] [blame] | 1278 | auto url = boost::urls::format("/redfish/v1/Systems/{}/Bios", |
| 1279 | BMCWEB_REDFISH_SYSTEM_URI_NAME); |
| 1280 | |
| 1281 | addRelatedItem(asyncResp, url); |
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1282 | } |
| 1283 | else |
| 1284 | { |
| Carson Labrado | bf2dded | 2023-08-10 00:37:06 +0000 | [diff] [blame] | 1285 | BMCWEB_LOG_DEBUG("Unknown software purpose {}", purpose); |
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | |
| Alexander Hansen | 78e6902 | 2025-08-13 16:20:29 +0200 | [diff] [blame] | 1289 | inline void getSoftwareVersionCallback( |
| 1290 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1291 | const std::string& swId, const boost::system::error_code& ec, |
| 1292 | const dbus::utility::DBusPropertiesMap& propertiesList) |
| 1293 | { |
| 1294 | if (ec) |
| 1295 | { |
| 1296 | messages::internalError(asyncResp->res); |
| 1297 | return; |
| 1298 | } |
| 1299 | const std::string* swInvPurpose = nullptr; |
| 1300 | const std::string* version = nullptr; |
| 1301 | const bool success = sdbusplus::unpackPropertiesNoThrow( |
| 1302 | dbus_utils::UnpackErrorPrinter(), propertiesList, "Purpose", |
| 1303 | swInvPurpose, "Version", version); |
| 1304 | if (!success) |
| 1305 | { |
| 1306 | messages::internalError(asyncResp->res); |
| 1307 | return; |
| 1308 | } |
| 1309 | if (swInvPurpose == nullptr) |
| 1310 | { |
| 1311 | BMCWEB_LOG_DEBUG("Can't find property \"Purpose\"!"); |
| 1312 | messages::internalError(asyncResp->res); |
| 1313 | return; |
| 1314 | } |
| 1315 | BMCWEB_LOG_DEBUG("swInvPurpose = {}", *swInvPurpose); |
| 1316 | if (version == nullptr) |
| 1317 | { |
| 1318 | BMCWEB_LOG_DEBUG("Can't find property \"Version\"!"); |
| 1319 | messages::internalError(asyncResp->res); |
| 1320 | return; |
| 1321 | } |
| 1322 | asyncResp->res.jsonValue["Version"] = *version; |
| 1323 | asyncResp->res.jsonValue["Id"] = swId; |
| 1324 | // swInvPurpose is of format: |
| 1325 | // xyz.openbmc_project.Software.Version.VersionPurpose.ABC |
| 1326 | // Translate this to "ABC image" |
| 1327 | size_t endDesc = swInvPurpose->rfind('.'); |
| 1328 | if (endDesc == std::string::npos) |
| 1329 | { |
| 1330 | messages::internalError(asyncResp->res); |
| 1331 | return; |
| 1332 | } |
| 1333 | endDesc++; |
| 1334 | if (endDesc >= swInvPurpose->size()) |
| 1335 | { |
| 1336 | messages::internalError(asyncResp->res); |
| 1337 | return; |
| 1338 | } |
| 1339 | std::string formatDesc = swInvPurpose->substr(endDesc); |
| 1340 | asyncResp->res.jsonValue["Description"] = formatDesc + " image"; |
| 1341 | getRelatedItems(asyncResp, *swInvPurpose); |
| 1342 | } |
| 1343 | |
| Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 1344 | inline void getSoftwareVersion( |
| 1345 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1346 | const std::string& service, const std::string& path, |
| 1347 | const std::string& swId) |
| Willy Tu | af24660 | 2022-06-14 15:51:53 -0700 | [diff] [blame] | 1348 | { |
| Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 1349 | dbus::utility::getAllProperties( |
| 1350 | service, path, "xyz.openbmc_project.Software.Version", |
| Alexander Hansen | 78e6902 | 2025-08-13 16:20:29 +0200 | [diff] [blame] | 1351 | std::bind_front(getSoftwareVersionCallback, asyncResp, swId)); |
| Willy Tu | af24660 | 2022-06-14 15:51:53 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
| Alexander Hansen | 609b4ab | 2025-08-13 16:17:26 +0200 | [diff] [blame] | 1354 | inline void handleUpdateServiceFirmwareInventoryGetCallback( |
| 1355 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1356 | const std::shared_ptr<std::string>& swId, |
| 1357 | const boost::system::error_code& ec, |
| 1358 | const dbus::utility::MapperGetSubTreeResponse& subtree) |
| 1359 | { |
| 1360 | BMCWEB_LOG_DEBUG("doGet callback..."); |
| 1361 | if (ec) |
| 1362 | { |
| 1363 | messages::internalError(asyncResp->res); |
| 1364 | return; |
| 1365 | } |
| 1366 | // Ensure we find our input swId, otherwise return an error |
| 1367 | bool found = false; |
| 1368 | for (const std::pair< |
| 1369 | std::string, |
| 1370 | std::vector<std::pair<std::string, std::vector<std::string>>>>& |
| 1371 | obj : subtree) |
| 1372 | { |
| 1373 | sdbusplus::message::object_path path(obj.first); |
| 1374 | std::string id = path.filename(); |
| 1375 | if (id.empty()) |
| 1376 | { |
| 1377 | BMCWEB_LOG_DEBUG("Failed to find software id in {}", obj.first); |
| 1378 | continue; |
| 1379 | } |
| 1380 | if (id != *swId) |
| 1381 | { |
| 1382 | continue; |
| 1383 | } |
| 1384 | if (obj.second.empty()) |
| 1385 | { |
| 1386 | continue; |
| 1387 | } |
| 1388 | found = true; |
| 1389 | sw_util::getSwStatus(asyncResp, swId, obj.second[0].first); |
| 1390 | sw_util::getSwMinimumVersion(asyncResp, swId, obj.second[0].first); |
| 1391 | getSoftwareVersion(asyncResp, obj.second[0].first, obj.first, *swId); |
| 1392 | } |
| 1393 | if (!found) |
| 1394 | { |
| 1395 | BMCWEB_LOG_WARNING("Input swID {} not found!", *swId); |
| 1396 | messages::resourceMissingAtURI( |
| 1397 | asyncResp->res, |
| 1398 | boost::urls::format( |
| 1399 | "/redfish/v1/UpdateService/FirmwareInventory/{}", *swId)); |
| 1400 | return; |
| 1401 | } |
| 1402 | asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( |
| 1403 | "/redfish/v1/UpdateService/FirmwareInventory/{}", *swId); |
| 1404 | asyncResp->res.jsonValue["@odata.type"] = |
| 1405 | "#SoftwareInventory.v1_1_0.SoftwareInventory"; |
| 1406 | asyncResp->res.jsonValue["Name"] = "Software Inventory"; |
| 1407 | asyncResp->res.jsonValue["Status"]["HealthRollup"] = resource::Health::OK; |
| 1408 | asyncResp->res.jsonValue["Updateable"] = false; |
| 1409 | sw_util::getSwUpdatableStatus(asyncResp, swId); |
| 1410 | } |
| 1411 | |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 1412 | inline void handleUpdateServiceFirmwareInventoryGet( |
| 1413 | App& app, const crow::Request& req, |
| 1414 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1415 | const std::string& param) |
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1416 | { |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 1417 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 1418 | { |
| 1419 | return; |
| 1420 | } |
| 1421 | std::shared_ptr<std::string> swId = std::make_shared<std::string>(param); |
| 1422 | |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 1423 | constexpr std::array<std::string_view, 1> interfaces = { |
| 1424 | "xyz.openbmc_project.Software.Version"}; |
| 1425 | dbus::utility::getSubTree( |
| Abiola Asojo | f90af52 | 2025-03-18 16:29:31 +0000 | [diff] [blame] | 1426 | "/xyz/openbmc_project/software/", 0, interfaces, |
| Alexander Hansen | 609b4ab | 2025-08-13 16:17:26 +0200 | [diff] [blame] | 1427 | std::bind_front(handleUpdateServiceFirmwareInventoryGetCallback, |
| 1428 | asyncResp, swId)); |
| John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1429 | } |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1430 | |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 1431 | inline void requestRoutesUpdateService(App& app) |
| 1432 | { |
| Ed Tanous | 6a37140 | 2024-12-03 14:01:25 -0800 | [diff] [blame] | 1433 | if constexpr (BMCWEB_REDFISH_ALLOW_SIMPLE_UPDATE) |
| 1434 | { |
| 1435 | BMCWEB_ROUTE( |
| 1436 | app, |
| 1437 | "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/") |
| 1438 | .privileges(redfish::privileges::postUpdateService) |
| 1439 | .methods(boost::beast::http::verb::post)(std::bind_front( |
| 1440 | handleUpdateServiceSimpleUpdateAction, std::ref(app))); |
| 1441 | } |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 1442 | BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/") |
| 1443 | .privileges(redfish::privileges::getSoftwareInventory) |
| 1444 | .methods(boost::beast::http::verb::get)(std::bind_front( |
| 1445 | handleUpdateServiceFirmwareInventoryGet, std::ref(app))); |
| 1446 | |
| 1447 | BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/") |
| 1448 | .privileges(redfish::privileges::getUpdateService) |
| 1449 | .methods(boost::beast::http::verb::get)( |
| 1450 | std::bind_front(handleUpdateServiceGet, std::ref(app))); |
| 1451 | |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 1452 | BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/update/") |
| 1453 | .privileges(redfish::privileges::postUpdateService) |
| 1454 | .methods(boost::beast::http::verb::post)( |
| 1455 | std::bind_front(handleUpdateServicePost, std::ref(app))); |
| 1456 | |
| rajeeranjan | 0c814aa | 2025-03-25 13:31:49 +0530 | [diff] [blame] | 1457 | BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/update-multipart/") |
| 1458 | .privileges(redfish::privileges::postUpdateService) |
| 1459 | .methods(boost::beast::http::verb::post)(std::bind_front( |
| 1460 | handleUpdateServiceMultipartUpdatePost, std::ref(app))); |
| 1461 | |
| Ed Tanous | f513933 | 2024-04-03 13:25:04 -0700 | [diff] [blame] | 1462 | BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/") |
| 1463 | .privileges(redfish::privileges::getSoftwareInventoryCollection) |
| 1464 | .methods(boost::beast::http::verb::get)(std::bind_front( |
| 1465 | handleUpdateServiceFirmwareInventoryCollectionGet, std::ref(app))); |
| 1466 | } |
| 1467 | |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1468 | } // namespace redfish |