Search code examples
assemblynasmx86-16bootloaderbios

OSDev - Replace cylinder address to make OS run on real hardware


I'm following a github tutorial (https://github.com/gmarino2048/64bit-os-tutorial/)
and came across these lines of code in load.asm in chapter 1.3, part of the bootloader that loads the actual kernel:

    ; Next are the cylinder and cylinder head to read from. You
    ; would need to change these if reading from an actual drive, but with
    ; QEMU they're just 0
    mov ch, 0x00        ; Cylinder goes in ch
    mov dh, 0x00        ; Cylinder head goes in dh

; other register inputs also set up with instructions omitted here
    mov ah, 0x02
    int 0x13          ; BIOS disk read

I wanted to know how I could make it run on real hardware. Don't ask questions on why. So what address shall I replace "0x00" with? I am beginner to ASM but I know enough theory to be comfortable in it.

I tried search online for some details but saw no information. Also, when I tried look through the README.md for that specific chapter, it didn't give me an answer.


Solution

  • The answer will depend on your hardware, and specifically on where on the disk your boot sector is located. Hardcoding cylinder, head and sector numbers (or LBA sector number, which is what is normally used for everything except floppies) means that your boot sector will work only when written to a specific place on your device. If you are using the whole device, rather than a partition of it, your boot sector will be at CHS 0:0:1, or equivalently LBA 0, and the rest of stuff read by load.asm will be in subsequent sectors on the same cylinder 0 and head 0 as long as it fits inside 63 sectors of the CHS emulation mode (but really you should just use LBA addressing). Refer to OSDev Wiki on booting 1 2 and on the BIOS int 13h interface.