Hot!MDDF Library - Extra SD Card info

Author
Brek
Starting Member
  • Total Posts : 45
  • Reward points : 0
  • Joined: 2016/08/29 19:11:39
  • Location: 0
  • Status: offline
2018/03/11 18:08:22 (permalink)
0

MDDF Library - Extra SD Card info

Hello,
I’m using Microchip MDDFS library, and trying to get extra information abou the SD Card
that is not usually provided by the library’s APIs, such as OEMname, and Volume Label.
My results are the same with both, so I’ll just talk about OEM name here.
Firstly, I have an external character buffer in FSIO.h, which is included in my main.c source.

extern char oemsdname[9];

Then to prove it’s working, I can write a static string to it in FSIO.c

char oemsdname[9];
sprintf (oemsdname,”ABCD”);

This string, I can print from my main.c source, to an LCD display.
Problems arise when I try to extract information from this buffer given in FSdefs.h

// Summary: A structure containing the bios parameter block for a FAT32 file system (in the boot sector)
// Description: The _BPB_FAT32 structure provides a layout of the "bios parameter block" in the boot sector of a FAT32 partition.
typedef struct {
SWORD BootSec_jmpBoot; // Jump Command
BYTE BootSec_OEMName[8]; // OEM name
WORD BootSec_BytsPerSec; // Number of bytes per sector
BYTE BootSec_SecPerClus; // Number of sectors per cluster
WORD BootSec_RsvdSecCnt; // Number of reserved sectors at the beginning of the partition
BYTE BootSec_NumFATs; // Number of FATs on the partition
WORD BootSec_RootEntCnt; // Number of root directory entries
WORD BootSec_TotSec16; // Total number of sectors
BYTE BootSec_Media; // Media descriptor
WORD BootSec_FATSz16; // Number of sectors per FAT
WORD BootSec_SecPerTrk; // Number of sectors per track
WORD BootSec_NumHeads; // Number of heads
DWORD BootSec_HiddSec; // Number of hidden sectors
DWORD BootSec_TotSec32; // Total sector count (32 bits)
DWORD BootSec_FATSz32; // Sectors per FAT (32 bits)
WORD BootSec_ExtFlags; // Presently active FAT. Defined by bits 0-3 if bit 7 is 1.
WORD BootSec_FSVers; // FAT32 filesystem version. Should be 0:0
DWORD BootSec_RootClus; // Start cluster of the root directory (should be 2)
WORD BootSec_FSInfo; // File system information
WORD BootSec_BkBootSec; // Backup boot sector address.
BYTE BootSec_Reserved[12]; // Reserved space
BYTE BootSec_DrvNum; // Drive number
BYTE BootSec_Reserved1; // Reserved space
BYTE BootSec_BootSig; // Boot signature - 0x29
BYTE BootSec_VolID[4]; // Volume ID
BYTE BootSec_VolLab[11]; // Volume Label
BYTE BootSec_FilSysType[8]; // File system type in ASCII. Not used for determination
} __attribute__ ((packed)) _BPB_FAT32;

I already know the card is formatted FAT32, and can assume the FAT32 offsets in the buffer.
Functions in FSIO.c such as BYTE LoadMBR(DISK *dsk), and BYTE LoadBootSector(DISK *dsk), already access this buffer to determin fat type, and retrieve other information such as
bytes per sector, total secots, and other information to be able to use the card,
but when I add my own routine to extract additional information, I get an empty string.

int recsrch = 0;
while (recsrch < 8) {
oemsdname[recsrch] = ReadByte(dsk->buffer,4+recsrch);
recsrch++;
} // recsrch
oemsdname[8] = 0; // terminate the string

This could be amongst other lines that already take info from this very same buffer:

dsk->SecPerClus = ReadByte( dsk->buffer, BSI_SPC );
// Load the sector number of the first FAT sector
dsk->fat = dsk->firsts + ReservedSectorCount;
// Load the count of FAT tables
dsk->fatcopy = ReadByte( dsk->buffer, BSI_FATCOUNT );
// Load the size of the FATs
dsk->fatsize = ReadWord( dsk->buffer, BSI_SPF );
if(dsk->fatsize == 0)
dsk->fatsize = ReadDWord( dsk->buffer, BSI_FATSZ32 );
// Calculate the location of the root sector (for FAT12/16)
dsk->root = dsk->fat + (DWORD)(dsk->fatcopy * (DWORD)dsk->fatsize);
// Determine the max size of the root (will be 0 for FAT32)
dsk->maxroot = ReadWord( dsk->buffer, BSI_ROOTDIRENTS );


Does anyone have any idea where I might be going wrong?
Or has anyone extracted any of this extra information?
Cheers, Brek.
#1

4 Replies Related Threads

    Brek
    Starting Member
    • Total Posts : 45
    • Reward points : 0
    • Joined: 2016/08/29 19:11:39
    • Location: 0
    • Status: offline
    Re: MDDF Library - Extra SD Card info 2018/03/13 20:26:06 (permalink)
    0
    Hi :)
    It seems I’ve found the data I was looking for by extracting it in the LoadBootSector function,
    right after it has been determined what the file system type is.
    for FAT16:

    unsigned char rcsrch = 0x2B; // read partition volume name
    while (rcsrch < 0x2B + 0x0B) {
    sdvolabel[rcsrch-0x2B] = ReadByte(dsk->buffer,rcsrch);
    rcsrch++;
    } // rcsrch

    For FAT32:

    unsigned char rcsrch = 0x47; // read partition volume name
    while (rcsrch < 0x47 + 0x0B) {
    sdvolabel[rcsrch-0x47] = ReadByte(dsk->buffer,rcsrch);
    rcsrch++;
    } // rcsrch

    Interestingly, the correct partition volume label is extracted only if the SD Card is
    renamed in Mac OS. If I rename it in Windows 7, the card is labelled that way in Windows 7
    and MAC OS, but is still called “NO NAME” in the microcontroller program.
     
    #2
    Antipodean
    Super Member
    • Total Posts : 1667
    • Reward points : 0
    • Joined: 2008/12/09 10:19:08
    • Location: Didcot, United Kingdom
    • Status: offline
    Re: MDDF Library - Extra SD Card info 2018/03/14 04:35:34 (permalink)
    0
    Brek
    Hi :)
    It seems I’ve found the data I was looking for by extracting it in the LoadBootSector function,
    right after it has been determined what the file system type is.
    for FAT16:
    ...
    Interestingly, the correct partition volume label is extracted only if the SD Card is
    renamed in Mac OS. If I rename it in Windows 7, the card is labelled that way in Windows 7
    and MAC OS, but is still called “NO NAME” in the microcontroller program.
     

     
    IIRC the name displayed on a windows machine is the name put in an Autorun file on the card. I know i have been through a similar loop with this on a windows machine.

    Do not use my alias in your message body when replying, your message will disappear ...

    Alan
    #3
    Brek
    Starting Member
    • Total Posts : 45
    • Reward points : 0
    • Joined: 2016/08/29 19:11:39
    • Location: 0
    • Status: offline
    Re: MDDF Library - Extra SD Card info 2018/03/14 07:07:10 (permalink)
    0
    Hi, Thanks for the reply, but if you’re talking about a file that can be seen when the
    “show hidden files” option is set in Windows Explorer, no unexpected files are visible.
     
    It would have been worth opening it to get the name out of it if the first way failed,
    or to check it whenever a “NO NAME” string was read from the active boot sector.
    Right now, I’m defaulting to a name like “SD” for this kind if thing.
     
    #4
    Brek
    Starting Member
    • Total Posts : 45
    • Reward points : 0
    • Joined: 2016/08/29 19:11:39
    • Location: 0
    • Status: offline
    Re: MDDF Library - Extra SD Card info 2018/03/15 19:36:20 (permalink)
    0

    It turns out it is a file written to the root directory by Windows afterall,
    and Windows does not write the volume label to the partition boot sector.
    So, I read the volume label from the boot sector first, and then call this function
    if the string begins with “NO NAME”, is null terminated, or begins with a space.
    Then this function will overwrite the volume label string if it finds a label as a
    volume label file entry that is not a null string, and does not begin with a space.
    Windows simply writes one file to a FAT16 or FAT32 root directory that has the
    “VOLUME” attribute (the same category as HIDDEN, READONLY, SYSTEM, etc.).
    The name of this file is the name of the volume, and I assume the file is useless
    for anything else. It doesn’t have to be opened.
    MacOS must also be checking for this file to display the label, even though it
    writes it to the partition boot sector location when it formats or renames a volume.
    Now my program can read the volume label from an SD Card formatted with either OS! :)

    // check if the partition has a windows volume label
    void sdwinvollabel() {
    int volfound = 1; // set flag default as not found
    int volcnt = 0; // counter
    volfound = FindFirst ("*", 0x08, &rec); // find win vol entry
    if (volfound == 0) { // win volume label was found
    if (rec.filename[0] != 0x00 && rec.filename[0] != 0x20) { // check for invalid name
    while (volcnt < 11) { // copy win volume label
    sdvolabel[volcnt] = rec.filename[volcnt];
    volcnt++;
    } // filename
    } // volcnt
    } // volfound
    }

    #5
    Jump to:
    © 2018 APG vNext Commercial Version 4.5