[help]LBA to CHS function

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
a07859
Posts: 10
Joined: Mon May 24, 2010 10:28 am
Contact:

[help]LBA to CHS function

Post by a07859 »

I try to load my loader on my boot loader 32 bit with FAT32. But my LBACHS function do not work properly. LBA to CHS formula is:
absolute sector = (logical sector / sectors per track) + 1
absolute head = (logical sector / sectors per track) MOD number of heads
absolute track = logical sector / (sectors per track * number of heads)

I try to load my root directory which is locate on LBA sector 0x2000 to memory 0:0x8000.
On BPB my sector per track is 0x3f and number of head is 0xffh.

From the formula it must be C=0 H=0x82 and S=3, but my function show C=0x82 H=3 S=0x82

here is my LBACHS code

Code: Select all

     ; PROCEDURE LBACHS
     ; convert "ax" LBA addressing scheme to CHS addressing scheme
     ; absolute sector = (logical sector / sectors per track) + 1
     ; absolute head   = (logical sector / sectors per track) MOD number of heads
     ; absolute track  = logical sector / (sectors per track * number of heads)
     ;*************************************************************************
     LBACHS:
          xor     dx, dx                              ; prepare dx:ax for operation
          div     WORD [A_BF_BPB_SectorsPerTrack]              ; calculate
          inc     dl                                  ; adjust for sector 0
          mov     BYTE [absoluteSector], dl
          xor     dx, dx                              ; prepare dx:ax for operation
          div     WORD [A_BF_BPB_Heads]                     ; calculate
          mov     BYTE [absoluteHead], dl
          mov     BYTE [absoluteTrack], al
          ret

Can someone tell me what's wrong on the function?

Thanks, best regards
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: [help]LBA to CHS function

Post by Brendan »

Hi,
a07859 wrote:I try to load my loader on my boot loader 32 bit with FAT32. But my LBACHS function do not work properly. LBA to CHS formula is:
absolute sector = (logical sector / sectors per track) + 1
absolute head = (logical sector / sectors per track) MOD number of heads
absolute track = logical sector / (sectors per track * number of heads)
That should be "absolute sector = (logical sector MOD sectors per track) + 1" (which is what your code is actually doing).

From the code in your previous post, we're working with 63 sectors per track, 255 heads and a total of 0x3C3F00 sectors. It's obvious that the LBA address won't fit in AX alone, and your function should be converting a 32-bit LBA address (in either EAX or "DX:AX") " into CHS because if you use AX alone it will only support 65536 sectors (or about 32 MiB).

Also note that for the BIOS "read sectors into memory" function the cylinder number is a 10-bit number and won't fit in an 8-bit register (and therefore your "absoluteTrack" should be 16-bit).

Of course when the LBA address is 0x2000 none of the problems I mentioned above would matter (it's only when you start using this LBA->CHS conversion code as a general purpose routine used for other things, like when you realise your OS needs to be in a partition and not using the entire device).
a07859 wrote:Can someone tell me what's wrong on the function?
Sooner or later you're going to need to setup some sort of emulator that allows you to debug your code properly (e.g. Bochs with it's inbuilt debugger, or Qemu with GDB). I'd suggest that now is the perfect opportunity for you to learn how to debug your code with this technique, as the knowledge/experience you gain with the debugger to solve this simple problem now will be extremely useful for fixing all of the (potentially more tricky) bugs you're going to have in future.... :)


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
a07859
Posts: 10
Joined: Mon May 24, 2010 10:28 am
Contact:

Re: [help]LBA to CHS function

Post by a07859 »

Also note that for the BIOS "read sectors into memory" function the cylinder number is a 10-bit number and won't fit in an 8-bit register (and therefore your "absoluteTrack" should be 16-bit).
I think is okay on 8 bit because the CHS of my disk is 0xff,0xff,0x3f and if I use 16 bit, int 13 for read sectors use 8 bit register for store the value of CHS (ch,cl,al)

thx,
Best Regards,
Post Reply