19
19
#include < app/clusters/ota-requestor/OTADownloader.h>
20
20
#include < app/clusters/ota-requestor/OTARequestorInterface.h>
21
21
#include < lib/support/logging/CHIPLogging.h>
22
-
23
22
#include < platform/Ameba/AmebaOTAImageProcessor.h>
23
+ #include < platform/Ameba/AmebaUtils.h>
24
24
25
- #if defined(CONFIG_PLATFORM_8710C)
26
- static flash_t flash_ota;
27
- #endif
25
+ using namespace chip ::DeviceLayer::Internal;
28
26
29
27
namespace chip {
30
28
@@ -127,18 +125,8 @@ void AmebaOTAImageProcessor::HandlePrepareDownload(intptr_t context)
127
125
128
126
imageProcessor->mHeaderParser .Init ();
129
127
130
- // Get OTA update partition
131
- #if defined(CONFIG_PLATFORM_8721D)
132
- if (ota_get_cur_index () == OTA_INDEX_1)
133
- imageProcessor->ota_target_index = OTA_INDEX_2;
134
- else if (ota_get_cur_index () == OTA_INDEX_2)
135
- imageProcessor->ota_target_index = OTA_INDEX_1;
136
- ChipLogProgress (SoftwareUpdate, " OTA%d address space will be upgraded" , imageProcessor->ota_target_index + 1 );
137
- #elif defined(CONFIG_PLATFORM_8710C)
138
- imageProcessor->flash_addr = sys_update_ota_prepare_addr ();
139
- ChipLogProgress (SoftwareUpdate, " New Flash Address: 0x%X" , imageProcessor->flash_addr );
140
- #endif
141
-
128
+ // prepare OTA update partition
129
+ matter_ota_prepare_partition ();
142
130
imageProcessor->mDownloader ->OnPreparedForDownload (CHIP_NO_ERROR);
143
131
}
144
132
@@ -151,15 +139,11 @@ void AmebaOTAImageProcessor::HandleFinalize(intptr_t context)
151
139
return ;
152
140
}
153
141
154
- // Verify checksum
155
- #if defined(CONFIG_PLATFORM_8721D)
156
- if (verify_ota_checksum (imageProcessor->pOtaTgtHdr ) != 1 )
142
+ if (AmebaUtils::MapError (matter_ota_flush_last (), AmebaErrorType::kFlashError ) != CHIP_NO_ERROR)
157
143
{
158
- ota_update_free (imageProcessor->pOtaTgtHdr );
159
- ChipLogError (SoftwareUpdate, " OTA checksum verification failed" );
144
+ ChipLogError (SoftwareUpdate, " Failed to finalize OTA" );
160
145
return ;
161
146
}
162
- #endif
163
147
164
148
imageProcessor->ReleaseBlock ();
165
149
@@ -175,24 +159,8 @@ void AmebaOTAImageProcessor::HandleAbort(intptr_t context)
175
159
return ;
176
160
}
177
161
178
- // Abort OTA procedure
179
- #if defined(CONFIG_PLATFORM_8721D)
180
- ChipLogProgress (SoftwareUpdate, " Erasing target partition..." );
181
- erase_ota_target_flash (imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].FlashAddr , imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].ImgLen );
182
- ChipLogProgress (SoftwareUpdate, " Erased partition OTA%d" , imageProcessor->ota_target_index + 1 );
183
- #elif defined(CONFIG_PLATFORM_8710C)
184
- ChipLogProgress (SoftwareUpdate, " Erasing partition" );
185
- imageProcessor->NewFWBlkSize = ((0x1AC000 - 1 ) / 4096 ) + 1 ; // Use a fixed image length of 0x1AC000, change in the future
186
- ChipLogProgress (SoftwareUpdate, " Erasing %d sectors" , imageProcessor->NewFWBlkSize );
187
- device_mutex_lock (RT_DEV_LOCK_FLASH);
188
- for (int i = 0 ; i < imageProcessor->NewFWBlkSize ; i++)
189
- flash_erase_sector (&flash_ota, imageProcessor->flash_addr + i * 4096 );
190
- device_mutex_unlock (RT_DEV_LOCK_FLASH);
191
- #endif
192
-
193
- #if defined(CONFIG_PLATFORM_8721D)
194
- ota_update_free (imageProcessor->pOtaTgtHdr );
195
- #endif
162
+ // to make flash erase non-blocking, create background task to cleanup flash, will self-delete upon completion
163
+ matter_ota_create_abort_task ();
196
164
197
165
imageProcessor->ReleaseBlock ();
198
166
}
@@ -211,185 +179,47 @@ void AmebaOTAImageProcessor::HandleProcessBlock(intptr_t context)
211
179
return ;
212
180
}
213
181
214
- ByteSpan block = imageProcessor->mBlock ;
215
- CHIP_ERROR error = imageProcessor->ProcessHeader (block);
216
- uint8_t HeaderOffset = 32 - imageProcessor->RemainHeader ;
182
+ ByteSpan block = imageProcessor->mBlock ;
183
+ CHIP_ERROR error = imageProcessor->ProcessHeader (block); // process matter ota header
184
+ uint8_t remainHeader =
185
+ matter_ota_get_total_header_size () - matter_ota_get_current_header_size (); // size of ameba header received
186
+ uint16_t writeLength = block.size (); // length of actual data to write to flash, excluding header
187
+ uint8_t * writePointer = (uint8_t *) block.data (); // pointer to the actual data to write to flash, excluding header
217
188
218
- # if defined(CONFIG_PLATFORM_8721D)
219
- if (imageProcessor-> RemainHeader != 0 ) // Still not yet received full ameba header
189
+ // 1. Check if 32-bytes Ameba header has already been received
190
+ if (remainHeader > 0 )
220
191
{
221
- if (block.size () >= imageProcessor-> RemainHeader )
192
+ if (block.size () >= remainHeader )
222
193
{
223
- memcpy (imageProcessor->AmebaHeader + HeaderOffset, block.data (), imageProcessor->RemainHeader );
224
- imageProcessor->pOtaTgtHdr = (update_ota_target_hdr *) ota_update_malloc (sizeof (update_ota_target_hdr));
225
-
226
- memcpy (imageProcessor->pOtaTgtHdr , imageProcessor->AmebaHeader , 8 ); // Store FwVer, HdrNum
227
- memcpy (&(imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].ImgId ), imageProcessor->AmebaHeader + 8 , 4 ); // Store OTA id
228
- memcpy (&(imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].ImgHdrLen ), imageProcessor->AmebaHeader + 12 ,
229
- 16 ); // Store ImgHdrLen, Checksum, ImgLen, Offset
230
-
231
- if (imageProcessor->ota_target_index == OTA_INDEX_1)
232
- imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].FlashAddr = LS_IMG2_OTA1_ADDR;
233
- else if (imageProcessor->ota_target_index == OTA_INDEX_2)
234
- imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].FlashAddr = LS_IMG2_OTA2_ADDR;
235
-
236
- imageProcessor->pOtaTgtHdr ->ValidImgCnt = 1 ;
237
-
238
- if (strncmp (" OTA" , (const char *) &(imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].ImgId ), 3 ) != 0 )
194
+ if (AmebaUtils::MapError (matter_ota_store_header ((uint8_t *) block.data (), remainHeader),
195
+ AmebaErrorType::kFlashError ) != CHIP_NO_ERROR)
239
196
{
240
- ota_update_free (imageProcessor->pOtaTgtHdr );
241
- ChipLogError (SoftwareUpdate, " Wrong Image ID for OTA" );
197
+ ChipLogError (SoftwareUpdate, " Failed to store OTA header" );
242
198
return ;
243
199
}
244
-
245
- ChipLogProgress (SoftwareUpdate, " Correct OTA Image ID, get firmware header success" );
246
- ChipLogProgress (SoftwareUpdate, " FwVer: 0x%X" , imageProcessor->pOtaTgtHdr ->FileHdr .FwVer );
247
- ChipLogProgress (SoftwareUpdate, " HdrNum: 0x%X" , imageProcessor->pOtaTgtHdr ->FileHdr .HdrNum );
248
- ChipLogProgress (SoftwareUpdate, " ImgHdrLen: 0x%X" , imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].ImgHdrLen );
249
- ChipLogProgress (SoftwareUpdate, " Checksum: 0x%X" , imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].Checksum );
250
- ChipLogProgress (SoftwareUpdate, " ImgLen: 0x%X" , imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].ImgLen );
251
- ChipLogProgress (SoftwareUpdate, " Offset: 0x%X" , imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].Offset );
252
- ChipLogProgress (SoftwareUpdate, " FlashAddr: 0x%X" , imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].FlashAddr );
253
-
254
- // Erase update partition
255
- ChipLogProgress (SoftwareUpdate, " Erasing target partition..." );
256
- erase_ota_target_flash (imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].FlashAddr ,
257
- imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].ImgLen );
258
- ChipLogProgress (SoftwareUpdate, " Erased partition OTA%d" , imageProcessor->ota_target_index + 1 );
259
-
260
- // Set RemainBytes to image length, excluding 8bytes of signature
261
- imageProcessor->RemainBytes = imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].ImgLen - 8 ;
262
-
263
- // Set flash address, incremented by 8bytes to account for signature
264
- imageProcessor->flash_addr = imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].FlashAddr - SPI_FLASH_BASE + 8 ;
265
-
266
- // Set signature to point to pOtaTgtHdr->Sign
267
- imageProcessor->signature = &(imageProcessor->pOtaTgtHdr ->Sign [0 ][0 ]);
268
-
269
- // Store the signature temporarily
270
- uint8_t * tempbufptr = const_cast <uint8_t *>(block.data () + imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].Offset );
271
- memcpy (imageProcessor->signature , tempbufptr, 8 );
272
- tempbufptr += 8 ;
273
-
274
- // Write remaining downloaded bytes to flash_addr
275
- uint32_t tempsize = block.size () - imageProcessor->pOtaTgtHdr ->FileImgHdr [0 ].Offset - 8 ;
276
- device_mutex_lock (RT_DEV_LOCK_FLASH);
277
- if (ota_writestream_user (imageProcessor->flash_addr + imageProcessor->size , tempsize, tempbufptr) < 0 )
278
- {
279
- ChipLogError (SoftwareUpdate, " Write to flash failed" );
280
- device_mutex_unlock (RT_DEV_LOCK_FLASH);
281
- imageProcessor->mDownloader ->EndDownload (CHIP_ERROR_WRITE_FAILED);
282
- return ;
283
- }
284
- device_mutex_unlock (RT_DEV_LOCK_FLASH);
285
-
286
- imageProcessor->RemainHeader = 0 ;
287
- imageProcessor->size += tempsize;
288
- imageProcessor->RemainBytes -= tempsize;
200
+ writeLength -= remainHeader;
201
+ writePointer += remainHeader;
289
202
}
290
- else // block.size < imageProcessor->RemainHeader
291
- {
292
- memcpy (imageProcessor->AmebaHeader + HeaderOffset, block.data (), block.size ());
293
- imageProcessor->RemainHeader -= block.size ();
294
- }
295
- }
296
- else // received subsequent blocks
297
- {
298
- device_mutex_lock (RT_DEV_LOCK_FLASH);
299
- if (ota_writestream_user (imageProcessor->flash_addr + imageProcessor->size , block.size (), block.data ()) < 0 )
300
- {
301
- ChipLogError (SoftwareUpdate, " Write to flash failed" );
302
- device_mutex_unlock (RT_DEV_LOCK_FLASH);
303
- imageProcessor->mDownloader ->EndDownload (CHIP_ERROR_WRITE_FAILED);
304
- return ;
305
- }
306
- device_mutex_unlock (RT_DEV_LOCK_FLASH);
307
-
308
- imageProcessor->size += block.size ();
309
- imageProcessor->RemainBytes -= block.size ();
310
- }
311
- #elif defined(CONFIG_PLATFORM_8710C)
312
- if (imageProcessor->RemainHeader != 0 ) // Still not yet received full ameba header
313
- {
314
- if (block.size () >= imageProcessor->RemainHeader )
203
+ else
315
204
{
316
- // Store signature temporarily
317
- memcpy (imageProcessor->signature + HeaderOffset, block.data (), imageProcessor->RemainHeader );
318
-
319
- imageProcessor->block_len = block.size () - 32 ; // minus 32 to account for signature
320
-
321
- // Erase target partition
322
- ChipLogProgress (SoftwareUpdate, " Erasing partition" );
323
- imageProcessor->NewFWBlkSize =
324
- ((0x1AC000 - 1 ) / 4096 ) + 1 ; // Use a fixed image length of 0x1AC000, change in the future
325
- ChipLogProgress (SoftwareUpdate, " Erasing %d sectors" , imageProcessor->NewFWBlkSize );
326
- device_mutex_lock (RT_DEV_LOCK_FLASH);
327
- for (int i = 0 ; i < imageProcessor->NewFWBlkSize ; i++)
328
- flash_erase_sector (&flash_ota, imageProcessor->flash_addr + i * 4096 );
329
- device_mutex_unlock (RT_DEV_LOCK_FLASH);
330
-
331
- // Write first block to target flash
332
- if (imageProcessor->block_len > 0 )
333
- {
334
- device_mutex_lock (RT_DEV_LOCK_FLASH);
335
- if (flash_burst_write (&flash_ota, imageProcessor->flash_addr + 32 , imageProcessor->block_len , block.data () + 32 ) <
336
- 0 )
337
- {
338
- device_mutex_unlock (RT_DEV_LOCK_FLASH);
339
- ChipLogError (SoftwareUpdate, " Write to flash failed" );
340
- imageProcessor->mDownloader ->EndDownload (CHIP_ERROR_WRITE_FAILED);
341
- return ;
342
- }
343
- else
344
- {
345
- imageProcessor->size += imageProcessor->block_len ;
346
- device_mutex_unlock (RT_DEV_LOCK_FLASH);
347
- }
348
- }
349
- else
205
+ if (AmebaUtils::MapError (matter_ota_store_header ((uint8_t *) block.data (), block.size ()),
206
+ AmebaErrorType::kFlashError ) != CHIP_NO_ERROR)
350
207
{
351
- ChipLogError (SoftwareUpdate, " Invalid size" );
352
- imageProcessor->mDownloader ->EndDownload (CHIP_ERROR_WRITE_FAILED);
208
+ ChipLogError (SoftwareUpdate, " Failed to store OTA header" );
353
209
return ;
354
210
}
355
-
356
- imageProcessor->RemainHeader = 0 ;
357
- }
358
- else // block.size() < imageProcessor->RemainHeader
359
- {
360
- memcpy (imageProcessor->AmebaHeader + HeaderOffset, block.data (), block.size ());
361
- imageProcessor->RemainHeader -= block.size ();
211
+ writeLength = 0 ;
362
212
}
363
213
}
364
- else // received subsequent blocks
365
- {
366
- imageProcessor->block_len = block.size ();
367
214
368
- if (imageProcessor->block_len > 0 )
369
- {
370
- device_mutex_lock (RT_DEV_LOCK_FLASH);
371
- if (flash_burst_write (&flash_ota, imageProcessor->flash_addr + 32 + imageProcessor->size , imageProcessor->block_len ,
372
- block.data ()) < 0 )
373
- {
374
- device_mutex_unlock (RT_DEV_LOCK_FLASH);
375
- ChipLogError (SoftwareUpdate, " Write to flash failed" );
376
- imageProcessor->mDownloader ->EndDownload (CHIP_ERROR_WRITE_FAILED);
377
- return ;
378
- }
379
- else
380
- {
381
- imageProcessor->size += imageProcessor->block_len ;
382
- device_mutex_unlock (RT_DEV_LOCK_FLASH);
383
- }
384
- }
385
- else
386
- {
387
- ChipLogError (SoftwareUpdate, " Invalid size" );
388
- imageProcessor->mDownloader ->EndDownload (CHIP_ERROR_WRITE_FAILED);
389
- return ;
390
- }
215
+ // 2. Write data to flash, flash erasure logic will be handled by lower layer
216
+ if (AmebaUtils::MapError (matter_ota_flash_burst_write (writePointer, writeLength), AmebaErrorType::kFlashError ) != CHIP_NO_ERROR)
217
+ {
218
+ ChipLogError (SoftwareUpdate, " OTA write failed" );
219
+ return ;
391
220
}
392
- #endif
221
+
222
+ // 3. Fetch next data block
393
223
imageProcessor->mParams .downloadedBytes += block.size ();
394
224
imageProcessor->mDownloader ->FetchNextData ();
395
225
}
@@ -405,26 +235,12 @@ void AmebaOTAImageProcessor::HandleApply(intptr_t context)
405
235
}
406
236
407
237
// Update signature
408
- #if defined(CONFIG_PLATFORM_8721D)
409
- if (change_ota_signature (imageProcessor->pOtaTgtHdr , imageProcessor->ota_target_index ) != 1 )
238
+ if (AmebaUtils::MapError (matter_ota_update_signature (), AmebaErrorType::kFlashError ) != CHIP_NO_ERROR)
410
239
{
411
- ota_update_free (imageProcessor->pOtaTgtHdr );
412
240
ChipLogError (SoftwareUpdate, " OTA update signature failed" );
413
241
GetRequestorInstance ()->CancelImageUpdate ();
414
242
return ;
415
243
}
416
- #elif defined(CONFIG_PLATFORM_8710C)
417
- if (update_ota_signature (imageProcessor->signature , imageProcessor->flash_addr ) < 0 )
418
- {
419
- ChipLogError (SoftwareUpdate, " OTA update signature failed" );
420
- GetRequestorInstance ()->CancelImageUpdate ();
421
- return ;
422
- }
423
- #endif
424
-
425
- #if defined(CONFIG_PLATFORM_8721D)
426
- ota_update_free (imageProcessor->pOtaTgtHdr );
427
- #endif
428
244
429
245
ChipLogProgress (SoftwareUpdate, " Rebooting in 10 seconds..." );
430
246
@@ -434,7 +250,7 @@ void AmebaOTAImageProcessor::HandleApply(intptr_t context)
434
250
435
251
void AmebaOTAImageProcessor::HandleRestart (chip::System::Layer * systemLayer, void * appState)
436
252
{
437
- ota_platform_reset ();
253
+ matter_ota_platform_reset ();
438
254
}
439
255
440
256
CHIP_ERROR AmebaOTAImageProcessor::ProcessHeader (ByteSpan & block)
0 commit comments