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