forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathOTAFirmwareProcessor.cpp
129 lines (102 loc) · 3.65 KB
/
OTAFirmwareProcessor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <platform/internal/CHIPDeviceLayerInternal.h>
#include <platform/nxp/common/ota/OTAFirmwareProcessor.h>
#include <platform/nxp/common/ota/OTAImageProcessorImpl.h>
#include "OtaSupport.h"
namespace chip {
CHIP_ERROR OTAFirmwareProcessor::Init()
{
VerifyOrReturnError(mCallbackProcessDescriptor != nullptr, CHIP_ERROR_OTA_PROCESSOR_CB_NOT_REGISTERED);
mAccumulator.Init(sizeof(Descriptor));
VerifyOrReturnError(gOtaSuccess_c == OTA_SelectExternalStoragePartition(), CHIP_ERROR_OTA_PROCESSOR_EXTERNAL_STORAGE);
otaResult_t ota_status;
ota_status = OTA_ServiceInit(&mPostedOperationsStorage[0], NB_PENDING_TRANSACTIONS * TRANSACTION_SZ);
VerifyOrReturnError(ota_status == gOtaSuccess_c, CHIP_ERROR_OTA_PROCESSOR_CLIENT_INIT);
VerifyOrReturnError(gOtaSuccess_c == OTA_StartImage(mLength - sizeof(Descriptor)), CHIP_ERROR_OTA_PROCESSOR_START_IMAGE);
return CHIP_NO_ERROR;
}
CHIP_ERROR OTAFirmwareProcessor::Clear()
{
OTATlvProcessor::ClearInternal();
mAccumulator.Clear();
mDescriptorProcessed = false;
return CHIP_NO_ERROR;
}
CHIP_ERROR OTAFirmwareProcessor::ProcessInternal(ByteSpan & block)
{
otaResult_t status;
static uint32_t ulEraseLen = 0;
static uint32_t ulCrtAddr = 0;
if (!mDescriptorProcessed)
{
ReturnErrorOnFailure(ProcessDescriptor(block));
}
ulCrtAddr += block.size();
if (ulCrtAddr >= ulEraseLen)
{
ulEraseLen += 0x1000; // flash sector size
status = OTA_MakeHeadRoomForNextBlock(block.size(), OTAImageProcessorImpl::FetchNextData, 0);
if (gOtaSuccess_c != status)
{
ChipLogError(SoftwareUpdate, "Failed to make room for next block. Status: %d", status);
return CHIP_ERROR_OTA_PROCESSOR_MAKE_ROOM;
}
}
else
{
OTAImageProcessorImpl::FetchNextData(0);
}
status = OTA_PushImageChunk((uint8_t *) block.data(), (uint16_t) block.size(), NULL, NULL);
if (gOtaSuccess_c != status)
{
ChipLogError(SoftwareUpdate, "Failed to write image block. Status: %d", status);
return CHIP_ERROR_OTA_PROCESSOR_PUSH_CHUNK;
}
return CHIP_ERROR_OTA_FETCH_ALREADY_SCHEDULED;
}
CHIP_ERROR OTAFirmwareProcessor::ProcessDescriptor(ByteSpan & block)
{
ReturnErrorOnFailure(mAccumulator.Accumulate(block));
ReturnErrorOnFailure(mCallbackProcessDescriptor(static_cast<void *>(mAccumulator.data())));
mDescriptorProcessed = true;
mAccumulator.Clear();
return CHIP_NO_ERROR;
}
CHIP_ERROR OTAFirmwareProcessor::ApplyAction()
{
return CHIP_NO_ERROR;
}
CHIP_ERROR OTAFirmwareProcessor::AbortAction()
{
OTA_CancelImage();
OTA_ServiceDeInit();
Clear();
return CHIP_NO_ERROR;
}
CHIP_ERROR OTAFirmwareProcessor::ExitAction()
{
if (OTA_CommitImage(NULL) != gOtaSuccess_c)
{
ChipLogError(SoftwareUpdate, "Failed to commit firmware image.");
mApplyState = ApplyState::kDoNotApply;
return CHIP_ERROR_OTA_PROCESSOR_IMG_COMMIT;
}
return CHIP_NO_ERROR;
}
} // namespace chip