LBA / CHS confusion !

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
deph
Posts: 13
Joined: Sun Jun 22, 2008 6:04 am

LBA / CHS confusion !

Post by deph »

Hello there !
I am new to operating system development, and before learning about the kernel, I decided to write a small (2 stage) bootloader;
I have read about CHS and LBA, but after looking at GRub Stage1 I was confused. And here are my questions:
a. Is LBA just a method of expressing blocks in a linear mode ? (you still have to convert lba address to chs in order to read from a device)
b. Whose extension is INT 0x13 AH=41 checking ? (BIOS extension AH=42 for reading blocks using LBA addressing mode ?!)
c. Is GRub loading stage2 from a well known-place and implementing FS i/o in stage2 ? ... or it searches the device for a file named " ... "
User avatar
Stevo14
Member
Member
Posts: 179
Joined: Fri Mar 07, 2008 3:40 am
Location: Arad, Romania

Re: LBA / CHS confusion !

Post by Stevo14 »

deph wrote:Hello there !
I am new to operating system development, and before learning about the kernel, I decided to write a small (2 stage) bootloader;
I have read about CHS and LBA, but after looking at GRub Stage1 I was confused. And here are my questions:
a. Is LBA just a method of expressing blocks in a linear mode ? (you still have to convert lba address to chs in order to read from a device)
Yes, LBA is a way of addressing the disk in a linear way, but for any modern drive there is no need to convert the value to CHS before sending it to the drive. The drive will happily accept, and properly work with a LBA address.

Also, Grub's stage 2 is loaded from a known sector. Stage 1 is told which sector when Grub is installed on the hard drive.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: LBA / CHS confusion !

Post by JamesM »

deph wrote:Hello there !
I am new to operating system development, and before learning about the kernel, I decided to write a small (2 stage) bootloader;
I have read about CHS and LBA, but after looking at GRub Stage1 I was confused. And here are my questions:
a. Is LBA just a method of expressing blocks in a linear mode ? (you still have to convert lba address to chs in order to read from a device)
b. Whose extension is INT 0x13 AH=41 checking ? (BIOS extension AH=42 for reading blocks using LBA addressing mode ?!)
c. Is GRub loading stage2 from a well known-place and implementing FS i/o in stage2 ? ... or it searches the device for a file named " ... "
Hi,

(a) LBA stands for Logical Block Addressing - it's a method of expressing the location of a sector on disk. The three currently (normally) supported methods are CHS, LBA24 and LBA48 - the two latter being variations on the same scheme that merely use a different number of bits to represent an address (28 and 48 bits, respectively).

Most hard disk controllers support LBA28 - you can tell if a bit is set in the HDD's response to the ATA IDENTIFY command. To address in LBA you actually use the same registers as CHS (lba address >> 16 goes in the "head" register, address >> 8 goes in "cylinder", etc) so to someone looking at LBA code it could look like CHS is actually being used, and I assume this is what's confusing you.

(b) I'm not qualified to comment on - I've never used BIOS functions.

(c) Again, I'm not fully qualified to comment but I believe that GRUB uses a three-stage bootload - stage1 (in the MBR), stage1.5 then finally stage2. I'll let someone else take over from here ... ;)

Cheers,

James
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: LBA / CHS confusion !

Post by bewing »

deph wrote: a-1. Is LBA just a method of expressing blocks in a linear mode?
As said above, yes.
deph wrote: a-2. (you still have to convert lba address to chs in order to read from a device)
It depends. Maybe. Not necessarily. (Insert your weaselword of choice.) See answer to b.
deph wrote: b. Whose extension is INT 0x13 AH=41 checking ? (BIOS extension AH=42 for reading blocks using LBA addressing mode ?!)
Originally, there was no LBA ... disks only had CHS mode. You used CHS mode by calling standardized BIOS INT13h functions with CHS values. This worked for 10 years, until disks outgrew CHS addressing. So disk manufacturers invented LBA28. The different BIOS manufacturers all created different ways of handling LBA. It took a few years for the BIOS industry to settle on a standard for LBA addressing, but eventually they did. They created a new standardized set of INT13h functions called "The INT13h Extensions". After not too long, all the BIOSes on all the PCs supported the Extensions. This was in the mid-1990's. The AH=41 BIOS call is simply checking itself to verify that this extended set of BIOS function calls is supported by the BIOS. And really, if the AH=41 call even exists, then all the Extensions exist -- but you can't know until you make the call. :wink:

So, the answer really is: if the BIOS of your target PC is older than the mid-1990's, then LBA mode will not be supported by the BIOS, and the INT13h Extensions will not be supported by the BIOS, and the AH=41 and AH=42 calls will return errors, and you will need to calculate CHS conversions for any LBA of any drive you want to access through the BIOS.

If all your target machines are newer than mid-1990's, then the INT13h Extensions are always supported, and you can access LBAs directly with no conversions ever.

So: do you really want to kill yourself trying to support 80486 machines that are 15 or more years old?
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: LBA / CHS confusion !

Post by pcmattman »

It should be mentioned that it is possible that, if the drive does not support LBA, that the BIOS will automatically convert the LBA address into a CHS address that the drive can use. You can also do this in your own drivers (it's the main reason for my ATA drivers I always write 3 read/write functions, one for CHS, one for LBA28, and one for LBA48).
deph
Posts: 13
Joined: Sun Jun 22, 2008 6:04 am

Thanks !

Post by deph »

Thank you all for your replies;
One more (noob) question on this topic:
- How do I read form HDD in protected mode ? ... Is this done by using in/out from mapped software ports (and if I have ports, when hard disk interrupts <irqs> come handy ?)
- Do I have to implement functions for I/O with ATA and S-ATA devices ? Where can I get docs on I/O with ATA/S-ATA devices (some simple "HOW TO" articles, which can explain how to do I/O with such devices without BIOS support <since I'm planning to run in PMode, and Long mode maybe>).
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: LBA / CHS confusion !

Post by pcmattman »

I highly suggest finding the ATA specification, it has all the information you need. Also, try ATA_PIO_Mode on the wiki.
Post Reply