Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating Head, Cylinder (Track) and Sector for BIOS Interrupt 13h [duplicate]

I am currently trying to learn how to make a bootloader, and eventually an OS by studying open source code. The project I selected is MikeOS: http://mikeos.berlios.de/

While I was reading the bootloader code, I got stuck at the part where the code calculates the Head/Cylinder/Sector for BIOS INT 13h call, trying to read root directories from the disk.

If I understood correctly, the root directory begins at Sector 19. (Sector 0: Boot record, Sector 1 to 9: FAT12 copy 1, Sector 10 to 18: FAT12 copy 2) So, I think after the call, the output of the routine should be: Head 0, Track 1, Sector 19

However, when I follow the calculation, I obtain Head 1 (DL), Track 0 (CH), Sector 2 (CL) in corresponding register.

I might have done it wrong, but are these numbers what I am supposed to get? I don't question the code, since it is a working code. Apparently, I am missing some concept about either the partition table, or about the disk addressing.

Can anyone see what I may have done wrong, and how to correct it?

The routine is shown below: (comments are written by the designer)

Before the call:

mov ax, 19                          ; Root dir starts at logical sector 19

12hts:   ; Calculate head, track and sector settings for int 13h
         ; IN: logical sector in AX, OUT: correct registers for int 13h

push bx
push ax

mov bx, ax                          ; Save logical sector

mov dx, 0
div word [SectorsPerTrack]          ; First the sector
add dl, 01h                         ; Physical sectors start at 1

mov cl, dl                          ; Sectors belong in CL for int 13h
mov ax, bx

mov dx, 0                           ; Now calculate the head
div word [SectorsPerTrack]
mov dx, 0
div word [Sides]
mov dh, dl                          ; Head/side
mov ch, al                          ; Track

pop ax
pop bx

mov dl, byte [bootdev]              ; Set correct device

ret

bootdev         db   0   ; Boot device number
SectorPerTrack  dw  18   ; Sectors per track (36/cylinder)
Sides           dw   2   ; Number of sides/heads
like image 517
ElectroJunkie Avatar asked Jan 19 '26 00:01

ElectroJunkie


1 Answers

Keep in mind that the CHS stuff is for floppies only. Nowadays, you'll be using a HDD or even a USB device and your drive will most likely be formatted in FAT32.

At that point, you'll have to use the LBA to calculate your INT 13h values:

C = LBA ÷ (HPC × SPT)

H = (LBA ÷ SPT) mod HPC

S = (LBA mod SPT) + 1

For instance, Sector 2048 (where the 1st partition will most likely be, using qemu-img for example) will be CX = 0x0221 (C = 2, S = 21, H = 0)

Sadly enough, it took me a while to figure this out. I was doing the CHS calculation and I was getting the wrong values...

like image 102
E.T Avatar answered Jan 21 '26 19:01

E.T



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!