OTAUtil.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2022 Project CHIP Authors
  3. * All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include "OTAUtil.h"
  18. #if CONFIG_CHIP_OTA_REQUESTOR
  19. #include <app/clusters/ota-requestor/BDXDownloader.h>
  20. #include <app/clusters/ota-requestor/DefaultOTARequestor.h>
  21. #include <app/clusters/ota-requestor/DefaultOTARequestorDriver.h>
  22. #include <app/clusters/ota-requestor/DefaultOTARequestorStorage.h>
  23. #include <app/server/Server.h>
  24. #include <platform/CHIPDeviceLayer.h>
  25. #include <platform/nrfconnect/OTAImageProcessorImpl.h>
  26. #include <zephyr/dfu/mcuboot.h>
  27. #endif
  28. using namespace chip;
  29. using namespace chip::DeviceLayer;
  30. #if CONFIG_CHIP_OTA_REQUESTOR
  31. namespace {
  32. DefaultOTARequestorStorage sOTARequestorStorage;
  33. DefaultOTARequestorDriver sOTARequestorDriver;
  34. chip::BDXDownloader sBDXDownloader;
  35. chip::DefaultOTARequestor sOTARequestor;
  36. } // namespace
  37. // compile-time factory method
  38. OTAImageProcessorImpl & GetOTAImageProcessor()
  39. {
  40. #if CONFIG_PM_DEVICE && CONFIG_NORDIC_QSPI_NOR
  41. static OTAImageProcessorImpl sOTAImageProcessor(&GetFlashHandler());
  42. #else
  43. static OTAImageProcessorImpl sOTAImageProcessor;
  44. #endif
  45. return sOTAImageProcessor;
  46. }
  47. void InitBasicOTARequestor()
  48. {
  49. VerifyOrReturn(GetRequestorInstance() == nullptr);
  50. OTAImageProcessorImpl & imageProcessor = GetOTAImageProcessor();
  51. imageProcessor.SetOTADownloader(&sBDXDownloader);
  52. sBDXDownloader.SetImageProcessorDelegate(&imageProcessor);
  53. sOTARequestorStorage.Init(Server::GetInstance().GetPersistentStorage());
  54. sOTARequestor.Init(Server::GetInstance(), sOTARequestorStorage, sOTARequestorDriver, sBDXDownloader);
  55. chip::SetRequestorInstance(&sOTARequestor);
  56. sOTARequestorDriver.Init(&sOTARequestor, &imageProcessor);
  57. imageProcessor.TriggerFlashAction(ExternalFlashManager::Action::SLEEP);
  58. }
  59. void OtaConfirmNewImage()
  60. {
  61. #ifndef CONFIG_SOC_SERIES_NRF53X
  62. /* Check if the image is run in the REVERT mode and eventually
  63. confirm it to prevent reverting on the next boot.
  64. On nRF53 target there is not way to verify current swap type
  65. because we use permanent swap so we can skip it. */
  66. VerifyOrReturn(mcuboot_swap_type() == BOOT_SWAP_TYPE_REVERT);
  67. #endif
  68. OTAImageProcessorImpl & imageProcessor = GetOTAImageProcessor();
  69. if (!boot_is_img_confirmed())
  70. {
  71. CHIP_ERROR err = System::MapErrorZephyr(boot_write_img_confirmed());
  72. if (CHIP_NO_ERROR == err)
  73. {
  74. imageProcessor.SetImageConfirmed();
  75. ChipLogProgress(SoftwareUpdate, "New firmware image confirmed");
  76. }
  77. else
  78. {
  79. ChipLogError(SoftwareUpdate, "Failed to confirm firmware image, it will be reverted on the next boot");
  80. }
  81. }
  82. }
  83. #endif
  84. ExternalFlashManager & GetFlashHandler()
  85. {
  86. static ExternalFlashManager sFlashHandler;
  87. return sFlashHandler;
  88. }