Sergei Lissianoi | 42d842c | 2021-11-30 11:29:27 -0500 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) 2021 Project CHIP Authors |
Sergei Lissianoi | 42d842c | 2021-11-30 11:29:27 -0500 | [diff] [blame] | 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 | |
Carol Yang | ae57e32 | 2021-12-02 15:34:15 -0800 | [diff] [blame] | 18 | #pragma once |
Sergei Lissianoi | 42d842c | 2021-11-30 11:29:27 -0500 | [diff] [blame] | 19 | |
Wang Qixiang | a33bda0 | 2021-12-16 16:48:40 +0800 | [diff] [blame] | 20 | #include "esp_ota_ops.h" |
| 21 | #include <app/clusters/ota-requestor/BDXDownloader.h> |
Shubham Patil | a1ade7b | 2022-02-09 23:07:56 +0530 | [diff] [blame] | 22 | #include <lib/core/OTAImageHeader.h> |
Carol Yang | ae57e32 | 2021-12-02 15:34:15 -0800 | [diff] [blame] | 23 | #include <platform/CHIPDeviceLayer.h> |
Damian Królik | dc3aeaf | 2021-12-11 08:20:02 +0100 | [diff] [blame] | 24 | #include <platform/OTAImageProcessor.h> |
Sergei Lissianoi | 42d842c | 2021-11-30 11:29:27 -0500 | [diff] [blame] | 25 | |
Shubham Patil | f980f2c | 2024-05-16 22:13:24 +0530 | [diff] [blame] | 26 | #ifdef CONFIG_ENABLE_ENCRYPTED_OTA |
Shubham Patil | 81f71a0 | 2023-06-07 01:16:02 +0530 | [diff] [blame] | 27 | #include <esp_encrypted_img.h> |
| 28 | #endif // CONFIG_ENABLE_ENCRYPTED_OTA |
| 29 | |
PSONALl | dc5bba7 | 2024-08-12 18:42:39 +0530 | [diff] [blame] | 30 | #ifdef CONFIG_ENABLE_DELTA_OTA |
| 31 | #include "esp_app_format.h" |
| 32 | #include <esp_delta_ota.h> |
| 33 | #define IMG_HEADER_LEN sizeof(esp_image_header_t) |
| 34 | #endif // CONFIG_ENABLE_DELTA_OTA |
| 35 | |
Carol Yang | ae57e32 | 2021-12-02 15:34:15 -0800 | [diff] [blame] | 36 | namespace chip { |
| 37 | |
Wang Qixiang | a33bda0 | 2021-12-16 16:48:40 +0800 | [diff] [blame] | 38 | class OTAImageProcessorImpl : public OTAImageProcessorInterface |
Sergei Lissianoi | 42d842c | 2021-11-30 11:29:27 -0500 | [diff] [blame] | 39 | { |
Carol Yang | ae57e32 | 2021-12-02 15:34:15 -0800 | [diff] [blame] | 40 | public: |
| 41 | //////////// OTAImageProcessorInterface Implementation /////////////// |
| 42 | CHIP_ERROR PrepareDownload() override; |
| 43 | CHIP_ERROR Finalize() override; |
Damian Królik | dc3aeaf | 2021-12-11 08:20:02 +0100 | [diff] [blame] | 44 | CHIP_ERROR Apply() override; |
Carol Yang | ae57e32 | 2021-12-02 15:34:15 -0800 | [diff] [blame] | 45 | CHIP_ERROR Abort() override; |
| 46 | CHIP_ERROR ProcessBlock(ByteSpan & block) override; |
Wang Qixiang | a33bda0 | 2021-12-16 16:48:40 +0800 | [diff] [blame] | 47 | void SetOTADownloader(OTADownloader * downloader) { mDownloader = downloader; }; |
Rohit Jadhav | c1cbfb0 | 2022-04-25 21:54:16 +0530 | [diff] [blame] | 48 | bool IsFirstImageRun() override; |
| 49 | CHIP_ERROR ConfirmCurrentImage() override; |
Trevor Holbrook | b1e0023 | 2021-12-04 11:49:50 -0800 | [diff] [blame] | 50 | |
Shubham Patil | f980f2c | 2024-05-16 22:13:24 +0530 | [diff] [blame] | 51 | #ifdef CONFIG_ENABLE_ENCRYPTED_OTA |
Shubham Patil | 81f71a0 | 2023-06-07 01:16:02 +0530 | [diff] [blame] | 52 | // @brief This API initializes the handling of encrypted OTA image |
| 53 | // @param key null terminated RSA-3072 key in PEM format |
| 54 | // @return CHIP_NO_ERROR on success, appropriate error code otherwise |
| 55 | CHIP_ERROR InitEncryptedOTA(const CharSpan & key); |
| 56 | #endif // CONFIG_ENABLE_ENCRYPTED_OTA |
| 57 | |
Carol Yang | ae57e32 | 2021-12-02 15:34:15 -0800 | [diff] [blame] | 58 | private: |
Carol Yang | ae57e32 | 2021-12-02 15:34:15 -0800 | [diff] [blame] | 59 | static void HandlePrepareDownload(intptr_t context); |
| 60 | static void HandleFinalize(intptr_t context); |
| 61 | static void HandleAbort(intptr_t context); |
| 62 | static void HandleProcessBlock(intptr_t context); |
Wang Qixiang | a33bda0 | 2021-12-16 16:48:40 +0800 | [diff] [blame] | 63 | static void HandleApply(intptr_t context); |
Sergei Lissianoi | 42d842c | 2021-11-30 11:29:27 -0500 | [diff] [blame] | 64 | |
Carol Yang | ae57e32 | 2021-12-02 15:34:15 -0800 | [diff] [blame] | 65 | CHIP_ERROR SetBlock(ByteSpan & block); |
Carol Yang | ae57e32 | 2021-12-02 15:34:15 -0800 | [diff] [blame] | 66 | CHIP_ERROR ReleaseBlock(); |
Shubham Patil | a1ade7b | 2022-02-09 23:07:56 +0530 | [diff] [blame] | 67 | CHIP_ERROR ProcessHeader(ByteSpan & block); |
Sergei Lissianoi | 42d842c | 2021-11-30 11:29:27 -0500 | [diff] [blame] | 68 | |
Wang Qixiang | a33bda0 | 2021-12-16 16:48:40 +0800 | [diff] [blame] | 69 | OTADownloader * mDownloader = nullptr; |
Carol Yang | ae57e32 | 2021-12-02 15:34:15 -0800 | [diff] [blame] | 70 | MutableByteSpan mBlock; |
Wang Qixiang | a33bda0 | 2021-12-16 16:48:40 +0800 | [diff] [blame] | 71 | const esp_partition_t * mOTAUpdatePartition = nullptr; |
| 72 | esp_ota_handle_t mOTAUpdateHandle; |
PSONALl | dc5bba7 | 2024-08-12 18:42:39 +0530 | [diff] [blame] | 73 | #ifdef CONFIG_ENABLE_DELTA_OTA |
| 74 | esp_delta_ota_handle_t mDeltaOTAUpdateHandle; |
| 75 | esp_delta_ota_cfg_t deltaOtaCfg; |
| 76 | bool patchHeaderVerified = false; |
| 77 | bool chipIdVerified = false; |
| 78 | |
| 79 | static void DeltaOTACleanUp(intptr_t context); |
| 80 | static bool VerifyChipId(esp_chip_id_t chipId); |
| 81 | static bool VerifyPatchHeader(void * imgHeaderData); |
| 82 | esp_err_t VerifyHeaderData(const uint8_t * buf, size_t size, int * index); |
| 83 | static esp_err_t DeltaOTAReadCallback(uint8_t * buf_p, size_t size, int src_offset); |
| 84 | static esp_err_t DeltaOTAWriteCallback(const uint8_t * buf_p, size_t size, void * arg); |
| 85 | #endif // CONFIG_ENABLE_DELTA_OTA |
Shubham Patil | a1ade7b | 2022-02-09 23:07:56 +0530 | [diff] [blame] | 86 | OTAImageHeaderParser mHeaderParser; |
Shubham Patil | 81f71a0 | 2023-06-07 01:16:02 +0530 | [diff] [blame] | 87 | |
Shubham Patil | f980f2c | 2024-05-16 22:13:24 +0530 | [diff] [blame] | 88 | #ifdef CONFIG_ENABLE_ENCRYPTED_OTA |
Shubham Patil | 9c56858 | 2023-08-25 16:00:56 +0530 | [diff] [blame] | 89 | CHIP_ERROR DecryptStart(); |
| 90 | CHIP_ERROR DecryptEnd(); |
| 91 | void DecryptAbort(); |
| 92 | |
| 93 | // This API decrypts the blockToDecrypt, dynamically allocates the memory for storing the |
| 94 | // plain text, and return that in decryptedBlock. |
| 95 | // Caller shall free the memory after use by calling free() on decryptedBlock.data() |
| 96 | CHIP_ERROR DecryptBlock(const ByteSpan & blockToDecrypt, ByteSpan & decryptedBlock); |
Shubham Patil | 81f71a0 | 2023-06-07 01:16:02 +0530 | [diff] [blame] | 97 | |
| 98 | CharSpan mKey; |
| 99 | bool mEncryptedOTAEnabled = false; |
| 100 | esp_decrypt_handle_t mOTADecryptionHandle = nullptr; |
| 101 | #endif // CONFIG_ENABLE_ENCRYPTED_OTA |
Sergei Lissianoi | 42d842c | 2021-11-30 11:29:27 -0500 | [diff] [blame] | 102 | }; |
Carol Yang | ae57e32 | 2021-12-02 15:34:15 -0800 | [diff] [blame] | 103 | |
| 104 | } // namespace chip |