@@ -385,7 +385,7 @@ static inline uint16_t lfs_tag_type(uint32_t tag) {
385
385
}
386
386
387
387
static inline uint16_t lfs_tag_subtype (uint32_t tag ) {
388
- return (tag & 0x7c000000 ) >> 22 ;
388
+ return (( tag & 0x7c000000 ) >> 26 ) << 4 ;
389
389
}
390
390
391
391
static inline uint16_t lfs_tag_id (uint32_t tag ) {
@@ -470,7 +470,7 @@ static int32_t lfs_commit_get(lfs_t *lfs, lfs_block_t block, lfs_off_t off,
470
470
while (off >= 2 * sizeof (tag )+ lfs_tag_size (tag )) {
471
471
off -= sizeof (tag )+ lfs_tag_size (tag );
472
472
473
- if (lfs_tag_type (tag ) == LFS_TYPE_CRC && stopatcommit ) {
473
+ if (lfs_tag_subtype (tag ) == LFS_TYPE_CRC && stopatcommit ) {
474
474
break ;
475
475
} else if (lfs_tag_type (tag ) == LFS_TYPE_DELETE ) {
476
476
if (lfs_tag_id (tag ) <= lfs_tag_id (gettag + getdiff )) {
@@ -502,6 +502,7 @@ static int32_t lfs_commit_get(lfs_t *lfs, lfs_block_t block, lfs_off_t off,
502
502
return err ;
503
503
}
504
504
tag ^= lfs_fromle32 (ntag );
505
+ tag &= 0x7fffffff
505
506
}
506
507
507
508
return LFS_ERR_NOENT ;
@@ -632,8 +633,7 @@ static int lfs_commit_move(lfs_t *lfs, struct lfs_commit *commit,
632
633
return err ;
633
634
}
634
635
635
- ntag = lfs_fromle32 (ntag );
636
- ntag ^= tag ;
636
+ ntag = lfs_fromle32 (ntag ) ^ tag ;
637
637
tag |= 0x80000000 ;
638
638
}
639
639
@@ -687,7 +687,7 @@ static int lfs_commit_crc(lfs_t *lfs, struct lfs_commit *commit) {
687
687
lfs -> cfg -> prog_size );
688
688
689
689
// read erased state from next program unit
690
- uint32_t tag = 0 ;
690
+ uint32_t tag ;
691
691
int err = lfs_bd_read (lfs ,
692
692
& lfs -> pcache , & lfs -> rcache , sizeof (tag ),
693
693
commit -> block , off , & tag , sizeof (tag ));
@@ -696,10 +696,9 @@ static int lfs_commit_crc(lfs_t *lfs, struct lfs_commit *commit) {
696
696
}
697
697
698
698
// build crc tag
699
- tag = lfs_fromle32 (tag );
700
- tag = (0x80000000 & ~tag ) |
701
- LFS_MKTAG (LFS_TYPE_CRC , 0x3ff ,
702
- off - (commit -> off + sizeof (uint32_t )));
699
+ bool reset = ~lfs_fromle32 (tag ) >> 31 ;
700
+ tag = LFS_MKTAG (LFS_TYPE_CRC + reset ,
701
+ 0x3ff , off - (commit -> off + sizeof (uint32_t )));
703
702
704
703
// write out crc
705
704
uint32_t footer [2 ];
@@ -713,7 +712,7 @@ static int lfs_commit_crc(lfs_t *lfs, struct lfs_commit *commit) {
713
712
return err ;
714
713
}
715
714
commit -> off += sizeof (tag )+ lfs_tag_size (tag );
716
- commit -> ptag = tag ;
715
+ commit -> ptag = tag ^ ( reset << 31 ) ;
717
716
718
717
// flush buffers
719
718
err = lfs_bd_sync (lfs , & lfs -> pcache , & lfs -> rcache , false);
@@ -774,7 +773,7 @@ static int lfs_dir_alloc(lfs_t *lfs, lfs_mdir_t *dir) {
774
773
775
774
// set defaults
776
775
dir -> off = sizeof (dir -> rev );
777
- dir -> etag = 0 ;
776
+ dir -> etag = 0xffffffff ;
778
777
dir -> count = 0 ;
779
778
dir -> tail [0 ] = 0xffffffff ;
780
779
dir -> tail [1 ] = 0xffffffff ;
@@ -817,7 +816,7 @@ static int32_t lfs_dir_fetchmatch(lfs_t *lfs,
817
816
// load blocks and check crc
818
817
for (int i = 0 ; i < 2 ; i ++ ) {
819
818
lfs_off_t off = sizeof (dir -> rev );
820
- uint32_t ptag = 0 ;
819
+ uint32_t ptag = 0xffffffff ;
821
820
uint32_t crc = 0xffffffff ;
822
821
823
822
dir -> rev = lfs_tole32 (rev [0 ]);
@@ -851,8 +850,8 @@ static int32_t lfs_dir_fetchmatch(lfs_t *lfs,
851
850
tag = lfs_fromle32 (tag ) ^ ptag ;
852
851
853
852
// next commit not yet programmed
854
- if (lfs_tag_type ( ptag ) == LFS_TYPE_CRC && !lfs_tag_isvalid (tag )) {
855
- dir -> erased = true ;
853
+ if (!lfs_tag_isvalid (tag )) {
854
+ dir -> erased = ( lfs_tag_subtype ( ptag ) == LFS_TYPE_CRC ) ;
856
855
break ;
857
856
}
858
857
@@ -862,7 +861,7 @@ static int32_t lfs_dir_fetchmatch(lfs_t *lfs,
862
861
break ;
863
862
}
864
863
865
- if (lfs_tag_type (tag ) == LFS_TYPE_CRC ) {
864
+ if (lfs_tag_subtype (tag ) == LFS_TYPE_CRC ) {
866
865
// check the crc attr
867
866
uint32_t dcrc ;
868
867
err = lfs_bd_read (lfs ,
@@ -882,6 +881,10 @@ static int32_t lfs_dir_fetchmatch(lfs_t *lfs,
882
881
break ;
883
882
}
884
883
884
+ // reset the next bit if we need to
885
+ tag ^= (lfs_tag_type (tag ) & 1 ) << 31 ;
886
+
887
+ // update with what's found so far
885
888
foundtag = tempfoundtag ;
886
889
dir -> off = off + sizeof (tag )+ lfs_tag_size (tag );
887
890
dir -> etag = tag ;
@@ -1096,7 +1099,7 @@ static int lfs_dir_compact(lfs_t *lfs,
1096
1099
// setup commit state
1097
1100
commit .off = 0 ;
1098
1101
commit .crc = 0xffffffff ;
1099
- commit .ptag = 0 ;
1102
+ commit .ptag = 0xffffffff ;
1100
1103
1101
1104
// space is complicated, we need room for tail, crc, globals,
1102
1105
// cleanup delete, and we cap at half a block to give room
0 commit comments