OS development help - unable to load on real computer

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
hunter21
Posts: 1
Joined: Sun Oct 22, 2023 8:31 am

OS development help - unable to load on real computer

Post by hunter21 »

I have been using a couple of tutorials and using QEMU and VirtualBox to learn OS development. I have recently made three bootloaders which are a basic bootloader prints letter A using 0x10 interrupt, a bootloader that uses 0x13 to access disc and jumps to it, a bootloader that calls external C code.

I compile the code to .bin format using nasm and gcc -ffreestanding and then combine them using cat (in case of c kernel). The images work on qemu and if I turn them into floppy images they work on virtualbox too. When booting on a real computer the invalid partition table error is thrown so I followed the first part of this tutorial (https://wiki.osdev.org/Bootable_Disk#Cr ... _Partition) and used fdisk to just create partition table using option 'o' (DOS/MBR) then the last part adding the BIOS bootloader. This did not work. I have tested it on three computers, the first one says invalid partition table but does print letter B using the second bootloader if enter key is pressed, the second and third one does nothing except a blinking cursor. I have used legacy mode on all three computers. The third bootloader with external C code does not work or print anything on the real computer at all. Code for the first two bootloaders is below but I can post the bootloader with externel C code too if needed, it is just longer which is why I have not posted it.

Code: Select all


; Basic bootloader to print letter A
[org 0x7c00]

mov ah, 0x0e ; tell bios we want to print character
mov al, 'A'
int 0x10

times 510 - ($ - $$) db 0 
dw 0xAA55

Code: Select all


; Basic bootloader to print letter A
[org 0x7c00]

xor ax, ax 
mov ds, ax
cld

mov ah, 0x02 ;Read
mov al, 0x02 ;Read second sector
mov ch, 0x00 ;Cylinder 0
mov cl, 0x02 ;Sector
mov dh, 0x00
xor bx, bx
mov es, bx
mov bx, 0x8001 ;where to jump after reading sector
int 0x13

jmp 0x8001 ;Now load the second sector

times 510 - ($ - $$) db 0 
dw 0xAA55

mov ah, 0x0e
mov al, 'B'
int 0x10

jmp $ ;loop here

Any guidance on what I am doing wrong? Thank you.
Octocontrabass
Member
Member
Posts: 5486
Joined: Mon Mar 25, 2013 7:01 pm

Re: OS development help - unable to load on real computer

Post by Octocontrabass »

hunter21 wrote:I have been using a couple of tutorials
Bootloader tutorials are usually not very good. Also, writing your own bootloader takes time away from writing your OS. If you want to write an OS, you should use an existing bootloader like GRUB or Limine.
hunter21 wrote:I compile the code to .bin format using nasm and gcc -ffreestanding and then combine them using cat (in case of c kernel).
That doesn't sound like the correct way to use GCC...
hunter21 wrote:When booting on a real computer the invalid partition table error is thrown so I followed the first part of this tutorial
Huh. That tutorial has terrible advice, you don't need GPT or an ESP system partition to boot from USB.
hunter21 wrote:and used fdisk to just create partition table using option 'o' (DOS/MBR) then the last part adding the BIOS bootloader. This did not work.
If you want to partition your USB drive for boot, your MBR must include exactly one active primary partition. Some BIOSes are picky about the geometry, too.
hunter21 wrote:Code for the first two bootloaders is below but I can post the bootloader with externel C code too if needed, it is just longer which is why I have not posted it.
There are several websites (including GitHub and Gitlab) that can host all of your source code plus version history. You can use one of those to share your code. (You should use version control even if you don't want to use one of those websites.)

Code: Select all

mov al, 0x02 ;Read second sector
The comment is incorrect. That value determines how many sectors to read, not which sector to read.

Code: Select all

mov bx, 0x8001 ;where to jump after reading sector
You should use an aligned address. Also, you have no idea where the BIOS might have put its stack, so you might be overwriting the stack when you load data.
Post Reply