Page 1 of 1

Absolute and Relative LBA addressing mode

Posted: Thu Mar 03, 2016 3:52 am
by Jlouro
I was surfing on the wiki and found an x86 ATA PIO Mode thread. I found two references of LBA, the Absolute and the Relative Mode.

If I want to convert from CHS to LBA there's an expression to calculate it:

LBA = (Cylinder * TotalHeads + SelectedHead) * SectorsPerTrack + (SectorNum - 1)

If I want to access CHS(0, 0, 1) through LBA, replacing in that expression I'll have:

LBA 0 = (0 * 16 + 0) * 63 + (1 - 1)

Is that LBA 0 absolute or relative ?
If I want to read, for example 3 sectors, do I need to calculate all the LBAs?
Example:

Reading 3 sectors starting CHS(0, 1, 1):

LBA 63 = (0 * 16 + 1) * 63 + (1 - 1)
LBA 64 = (0 * 16 + 1) * 63 + (2 - 1)
LBA 65 = (0 * 16 + 1) * 63 + (3 - 1)

And one more question:

To send that to LBA ports (0x1f3-0x1f5 (LBA High)) I need to get the result of that expression right?

Example:

Code: Select all

;ECX holds value 63 (from LBA 63)

inc edx            ;dx is now 0x1f3 - LBAlo
mov al, cl        ;cl has LBA
out dx, al
inc edx            ;dx is now 0x1f4 - LBAmid
mov al, ch       ;highest of 8 bits value
out dx, al
inc edx            ;dx is 0x1f5 the LBAhi
bswap ecx        ;swap the bits, cl will have bits 24-28 and ch will have bits 16-23
mov al, ch        ;put it in al
out dx, al

Re: Absolute and Relative LBA addressing mode

Posted: Thu Mar 03, 2016 4:14 am
by Octocontrabass
Jlouro wrote:If I want to convert from CHS to LBA
Why do you want to convert from CHS to LBA?
Jlouro wrote:Is that LBA 0 absolute or relative ?
Is that CHS (0,0,1) absolute or relative?
Jlouro wrote:If I want to read, for example 3 sectors, do I need to calculate all the LBAs?
If you are reading three sequential sectors, then they will have three sequential LBAs.

Re: Absolute and Relative LBA addressing mode

Posted: Thu Mar 03, 2016 4:37 am
by Jlouro
Octocontrabass wrote: Why do you want to convert from CHS to LBA?.
Is there other way to use LBA instead of converting it?
Isn't it easier to convert it through CHS?
Octocontrabass wrote: Is that CHS (0,0,1) absolute or relative?
What is main the difference between absolute and relative? (This is my main question)
Octocontrabass wrote: If you are reading three sequential sectors, then they will have three sequential LBAs.
So, I'm in the logic of LBA (I made that question only to be sure.)

I'm a newbie at LBA addressing and I want to implement that kind of addressing in my OS. That's the main reason I don't know other way to get LBAs without converting it through CHS.

Re: Absolute and Relative LBA addressing mode

Posted: Thu Mar 03, 2016 4:45 am
by Octocontrabass
Jlouro wrote:Is there other wayt to use LBA instead of converting it (with assembly)?
Yes, use LBA everywhere and never use CHS unless the hardware requires it.
Jlouro wrote:Isn't it easier to convert it?
No, it's easier to use LBA everywhere.
Jlouro wrote:What is main the difference between absolute and relative? (This is my main question)
Absolute LBAs refer to a specific sector on the disk. Relative LBAs are relative to some starting point, such as the start of the partition.

Re: Absolute and Relative LBA addressing mode

Posted: Thu Mar 03, 2016 4:54 am
by Jlouro
Octocontrabass wrote: Absolute LBAs refer to a specific sector on the disk. Relative LBAs are relative to some starting point, such as the start of the partition.
So, if I want to read LBA 54, it will be absolute.

If my disk starts at LBA 0 and ends at LBA 6556 (example) will be relative, right?

So, I want to read my disk from sector x to y. Let's say my sector X is LBA 64 and Y is LBA 70.

Read specific LBA 64 will be my Absolute LBA to say it will be from LBA 64 to 70 will be my relative LBA.

Is that the meaning of LBA absolute and relative?

Re: Absolute and Relative LBA addressing mode

Posted: Thu Mar 03, 2016 5:26 am
by Octocontrabass
Jlouro wrote:Is that the meaning of LBA absolute and relative?
No.

Relative LBA means LBA 0 is not the first sector on the disk. For example, if you have a partition that starts at absolute LBA 63, then relative LBA 100 in that partition is absolute LBA 163. Then, if you have another partition that starts at absolute LBA 2048, relative LBA 100 in that partition will be absolute LBA 2148.