Author Topic: Reporting incorrect free space..  (Read 16034 times)

Offline neil

  • Sr. Member
  • ****
  • Posts: 438
    • View Profile
Reporting incorrect free space..
« on: November 15, 2011, 07:23:29 PM »
Hi Mark,
   I have purchased 10 off  Kingston 2GB micro SD cards, and have problem with 1 of them. When I request the disk size, and free space , I get back a strange value for the free space.  I get Disk Size=1973MB, and Free Space 4294MB. When I check it on my PC I get 1.83GB free and same for size (FAT32). The other cards I get Disk Size :1967MB  Free space 1960MB.   It still seems to work correctly , but seems a bit slower. Think this may be a faulty card, even when reporte on Windows as fine?

Below is the calculation I use.

   long Freespace=(ptrDiskInfo->utFileInfo.ulFreeClusterCount * ptrDiskInfo->utFAT.ucSectorsPerCluster * ptrDiskInfo->utFAT.usBytesPerSector)/1000000;
   long CardSize=(ptrDiskInfo->ulSD_sectors * ptrDiskInfo->utFAT.usBytesPerSector)/1000000;


Best Regards
Neil

Offline neil

  • Sr. Member
  • ****
  • Posts: 438
    • View Profile
Re: Reporting incorrect free space..
« Reply #1 on: November 15, 2011, 07:32:56 PM »
Hi Mark,
  As I formatted it withing Windows, I re-formatted it withing uTasker and it now reports the correct value, why is this? And is it always better re-formatting it withing utasker?

Thanks
Neil

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Reporting incorrect free space..
« Reply #2 on: November 15, 2011, 07:48:14 PM »
Hi Neil

Theoretically it shouldn't make any difference whether formatted by Windows or by uTasker. Windows will probably format in in a more complicated way, also writing some sections which are not needed for general operation.

The info sector contains the details of the free space but the info sector is also optional. It does however make the operation faster since it tells the FAT (when correct) where the next free cluster is and also the size of the remaining space. If this is corrupted or missing the card will still work but will tend to be slower when writing data (since the next cluster needs to be searched for) and when listing the free space (since it has to be actively counted).

If the info block was not good it may be that uTasker was simply displaying the 'bad' values but windows was actually counting the blocks (to be sure) - also using the "info" command may have given some insight (or maybe just showing the bad values).

If after a reformat (with whatever method used) the sizes are correctly displayed I would assume that the reformat 'corrected' the bad details in the info sector. Presumably the card will now 'behave' - even if the info block was corrupted before, the card will probably locate it at a physically different location when reformatted and so it will probably remain reliable now. I can't explain why it originally had the problem though.

Regards

Mark

Offline neil

  • Sr. Member
  • ****
  • Posts: 438
    • View Profile
Re: Reporting incorrect free space..
« Reply #3 on: May 09, 2012, 06:00:17 PM »
Hi Mark,
  When I do a dir, and info I get 268MB, and windows displays 2GB.When I did a re-format I got the below,and now windows only display a 255MB and cant re-format as a 2GB. Any ideas?

SD-card  (268435456 bytes)
Bytes per sector: 512
Cluster size: 512
Directory base: 0x00000002
FAT start: 0x0000005f
FAT size: 0x00000fe0
Number of FATs: 2
LBA: 0x0000201f
Total clusters: 0x0007dfe1
Info sect: 0x00000040
Free clusters: 0x0007dfe0
Next free: 0x00000003
CSD: 0x2e 0x00 0x32 0x5b 0x5a 0xa3 0xb4 0xff 0xff 0xff 0x80 0x0a 0x80 0x00 0x8d
0xd5
D:\>
Directory D:\

0 files with 0 bytes
0 directories, 264224768 bytes free

Neil
« Last Edit: May 09, 2012, 06:03:49 PM by neil »

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Reporting incorrect free space..
« Reply #4 on: May 11, 2012, 01:17:28 PM »
Hi Neil

The CSD value read from the card contains infomration about the size of the card.
What is being read from the card doesn't look to be correct. It looks as though the CSD is losing a byte since the value 0x32 (could also be 0x5a) is expected to be the 4th byte and not the 3rd byte.

If the content is shifted by one byte (or a byte missing) the calculation of its size based on some fields would probably be incorrect.

Why the CSD looks to be bad and how Windows calculates the 255MB size is not clear - the fact that the ufFAT calculates 2.6G is certainly due to the shift.

I would compare several cards so verify that this problem is local to just one of them.

The CSD is a read-only register in the SD and has nothing to do with FAT and formatting fo the card. Therefore I don't understand why a re-format could influence it.

Regards

Mark



Offline neil

  • Sr. Member
  • ****
  • Posts: 438
    • View Profile
Re: Reporting incorrect free space..
« Reply #5 on: May 11, 2012, 01:33:45 PM »
Hi Mark,
  Isnt "SD-card  (268435456 bytes)" 268MB 'sh?  I bought 10 2GB Kingston micro SD's and quite a few have been like this, I am wondering if it is a faulty batch. I just formatted one in Windows (and mentions 2GB), and utasker displays 268MB..

Neil
« Last Edit: May 11, 2012, 01:41:09 PM by neil »

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Reporting incorrect free space..
« Reply #6 on: May 11, 2012, 09:55:50 PM »
Hi Neil

268435456 bytes is 256M (I made a mistake in the last post) and it is the value that is calculated from the read CSD [0x2e, 0x00, 0x32, 0x5b, 0x5a, 0xa3, 0xb4, 0xff, 0xff, 0xff, 0x80, 0x0a, 0x80, 0x00, 0x8d, 0xd5].

It seems that it is not read the same when done by windows, where it is correct in the one case.

If one byte 0x00 were to be added before the read data [0x00, 0x2e, 0x00, 0x32, 0x5b, 0x5a, 0xa3, 0xb4, 0xff, 0xff, 0xff, 0x80, 0x0a, 0x80, 0x00, 0x8d] (also the final byte has been deleted) it would then calculate 1990197248 bytes (2G).

Based on your observations uTasker reads some cards and loses a byte. I assume that some of the cards don't have this problem - check the CSD that is read from them (with "info" command) and see whether there is in fact an extra byte at the beginning.
Further mode you also had the case that Windows detected only 255M, which looks as though also Windows did (or could) have the same problem with particular cards (?).


I would do a few more tests to get an idea of whether this changes between mounting (i.e. whether it is consistent for a particular card or whether it changes). I can't explain a lost byte at the moment since the same interface is used to read from the card, whether the CSD is being read or something else.
You coudl also try changing the SPI clock rate to see whether a slower one durting the mounting phase could have an effect.

The part of the code reading the CSD is below:

Code: [Select]
        case SD_STATE_GET_SECTORS:
            if ((iActionResult = fnSendSD_command(ucSEND_CSD_CMD9, &ucResult, ucData)) != UTFAT_SUCCESS) { // get card capacity information
                if (iActionResult == CARD_BUSY_WAIT) {
                    return;                                              // read is taking time to complete so quit for the moment
                }
                fnInitialisationError(0);                                // the card is behaving incorrectly - stop and try again later
                break;
            }
            uMemcpy(utDisks[0].utFileInfo.ucCardSpecificData, &ucData[2 - SD_CONTROLLER_SHIFT], 16); // back up the card specific data since it can be of interest later
            if (ucData[2 - SD_CONTROLLER_SHIFT] & HIGH_CAPACITY_SD_CARD_MEMORY) { // high capacity
                utDisks[0].ulSD_sectors = (((ucData[9 - SD_CONTROLLER_SHIFT] << 16) + (ucData[10 - SD_CONTROLLER_SHIFT] << 8)  + ucData[11 - SD_CONTROLLER_SHIFT]) + 1);// SD version 2 assumed
                utDisks[0].ulSD_sectors *= 1024;                         // the number of sectors on the SD card
            }
            else {                                                       // standard capacity
                utDisks[0].ulSD_sectors = ((((ucData[8 - SD_CONTROLLER_SHIFT] & 0x03) << 10) + (ucData[9 - SD_CONTROLLER_SHIFT] << 2) + (ucData[10 - SD_CONTROLLER_SHIFT] >> 6)) + 1);// SD version 2 assumed
                utDisks[0].ulSD_sectors *= (1 << ((((ucData[11 - SD_CONTROLLER_SHIFT] & 0x03) << 1) + (ucData[12 - SD_CONTROLLER_SHIFT] >> 7)) + 2)); // the number of 512 byte sectors on the SD card
                if ((ucData[7 - SD_CONTROLLER_SHIFT] & 0x0f) == 0x0a) {  // 1024 byte block length indicates 2G card
                    utDisks[0].ulSD_sectors *= 2;
                }
            }
            fnMemoryDebugMsg("\r\n");

You could try repeating this a few times to see whether the result varies (unreliability somewhere). It would also be interesting if some card have the problem and others never do.

You could also send me a 'bad' card because I could test it with different processors via SPI and also SDHC to see whether I can identify what is going on and possibly a workaround (?)

Regards

Mark





Offline neil

  • Sr. Member
  • ****
  • Posts: 438
    • View Profile
Re: Reporting incorrect free space..
« Reply #7 on: May 12, 2012, 05:23:47 AM »
Hi Mark,
 I have tried the test quite a few times with the same result. I can send you the SD card, what address will I send it to?

Thanks
Neil

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Reporting incorrect free space..
« Reply #8 on: May 12, 2012, 10:58:36 AM »
Neil

Could you confirm that you also have cards which don't (ever) have the problem?

Regards

Mark


Offline neil

  • Sr. Member
  • ****
  • Posts: 438
    • View Profile
Re: Reporting incorrect free space..
« Reply #9 on: May 12, 2012, 11:56:30 AM »
Hi Mark,
  I just received Sandisk 2GB cards this morning and tried one, seems to work fine, here is waht is returned:

SD-card  (2002780160 bytes)
Bytes per sector: 512
Cluster size: 4096
Directory base: 0x00000002
FAT start: 0x000000a7
FAT size: 0x00000ee5
Number of FATs: 2
LBA: 0x00001e71
Total clusters: 0x00077231
Info sect: 0x00000082
Free clusters: 0xffffffff
Next free: 0x00000002
CSD: 0x00 0x7f 0xff 0x32 0x5b 0x5a 0x83 0xba 0xf6 0xdb 0xdf 0xff 0x0e 0x80 0x00
0xb5

I tried the one giving problems (also a new Kingston 2GB), and it still produced the same problems (also windows displays 2GB).

I can send the faulty one (must be about 5 in the batch of 10 I bought was the same) to you if you wiash to have a look?

Regards
Neil

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Reporting incorrect free space..
« Reply #10 on: May 12, 2012, 08:46:12 PM »
Hi Neil

The value 0x32 at the 4th byte location and then all is fine.

I suppose that it woudl also be possible to check the location of the 0x32 (it can also have a different value) and shift the values if needed. The problsm is that the first byte (that is going missing) contains details fo whether the card is HC or not and so it would have to be assumed that it isn't > 2G. If a bigger card were to lose the first byte then it would again calculate the wrong size, even after shifting the data.

It woud certainly be good to work out why thi sshift can take place!

I'll send you my address and you can send me on the the cards that behave strangely.

Regards

Mark


Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Reporting incorrect free space..
« Reply #11 on: May 27, 2012, 11:30:35 PM »
Neil

Thanks for sending me one of these cards.

I have done an initial test with the following results:
- I tested your card and other 2G cards on different boards with different processors in SDIO mode (4-bit)
In SDIO mode (on both Kinetis and STM32 processors with this controller built in):
-- CSD: 0x00 0x26 0x00 0x32 0x5f 0x5a 0x83 0xae 0xfe 0xfb 0xcf 0xff 0x92 0x80 0x40 0xde - equivalent SD card
-- CSD: 0x00 0x2e 0x00 0x32 0x5b 0x5a 0xa3 0xb4 0xff 0xff 0xff 0x80 0x0a 0x80 0x00 0x8c - your Kingston 2G SD card

The differences are minor and both report the correct size

- Then I repeated using a board with SPI interface instead (LPC2148 to be exact)
-- CSD: 0x00 0x26 0x00 0x32 0x5f 0x5a 0x83 0xae 0xfe 0xfb 0xcf 0xff 0x92 0x80 0x40 0xdf - equivalent SD card [last bit is different but that is normal for SPI]
-- CSD: 0x2e 0x00 0x32 0x5b 0x5a 0xa3 0xb4 0xff 0xff 0xff 0x80 0x0a 0x80 0x00 0x8d 0xd5 - your Kingston 2G SD card

This shows that I also get the shift in the returned CSD. Its content is (ignoring the shift) accurate, including the final bit in the last byte - which is a check sum and SPI mode also sets the last (unused) bit to a different polarity than SDIO mode.


This shows that the card operates 'normally' in SDIO mode but behaves differently in SPI mode. Since I get the same result as you on a different board/processor in SPI mode and only get the shift with this card it looks to be a general SPI mode problem that it has.

This would explain why Windows sees its size correctly - it will be using an SDIO controller internally. Also the utFAT on a chip with SDIO controller then reads it normally.

Since this register read seems to be the only one with a problem (the card can be otherwise mounted and data on it read and written) I wonder whether it may be worth making a workaround? Since the first byte expected is either 0x40 (for HC) or 0x00 for standard card a 0x00 could be inserted into the data if neither of these are found (the rest shifted to the right). Then the CSD will have been 'corrected' and the card size be accurate, also in SPI mode.

This would allow the cards with this effect to be used normally again. I am presuming that HC cards will not have such an effect...

Regards

Mark


Offline neil

  • Sr. Member
  • ****
  • Posts: 438
    • View Profile
Re: Reporting incorrect free space..
« Reply #12 on: May 31, 2012, 10:35:26 AM »
Hi Mark,
  Thanks for taking a look at the card. I am wondering if this is a faulty batch, as another one of the 2GB cards displays 255MB when formatted in Windows FAT 32.

Thanks
neil