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 | |
| 18 | #include "node.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 19 | |
Jennifer Lee | 729dae7 | 2018-04-24 15:59:34 -0700 | [diff] [blame] | 20 | #include <boost/container/flat_map.hpp> |
Andrew Geissler | 87d8472 | 2019-02-28 14:28:39 -0600 | [diff] [blame] | 21 | #include <utils/fw_utils.hpp> |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 22 | #include <variant> |
Jennifer Lee | 729dae7 | 2018-04-24 15:59:34 -0700 | [diff] [blame] | 23 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 24 | namespace redfish |
| 25 | { |
Ed Tanous | 27826b5 | 2018-10-29 11:40:58 -0700 | [diff] [blame] | 26 | |
Andrew Geissler | 0e7de46 | 2019-03-04 19:11:54 -0600 | [diff] [blame] | 27 | // Match signals added on software path |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 28 | static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher; |
Andrew Geissler | 0e7de46 | 2019-03-04 19:11:54 -0600 | [diff] [blame] | 29 | // Only allow one update at a time |
| 30 | static bool fwUpdateInProgress = false; |
Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 31 | // Timer for software available |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 32 | static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer; |
Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 33 | |
| 34 | static void cleanUp() |
| 35 | { |
| 36 | fwUpdateInProgress = false; |
| 37 | fwUpdateMatcher = nullptr; |
| 38 | } |
| 39 | static void activateImage(const std::string &objPath, |
| 40 | const std::string &service) |
| 41 | { |
| 42 | BMCWEB_LOG_DEBUG << "Activate image for " << objPath << " " << service; |
| 43 | crow::connections::systemBus->async_method_call( |
| 44 | [](const boost::system::error_code error_code) { |
| 45 | if (error_code) |
| 46 | { |
| 47 | BMCWEB_LOG_DEBUG << "error_code = " << error_code; |
| 48 | BMCWEB_LOG_DEBUG << "error msg = " << error_code.message(); |
| 49 | } |
| 50 | }, |
| 51 | service, objPath, "org.freedesktop.DBus.Properties", "Set", |
| 52 | "xyz.openbmc_project.Software.Activation", "RequestedActivation", |
| 53 | std::variant<std::string>( |
| 54 | "xyz.openbmc_project.Software.Activation.RequestedActivations." |
| 55 | "Active")); |
| 56 | } |
Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 57 | |
| 58 | // Note that asyncResp can be either a valid pointer or nullptr. If nullptr |
| 59 | // then no asyncResp updates will occur |
Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 60 | static void softwareInterfaceAdded(std::shared_ptr<AsyncResp> asyncResp, |
| 61 | sdbusplus::message::message &m) |
| 62 | { |
| 63 | std::vector<std::pair< |
| 64 | std::string, |
| 65 | std::vector<std::pair<std::string, std::variant<std::string>>>>> |
| 66 | interfacesProperties; |
| 67 | |
| 68 | sdbusplus::message::object_path objPath; |
| 69 | |
| 70 | m.read(objPath, interfacesProperties); |
| 71 | |
| 72 | BMCWEB_LOG_DEBUG << "obj path = " << objPath.str; |
| 73 | for (auto &interface : interfacesProperties) |
| 74 | { |
| 75 | BMCWEB_LOG_DEBUG << "interface = " << interface.first; |
| 76 | |
| 77 | if (interface.first == "xyz.openbmc_project.Software.Activation") |
| 78 | { |
| 79 | // Found our interface, disable callbacks |
| 80 | fwUpdateMatcher = nullptr; |
| 81 | |
| 82 | // Retrieve service and activate |
| 83 | crow::connections::systemBus->async_method_call( |
| 84 | [objPath, asyncResp]( |
| 85 | const boost::system::error_code error_code, |
| 86 | const std::vector<std::pair< |
| 87 | std::string, std::vector<std::string>>> &objInfo) { |
| 88 | if (error_code) |
| 89 | { |
| 90 | BMCWEB_LOG_DEBUG << "error_code = " << error_code; |
| 91 | BMCWEB_LOG_DEBUG << "error msg = " |
| 92 | << error_code.message(); |
Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 93 | if (asyncResp) |
| 94 | { |
| 95 | messages::internalError(asyncResp->res); |
| 96 | } |
Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 97 | cleanUp(); |
| 98 | return; |
| 99 | } |
| 100 | // Ensure we only got one service back |
| 101 | if (objInfo.size() != 1) |
| 102 | { |
| 103 | BMCWEB_LOG_ERROR << "Invalid Object Size " |
| 104 | << objInfo.size(); |
Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 105 | if (asyncResp) |
| 106 | { |
| 107 | messages::internalError(asyncResp->res); |
| 108 | } |
Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 109 | cleanUp(); |
| 110 | return; |
| 111 | } |
| 112 | // cancel timer only when |
| 113 | // xyz.openbmc_project.Software.Activation interface |
| 114 | // is added |
| 115 | fwAvailableTimer = nullptr; |
| 116 | |
| 117 | activateImage(objPath.str, objInfo[0].first); |
Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 118 | if (asyncResp) |
| 119 | { |
| 120 | redfish::messages::success(asyncResp->res); |
| 121 | } |
Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 122 | fwUpdateInProgress = false; |
| 123 | }, |
| 124 | "xyz.openbmc_project.ObjectMapper", |
| 125 | "/xyz/openbmc_project/object_mapper", |
| 126 | "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str, |
| 127 | std::array<const char *, 1>{ |
| 128 | "xyz.openbmc_project.Software.Activation"}); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 133 | // Note that asyncResp can be either a valid pointer or nullptr. If nullptr |
| 134 | // then no asyncResp updates will occur |
Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 135 | static void monitorForSoftwareAvailable(std::shared_ptr<AsyncResp> asyncResp, |
Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 136 | const crow::Request &req, |
| 137 | int timeoutTimeSeconds = 5) |
Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 138 | { |
| 139 | // Only allow one FW update at a time |
| 140 | if (fwUpdateInProgress != false) |
| 141 | { |
Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 142 | if (asyncResp) |
| 143 | { |
| 144 | asyncResp->res.addHeader("Retry-After", "30"); |
| 145 | messages::serviceTemporarilyUnavailable(asyncResp->res, "30"); |
| 146 | } |
Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | |
Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 150 | fwAvailableTimer = |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 151 | std::make_unique<boost::asio::steady_timer>(*req.ioService); |
Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 152 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 153 | fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds)); |
Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 154 | |
| 155 | fwAvailableTimer->async_wait( |
| 156 | [asyncResp](const boost::system::error_code &ec) { |
| 157 | cleanUp(); |
| 158 | if (ec == boost::asio::error::operation_aborted) |
| 159 | { |
| 160 | // expected, we were canceled before the timer completed. |
| 161 | return; |
| 162 | } |
| 163 | BMCWEB_LOG_ERROR |
| 164 | << "Timed out waiting for firmware object being created"; |
| 165 | BMCWEB_LOG_ERROR |
| 166 | << "FW image may has already been uploaded to server"; |
| 167 | if (ec) |
| 168 | { |
| 169 | BMCWEB_LOG_ERROR << "Async_wait failed" << ec; |
| 170 | return; |
| 171 | } |
Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 172 | if (asyncResp) |
| 173 | { |
| 174 | redfish::messages::internalError(asyncResp->res); |
| 175 | } |
Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 176 | }); |
| 177 | |
| 178 | auto callback = [asyncResp](sdbusplus::message::message &m) { |
| 179 | BMCWEB_LOG_DEBUG << "Match fired"; |
| 180 | softwareInterfaceAdded(asyncResp, m); |
| 181 | }; |
| 182 | |
| 183 | fwUpdateInProgress = true; |
| 184 | |
| 185 | fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>( |
| 186 | *crow::connections::systemBus, |
| 187 | "interface='org.freedesktop.DBus.ObjectManager',type='signal'," |
| 188 | "member='InterfacesAdded',path='/xyz/openbmc_project/software'", |
| 189 | callback); |
| 190 | } |
Jennifer Lee | 729dae7 | 2018-04-24 15:59:34 -0700 | [diff] [blame] | 191 | |
Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 192 | /** |
| 193 | * UpdateServiceActionsSimpleUpdate class supports handle POST method for |
| 194 | * SimpleUpdate action. |
| 195 | */ |
| 196 | class UpdateServiceActionsSimpleUpdate : public Node |
| 197 | { |
| 198 | public: |
| 199 | UpdateServiceActionsSimpleUpdate(CrowApp &app) : |
| 200 | Node(app, |
| 201 | "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/") |
| 202 | { |
| 203 | entityPrivileges = { |
| 204 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 205 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 206 | {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, |
| 207 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 208 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 209 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
| 210 | } |
| 211 | |
| 212 | private: |
| 213 | void doPost(crow::Response &res, const crow::Request &req, |
| 214 | const std::vector<std::string> ¶ms) override |
| 215 | { |
| 216 | std::optional<std::string> transferProtocol; |
| 217 | std::string imageURI; |
| 218 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
| 219 | |
| 220 | BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost"; |
| 221 | |
| 222 | // User can pass in both TransferProtocol and ImageURI parameters or |
| 223 | // they can pass in just the ImageURI with the transfer protocl embedded |
| 224 | // within it. |
| 225 | // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin |
| 226 | // 2) ImageURI:tftp://1.1.1.1/myfile.bin |
| 227 | |
| 228 | if (!json_util::readJson(req, asyncResp->res, "TransferProtocol", |
| 229 | transferProtocol, "ImageURI", imageURI)) |
| 230 | { |
| 231 | BMCWEB_LOG_DEBUG |
| 232 | << "Missing TransferProtocol or ImageURI parameter"; |
| 233 | return; |
| 234 | } |
| 235 | if (!transferProtocol) |
| 236 | { |
| 237 | // Must be option 2 |
| 238 | // Verify ImageURI has transfer protocol in it |
| 239 | size_t separator = imageURI.find(":"); |
| 240 | if ((separator == std::string::npos) || |
| 241 | ((separator + 1) > imageURI.size())) |
| 242 | { |
| 243 | messages::actionParameterValueTypeError( |
| 244 | asyncResp->res, imageURI, "ImageURI", |
| 245 | "UpdateService.SimpleUpdate"); |
| 246 | BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: " |
| 247 | << imageURI; |
| 248 | return; |
| 249 | } |
| 250 | transferProtocol = imageURI.substr(0, separator); |
| 251 | // Ensure protocol is upper case for a common comparison path below |
| 252 | boost::to_upper(*transferProtocol); |
| 253 | BMCWEB_LOG_DEBUG << "Encoded transfer protocol " |
| 254 | << *transferProtocol; |
| 255 | |
| 256 | // Adjust imageURI to not have the protocol on it for parsing |
| 257 | // below |
| 258 | // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin |
| 259 | imageURI = imageURI.substr(separator + 3); |
| 260 | BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI; |
| 261 | } |
| 262 | |
| 263 | // OpenBMC currently only supports TFTP |
| 264 | if (*transferProtocol != "TFTP") |
| 265 | { |
| 266 | messages::actionParameterNotSupported(asyncResp->res, |
| 267 | "TransferProtocol", |
| 268 | "UpdateService.SimpleUpdate"); |
| 269 | BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: " |
| 270 | << *transferProtocol; |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | // Format should be <IP or Hostname>/<file> for imageURI |
| 275 | size_t separator = imageURI.find("/"); |
| 276 | if ((separator == std::string::npos) || |
| 277 | ((separator + 1) > imageURI.size())) |
| 278 | { |
| 279 | messages::actionParameterValueTypeError( |
| 280 | asyncResp->res, imageURI, "ImageURI", |
| 281 | "UpdateService.SimpleUpdate"); |
| 282 | BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI; |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | std::string tftpServer = imageURI.substr(0, separator); |
| 287 | std::string fwFile = imageURI.substr(separator + 1); |
| 288 | BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile; |
| 289 | |
| 290 | // Setup callback for when new software detected |
| 291 | // Give TFTP 2 minutes to complete |
| 292 | monitorForSoftwareAvailable(nullptr, req, 120); |
| 293 | |
| 294 | // TFTP can take up to 2 minutes depending on image size and |
| 295 | // connection speed. Return to caller as soon as the TFTP operation |
| 296 | // has been started. The callback above will ensure the activate |
| 297 | // is started once the download has completed |
| 298 | redfish::messages::success(asyncResp->res); |
| 299 | |
| 300 | // Call TFTP service |
| 301 | crow::connections::systemBus->async_method_call( |
| 302 | [](const boost::system::error_code ec) { |
| 303 | if (ec) |
| 304 | { |
| 305 | // messages::internalError(asyncResp->res); |
| 306 | cleanUp(); |
| 307 | BMCWEB_LOG_DEBUG << "error_code = " << ec; |
| 308 | BMCWEB_LOG_DEBUG << "error msg = " << ec.message(); |
| 309 | } |
| 310 | else |
| 311 | { |
| 312 | BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success"; |
| 313 | } |
| 314 | }, |
| 315 | "xyz.openbmc_project.Software.Download", |
| 316 | "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP", |
| 317 | "DownloadViaTFTP", fwFile, tftpServer); |
| 318 | |
| 319 | BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost"; |
| 320 | } |
| 321 | }; |
| 322 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 323 | class UpdateService : public Node |
| 324 | { |
| 325 | public: |
| 326 | UpdateService(CrowApp &app) : Node(app, "/redfish/v1/UpdateService/") |
| 327 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 328 | entityPrivileges = { |
| 329 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 330 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 331 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 332 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 333 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 334 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 335 | } |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 336 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 337 | private: |
| 338 | void doGet(crow::Response &res, const crow::Request &req, |
| 339 | const std::vector<std::string> ¶ms) override |
| 340 | { |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 341 | res.jsonValue["@odata.type"] = "#UpdateService.v1_2_0.UpdateService"; |
| 342 | res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService"; |
| 343 | res.jsonValue["@odata.context"] = |
| 344 | "/redfish/v1/$metadata#UpdateService.UpdateService"; |
| 345 | res.jsonValue["Id"] = "UpdateService"; |
| 346 | res.jsonValue["Description"] = "Service for Software Update"; |
| 347 | res.jsonValue["Name"] = "Update Service"; |
| 348 | res.jsonValue["HttpPushUri"] = "/redfish/v1/UpdateService"; |
| 349 | // UpdateService cannot be disabled |
| 350 | res.jsonValue["ServiceEnabled"] = true; |
| 351 | res.jsonValue["FirmwareInventory"] = { |
| 352 | {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}}; |
Andrew Geissler | 0554c98 | 2019-04-23 14:40:12 -0500 | [diff] [blame] | 353 | #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE |
| 354 | // Update Actions object. |
| 355 | nlohmann::json &updateSvcSimpleUpdate = |
| 356 | res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"]; |
| 357 | updateSvcSimpleUpdate["target"] = |
| 358 | "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate"; |
| 359 | updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = { |
| 360 | "TFTP"}; |
| 361 | #endif |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 362 | res.end(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 363 | } |
Andrew Geissler | 0e7de46 | 2019-03-04 19:11:54 -0600 | [diff] [blame] | 364 | |
Jayashankar Padath | fa1a5a3 | 2019-05-28 23:54:37 +0530 | [diff] [blame] | 365 | void doPatch(crow::Response &res, const crow::Request &req, |
| 366 | const std::vector<std::string> ¶ms) override |
| 367 | { |
| 368 | BMCWEB_LOG_DEBUG << "doPatch..."; |
| 369 | |
| 370 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
| 371 | std::string applyTime; |
| 372 | |
| 373 | if (!json_util::readJson(req, res, "ApplyTime", applyTime)) |
| 374 | { |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | if ((applyTime == "Immediate") || (applyTime == "OnReset")) |
| 379 | { |
| 380 | std::string applyTimeNewVal; |
| 381 | if (applyTime == "Immediate") |
| 382 | { |
| 383 | applyTimeNewVal = "xyz.openbmc_project.Software.ApplyTime." |
| 384 | "RequestedApplyTimes.Immediate"; |
| 385 | } |
| 386 | else |
| 387 | { |
| 388 | applyTimeNewVal = "xyz.openbmc_project.Software.ApplyTime." |
| 389 | "RequestedApplyTimes.OnReset"; |
| 390 | } |
| 391 | |
| 392 | // Set the requested image apply time value |
| 393 | crow::connections::systemBus->async_method_call( |
| 394 | [asyncResp](const boost::system::error_code ec) { |
| 395 | if (ec) |
| 396 | { |
| 397 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; |
| 398 | messages::internalError(asyncResp->res); |
| 399 | return; |
| 400 | } |
| 401 | messages::success(asyncResp->res); |
| 402 | }, |
| 403 | "xyz.openbmc_project.Settings", |
| 404 | "/xyz/openbmc_project/software/apply_time", |
| 405 | "org.freedesktop.DBus.Properties", "Set", |
| 406 | "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime", |
| 407 | std::variant<std::string>{applyTimeNewVal}); |
| 408 | } |
| 409 | else |
| 410 | { |
| 411 | BMCWEB_LOG_INFO << "ApplyTime value is not in the list of " |
| 412 | "acceptable values"; |
| 413 | messages::propertyValueNotInList(asyncResp->res, applyTime, |
| 414 | "ApplyTime"); |
| 415 | } |
| 416 | } |
| 417 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 418 | void doPost(crow::Response &res, const crow::Request &req, |
| 419 | const std::vector<std::string> ¶ms) override |
| 420 | { |
| 421 | BMCWEB_LOG_DEBUG << "doPost..."; |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 422 | |
Andrew Geissler | 0e7de46 | 2019-03-04 19:11:54 -0600 | [diff] [blame] | 423 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 424 | |
Andrew Geissler | 86adcd6 | 2019-04-18 10:58:05 -0500 | [diff] [blame] | 425 | // Setup callback for when new software detected |
| 426 | monitorForSoftwareAvailable(asyncResp, req); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 427 | |
| 428 | std::string filepath( |
| 429 | "/tmp/images/" + |
| 430 | boost::uuids::to_string(boost::uuids::random_generator()())); |
| 431 | BMCWEB_LOG_DEBUG << "Writing file to " << filepath; |
| 432 | std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary | |
| 433 | std::ofstream::trunc); |
| 434 | out << req.body; |
| 435 | out.close(); |
| 436 | BMCWEB_LOG_DEBUG << "file upload complete!!"; |
| 437 | } |
Jennifer Lee | 729dae7 | 2018-04-24 15:59:34 -0700 | [diff] [blame] | 438 | }; |
Ed Tanous | c711bf8 | 2018-07-30 16:31:33 -0700 | [diff] [blame] | 439 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 440 | class SoftwareInventoryCollection : public Node |
| 441 | { |
| 442 | public: |
| 443 | template <typename CrowApp> |
| 444 | SoftwareInventoryCollection(CrowApp &app) : |
| 445 | Node(app, "/redfish/v1/UpdateService/FirmwareInventory/") |
| 446 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 447 | entityPrivileges = { |
| 448 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 449 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 450 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 451 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 452 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 453 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
Jennifer Lee | 729dae7 | 2018-04-24 15:59:34 -0700 | [diff] [blame] | 454 | } |
Jennifer Lee | 729dae7 | 2018-04-24 15:59:34 -0700 | [diff] [blame] | 455 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 456 | private: |
| 457 | void doGet(crow::Response &res, const crow::Request &req, |
| 458 | const std::vector<std::string> ¶ms) override |
| 459 | { |
| 460 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 461 | res.jsonValue["@odata.type"] = |
| 462 | "#SoftwareInventoryCollection.SoftwareInventoryCollection"; |
| 463 | res.jsonValue["@odata.id"] = |
| 464 | "/redfish/v1/UpdateService/FirmwareInventory"; |
| 465 | res.jsonValue["@odata.context"] = |
| 466 | "/redfish/v1/" |
| 467 | "$metadata#SoftwareInventoryCollection.SoftwareInventoryCollection"; |
| 468 | res.jsonValue["Name"] = "Software Inventory Collection"; |
Ed Tanous | c711bf8 | 2018-07-30 16:31:33 -0700 | [diff] [blame] | 469 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 470 | crow::connections::systemBus->async_method_call( |
| 471 | [asyncResp]( |
| 472 | const boost::system::error_code ec, |
| 473 | const std::vector<std::pair< |
| 474 | std::string, std::vector<std::pair< |
| 475 | std::string, std::vector<std::string>>>>> |
| 476 | &subtree) { |
| 477 | if (ec) |
| 478 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 479 | messages::internalError(asyncResp->res); |
Ed Tanous | c711bf8 | 2018-07-30 16:31:33 -0700 | [diff] [blame] | 480 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 481 | } |
| 482 | asyncResp->res.jsonValue["Members"] = nlohmann::json::array(); |
| 483 | asyncResp->res.jsonValue["Members@odata.count"] = 0; |
Jennifer Lee | 6c4eb9d | 2018-05-22 10:58:31 -0700 | [diff] [blame] | 484 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 485 | for (auto &obj : subtree) |
| 486 | { |
Jennifer Lee | f4b65ab | 2018-09-18 12:00:13 -0700 | [diff] [blame] | 487 | // if can't parse fw id then return |
Ed Tanous | 27826b5 | 2018-10-29 11:40:58 -0700 | [diff] [blame] | 488 | std::size_t idPos; |
| 489 | if ((idPos = obj.first.rfind("/")) == std::string::npos) |
Jennifer Lee | f4b65ab | 2018-09-18 12:00:13 -0700 | [diff] [blame] | 490 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 491 | messages::internalError(asyncResp->res); |
Jennifer Lee | f4b65ab | 2018-09-18 12:00:13 -0700 | [diff] [blame] | 492 | BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!"; |
| 493 | return; |
| 494 | } |
Jennifer Lee | f4b65ab | 2018-09-18 12:00:13 -0700 | [diff] [blame] | 495 | std::string swId = obj.first.substr(idPos + 1); |
| 496 | |
Andrew Geissler | e0dd805 | 2019-06-18 16:05:10 -0500 | [diff] [blame] | 497 | nlohmann::json &members = |
| 498 | asyncResp->res.jsonValue["Members"]; |
| 499 | members.push_back( |
| 500 | {{"@odata.id", "/redfish/v1/UpdateService/" |
| 501 | "FirmwareInventory/" + |
| 502 | swId}}); |
| 503 | asyncResp->res.jsonValue["Members@odata.count"] = |
| 504 | members.size(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 505 | } |
| 506 | }, |
| 507 | "xyz.openbmc_project.ObjectMapper", |
| 508 | "/xyz/openbmc_project/object_mapper", |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 509 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", |
| 510 | static_cast<int32_t>(0), |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 511 | std::array<const char *, 1>{ |
| 512 | "xyz.openbmc_project.Software.Version"}); |
| 513 | } |
Jennifer Lee | 729dae7 | 2018-04-24 15:59:34 -0700 | [diff] [blame] | 514 | }; |
| 515 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 516 | class SoftwareInventory : public Node |
| 517 | { |
| 518 | public: |
| 519 | template <typename CrowApp> |
| 520 | SoftwareInventory(CrowApp &app) : |
| 521 | Node(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/", |
| 522 | std::string()) |
| 523 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 524 | entityPrivileges = { |
| 525 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 526 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 527 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 528 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 529 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 530 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
| 531 | } |
| 532 | |
| 533 | private: |
Andrew Geissler | 87d8472 | 2019-02-28 14:28:39 -0600 | [diff] [blame] | 534 | /* Fill related item links (i.e. bmc, bios) in for inventory */ |
| 535 | static void getRelatedItems(std::shared_ptr<AsyncResp> aResp, |
| 536 | const std::string &purpose) |
| 537 | { |
| 538 | if (purpose == fw_util::bmcPurpose) |
| 539 | { |
| 540 | nlohmann::json &members = aResp->res.jsonValue["RelatedItem"]; |
| 541 | members.push_back({{"@odata.id", "/redfish/v1/Managers/bmc"}}); |
| 542 | aResp->res.jsonValue["Members@odata.count"] = members.size(); |
| 543 | } |
| 544 | else if (purpose == fw_util::biosPurpose) |
| 545 | { |
| 546 | // TODO(geissonator) Need BIOS schema support added for this |
| 547 | // to be valid |
| 548 | // nlohmann::json &members = aResp->res.jsonValue["RelatedItem"]; |
| 549 | // members.push_back( |
| 550 | // {{"@odata.id", "/redfish/v1/Systems/system/BIOS"}}); |
| 551 | // aResp->res.jsonValue["Members@odata.count"] = members.size(); |
| 552 | } |
| 553 | else |
| 554 | { |
| 555 | BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose; |
| 556 | } |
| 557 | } |
| 558 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 559 | void doGet(crow::Response &res, const crow::Request &req, |
| 560 | const std::vector<std::string> ¶ms) override |
| 561 | { |
| 562 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 563 | |
| 564 | if (params.size() != 1) |
| 565 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 566 | messages::internalError(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 567 | res.end(); |
| 568 | return; |
| 569 | } |
| 570 | |
Ed Tanous | 3ae837c | 2018-08-07 14:41:19 -0700 | [diff] [blame] | 571 | std::shared_ptr<std::string> swId = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 572 | std::make_shared<std::string>(params[0]); |
| 573 | |
| 574 | res.jsonValue["@odata.id"] = |
Ed Tanous | 3ae837c | 2018-08-07 14:41:19 -0700 | [diff] [blame] | 575 | "/redfish/v1/UpdateService/FirmwareInventory/" + *swId; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 576 | |
| 577 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 3ae837c | 2018-08-07 14:41:19 -0700 | [diff] [blame] | 578 | [asyncResp, swId]( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 579 | const boost::system::error_code ec, |
| 580 | const std::vector<std::pair< |
| 581 | std::string, std::vector<std::pair< |
| 582 | std::string, std::vector<std::string>>>>> |
| 583 | &subtree) { |
| 584 | BMCWEB_LOG_DEBUG << "doGet callback..."; |
| 585 | if (ec) |
| 586 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 587 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 588 | return; |
| 589 | } |
| 590 | |
Andrew Geissler | 6913228 | 2019-07-01 11:00:35 -0500 | [diff] [blame] | 591 | // Ensure we find our input swId, otherwise return an error |
| 592 | bool found = false; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 593 | for (const std::pair< |
| 594 | std::string, |
| 595 | std::vector< |
| 596 | std::pair<std::string, std::vector<std::string>>>> |
| 597 | &obj : subtree) |
| 598 | { |
Ed Tanous | 3ae837c | 2018-08-07 14:41:19 -0700 | [diff] [blame] | 599 | if (boost::ends_with(obj.first, *swId) != true) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 600 | { |
| 601 | continue; |
| 602 | } |
| 603 | |
Jennifer Lee | f4b65ab | 2018-09-18 12:00:13 -0700 | [diff] [blame] | 604 | if (obj.second.size() < 1) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 605 | { |
| 606 | continue; |
| 607 | } |
| 608 | |
Andrew Geissler | 6913228 | 2019-07-01 11:00:35 -0500 | [diff] [blame] | 609 | found = true; |
Andrew Geissler | e0dd805 | 2019-06-18 16:05:10 -0500 | [diff] [blame] | 610 | fw_util::getFwStatus(asyncResp, swId, obj.second[0].first); |
| 611 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 612 | crow::connections::systemBus->async_method_call( |
| 613 | [asyncResp, |
Ed Tanous | 3ae837c | 2018-08-07 14:41:19 -0700 | [diff] [blame] | 614 | swId](const boost::system::error_code error_code, |
| 615 | const boost::container::flat_map< |
| 616 | std::string, VariantType> &propertiesList) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 617 | if (error_code) |
| 618 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 619 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 620 | return; |
| 621 | } |
| 622 | boost::container::flat_map< |
| 623 | std::string, VariantType>::const_iterator it = |
| 624 | propertiesList.find("Purpose"); |
| 625 | if (it == propertiesList.end()) |
| 626 | { |
| 627 | BMCWEB_LOG_DEBUG |
| 628 | << "Can't find property \"Purpose\"!"; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 629 | messages::propertyMissing(asyncResp->res, |
| 630 | "Purpose"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 631 | return; |
| 632 | } |
Ed Tanous | 3ae837c | 2018-08-07 14:41:19 -0700 | [diff] [blame] | 633 | const std::string *swInvPurpose = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 634 | std::get_if<std::string>(&it->second); |
Ed Tanous | 3ae837c | 2018-08-07 14:41:19 -0700 | [diff] [blame] | 635 | if (swInvPurpose == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 636 | { |
| 637 | BMCWEB_LOG_DEBUG |
| 638 | << "wrong types for property\"Purpose\"!"; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 639 | messages::propertyValueTypeError(asyncResp->res, |
| 640 | "", "Purpose"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 641 | return; |
| 642 | } |
| 643 | |
Ed Tanous | 3ae837c | 2018-08-07 14:41:19 -0700 | [diff] [blame] | 644 | BMCWEB_LOG_DEBUG << "swInvPurpose = " |
| 645 | << *swInvPurpose; |
Jennifer Lee | f4b65ab | 2018-09-18 12:00:13 -0700 | [diff] [blame] | 646 | it = propertiesList.find("Version"); |
| 647 | if (it == propertiesList.end()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 648 | { |
Jennifer Lee | f4b65ab | 2018-09-18 12:00:13 -0700 | [diff] [blame] | 649 | BMCWEB_LOG_DEBUG |
| 650 | << "Can't find property \"Version\"!"; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 651 | messages::propertyMissing(asyncResp->res, |
| 652 | "Version"); |
Jennifer Lee | f4b65ab | 2018-09-18 12:00:13 -0700 | [diff] [blame] | 653 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 654 | } |
Jennifer Lee | f4b65ab | 2018-09-18 12:00:13 -0700 | [diff] [blame] | 655 | |
| 656 | BMCWEB_LOG_DEBUG << "Version found!"; |
| 657 | |
| 658 | const std::string *version = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 659 | std::get_if<std::string>(&it->second); |
Jennifer Lee | f4b65ab | 2018-09-18 12:00:13 -0700 | [diff] [blame] | 660 | |
| 661 | if (version == nullptr) |
| 662 | { |
| 663 | BMCWEB_LOG_DEBUG |
| 664 | << "Can't find property \"Version\"!"; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 665 | |
| 666 | messages::propertyValueTypeError(asyncResp->res, |
| 667 | "", "Version"); |
Jennifer Lee | f4b65ab | 2018-09-18 12:00:13 -0700 | [diff] [blame] | 668 | return; |
| 669 | } |
| 670 | asyncResp->res.jsonValue["Version"] = *version; |
| 671 | asyncResp->res.jsonValue["Id"] = *swId; |
Andrew Geissler | 54daabe | 2019-02-13 13:54:15 -0600 | [diff] [blame] | 672 | |
| 673 | // swInvPurpose is of format: |
| 674 | // xyz.openbmc_project.Software.Version.VersionPurpose.ABC |
James Feist | e2e9677 | 2019-10-03 10:51:43 -0700 | [diff] [blame] | 675 | // Translate this to "ABC image" |
Andrew Geissler | 54daabe | 2019-02-13 13:54:15 -0600 | [diff] [blame] | 676 | size_t endDesc = swInvPurpose->rfind("."); |
| 677 | if (endDesc == std::string::npos) |
| 678 | { |
| 679 | messages::internalError(asyncResp->res); |
| 680 | return; |
| 681 | } |
| 682 | endDesc++; |
| 683 | if (endDesc >= swInvPurpose->size()) |
| 684 | { |
| 685 | messages::internalError(asyncResp->res); |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | std::string formatDesc = |
| 690 | swInvPurpose->substr(endDesc); |
| 691 | asyncResp->res.jsonValue["Description"] = |
James Feist | e2e9677 | 2019-10-03 10:51:43 -0700 | [diff] [blame] | 692 | formatDesc + " image"; |
Andrew Geissler | 87d8472 | 2019-02-28 14:28:39 -0600 | [diff] [blame] | 693 | getRelatedItems(asyncResp, *swInvPurpose); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 694 | }, |
| 695 | obj.second[0].first, obj.first, |
| 696 | "org.freedesktop.DBus.Properties", "GetAll", |
| 697 | "xyz.openbmc_project.Software.Version"); |
| 698 | } |
Andrew Geissler | 6913228 | 2019-07-01 11:00:35 -0500 | [diff] [blame] | 699 | if (!found) |
| 700 | { |
| 701 | BMCWEB_LOG_ERROR << "Input swID " + *swId + " not found!"; |
| 702 | messages::resourceMissingAtURI( |
| 703 | asyncResp->res, |
| 704 | "/redfish/v1/UpdateService/FirmwareInventory/" + *swId); |
| 705 | return; |
| 706 | } |
Ayushi Smriti | 4e68c45 | 2019-09-04 14:37:55 +0530 | [diff] [blame] | 707 | asyncResp->res.jsonValue["@odata.type"] = |
| 708 | "#SoftwareInventory.v1_1_0.SoftwareInventory"; |
| 709 | asyncResp->res.jsonValue["@odata.context"] = |
| 710 | "/redfish/v1/$metadata#SoftwareInventory.SoftwareInventory"; |
| 711 | asyncResp->res.jsonValue["Name"] = "Software Inventory"; |
| 712 | asyncResp->res.jsonValue["Updateable"] = false; |
| 713 | asyncResp->res.jsonValue["Status"]["Health"] = "OK"; |
| 714 | asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 715 | }, |
| 716 | "xyz.openbmc_project.ObjectMapper", |
| 717 | "/xyz/openbmc_project/object_mapper", |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 718 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", |
| 719 | static_cast<int32_t>(0), |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 720 | std::array<const char *, 1>{ |
| 721 | "xyz.openbmc_project.Software.Version"}); |
| 722 | } |
| 723 | }; |
| 724 | |
| 725 | } // namespace redfish |