@@ -267,21 +267,22 @@ static int cmd_update_xmodem(const char *args)
267267{
268268 int ret = -1 ;
269269 uint8_t xpkt [XMODEM_PACKET_SIZE ];
270- uint32_t dst_flash = ( uint32_t ) WOLFBOOT_PARTITION_UPDATE_ADDRESS ;
270+ uint32_t dst_offset = 0 ;
271271 uint8_t pkt_num = 0 , pkt_num_expected = 0xFF ;
272272 uint32_t pkt_size = XMODEM_PACKET_SIZE ;
273+ uint32_t t_size = 0 ;
273274 uint32_t update_ver = 0 ;
274275 uint32_t now = jiffies ;
275276 uint32_t i = 0 ;
276277 uint8_t pkt_num_inv ;
277278 uint8_t crc , calc_crc ;
278279 int transfer_started = 0 ;
280+ int eot_expected = 0 ;
279281
280282
281283 printf ("Erasing update partition..." );
282284 fflush (stdout );
283- hal_flash_unlock ();
284- hal_flash_erase (dst_flash , WOLFBOOT_PARTITION_SIZE );
285+ wolfBoot_nsc_erase_update (dst_offset , WOLFBOOT_PARTITION_SIZE );
285286 printf ("Done.\r\n" );
286287
287288 printf ("Waiting for XMODEM transfer...\r\n" );
@@ -303,6 +304,8 @@ static int cmd_update_xmodem(const char *args)
303304 }
304305 } else {
305306 now = jiffies ;
307+ if (i == 0 && xpkt [0 ] == XEOT )
308+ break ;
306309 i += ret ;
307310 }
308311 }
@@ -313,6 +316,12 @@ static int cmd_update_xmodem(const char *args)
313316 extra_led_on ();
314317 break ;
315318 }
319+ else if (eot_expected ) {
320+ ret = 1 ;
321+ uart_tx (XNAK );
322+ break ;
323+ }
324+
316325 if (xpkt [0 ] != XSOH ) {
317326 continue ;
318327 }
@@ -335,10 +344,9 @@ static int cmd_update_xmodem(const char *args)
335344 crc = xpkt [XMODEM_PACKET_SIZE - 1 ];
336345 calc_crc = crc8 (xpkt , XMODEM_PACKET_SIZE - 1 );
337346 if (crc == calc_crc ) {
338- uint32_t t_size ;
339347 /* CRC is valid */
340348 memcpy (xpkt_payload , xpkt + 3 , XMODEM_PAYLOAD_SIZE );
341- ret = hal_flash_write ( dst_flash , xpkt_payload , XMODEM_PAYLOAD_SIZE );
349+ ret = wolfBoot_nsc_write_update ( dst_offset , xpkt_payload , XMODEM_PAYLOAD_SIZE );
342350 if (ret != 0 ) {
343351 xcancel ();
344352 printf ("Error writing to flash\r\n" );
@@ -347,15 +355,16 @@ static int cmd_update_xmodem(const char *args)
347355 uart_tx (XACK );
348356 pkt_num ++ ;
349357 pkt_num_expected ++ ;
350- dst_flash += XMODEM_PAYLOAD_SIZE ;
351- t_size = * ((uint32_t * )(WOLFBOOT_PARTITION_UPDATE_ADDRESS + 4 ));
352- t_size += IMAGE_HEADER_SIZE ;
353- if ((uint32_t )dst_flash >= (WOLFBOOT_PARTITION_UPDATE_ADDRESS + t_size )) {
354- ret = 0 ;
355- extra_led_off ();
356- break ;
358+ dst_offset += XMODEM_PAYLOAD_SIZE ;
359+ if (t_size == 0 ) {
360+ /* At first packet, save expected partition size */
361+ t_size = * (uint32_t * )(xpkt_payload + 4 );
362+ t_size += IMAGE_HEADER_SIZE ;
357363 }
358- uart_tx (XACK );
364+ if (dst_offset >= t_size ) {
365+ eot_expected = 1 ;
366+ }
367+ /*uart_tx(XACK);*/
359368 } else {
360369 uart_tx (XNAK );
361370 }
@@ -367,17 +376,22 @@ static int cmd_update_xmodem(const char *args)
367376 uart_tx ('\r' );
368377
369378 printf ("End of transfer. ret: %d\r\n" , ret );
370- update_ver = wolfBoot_update_firmware_version ();
371- if (update_ver != 0 ) {
372- printf ("New firmware version: 0x%lx\r\n" , update_ver );
373- printf ("Triggering update...\r\n" );
374- wolfBoot_update_trigger ();
375- printf ("Update completed successfully.\r\n" );
376- } else {
377- printf ("No valid image in update partition\r\n" );
379+ if (ret != 0 ) {
380+ printf ("Transfer failed\r\n" );
381+ }
382+ else {
383+ printf ("Transfer succeeded\r\n" );
384+ update_ver = wolfBoot_nsc_update_firmware_version ();
385+ if (update_ver != 0 ) {
386+ printf ("New firmware version: 0x%lx\r\n" , update_ver );
387+ printf ("Triggering update...\r\n" );
388+ wolfBoot_nsc_update_trigger ();
389+ printf ("Update written successfully. Reboot to apply.\r\n" );
390+ } else {
391+ printf ("No valid image in update partition\r\n" );
392+ }
378393 }
379394
380- hal_flash_lock ();
381395 return ret ;
382396}
383397
@@ -427,17 +441,17 @@ static int cmd_info(const char *args)
427441 uint16_t hdrSz ;
428442 uint8_t boot_part_state = IMG_STATE_NEW , update_part_state = IMG_STATE_NEW ;
429443
430- cur_fw_version = wolfBoot_current_firmware_version ();
431- update_fw_version = wolfBoot_update_firmware_version ();
444+ cur_fw_version = wolfBoot_nsc_current_firmware_version ();
445+ update_fw_version = wolfBoot_nsc_update_firmware_version ();
432446
433- wolfBoot_get_partition_state (PART_BOOT , & boot_part_state );
434- wolfBoot_get_partition_state (PART_UPDATE , & update_part_state );
447+ wolfBoot_nsc_get_partition_state (PART_BOOT , & boot_part_state );
448+ wolfBoot_nsc_get_partition_state (PART_UPDATE , & update_part_state );
435449
436450 printf ("\r\n" );
437451 printf ("System information\r\n" );
438452 printf ("====================================\r\n" );
439453 printf ("Flash banks are %sswapped.\r\n" , ((FLASH_OPTSR_CUR & (FLASH_OPTSR_SWAP_BANK )) == 0 )?"not " :"" );
440- printf ("Firmware version : 0x%lx\r\n" , wolfBoot_current_firmware_version () );
454+ printf ("Firmware version : 0x%lx\r\n" , cur_fw_version );
441455 printf ("Current firmware state: %s\r\n" , part_state_name (boot_part_state ));
442456 if (update_fw_version != 0 ) {
443457 if (update_part_state == IMG_STATE_UPDATING )
@@ -482,7 +496,7 @@ static int cmd_info(const char *args)
482496
483497static int cmd_success (const char * args )
484498{
485- wolfBoot_success ();
499+ wolfBoot_nsc_success ();
486500 printf ("update success confirmed.\r\n" );
487501 return 0 ;
488502}
@@ -741,14 +755,13 @@ void main(void)
741755 int ret ;
742756 uint32_t app_version ;
743757
744-
745758 /* Turn on boot LED */
746759 boot_led_on ();
747760
748761 /* Enable SysTick */
749762 systick_enable ();
750763
751- app_version = wolfBoot_current_firmware_version ();
764+ app_version = wolfBoot_nsc_current_firmware_version ();
752765
753766 nvic_irq_setprio (NVIC_USART3_IRQN , 0 );
754767 nvic_irq_enable (NVIC_USART3_IRQN );
0 commit comments