@@ -428,21 +428,24 @@ static int get_proxmark_state(uint32_t *state) {
428428
429429// Enter the bootloader to be able to start flashing
430430static int enter_bootloader (char * serial_port_name , bool wait_appear ) {
431- uint32_t state ;
432- int ret ;
433431
434- if ((ret = get_proxmark_state (& state )) != PM3_SUCCESS )
432+ uint32_t state = 0 ;
433+ int ret = get_proxmark_state (& state );
434+ if (ret != PM3_SUCCESS ) {
435435 return ret ;
436+ }
436437
437438 /* Already in flash state, we're done. */
438- if (state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM )
439+ if (( state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM ) == DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM ) {
439440 return PM3_SUCCESS ;
441+ }
440442
441- if (state & DEVICE_INFO_FLAG_CURRENT_MODE_OS ) {
443+ if (( state & DEVICE_INFO_FLAG_CURRENT_MODE_OS ) == DEVICE_INFO_FLAG_CURRENT_MODE_OS ) {
442444 PrintAndLogEx (SUCCESS , _CYAN_ ("Entering bootloader..." ));
443445
444- if ((state & DEVICE_INFO_FLAG_BOOTROM_PRESENT )
445- && (state & DEVICE_INFO_FLAG_OSIMAGE_PRESENT )) {
446+ if (
447+ ((state & DEVICE_INFO_FLAG_BOOTROM_PRESENT ) == DEVICE_INFO_FLAG_BOOTROM_PRESENT ) &&
448+ ((state & DEVICE_INFO_FLAG_OSIMAGE_PRESENT ) == DEVICE_INFO_FLAG_OSIMAGE_PRESENT )) {
446449 // New style handover: Send CMD_START_FLASH, which will reset the board
447450 // and enter the bootrom on the next boot.
448451 SendCommandBL (CMD_START_FLASH , 0 , 0 , 0 , NULL , 0 );
@@ -453,6 +456,7 @@ static int enter_bootloader(char *serial_port_name, bool wait_appear) {
453456 PrintAndLogEx (SUCCESS , "Press and hold down button NOW if your bootloader requires it." );
454457 }
455458 msleep (500 );
459+ PrintAndLogEx (SUCCESS , _CYAN_ ("Trigger restart..." ));
456460 CloseProxmark (g_session .current_device );
457461 // Let time to OS to make the port disappear
458462 msleep (1000 );
@@ -487,8 +491,9 @@ static int wait_for_ack(PacketResponseNG *ack) {
487491
488492static bool gs_printed_msg = false;
489493static void flash_suggest_update_bootloader (void ) {
490- if (gs_printed_msg )
494+ if (gs_printed_msg ) {
491495 return ;
496+ }
492497
493498 PrintAndLogEx (ERR , _RED_ ("It is recommended that you first" _YELLOW_ (" update your bootloader" ) _RED_ (" alone," )));
494499 PrintAndLogEx (ERR , _RED_ ("reboot the Proxmark3 then only update the main firmware" ) "\n" );
@@ -510,31 +515,36 @@ static void flash_suggest_update_flasher(void) {
510515
511516// Go into flashing mode
512517int flash_start_flashing (int enable_bl_writes , char * serial_port_name , uint32_t * max_allowed ) {
513- uint32_t state ;
514- uint32_t chipinfo = 0 ;
515- int ret ;
516518
517- ret = enter_bootloader (serial_port_name , true);
518- if (ret != PM3_SUCCESS )
519+ int ret = enter_bootloader (serial_port_name , true);
520+ if (ret != PM3_SUCCESS ) {
519521 return ret ;
522+ }
520523
524+ uint32_t state ;
521525 ret = get_proxmark_state (& state );
522- if (ret != PM3_SUCCESS )
526+ if (ret != PM3_SUCCESS ) {
523527 return ret ;
528+ }
529+
530+ uint32_t chipinfo = 0 ;
524531
525- if (state & DEVICE_INFO_FLAG_UNDERSTANDS_CHIP_INFO ) {
532+ if (( state & DEVICE_INFO_FLAG_UNDERSTANDS_CHIP_INFO ) == DEVICE_INFO_FLAG_UNDERSTANDS_CHIP_INFO ) {
526533 SendCommandBL (CMD_CHIP_INFO , 0 , 0 , 0 , NULL , 0 );
527534 PacketResponseNG resp ;
528535 WaitForResponse (CMD_CHIP_INFO , & resp );
529536 chipinfo = resp .oldarg [0 ];
530537 }
531538
532539 int version = BL_VERSION_INVALID ;
533- if (state & DEVICE_INFO_FLAG_UNDERSTANDS_VERSION ) {
540+
541+ if ((state & DEVICE_INFO_FLAG_UNDERSTANDS_VERSION ) == DEVICE_INFO_FLAG_UNDERSTANDS_VERSION ) {
542+
534543 SendCommandBL (CMD_BL_VERSION , 0 , 0 , 0 , NULL , 0 );
535544 PacketResponseNG resp ;
536545 WaitForResponse (CMD_BL_VERSION , & resp );
537546 version = resp .oldarg [0 ];
547+
538548 if ((BL_VERSION_MAJOR (version ) < BL_VERSION_FIRST_MAJOR ) || (BL_VERSION_MAJOR (version ) > BL_VERSION_LAST_MAJOR )) {
539549 // version info seems fishy
540550 version = BL_VERSION_INVALID ;
@@ -562,7 +572,9 @@ int flash_start_flashing(int enable_bl_writes, char *serial_port_name, uint32_t
562572
563573 int mem_avail = chipid_to_mem_avail (chipinfo );
564574 if (mem_avail != 0 ) {
575+
565576 PrintAndLogEx (INFO , "Available memory on this board: " _YELLOW_ ("%uK" ) " bytes\n" , mem_avail );
577+
566578 if (mem_avail > 256 ) {
567579 if (BL_VERSION_MAJOR (version ) < BL_VERSION_MAJOR (BL_VERSION_1_0_0 )) {
568580 PrintAndLogEx (ERR , _RED_ ("====================== OBS ! ======================" ));
@@ -573,6 +585,7 @@ int flash_start_flashing(int enable_bl_writes, char *serial_port_name, uint32_t
573585 * max_allowed = mem_avail ;
574586 }
575587 }
588+
576589 } else {
577590 PrintAndLogEx (INFO , "Available memory on this board: " _RED_ ("UNKNOWN" )"\n" );
578591 PrintAndLogEx (ERR , _RED_ ("====================== OBS ! ======================================" ));
@@ -585,15 +598,17 @@ int flash_start_flashing(int enable_bl_writes, char *serial_port_name, uint32_t
585598 } else {
586599 PrintAndLogEx (INFO , "Permitted flash range: 0x%08x-0x%08x" , BOOTLOADER_END , flash_end );
587600 }
588- if ( state & DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH ) {
589- PacketResponseNG resp ;
601+
602+ if (( state & DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH ) == DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH ) {
590603
591604 if (enable_bl_writes ) {
592605 SendCommandBL (CMD_START_FLASH , FLASH_START , flash_end , START_FLASH_MAGIC , NULL , 0 );
593606 } else {
594607 SendCommandBL (CMD_START_FLASH , BOOTLOADER_END , flash_end , 0 , NULL , 0 );
595608 }
609+ PacketResponseNG resp ;
596610 return wait_for_ack (& resp );
611+
597612 } else {
598613 PrintAndLogEx (ERR , _RED_ ("====================== OBS ! ========================================" ));
599614 PrintAndLogEx (ERR , _RED_ ("Note: Your bootloader does not understand the new" _YELLOW_ (" START_FLASH" ) _RED_ (" command" )));
@@ -664,8 +679,9 @@ int flash_write(flash_file_t *ctx) {
664679
665680 while (length ) {
666681 uint32_t block_size = length ;
667- if (block_size > BLOCK_SIZE )
682+ if (block_size > BLOCK_SIZE ) {
668683 block_size = BLOCK_SIZE ;
684+ }
669685
670686 if (write_block (baddr , data , block_size ) < 0 ) {
671687 PrintAndLogEx (ERR , "Error writing block %d of %u" , block , blocks );
@@ -676,9 +692,11 @@ int flash_write(flash_file_t *ctx) {
676692 baddr += block_size ;
677693 length -= block_size ;
678694 block ++ ;
695+
679696 if (len < ice3len ) {
680697 fprintf (stdout , "%c" , ice3 [len ++ ]);
681698 } else {
699+
682700 if ((len - ice3len ) % 67 == 0 ) {
683701 fprintf (stdout , "\n" );
684702 }
@@ -695,21 +713,29 @@ int flash_write(flash_file_t *ctx) {
695713
696714// free a file context
697715void flash_free (flash_file_t * ctx ) {
698- if (!ctx )
716+
717+ if (!ctx ) {
699718 return ;
719+ }
720+
700721 if (ctx -> filename != NULL ) {
701722 free (ctx -> filename );
702723 ctx -> filename = NULL ;
703724 }
725+
704726 if (ctx -> elf ) {
705727 free (ctx -> elf );
706728 ctx -> elf = NULL ;
707729 ctx -> phdrs = NULL ;
708730 ctx -> num_phdrs = 0 ;
709731 }
732+
710733 if (ctx -> segments ) {
711- for (int i = 0 ; i < ctx -> num_segs ; i ++ )
734+
735+ for (int i = 0 ; i < ctx -> num_segs ; i ++ ) {
712736 free (ctx -> segments [i ].data );
737+ }
738+
713739 free (ctx -> segments );
714740 ctx -> segments = NULL ;
715741 ctx -> num_segs = 0 ;
0 commit comments