@@ -814,42 +814,36 @@ ra_read(ra_device_t *dev, const char *file, uint32_t start, uint32_t size) {
814814}
815815
816816int
817- ra_verify (ra_device_t * dev , const char * file , uint32_t start , uint32_t size ) {
817+ ra_verify (
818+ ra_device_t * dev , const char * file , uint32_t start , uint32_t size , input_format_t format ) {
818819 uint8_t pkt [MAX_PKT_LEN ];
819820 uint8_t resp [CHUNK_SIZE + 6 ];
820821 uint8_t flash_chunk [CHUNK_SIZE ];
821- uint8_t file_chunk [CHUNK_SIZE ];
822822 uint8_t data [8 ];
823823 uint8_t ack_data [1 ] = { STATUS_OK };
824824 ssize_t pkt_len , n ;
825825 uint32_t end ;
826- int fd ;
827- struct stat st ;
826+ parsed_file_t parsed ;
828827
829- fd = open (file , O_RDONLY );
830- if (fd < 0 ) {
831- warn ("failed to open %s" , file );
828+ if (format_parse (file , format , & parsed ) < 0 )
832829 return -1 ;
833- }
834830
835- if (fstat (fd , & st ) < 0 ) {
836- warn ("failed to stat %s" , file );
837- close (fd );
838- return -1 ;
839- }
831+ /* Use embedded address if available and no explicit address given */
832+ if (start == 0 && parsed .has_addr )
833+ start = parsed .base_addr ;
840834
841- uint32_t file_size = (uint32_t )st . st_size ;
835+ uint32_t file_size = (uint32_t )parsed . size ;
842836 if (size == 0 )
843837 size = file_size ;
844838
845839 if (size > file_size ) {
846840 warnx ("verify size (%u) > file size (%u)" , size , file_size );
847- close ( fd );
841+ free ( parsed . data );
848842 return -1 ;
849843 }
850844
851845 if (set_read_boundaries (dev , start , size , & end ) < 0 ) {
852- close ( fd );
846+ free ( parsed . data );
853847 return -1 ;
854848 }
855849
@@ -858,78 +852,73 @@ ra_verify(ra_device_t *dev, const char *file, uint32_t start, uint32_t size) {
858852
859853 pkt_len = ra_pack_pkt (pkt , sizeof (pkt ), REA_CMD , data , 8 , false);
860854 if (pkt_len < 0 ) {
861- close ( fd );
855+ free ( parsed . data );
862856 return -1 ;
863857 }
864858
865859 if (ra_send (dev , pkt , pkt_len ) < 0 ) {
866- close ( fd );
860+ free ( parsed . data );
867861 return -1 ;
868862 }
869863
870864 uint32_t total_size = end - start + 1 ;
871865 uint32_t nr_packets = (end - start ) / CHUNK_SIZE ;
872866 uint32_t current_addr = start ;
867+ uint32_t file_offset = 0 ;
873868 progress_t prog ;
874869 progress_init (& prog , nr_packets + 1 , "Verifying" );
875870
876871 for (uint32_t i = 0 ; i <= nr_packets ; i ++ ) {
877872 n = ra_recv (dev , resp , CHUNK_SIZE + 6 , 1000 );
878873 if (n < 7 ) {
879874 warnx ("short response during verify (%zd bytes)" , n );
880- close ( fd );
875+ free ( parsed . data );
881876 return -1 ;
882877 }
883878
884879 size_t chunk_len ;
885880 if (unpack_with_error (resp , n , flash_chunk , & chunk_len , "verify read" ) < 0 ) {
886- close (fd );
887- return -1 ;
888- }
889-
890- /* Read corresponding chunk from file */
891- ssize_t file_read = read (fd , file_chunk , chunk_len );
892- if (file_read < 0 ) {
893- warn ("read from file failed" );
894- close (fd );
881+ free (parsed .data );
895882 return -1 ;
896883 }
897884
898- /* Compare flash data with file data */
899- size_t cmp_len = (size_t )file_read < chunk_len ? (size_t )file_read : chunk_len ;
885+ /* Compare flash data with file data from parsed buffer */
886+ size_t remaining = parsed .size - file_offset ;
887+ size_t cmp_len = remaining < chunk_len ? remaining : chunk_len ;
900888 for (size_t j = 0 ; j < cmp_len ; j ++ ) {
901- if (flash_chunk [j ] != file_chunk [ j ]) {
889+ if (flash_chunk [j ] != parsed . data [ file_offset + j ]) {
902890 progress_finish (& prog );
903891 warnx ("verify FAILED at 0x%08X: flash=0x%02X, file=0x%02X" ,
904892 current_addr + (uint32_t )j ,
905893 flash_chunk [j ],
906- file_chunk [ j ]);
907- close ( fd );
894+ parsed . data [ file_offset + j ]);
895+ free ( parsed . data );
908896 return -1 ;
909897 }
910898 }
911899
912900 /* If file is shorter than flash region, remaining flash bytes should be 0xFF */
913- if (( size_t ) file_read < chunk_len ) {
914- for (size_t j = ( size_t ) file_read ; j < chunk_len ; j ++ ) {
901+ if (cmp_len < chunk_len ) {
902+ for (size_t j = cmp_len ; j < chunk_len ; j ++ ) {
915903 if (flash_chunk [j ] != 0xFF ) {
916904 progress_finish (& prog );
917905 warnx ("verify FAILED at 0x%08X: flash=0x%02X, expected=0xFF (beyond file)" ,
918906 current_addr + (uint32_t )j ,
919907 flash_chunk [j ]);
920- close ( fd );
908+ free ( parsed . data );
921909 return -1 ;
922910 }
923911 }
924912 }
925913
914+ file_offset += cmp_len ;
926915 current_addr += chunk_len ;
927916
928917 /* Send ACK for all packets except the last one (per spec 6.20.3) */
929918 if (i < nr_packets ) {
930919 pkt_len = ra_pack_pkt (pkt , sizeof (pkt ), REA_CMD , ack_data , 1 , true);
931920 if (pkt_len < 0 ) {
932- close ( fd );
921+ free ( parsed . data );
933922 return -1 ;
934923 }
935924 ra_send (dev , pkt , pkt_len );
@@ -939,7 +928,7 @@ ra_verify(ra_device_t *dev, const char *file, uint32_t start, uint32_t size) {
939928 }
940929
941930 progress_finish (& prog );
942- close ( fd );
931+ free ( parsed . data );
943932
944933 printf ("Verify OK: %u bytes at 0x%08X match file\n" , total_size , start );
945934 return 0 ;
0 commit comments