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