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