OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 11:59 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: [help]LBA to CHS function
PostPosted: Wed Jan 12, 2011 10:11 pm 
Offline

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

_________________
http://osdeverdiary.blogspot.com


Top
 Profile  
 
 Post subject: Re: [help]LBA to CHS function
PostPosted: Thu Jan 13, 2011 2:36 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
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.


Top
 Profile  
 
 Post subject: Re: [help]LBA to CHS function
PostPosted: Thu Jan 13, 2011 3:43 am 
Offline

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

_________________
http://osdeverdiary.blogspot.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Amazonbot [bot] and 72 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group