Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
21 years ago

Reading the first sector of a CF card

I'm trying to read the first sector of a compact flash card. This is the boot sector and bpb of a fat filesystem. This sector has a lot of information about how the filesystem is(number of sectors, bytes per sector, sector per cluster, etc). The problem is that i cant read it. I can read the second sector. I'm using the compact flash component from this forum and the source code with modifications. This is the code for the test application:

/* This testapplication shall read the bpb*/
# include "system.h"# include "ideutils.h"# include <stdio.h>
int main(void)
{
  char *egenstr = "0 0 0";
  
  if (IDE_initialize()) {//We can do some after this
   readCHS(egenstr);    //trying to read first sector..
  }
  else
  {
    printf("Cannot initialize IDE device.\r");
  }
  IDE_deinitialize();
  printf("\rgoodbye\r\r4");
  return 0;
}

And this is the output:

wrote and verified 10 values successfully.
Identify Device:
serial number:     001705A0603R4656
firmware revision: Vdg 1.23
model number: SanDisk SDCFB-32                        
LBA mode supported
PIO mode supported: 4
device parameters:
 #  logical cylinders: 490
 #  logical heads: 4
 #  logical sectors/track: 32
  LBA capacity = 62720 sectors
successfully wrote default parameters to IDE drive.
readCHS: cylinder: 0; head; 0; sector: 0
CHS = (0, 0, 0); numSectors: 1
timeout awaiting DRQ=1
error register: 0x10
timeout awaiting DRQ in readSectorsCHS()
DRQ not set in status reg in readSectorsCHS()
readCHS() failed.
goodbye

I&#39;ve tried to increase the awaitDRQ time, but it&#39;s no use. Is it possible that this card isn&#39;t formatted properly? (It&#39;s not the one that came with the Nios Development kit.) Thanks for any help.

-Ole Kristian Tørresen

2 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I think I solved it. There is no sector number 0. I was confused by the second sector, it didn&#39;t contain what I expected. This is what the second sector contained:

    0000:  FA 33 C0 8E D0 BC 00 7C 8B F4 50 07 50 1F FB FC  .3.....|..P.P...
    0010:  BF 00 06 B9 00 01 F2 A5 EA 1D 06 00 00 BE BE 07  ................
    0020:  B3 04 80 3C 80 74 0E 80 3C 00 75 1C 83 C6 10 FE  ...<.t..<.u.....
    0030:  CB 75 EF CD 18 8B 14 8B 4C 02 8B EE 83 C6 10 FE  .u......L.......
    0040:  CB 74 1A 80 3C 00 74 F4 BE 8B 06 AC 3C 00 74 0B  .t..<.t.....<.t.
    0050:  56 BB 07 00 B4 0E CD 10 5E EB F0 EB FE BF 05 00  V.......^.......
    0060:  BB 00 7C B8 01 02 57 CD 13 5F 73 0C 33 C0 CD 13  ..|...W.._s.3...
    0070:  4F 75 ED BE A3 06 EB D3 BE C2 06 BF FE 7D 81 3D  Ou...........}.=
    0080:  55 AA 75 C7 8B F5 EA 00 7C 00 00 49 6E 76 61 6C  U.u.....|..Inval
    0090:  69 64 20 70 61 72 74 69 74 69 6F 6E 20 74 61 62  id partition tab
    00A0:  6C 65 00 45 72 72 6F 72 20 6C 6F 61 64 69 6E 67  le.Error loading
    00B0:  20 6F 70 65 72 61 74 69 6E 67 20 73 79 73 74 65   operating syste
    00C0:  6D 00 4D 69 73 73 69 6E 67 20 6F 70 65 72 61 74  m.Missing operat
    00D0:  69 6E 67 20 73 79 73 74 65 6D 00 00 00 00 00 00  ing system......
    00E0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    00F0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0100:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0110:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0120:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0130:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0140:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0150:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0160:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0170:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0180:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0190:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    01A0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    01B0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01  ................
    01C0:  01 00 04 03 60 E8 20 00 00 00 60 F4 00 00 00 00  ....`. ...`.....
    01D0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    01E0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    01F0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA  ..............U.

    And it didn&#39;t look like the boot sector and bpb. Please correct me if I&#39;m wrong.

    -Ole Kristian Tørresen
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi

    You are right. This is not the boot sector. From my experience, if you are using a FAT filesystem, you won&#39;t find the bpb at the actual first sectors. There are partition tables and other stuff before that. What I did was to read the first sectors until I found something that confirmed that this is the bpb, meaning the jumpBoot (starts with EB or E9) and also the last two bytes of the sector are 0x55 and 0xAA. I foudn the bpb at logical sector 32.

    Nir