cancel
Showing results for 
Search instead for 
Did you mean: 

CHS to LBA conversion - How to get CHS and SPT on uSD card?

Zaher
Senior II

I need to do conversion between CHS addressing to LBA using the following formula:

LBA = (( C x HPC ) + H ) x SPT + S - 1

where,

  • C, H and S are the cylinder number, head number, and the sector number
  • LBA is the logical block address
  • HPC is the number of heads per cylinder
  • SPT is the number of sectors per track

but I just don't know where and how to get those from a uSD card? Is there any way to get them using the MMC/SDIO HAL library or FATFS?

1 ACCEPTED SOLUTION

Accepted Solutions

CHS has been irrelevant for 20+ years, the values are maximal and you end up well beyond 1023 cylinders.

They are not LL parameters, pull them directly from the structures in the relevant sectors. The logical block count comes out of the SD/MMC card's CSD REGISTER

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

17 REPLIES 17

The cards use logical block addressing

The cylinder, head and sector parameters can inferred from the MBR and BPB

Typically 63 SPT and 255 HPC

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Zaher
Senior II

Clive,

Thank you for the quick answer. However, these values (SPT & HPC) are fixed for all types of SD cards? And how to get them from MBR and BPB? Is there any LL functions to access them?

Thanks again!

CHS has been irrelevant for 20+ years, the values are maximal and you end up well beyond 1023 cylinders.

They are not LL parameters, pull them directly from the structures in the relevant sectors. The logical block count comes out of the SD/MMC card's CSD REGISTER

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Zaher
Senior II

That’s true, but when it comes to SCSI, it is still relevant so I’m forced to adopt it in my code as I’m working on a SCSI project.

Getting block count from CSD register is no problem, but where or what structures are used to get the other values concerned?

I’m on my mobile device now, but I might need to look at the MMC/SD HAL driver once I’m back to my PC. Meanwhile, it would help a lot if you can provide more on how to get those values.

Thanks again!

Zaher
Senior II

OK, I believe the "diskio" layer of the FATFS provides this function to read sectors off the medium:

/**
  * @brief  Reads Sector(s) 
  * @param  pdrv: Physical drive number (0..)
  * @param  *buff: Data buffer to store read data
  * @param  sector: Sector address (LBA)
  * @param  count: Number of sectors to read (1..128)
  * @retval DRESULT: Operation result
  */
DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber to identify the drive */
	BYTE *buff,		/* Data buffer to store read data */
	DWORD sector,	        /* Sector address in LBA */
	UINT count		/* Number of sectors to read */
)

I did not try it yet, but I think reading the first sector one could get those values from MBR/BPB. Or there's another approach to do it?

The DISKIO is entirely LBA, similarly USBSTOR/MSC which can use SCSI READ6 or READ10 type command formats.

The MBR is typically at sector zero, and you can pull the BPB sector from the partition table. On "Floppy Format" media, the BPB is at sector zero and you dispense with the MBR.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
READ 0 1
00000000 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000010 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000020 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000030 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000040 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000050 : 00 00 00 00 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 01 ................
000001C0 : 01 00 0C FE FF B6 3F 00-00 00 41 0E E9 00 00 00 ......?...A.....
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 55 AA ..............U.
 
READ 0 1
00000000 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000010 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000020 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000030 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000040 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
00000050 : 00 00 00 00 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 01 ................
000001C0 : 01 00 0C FE FF B6 3F 00-00 00 41 0E E9 00 00 00 ......?...A.....
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 55 AA ..............U.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

@Community member​ 

I guess if LBA<>CHS conversion was added, one can use the MSD SCSI functions' set as glue/wrapper functions for most of the disk-type SCSI commands. Or what do you think?

"The DISKIO is entirely LBA" so, it is the best way to access the storage device (SD card) at the block level or you suggest something else? How you read the 512 bytes below?

Zaher
Senior II

You read sectors 0 and 1? I don't see any difference!