blob: 3414f90b425505c7d175d95a471b3490d8079b02 [file] [log] [blame]
Sergei Lissianoi42d842c2021-11-30 11:29:27 -05001/*
2 *
3 * Copyright (c) 2021 Project CHIP Authors
Sergei Lissianoi42d842c2021-11-30 11:29:27 -05004 *
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 Yangae57e322021-12-02 15:34:15 -080018#pragma once
Sergei Lissianoi42d842c2021-11-30 11:29:27 -050019
Wang Qixianga33bda02021-12-16 16:48:40 +080020#include "esp_ota_ops.h"
21#include <app/clusters/ota-requestor/BDXDownloader.h>
Shubham Patila1ade7b2022-02-09 23:07:56 +053022#include <lib/core/OTAImageHeader.h>
Carol Yangae57e322021-12-02 15:34:15 -080023#include <platform/CHIPDeviceLayer.h>
Damian Królikdc3aeaf2021-12-11 08:20:02 +010024#include <platform/OTAImageProcessor.h>
Sergei Lissianoi42d842c2021-11-30 11:29:27 -050025
Shubham Patilf980f2c2024-05-16 22:13:24 +053026#ifdef CONFIG_ENABLE_ENCRYPTED_OTA
Shubham Patil81f71a02023-06-07 01:16:02 +053027#include <esp_encrypted_img.h>
28#endif // CONFIG_ENABLE_ENCRYPTED_OTA
29
PSONALldc5bba72024-08-12 18:42:39 +053030#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 Yangae57e322021-12-02 15:34:15 -080036namespace chip {
37
Wang Qixianga33bda02021-12-16 16:48:40 +080038class OTAImageProcessorImpl : public OTAImageProcessorInterface
Sergei Lissianoi42d842c2021-11-30 11:29:27 -050039{
Carol Yangae57e322021-12-02 15:34:15 -080040public:
41 //////////// OTAImageProcessorInterface Implementation ///////////////
42 CHIP_ERROR PrepareDownload() override;
43 CHIP_ERROR Finalize() override;
Damian Królikdc3aeaf2021-12-11 08:20:02 +010044 CHIP_ERROR Apply() override;
Carol Yangae57e322021-12-02 15:34:15 -080045 CHIP_ERROR Abort() override;
46 CHIP_ERROR ProcessBlock(ByteSpan & block) override;
Wang Qixianga33bda02021-12-16 16:48:40 +080047 void SetOTADownloader(OTADownloader * downloader) { mDownloader = downloader; };
Rohit Jadhavc1cbfb02022-04-25 21:54:16 +053048 bool IsFirstImageRun() override;
49 CHIP_ERROR ConfirmCurrentImage() override;
Trevor Holbrookb1e00232021-12-04 11:49:50 -080050
Shubham Patilf980f2c2024-05-16 22:13:24 +053051#ifdef CONFIG_ENABLE_ENCRYPTED_OTA
Shubham Patil81f71a02023-06-07 01:16:02 +053052 // @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 Yangae57e322021-12-02 15:34:15 -080058private:
Carol Yangae57e322021-12-02 15:34:15 -080059 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 Qixianga33bda02021-12-16 16:48:40 +080063 static void HandleApply(intptr_t context);
Sergei Lissianoi42d842c2021-11-30 11:29:27 -050064
Carol Yangae57e322021-12-02 15:34:15 -080065 CHIP_ERROR SetBlock(ByteSpan & block);
Carol Yangae57e322021-12-02 15:34:15 -080066 CHIP_ERROR ReleaseBlock();
Shubham Patila1ade7b2022-02-09 23:07:56 +053067 CHIP_ERROR ProcessHeader(ByteSpan & block);
Sergei Lissianoi42d842c2021-11-30 11:29:27 -050068
Wang Qixianga33bda02021-12-16 16:48:40 +080069 OTADownloader * mDownloader = nullptr;
Carol Yangae57e322021-12-02 15:34:15 -080070 MutableByteSpan mBlock;
Wang Qixianga33bda02021-12-16 16:48:40 +080071 const esp_partition_t * mOTAUpdatePartition = nullptr;
72 esp_ota_handle_t mOTAUpdateHandle;
PSONALldc5bba72024-08-12 18:42:39 +053073#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 Patila1ade7b2022-02-09 23:07:56 +053086 OTAImageHeaderParser mHeaderParser;
Shubham Patil81f71a02023-06-07 01:16:02 +053087
Shubham Patilf980f2c2024-05-16 22:13:24 +053088#ifdef CONFIG_ENABLE_ENCRYPTED_OTA
Shubham Patil9c568582023-08-25 16:00:56 +053089 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 Patil81f71a02023-06-07 01:16:02 +053097
98 CharSpan mKey;
99 bool mEncryptedOTAEnabled = false;
100 esp_decrypt_handle_t mOTADecryptionHandle = nullptr;
101#endif // CONFIG_ENABLE_ENCRYPTED_OTA
Sergei Lissianoi42d842c2021-11-30 11:29:27 -0500102};
Carol Yangae57e322021-12-02 15:34:15 -0800103
104} // namespace chip