2

i am currently following an OS dev tutorial (https://www.youtube.com/watch?v=pXzortxPZR8&list=PLxN4E629pPnKKqYsNVXpmCza8l0Jb6l8-)

i reached episode 4 and i decided to test my os on real hardware, so i copied the binary contents of the "boot.bin" file and i put it on a USB flash drive using HxD Hex Editor. i also made sure to put the bytes into the correct sectors, but when i tried to boot from it all i got was a smiley face emoji at the bottom of the screen (the os is made to read 4 sectors from the disk and say "disk read successfully" if it successfully did so, it also is 32 bit and i made a Global Descriptor Table from the tutorial) the usb i used was a 4gb MBR formattet USB with the FAT file system, i also tried FAT32 but it didnt help, also the computer i used to boot from is an old PC that i decided to make an os for (it has a dead hdd so i cant install windows on it and i decided i could learn something new by doing this). can anyone help me? i would really like to get this working.

EDIT: i forgot to mention this, but as the title says, it works perfectly fine on emulators

Bootloader:

[org 0x7c00]

mov [BOOT_DISK], dl

mov bp, 0x7c00
mov sp, bp

call ReadDisk

jmp PROGRAM_SPACE

%include "print.asm"
%include "DskRead.asm"

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

Read Disk Program:

PROGRAM_SPACE equ 0x7e00

ReadDisk:
    mov ah, 0x02
    mov bx, PROGRAM_SPACE
    mov al, 4
    mov dl, [BOOT_DISK]
    mov ch, 0x00
    mov dh, 0x00
    mov cl, 0x02
    int 0x13
    jc DiskReadFailed
    ret

BOOT_DISK:
    db 0

DiskReadErrorStr:
    db 'Failed To Read Disk',0

DiskReadFailed:
    mov bx, DiskReadErrorStr
    call printstr
    jmp $

Extended Program (the code written on secotr 2 or higher):

[org 0x7e00]

jmp EnterProtectedMode

%include "gdt.asm"
%include "print.asm"

EnterProtectedMode:
    call EnableA20
    cli
    lgdt [gdt_descriptor]
    mov eax, cr0
    mov eax, 1
    mov cr0, eax
    jmp codeseg:StartProtectedMode

EnableA20:
    in al, 0x92
    or al, 2
    out 0x92, al
    ret

[bits 32]

StartProtectedMode:
    mov ax, dataseg
    mov ds, ax
    mov ss, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov [0xb8000], byte 'H'
    jmp $

times 2048-($-$$) db 0

print function code:

printstr:
    mov ah, 0x0e
    .Loop:
    cmp [bx], byte 0
    je .Exit
        mov al, [bx]
        int 0x10
        inc bx
        jmp .Loop
    .Exit:
    ret

GDT:

gdt_nulldesc:
    dd 0
    dd 0
gdt_codedesc:
    dw 0xFFFF
    dw 0x0000
    db 0x00
    db 10011010b
    db 11001111b
    db 0x00
gdt_datadesc:
    dw 0xFFFF
    dw 0x0000
    db 0x00
    db 10010010b
    db 11001111b
    db 0x00

gdt_end:


gdt_descriptor:
    gdt_size:
        dw gdt_end - gdt_nulldesc - 1
        dd gdt_nulldesc

codeseg equ gdt_codedesc - gdt_nulldesc
dataseg equ gdt_datadesc - gdt_nulldesc

Run Bat script (QEMU):

qemu-system-x86_64 boot.bin

Compile Bat Script (NASM):

nasm -f bin boot.asm -o boot.bin
nasm -f bin ExtPrg.asm -o ExtPrg.bin
copy /b boot.bin+ExtPrg.bin boot.bin
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Nutty
  • 33
  • 6
  • 1
    Please show your code. – fuz Aug 12 '21 at 11:51
  • 2
    Do you have a BPB in your boot sector? That's the #1 most common cause for stuff not working on real hardware. – Jester Aug 12 '21 at 11:59
  • I have no idea what a BPB is, i am new to this kind of programming and os dev :( it works fine on real hardware if i only make it write stuff on the screen using the bios int 0x10 and it also works fine if i make a simple keyboard program using int 0x16 – Nutty Aug 12 '21 at 12:09
  • @Nutty BPB = Bios Paramter Block. An area in the boot sector the BIOS overwrites with certain data if it believes you are booting from a floppy disk. – fuz Aug 12 '21 at 12:11
  • If i format it, a few bytes appear in the boot sector without me touching it, is that the BPB? If it isnt, i have no idea how to make one. Also i am not booting from a floppy – Nutty Aug 12 '21 at 12:15
  • 3
    See the _"Real Hardware / USB / Laptop Issues"_ section in [this answer](https://stackoverflow.com/a/43787939/547981) – Jester Aug 12 '21 at 12:30
  • Oh my god youre a genius! You dont even know for how much time ive been fighting with this problem, thank you so much! – Nutty Aug 12 '21 at 13:31
  • 5
    Your code also seems to assume that all segment registers are initialized to zero. Real BIOSes don't always do that, so you ought to do it yourself. – Nate Eldredge Aug 12 '21 at 18:44
  • Also you say you use 32 bits, although your code looks correct, you start in 8086 mode still, which is 16 bits. I see that you switch to 32 later. – Alexis Wilke Aug 14 '21 at 22:11

0 Answers0