3

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
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
ElectroJunkie
  • 301
  • 5
  • 16

2 Answers2

1

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...

Community
  • 1
  • 1
E.T
  • 1,095
  • 1
  • 10
  • 19
0

The simple answer is that logical sector 19 is the 20th sector (numbering starts at 0). 20 divided by 18 sectors per track results in a remainder of 2. Sector numbering begins at 1 so the sector number is 2. There is only one physical disk in a floppy disk and thus 2 sides - head 0 and head 1. The 2nd sector of the 2nd side is cylinder 0 (numbering starts at 0) and the 2nd side is head 1 (numbering starts at 0). Head 1 (DL), Cylinder 0 (CH), Sector 2 (CL)

The happy answer is that the latest version of MikeOS will boot and run from a USB flash drive. Say goodbye to floppy disks (if you can find one).

Mike Gonta
  • 644
  • 6
  • 11
  • Thanks for the kind response Mike. Much appreciated. – ElectroJunkie Feb 01 '12 at 22:48
  • 1
    It turns out that I was missing an important fact. When disk was written, I thought that data fills the head 0 first and moves to the head 1. In other words, I thought the track 1 was on the head 0, while it is actually on the head 1. I hope this helps for someone who has the same question or trouble understanding it as I did. – ElectroJunkie Feb 01 '12 at 23:07
  • Track are numbered from 0 up, starting at the edge of the disk and moving inwards. Then you change the head 0 (upper surface of the disk) to the head 1 (the "other side" of the disk) and the numeration of tracks starts again from 0. Upper track 0 (being read by head 0) and lower track 0 (being read by head 1) lay one above the other, in the same *cylinder*. Thus, all tracks in the same cylinder have the same number, but make "layers", as floors in a building. – SasQ Feb 05 '15 at 01:59