cfatfs

FatFS: f_findfirst returns a string that does not correspond to the file found


I am using the FatFs library to create and manage a file system on an SD card. The goal is to identify a file containing the firmware for the bootloader to update (or not) the device.

The procedure im following is the follwing:

DIR  directory;
FILINFO filinfo;

f_findfirst(&directory, &filinfo, "", "firmware_");

To my understanding, the first file that starts with "firmware_" should be identified and the information stored in filinfo . This does work, but when I try to extract the name from filinfo.fname the name that has been extracted is the follwing: "\0IRMWA~1.BIN".

I don't know the reason why the naming of the file is extracted in this format. The idea would be to later take the second part of the file: "firmware_01_01_01" and perform a char to int conversion to process the version number, but I cannot extract the full string of the file name.


Solution

  • I was using Long File Name (LFN) which had been activated in the ffconf.h file (the options file that comes with FatFs). The problem was that I was using filinfo.fname which refers to the 8.3 file name structure (short naming scheme).

    If LFN is activated, the fields lfname and lfsize within the FILINFO struct are active. These two fields are a pointer to a char array as well as the size. Both fields must be initialized before using them like follows:

    static char lfn[_MAX_LFN + 1] 
    //MAX_LFN is defined in ffconf.h and is 255, maximum value for LFN
    filinfo.lfname = lfn;
    filinfo.lfsize = sizeof lfn;
    f_findfirst(&directory, &filinfo, "", "firmware_*.bin");
    (...)
    

    The documentation that also shows an example can be read here.

    When LFN feature is enabled, lfname and lfsize in the file information structure must be initialized with valid value prior to use the f_readdir function. The lfname is a pointer to the string buffer to return the long file name. The lfsize is the size of the string buffer in unit of TCHAR.

    lfname and lfsize would work the same for f_findfirst as with f_readdir.

    This is a wrapper function of f_opendir and f_readdir