forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathOTAImageProcessorImpl.cpp
430 lines (357 loc) · 12.8 KB
/
OTAImageProcessorImpl.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
*
* Copyright (c) 2021-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 <lib/support/BufferReader.h>
#include <platform/DiagnosticDataProvider.h>
#include <platform/internal/CHIPDeviceLayerInternal.h>
#include <platform/internal/GenericConfigurationManagerImpl.h>
#include <src/app/clusters/ota-requestor/OTADownloader.h>
#include <src/app/clusters/ota-requestor/OTARequestorInterface.h>
#include <platform/nxp/common/ota/OTAImageProcessorImpl.h>
using namespace chip::DeviceLayer;
using namespace ::chip::DeviceLayer::Internal;
#if USE_SMU2_STATIC
// The attribute specifier should not be changed.
static chip::OTAImageProcessorImpl gImageProcessor __attribute__((section(".smu2")));
#else
static chip::OTAImageProcessorImpl gImageProcessor;
#endif
namespace chip {
CHIP_ERROR OTAImageProcessorImpl::Init(OTADownloader * downloader)
{
VerifyOrReturnError(downloader != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
mDownloader = downloader;
OtaHookInit();
return CHIP_NO_ERROR;
}
void OTAImageProcessorImpl::Clear()
{
mHeaderParser.Clear();
mAccumulator.Clear();
mParams.totalFileBytes = 0;
mParams.downloadedBytes = 0;
mCurrentProcessor = nullptr;
ReleaseBlock();
}
CHIP_ERROR OTAImageProcessorImpl::PrepareDownload()
{
DeviceLayer::PlatformMgr().ScheduleWork(HandlePrepareDownload, reinterpret_cast<intptr_t>(this));
return CHIP_NO_ERROR;
}
CHIP_ERROR OTAImageProcessorImpl::Finalize()
{
DeviceLayer::PlatformMgr().ScheduleWork(HandleFinalize, reinterpret_cast<intptr_t>(this));
return CHIP_NO_ERROR;
}
CHIP_ERROR OTAImageProcessorImpl::Apply()
{
DeviceLayer::PlatformMgr().ScheduleWork(HandleApply, reinterpret_cast<intptr_t>(this));
return CHIP_NO_ERROR;
}
CHIP_ERROR OTAImageProcessorImpl::Abort()
{
DeviceLayer::PlatformMgr().ScheduleWork(HandleAbort, reinterpret_cast<intptr_t>(this));
return CHIP_NO_ERROR;
}
CHIP_ERROR OTAImageProcessorImpl::ProcessBlock(ByteSpan & block)
{
if ((block.data() == nullptr) || block.empty())
{
return CHIP_ERROR_INVALID_ARGUMENT;
}
// Store block data for HandleProcessBlock to access
CHIP_ERROR err = SetBlock(block);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Cannot set block data: %" CHIP_ERROR_FORMAT, err.Format());
}
DeviceLayer::PlatformMgr().ScheduleWork(HandleProcessBlock, reinterpret_cast<intptr_t>(this));
return CHIP_NO_ERROR;
}
void OTAImageProcessorImpl::HandlePrepareDownload(intptr_t context)
{
auto * imageProcessor = reinterpret_cast<OTAImageProcessorImpl *>(context);
VerifyOrReturn(imageProcessor != nullptr, ChipLogError(SoftwareUpdate, "ImageProcessor context is null"));
VerifyOrReturn(imageProcessor->mDownloader != nullptr, ChipLogError(SoftwareUpdate, "mDownloader is null"));
GetRequestorInstance()->GetProviderLocation(imageProcessor->mBackupProviderLocation);
imageProcessor->mHeaderParser.Init();
imageProcessor->mAccumulator.Init(sizeof(OTATlvHeader));
imageProcessor->mDownloader->OnPreparedForDownload(CHIP_NO_ERROR);
}
CHIP_ERROR OTAImageProcessorImpl::ProcessHeader(ByteSpan & block)
{
OTAImageHeader header;
ReturnErrorOnFailure(mHeaderParser.AccumulateAndDecode(block, header));
mParams.totalFileBytes = header.mPayloadSize;
mHeaderParser.Clear();
ChipLogError(SoftwareUpdate, "Processed header successfully");
return CHIP_NO_ERROR;
}
CHIP_ERROR OTAImageProcessorImpl::ProcessPayload(ByteSpan & block)
{
CHIP_ERROR status = CHIP_NO_ERROR;
while (true)
{
if (!mCurrentProcessor)
{
ReturnErrorOnFailure(mAccumulator.Accumulate(block));
ByteSpan tlvHeader{ mAccumulator.data(), sizeof(OTATlvHeader) };
ReturnErrorOnFailure(SelectProcessor(tlvHeader));
ReturnErrorOnFailure(mCurrentProcessor->Init());
}
status = mCurrentProcessor->Process(block);
if (status == CHIP_ERROR_OTA_CHANGE_PROCESSOR)
{
mAccumulator.Clear();
mAccumulator.Init(sizeof(OTATlvHeader));
mCurrentProcessor = nullptr;
// If the block size is 0, it means that the processed data was a multiple of
// received BDX block size (e.g. 8 blocks of 1024 bytes were transferred).
// After state for selecting next processor is reset, a request for fetching next
// data must be sent.
if (block.size() == 0)
{
status = CHIP_NO_ERROR;
break;
}
}
else
{
break;
}
}
return status;
}
CHIP_ERROR OTAImageProcessorImpl::SelectProcessor(ByteSpan & block)
{
OTATlvHeader header;
Encoding::LittleEndian::Reader reader(block.data(), sizeof(header));
ReturnErrorOnFailure(reader.Read32(&header.tag).StatusCode());
ReturnErrorOnFailure(reader.Read32(&header.length).StatusCode());
auto pair = mProcessorMap.find(header.tag);
if (pair == mProcessorMap.end())
{
ChipLogError(SoftwareUpdate, "There is no registered processor for tag: %" PRIu32, header.tag);
return CHIP_ERROR_OTA_PROCESSOR_NOT_REGISTERED;
}
ChipLogDetail(SoftwareUpdate, "Selected processor with tag: %ld", pair->first);
mCurrentProcessor = pair->second;
mCurrentProcessor->SetLength(header.length);
mCurrentProcessor->SetWasSelected(true);
return CHIP_NO_ERROR;
}
CHIP_ERROR OTAImageProcessorImpl::RegisterProcessor(uint32_t tag, OTATlvProcessor * processor)
{
auto pair = mProcessorMap.find(tag);
if (pair != mProcessorMap.end())
{
ChipLogError(SoftwareUpdate, "A processor for tag %" PRIu32 " is already registered.", tag);
return CHIP_ERROR_OTA_PROCESSOR_ALREADY_REGISTERED;
}
mProcessorMap.insert({ tag, processor });
return CHIP_NO_ERROR;
}
void OTAImageProcessorImpl::SetRebootDelaySec(uint16_t rebootDelay)
{
mDelayBeforeRebootSec = rebootDelay;
}
void OTAImageProcessorImpl::HandleAbort(intptr_t context)
{
ChipLogError(SoftwareUpdate, "OTA was aborted");
auto * imageProcessor = reinterpret_cast<OTAImageProcessorImpl *>(context);
if (imageProcessor != nullptr)
{
imageProcessor->AbortAllProcessors();
}
imageProcessor->Clear();
OtaHookAbort();
}
void OTAImageProcessorImpl::HandleProcessBlock(intptr_t context)
{
auto * imageProcessor = reinterpret_cast<OTAImageProcessorImpl *>(context);
VerifyOrReturn(imageProcessor != nullptr, ChipLogError(SoftwareUpdate, "ImageProcessor context is null"));
VerifyOrReturn(imageProcessor->mDownloader != nullptr, ChipLogError(SoftwareUpdate, "mDownloader is null"));
CHIP_ERROR status;
auto block = ByteSpan(imageProcessor->mBlock.data(), imageProcessor->mBlock.size());
if (imageProcessor->mHeaderParser.IsInitialized())
{
status = imageProcessor->ProcessHeader(block);
if (status != CHIP_NO_ERROR)
{
imageProcessor->HandleStatus(status);
}
}
status = imageProcessor->ProcessPayload(block);
imageProcessor->HandleStatus(status);
}
void OTAImageProcessorImpl::HandleStatus(CHIP_ERROR status)
{
if (status == CHIP_NO_ERROR || status == CHIP_ERROR_BUFFER_TOO_SMALL)
{
mParams.downloadedBytes += mBlock.size();
FetchNextData(0);
}
else if (status == CHIP_ERROR_OTA_FETCH_ALREADY_SCHEDULED)
{
mParams.downloadedBytes += mBlock.size();
}
else
{
ChipLogError(SoftwareUpdate, "Image update canceled. Failed to process OTA block: %s", ErrorStr(status));
GetRequestorInstance()->CancelImageUpdate();
}
}
void OTAImageProcessorImpl::AbortAllProcessors()
{
ChipLogError(SoftwareUpdate, "All selected processors will call abort action");
for (auto const & pair : mProcessorMap)
{
if (pair.second->WasSelected())
{
pair.second->AbortAction();
pair.second->Clear();
pair.second->SetWasSelected(false);
}
}
}
bool OTAImageProcessorImpl::IsFirstImageRun()
{
OTARequestorInterface * requestor = chip::GetRequestorInstance();
if (requestor == nullptr)
{
return false;
}
return requestor->GetCurrentUpdateState() == OTARequestorInterface::OTAUpdateStateEnum::kApplying;
}
CHIP_ERROR OTAImageProcessorImpl::ConfirmCurrentImage()
{
uint32_t currentVersion;
uint32_t targetVersion;
OTARequestorInterface * requestor = chip::GetRequestorInstance();
VerifyOrReturnError(requestor != nullptr, CHIP_ERROR_INTERNAL);
targetVersion = requestor->GetTargetVersion();
ReturnErrorOnFailure(DeviceLayer::ConfigurationMgr().GetSoftwareVersion(currentVersion));
if (currentVersion != targetVersion)
{
ChipLogError(SoftwareUpdate, "Current sw version %" PRIu32 " is different than the expected sw version = %" PRIu32,
currentVersion, targetVersion);
return CHIP_ERROR_INCORRECT_STATE;
}
return CHIP_NO_ERROR;
}
CHIP_ERROR OTAImageProcessorImpl::SetBlock(ByteSpan & block)
{
if (!IsSpanUsable(block))
{
return CHIP_NO_ERROR;
}
if (mBlock.size() < block.size())
{
if (!mBlock.empty())
{
ReleaseBlock();
}
uint8_t * mBlock_ptr = static_cast<uint8_t *>(chip::Platform::MemoryAlloc(block.size()));
if (mBlock_ptr == nullptr)
{
return CHIP_ERROR_NO_MEMORY;
}
mBlock = MutableByteSpan(mBlock_ptr, block.size());
}
CHIP_ERROR err = CopySpanToMutableSpan(block, mBlock);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Cannot copy block data: %" CHIP_ERROR_FORMAT, err.Format());
return err;
}
return CHIP_NO_ERROR;
}
void OTAImageProcessorImpl::HandleFinalize(intptr_t context)
{
auto * imageProcessor = reinterpret_cast<OTAImageProcessorImpl *>(context);
if (imageProcessor == nullptr)
{
return;
}
imageProcessor->ReleaseBlock();
}
void OTAImageProcessorImpl::HandleApply(intptr_t context)
{
CHIP_ERROR error = CHIP_NO_ERROR;
auto * imageProcessor = reinterpret_cast<OTAImageProcessorImpl *>(context);
if (imageProcessor == nullptr)
{
return;
}
for (auto const & pair : imageProcessor->mProcessorMap)
{
if (pair.second->WasSelected())
{
error = pair.second->ApplyAction();
if (error != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Apply action for tag %d processor failed.", (uint8_t) pair.first);
// Revert all previously applied actions if current apply action fails.
// Reset image processor and requestor states.
imageProcessor->AbortAllProcessors();
imageProcessor->Clear();
GetRequestorInstance()->Reset();
return;
}
}
}
for (auto const & pair : imageProcessor->mProcessorMap)
{
pair.second->Clear();
pair.second->SetWasSelected(false);
}
imageProcessor->mAccumulator.Clear();
ConfigurationManagerImpl().StoreSoftwareUpdateCompleted();
PlatformMgr().HandleServerShuttingDown();
/*
* Set the necessary information to inform the SSBL/bootloader that a new image
* is available and trigger the actual device reboot after some time, to take
* into account queued actions, e.g. sending events to a subscription.
*/
SystemLayer().StartTimer(
chip::System::Clock::Milliseconds32(imageProcessor->mDelayBeforeRebootSec * 1000 + CHIP_DEVICE_LAYER_OTA_REBOOT_DELAY),
[](chip::System::Layer *, void *) { OtaHookReset(); }, nullptr);
}
CHIP_ERROR OTAImageProcessorImpl::ReleaseBlock()
{
if (mBlock.data() != nullptr)
{
chip::Platform::MemoryFree(mBlock.data());
}
mBlock = MutableByteSpan();
return CHIP_NO_ERROR;
}
void OTAImageProcessorImpl::FetchNextData(uint32_t context)
{
auto * imageProcessor = &OTAImageProcessorImpl::GetDefaultInstance();
SystemLayer().ScheduleLambda([imageProcessor] {
if (imageProcessor->mDownloader)
{
imageProcessor->mDownloader->FetchNextData();
}
});
}
OTAImageProcessorImpl & OTAImageProcessorImpl::GetDefaultInstance()
{
return gImageProcessor;
}
} // namespace chip