cancel
Showing results for 
Search instead for 
Did you mean: 

LittleFS dir fetch unexpected values in test parameter. STM32

GR88_gregni
Associate II

Hello everyone, 

I am using an STM32 microcontroller and I am facing a problem when I try to mount an external flash memory. The problem is inside the function lfs_dir_fetch which is returning unexpected values in the test buffer here: 

 

struct lfs_disk_dir test;
int err = lfs_bd_read(lfs, tpair[i], 0, &test, sizeof(test));
lfs_dir_fromle32(&test);

 

the returned values are: 

 

Invalid size: test.size = 335610112, block_size = 40960
Invalid size: test.size = 335610112, block_size = 40960
lfs error:495: Corrupted dir pair at 0 1
lfs error:2214: Invalid superblock at 0 1
 	(*)Error in mounting external flash memory

 

I have successfully communicate with the external flash, since I am able to read its manufacture ID. I have also configured properly the littlefs function and I test them by writing raw data to the external flash and I was able to receive them correctly. I cannot understand what is the test variable that is being declared and why I cannot proceed to the the calculation of the crc parameter since it has a continue statement inside the problematic block:

 

static int lfs_dir_fetch(lfs_t *lfs,
        lfs_dir_t *dir, const lfs_block_t pair[2]) {
    // copy out pair, otherwise may be aliasing dir
    const lfs_block_t tpair[2] = {pair[0], pair[1]};
    bool valid = false;

    // check both blocks for the most recent revision
    for (int i = 0; i < 2; i++) {
        struct lfs_disk_dir test;
        int err = lfs_bd_read(lfs, tpair[i], 0, &test, sizeof(test));
        lfs_dir_fromle32(&test);
        if (err) {
            if (err == LFS_ERR_CORRUPT) {
                continue;
            }
            return err;
        }

        if (valid && lfs_scmp(test.rev, dir->d.rev) < 0) {
            continue;
        }

        if ((0x7fffffff & test.size) < sizeof(test)+4 ||
            (0x7fffffff & test.size) > lfs->cfg->block_size) {
        	printf("Invalid size: test.size = %ld, block_size = %ld\n",
        	            (0x7fffffff & test.size), lfs->cfg->block_size);
            continue;
        }

        uint32_t crc = 0xffffffff;
        lfs_dir_tole32(&test);
        lfs_crc(&crc, &test, sizeof(test));
        lfs_dir_fromle32(&test);
        err = lfs_bd_crc(lfs, tpair[i], sizeof(test),
                (0x7fffffff & test.size) - sizeof(test), &crc);
        if (err) {
            if (err == LFS_ERR_CORRUPT) {
                continue;
            }
            return err;
        }

        if (crc != 0) {
            continue;
        }

        valid = true;

        // setup dir in case it's valid
        dir->pair[0] = tpair[(i+0) % 2];
        dir->pair[1] = tpair[(i+1) % 2];
        dir->off = sizeof(dir->d);
        dir->d = test;
    }

    if (!valid) {
        LFS_ERROR("Corrupted dir pair at %" PRIu32 " %" PRIu32 ,
                tpair[0], tpair[1]);
        return LFS_ERR_CORRUPT;
    }

    return 0;
}

 

1 REPLY 1

Explicitly clear local/auto variables otherwise they will contain random stack junk.

int arrayfoo[100] = {0};

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..