SD CARD SPI Communication Works - Reading Doesn't

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Everyone,

 

I need to create a SD Card reader for my project.

I have used a very good tutorial on how to get my SD Card to responds

http://codeandlife.com/2012/04/0... (including part 2,3,4)

and had very promising results as well.

 

-SPI communication is implemented and SD Card responds as expected.

-SD Card Init is implemented and responce from SD is satisfactory.

-Block size is set to 512b and response is again satisfactory.

 

However when reading of card is performed, I get all zeroes in first sector of SD Card.

To add insult to injury, end of sector is represented with usual 0x55 0xAA signoff.

 

I have checked SD Card on PC-based card reader and I know that first sector is populated properly with all necessary data (From Fat Table).

 

Strange thing is that this setup worked last week like a charm. (Nothing was touched and/or changed; SD Card was once formated during weekend and same data stored at it again)

 

I believe that I checked all usual "gotchas" and "stops", so I am asking if anyone had similar experience and possible to point me in right direction

 

Technical Specs for setup:

uC - Atmega 328p (dip casing) with external 16Mhz crystal (biased with 2x22pf to GND)

SD Card - Transcend 2GB SD card

Power supply: 3.4V

UART Comm: FTDI 232RL to USB

 

If needed I can include code snipets as well

 

As mentioned SD Command CMD0, CMD1, CMD16, (and even CMD10) work properly and SD Cards responds to them. However CMD17 (reading SD Card sector, any sector) just returns garbage data:

\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x02\0x0c\0x00\0x06\0x18\0xd8\0xb8\0x89\0x00\0x00\0x00w\0x97:\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00U\0xaa

This is reading of 0x00 sector; Note U\0xAA at the end.

 

Thanks for all information

 

This topic has a solution.
Last Edited: Wed. Apr 1, 2015 - 10:11 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I get all zeroes in first sector of SD Card.

Err no you don't...

\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x02\0x0c\0x00\0x06\0x18\0xd8\0xb8\0x89\0x00\0x00\0x00w\0x97:\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00

Surely that's a partition table entry at offset 0x1BE in the sector?

 

I have no idea why you have shown that in \0xNN format which makes it virtually unreadable but a typical partition table looks like:

Offset      0  1  2  3  4  5  6  7   8  9  A  B  C  D  E  F

...
000001B0   00 00 00 00 00 00 00 00  80 96 03 00 00 00 80 20           €–    € 
000001C0   21 00 83 1A 3B 1F 00 08  00 00 00 98 07 00 00 3B   ! ƒ ;      ˜   ;
000001D0   1B 1F 05 FE FF FF FE A7  07 00 02 88 C7 1D 00 00      þÿÿþ§   ˆÇ   
000001E0   00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00                   
000001F0   00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 AA                 Uª

This is my own HDD and it has two partitions. The first is defined 0x1BE..0x1CD and the second from 0x1CE..0x1DD

 

If you study just the first of those it is:

80           active bit set
20 21 00     CHS for first sector (unused these days because of LBA)
83           parition type = GNU/Linux
1A 3B 1F     CHS for last sector (again unused)
00 08 00 00  LBA of parition start = 0x800
00 98 07 00  Sectors in partition = 0x79800 = 497,664

If you can identify which bit in your MBR data is at offset 0x1BE I'll tell you where the boot/BPB sector is (basically you just use the four bytes that define the start and go straight to that sector - in my example 0x800).

 

BTW I don't ever know why people keep trying to reinvent FatFs - why not just use it? I suppose it is interesting to know about MBRs and BPBs and FATs and roots but if your goal here is simply to read and write FAT files why not simply use the software we all use and is known to be faultless?

Last Edited: Wed. Apr 1, 2015 - 10:42 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, curiosity got the better of me so I edited your data into a more readable format:

all 00 then..
1B0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 
1C0: 0c 00 06 18 d8 b8 89 00 00 00w 97: 00 00 00 00 00 
1D0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
1E0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
1F0: 00 00 00 00 00 00 00 00 00 00 00 00U aa

So those non-00 bytes are in the right place (0x1BE..0x1CD) to be the definition of the first and only partition on the disk. However the way you have printed the data means that some of it appears to be ASCII and some in \0xNN format). For example the ASCII code for 'U' is 0x55 so in:

1F0: 00 00 00 00 00 00 00 00 00 00 00 00U aa

I guess the U there is actually 55. Correcting for 'w' and ':' above I *think* that means you have:

all 00 then..
1B0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 
1C0: 0c 00 06 18 d8 b8 89 00 00 00 77 97 3a 00 00 00 
1D0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
1E0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
1F0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa

If this is the case then your partition is:

00           non-active
02 0c 00     CHS start
06           type = DOS 3.31+ FAT16
18 d8 b8     CHS end
89 00 00 00  LBA of start
77 97 3a 00  length = 0x3A9777 = 3,839,863 sectors = 1,919,931KB ~= 2GB

This all looks very plausible. I am pretty sure it would be FAT16, a start LBA of 0x0089 sounds likely and the length in sectors is just what you might expect for a "2GB" card

 

So read sector 0x89 - that is sector 137 and I bet you find the boot/BPB data you were hoping for there!

 

BTW if you don't have WinHex from X-ways software on your Windows PC already make it so. It would have let you read that card while plugged into an SD/MMC socket on the PC and it would have decoded this MBR (and then the BPB) for you.

Last Edited: Wed. Apr 1, 2015 - 11:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I  was looking at this represantation form for some time so I got accustomed to it. Forgot that it is a mess.

 

Here is in correct hex format (disregard everything before 0x23 0x23 0x23 and after 0x23 0x23 0x23 at the end), these are just enclosures

 

00000050: 0a 23 23 23 00 00 00 00 00 00 00 00 00 00 00 00

00000060: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00000070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00000080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00000090: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

000000a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

000000b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

000000c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

000000d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

000000e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

000000f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00000100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00000110: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00000120: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00000130: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00000140: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00000150: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00000160: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00000170: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00000180: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00000190: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

000001a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

000001b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

000001c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

000001d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

000001e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

000001f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00000200: 00 00 00 00 20 14 08 24 60 00 00 00 00 00 00 00

00000210: 00 00 00 02 0c 00 06 18 d8 b8 89 00 00 00 77 97

00000220: 3a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00000230: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00000240: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00000250: 00 00 55 aa 23 23 23 0d 0a 2a 2a 2a 2a 2a 2a 2a

 

What bugs me is that PC SD Card reader doesn't read this first sector like this.

 

This is how first sector is read by my SD Card reader

EB 3C 90 4D 53 44 4F 53 35 2E 30 00 02 40 02 00

02 00 02 00 00 F8 EB 00 3F 00 FF 00 89 00 00 00

77 97 3A 00 80 00 29 40 32 20 A6 4E 4F 20 4E 41

4D 45 20 20 20 20 46 41 54 31 36 20 20 20 33 C9

8E D1 BC F0 7B 8E D9 B8 00 20 8E C0 FC BD 00 7C

38 4E 24 7D 24 8B C1 99 E8 3C 01 72 1C 83 EB 3A

66 A1 1C 7C 26 66 3B 07 26 8A 57 FC 75 06 80 CA

02 88 56 02 80 C3 10 73 EB 33 C9 8A 46 10 98 F7

66 16 03 46 1C 13 56 1E 03 46 0E 13 D1 8B 76 11

60 89 46 FC 89 56 FE B8 20 00 F7 E6 8B 5E 0B 03

C3 48 F7 F3 01 46 FC 11 4E FE 61 BF 00 00 E8 E6

00 72 39 26 38 2D 74 17 60 B1 0B BE A1 7D F3 A6

61 74 32 4E 74 09 83 C7 20 3B FB 72 E6 EB DC A0

FB 7D B4 7D 8B F0 AC 98 40 74 0C 48 74 13 B4 0E

BB 07 00 CD 10 EB EF A0 FD 7D EB E6 A0 FC 7D EB

E1 CD 16 CD 19 26 8B 55 1A 52 B0 01 BB 00 00 E8

3B 00 72 E8 5B 8A 56 24 BE 0B 7C 8B FC C7 46 F0

3D 7D C7 46 F4 29 7D 8C D9 89 4E F2 89 4E F6 C6

06 96 7D CB EA 03 00 00 20 0F B6 C8 66 8B 46 F8

66 03 46 1C 66 8B D0 66 C1 EA 10 EB 5E 0F B6 C8

4A 4A 8A 46 0D 32 E4 F7 E2 03 46 FC 13 56 FE EB

4A 52 50 06 53 6A 01 6A 10 91 8B 46 18 96 92 33

D2 F7 F6 91 F7 F6 42 87 CA F7 76 1A 8A F2 8A E8

C0 CC 02 0A CC B8 01 02 80 7E 02 0E 75 04 B4 42

8B F4 8A 56 24 CD 13 61 61 72 0B 40 75 01 42 03

5E 0B 49 75 06 F8 C3 41 BB 00 00 60 66 6A 00 EB

B0 42 4F 4F 54 4D 47 52 20 20 20 20 0D 0A 52 65

6D 6F 76 65 20 64 69 73 6B 73 20 6F 72 20 6F 74

68 65 72 20 6D 65 64 69 61 2E FF 0D 0A 44 69 73

6B 20 65 72 72 6F 72 FF 0D 0A 50 72 65 73 73 20

61 6E 79 20 6B 65 79 20 74 6F 20 72 65 73 74 61

72 74 0D 0A 00 00 00 00 00 00 00 AC CB D8 55 AA

Same SD Card, FAT16 file system, different readouts from sector 0x00

 

Regarding FatFs - short answer is: not my call.

Project was conscieved by my cource professor (including idea of creating my own SD reader).

Help on subject was not included in equation when deliberations where made.  :-(

 

Forgot to mention

PC runs on openSUSE

Physical Card reader on PC is wxHexEditor

 

Last Edited: Wed. Apr 1, 2015 - 11:14 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is how first sector is read by my SD Card reader

Because it's showing you logical not physical drives(*)

 

If you were in Linux this would be the difference between reading /dev/sdc and /dev/sdc1. I don't know if Widows has a name for such things?

 

Anyway the first dump you showed really is sector 0 and is the MBR

 

The second dump you showed is the boot/BPB and is actually sector 137 (because the partition table in sector 0 told me it would be found in 137).

 

(*) when you start using WinHex note when you use Tools-Open Disk will show this:

 

 

It allows you to open both "Logical Volumes" and "Physical Media". At the level you want to inspect cards always choose "Physical Media". The issue you have with your PC card reader is that it's making this decision for you and always choosing "Logical" without offering "Physical". That's just one of a million reasons why it's better to use WinHex for this kind of stuff.

 

If you want to approach this in another way then seek out a copy of "WinDD" (or if using Linux just use dd itself). This will allow you to copy chunks of "drives" to files which you can then inspect. In fact to show you my partition table earlier I literally just used:

 

   sudo dd if=/dev/sda of=part.bin bs=512 count=1

 

to read the first 512 bytes from /dev/sda to a file called part.bin - then I read this in Windows using WinHex. Note that I used /dev/sda not /dev/sda1 as I was interested in what was in the true sector 0 not just sector 0 of partition 1 on the drive. If I do that:

 

   sudo dd if=/dev/sda1 of=boot.bin bs=512 count=1

 

I get:

Offset       0  1  2  3  4  5  6  7   8  9  A  B  C  D  E  F

000000000   33 C0 8E D0 BC 00 7C 8E  C0 8E D8 BE 00 7C BF 00   3ÀŽÐ¼ |ŽÀŽØ¾ |¿ 
000000010   06 B9 00 02 FC F3 A4 50  68 1C 06 CB FB B9 04 00    ¹  üó¤Ph  Ëû¹  
000000020   BD BE 07 80 7E 00 00 7C  0B 0F 85 0E 01 83 C5 10   ½¾ €~  |  …  ƒÅ 
000000030   E2 F1 CD 18 88 56 00 55  C6 46 11 05 C6 46 10 00   âñÍ ˆV UÆF  ÆF  
000000040   B4 41 BB AA 55 CD 13 5D  72 0F 81 FB 55 AA 75 09   ´A»ªUÍ ]r  ûUªu 
000000050   F7 C1 01 00 74 03 FE 46  10 66 60 80 7E 10 00 74   ÷Á  t þF f`€~  t
000000060   26 66 68 00 00 00 00 66  FF 76 08 68 00 00 68 00   &fh    fÿv h  h 
000000070   7C 68 01 00 68 10 00 B4  42 8A 56 00 8B F4 CD 13   |h  h  ´BŠV ‹ôÍ 
000000080   9F 83 C4 10 9E EB 14 B8  01 02 BB 00 7C 8A 56 00   ŸƒÄ žë ¸  » |ŠV 
000000090   8A 76 01 8A 4E 02 8A 6E  03 CD 13 66 61 73 1C FE   Šv ŠN Šn Í fas þ
0000000A0   4E 11 75 0C 80 7E 00 80  0F 84 8A 00 B2 80 EB 84   N u €~ € „Š ²€ë„
0000000B0   55 32 E4 8A 56 00 CD 13  5D EB 9E 81 3E FE 7D 55   U2äŠV Í ]ëž >þ}U
0000000C0   AA 75 6E FF 76 00 E8 8D  00 75 17 FA B0 D1 E6 64   ªunÿv è  u ú°Ñæd
0000000D0   E8 83 00 B0 DF E6 60 E8  7C 00 B0 FF E6 64 E8 75   èƒ °ßæ`è| °ÿædèu
0000000E0   00 FB B8 00 BB CD 1A 66  23 C0 75 3B 66 81 FB 54    û¸ »Í f#Àu;f ûT
0000000F0   43 50 41 75 32 81 F9 02  01 72 2C 66 68 07 BB 00   CPAu2 ù  r,fh » 
000000100   00 66 68 00 02 00 00 66  68 08 00 00 00 66 53 66    fh    fh    fSf
000000110   53 66 55 66 68 00 00 00  00 66 68 00 7C 00 00 66   SfUfh    fh |  f
000000120   61 68 00 00 07 CD 1A 5A  32 F6 EA 00 7C 00 00 CD   ah   Í Z2öê |  Í
000000130   18 A0 B7 07 EB 08 A0 B6  07 EB 03 A0 B5 07 32 E4     · ë  ¶ ë  µ 2ä
000000140   05 00 07 8B F0 AC 3C 00  74 09 BB 07 00 B4 0E CD      ‹ð¬< t »  ´ Í
000000150   10 EB F2 F4 EB FD 2B C9  E4 64 EB 00 24 02 E0 F8    ëòôëý+Éädë $ àø
000000160   24 02 C3 49 6E 76 61 6C  69 64 20 70 61 72 74 69   $ ÃInvalid parti
000000170   74 69 6F 6E 20 74 61 62  6C 65 00 45 72 72 6F 72   tion table Error
000000180   20 6C 6F 61 64 69 6E 67  20 6F 70 65 72 61 74 69    loading operati
000000190   6E 67 20 73 79 73 74 65  6D 00 4D 69 73 73 69 6E   ng system Missin
0000001A0   67 20 6F 70 65 72 61 74  69 6E 67 20 73 79 73 74   g operating syst
0000001B0   65 6D 00 00 00 63 7B 9A  A2 FF 6D 5A 00 00 00 20   em   c{š¢ÿmZ    
0000001C0   21 00 07 FE FF FF 00 08  00 00 00 F0 FF 09 80 FE   !  þÿÿ     ðÿ €þ
0000001D0   FF FF 07 FE FF FF 00 F8  FF 09 00 60 09 00 00 FE   ÿÿ þÿÿ øÿ  `   þ
0000001E0   FF FF 0F FE FF FF 00 58  09 0A 00 A0 76 02 00 00   ÿÿ þÿÿ X    v   
0000001F0   00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 AA                 Uª

which is clearly a boot/BPB not an MBR (though not FAT in this case).

Last Edited: Wed. Apr 1, 2015 - 11:30 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Clawson!

 

You have solved my conundrum! Upon detail inspection of PC SD hex reader I have seen that it has created two parts. I was reading partition instead of reading beginning of disk.

In essence, everything worked as it should, only I was to ignorant to "connect the dots"

 

You have jogged my memory and solved problem for me.

 

Thank you again for Your reply!

Cheers mate

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

All FAT reading software (dealing with SD/MMC) needs to be ready to spot two different types of sector 0. It can be like a "hard disk" and have a partition table in sector 0. Or it can be like a "floppy disk" and just start with a boot/BPB in sector 0. In the first case you just follow the details (usually just partition 1) and use that to step on to the boot/BPB by using that LBA field towards the end of the entry. Now you have a boot/BPB whichever way you got there. A FAT12/FAT16/FAT32 BPB has 'F','A','T' at offset 0x0052..0x0054 in the sector. So one way to tell if you hit a boot/BPB immediately or an MBR is just to test those 3 bytes for "FAT".

 

Another way to check is shown in my code here:

 

https://spaces.atmel.com/gf/proj...

 

See line 156 onwards. I'm simply using the fact that BPB_BytsPerSec field in a BPB is bound to hold 512 when you look at a boot/BPB. If I don't find this I must be looking at an MBR so I treat it as such and get the "sectors preceding partition 1" form the MBR then read that sector. By the time I reach line 168 I know I have a BPB in the buffer however I got there. I suppose it's possible BPB_BytesPerSec may be 4096 but that would be pretty unusual on "normal" size SD/MMC cards.

 

BTW I guess you know this but you need a copy of FatGen103.doc: http://download.microsoft.com/do... to understand all this stuff. As you may have spotted both Chan (some of who's code I "borrowed") and I use the names from FatGen103 in the code. It makes it easier to follow/understand if you stick to using the same names as the Microsoft document that is the "bible" when it comes to all this. I even comment my own code with equations lifted from FatGen103:

RootDir = BPBSec + RsvdSecCnt + (Buff[BPB_NumFATs] * FATSz);

 

Last Edited: Wed. Apr 1, 2015 - 11:48 AM