3

I'm done making my bootloader and kernel that switches from real-mode to protected-mode and Displays the character; 'S'. But when I boot it from Bochs, the last line of the my log is just: "Booting from 0000:7c00" and doesn't get past there. I edited and rewrote my code but still. I tried using QEmu to emulate but says: "boot device not found". I read through this Stack Overflow Question: "https://stackoverflow.com/questions/3285721/oss-boot-loader-doesnt-work" but that question has no answer and tried to read a couple OSDev Questions and Answers, but it didn't solve my problem here's the link: "https://forum.osdev.org/viewtopic.php?t=7877&p=46293", and some other links that I forgot. I tried other compilation options on gcc (about compiling down below.) and still doesn't work. I provided an disassembly for further analysis. and Thank you for taking a look at this Question!

Here's my code: (Filename "start_boot.asm")

 [org 7c000h]

DRIVE_ID db 0
KERNEL_HEX equ 0x1000

    mov bp, 9000h
    mov sp, bp

    mov bh, [_realmode]
    call _print

    call prep_kernel
    call init_gdt

    jmp $

%include "get_sector.asm"
%include "print_str.asm"
%include "print_str_pm.asm"
%include "gdt_conf.asm"
%include "init_protm.asm"

_realmode       db    "Bit mode: 16-bit", 0
_pmmode         db    "Bit mode: 32-bit", 0
_kernel_lod     db    "Kernel is loading... Please wait....", 0

[bits 16]

prep_kernel:
    mov bh, _kernel_lod
    call _print

    mov ah, 02h
    mov al, 15
    mov ch, 0
    mov cl, 1
    mov dh, 0
    mov dl, [DRIVE_ID]

    mov bx, KERNEL_HEX
    mov es, bx
    
    call _read

    ret

[bits 32]
launch_kernel:
    mov ebx, _kernel_lod
    call _print_pm

    call KERNEL_HEX

    jmp $

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

Headers of "start_boot.asm": ("get_sector.asm")

_disk_err db "Media error occured! we'll restart.", 0

_read:
    push dx
    xor ah, ah
    mov ah, 02h

    mov dl, DRIVE_ID
    mov ch, 00h
    mov dh, 00h
    mov al, dh
    mov cl, 02h

    int 13h
    
    jc err

    pop dx
    cmp dh, al
    jne err
    ret

err:
    mov bh, [_disk_err]
    call _print
    hlt

("print_str.asm")

    [bits 16]
    
    _print:
        mov ah, 0Eh
        mov al, bh
        int 10h

("print_str_pm.asm")

[bits 32]

VIDEO_HEX   equ 0xb8000
WHITE_BLACK equ 0Fh

_print_pm:
    pusha
    mov edx, VIDEO_HEX

print_pm_loop:
    mov al, [ebx]
    mov ah, WHITE_BLACK

    cmp al, 0
    je print_done

    mov [edx], ax

    add ebx, 1
    add edx, 2

    jmp print_pm_loop

print_done:
    popa
    ret

("gdt_conf.asm")

desc_start:

null_desc:
    dd 0x0
    dd 0x0

code_desc:
    dw 0xFFFF
    dw 0x0
    db 0x0
    db 10011010b
    db 11001111b
    db 0x0

data_desc:
    dw 0xFFFF
    dw 0x0
    db 0x0
    db 10010010b
    db 11001111b
    db 0x0

desc_end:

gdt_config_desc:
    dw desc_end - desc_start - 1

    dd desc_start

CODE_SEGM equ code_desc - desc_start
DATA_SEGM equ data_desc - desc_start

("init_protm.asm")

init_gdt:
    cli

    lgdt [gdt_config_desc]

    mov eax, cr0
    or eax, 0x1
    mov cr0, eax

    jmp CODE_SEGM:init_pm 

[bits 32]
init_pm:
    mov ax, DATA_SEGM
    mov ds, ax
    mov ss, ax
    mov es, ax
    mov fs, ax
    mov gs, ax

    mov ebp, 0x90000
    mov esp, ebp

    call launch_kernel

(and that's all)

I link this file to the kernel so that I can prevent the kernel from going back the boot sector if it encounters a ret instruction

("link.asm")

[bits 32]
[extern main]

call main
jmp $

("kernel.c")

void main(void)
    {
        char* VID_MEM = (char *) 0xB8000;
        *VID_MEM = 'S';
    }

And here's the Disassembly file of the compiled OS:

(it's long)

00000000  00BD009089EC      add [ebp-0x13767000],bh
00000006  8A3E              mov bh,[esi]
00000008  CB                retf
00000009  C0E84D            shr al,byte 0x4d
0000000C  00E8              add al,ch
0000000E  0201              add al,[ecx]
00000010  E88600EBFE        call 0xfeeb009b
00000015  4D                dec ebp
00000016  6564696120657272  imul esp,[fs:ecx+0x20],dword 0x6f727265
         -6F
0000001F  7220              jc 0x41
00000021  6F                outsd
00000022  636375            arpl [ebx+0x75],sp
00000025  7265              jc 0x8c
00000027  642120            and [fs:eax],esp
0000002A  7765              ja 0x91
0000002C  27                daa
0000002D  6C                insb
0000002E  6C                insb
0000002F  207265            and [edx+0x65],dh
00000032  7374              jnc 0xa8
00000034  61                popa
00000035  7274              jc 0xab
00000037  2E005230          add [cs:edx+0x30],dl
0000003B  E4B4              in al,0xb4
0000003D  02B200B500B6      add dh,[edx-0x49ff4b00]
00000043  0088F0B102CD      add [eax-0x32fd4e10],cl
00000049  137206            adc esi,[edx+0x6]
0000004C  5A                pop edx
0000004D  38C6              cmp dh,al
0000004F  7501              jnz 0x52
00000051  C3                ret
00000052  8A3E              mov bh,[esi]
00000054  15C0E80100        adc eax,0x1e8c0
00000059  F4                hlt
0000005A  B40E              mov ah,0xe
0000005C  88F8              mov al,bh
0000005E  CD10              int 0x10
00000060  60                pusha
00000061  BA00800B00        mov edx,0xb8000
00000066  8A03              mov al,[ebx]
00000068  B40F              mov ah,0xf
0000006A  3C00              cmp al,0x0
0000006C  740B              jz 0x79
0000006E  668902            mov [edx],ax
00000071  83C301            add ebx,byte +0x1
00000074  83C202            add edx,byte +0x2
00000077  EBED              jmp short 0x66
00000079  61                popa
0000007A  C3                ret
0000007B  0000              add [eax],al
0000007D  0000              add [eax],al
0000007F  0000              add [eax],al
00000081  0000              add [eax],al
00000083  FF                db 0xff
00000084  FF00              inc dword [eax]
00000086  0000              add [eax],al
00000088  9ACF00FFFF0000    call 0x0:0xffff00cf
0000008F  0092CF001700      add [edx+0x1700cf],dl
00000095  7BC0              jpo 0x57
00000097  07                pop es
00000098  00FA              add dl,bh
0000009A  0F011593C00700    lgdt [dword 0x7c093]
000000A1  0F20C0            mov eax,cr0
000000A4  83C801            or eax,byte +0x1
000000A7  0F22C0            mov cr0,eax
000000AA  EAB1C007000800    jmp 0x8:0x7c0b1
000000B1  66B81000          mov ax,0x10
000000B5  8ED8              mov ds,eax
000000B7  8ED0              mov ss,eax
000000B9  8EC0              mov es,eax
000000BB  8EE0              mov fs,eax
000000BD  8EE8              mov gs,eax
000000BF  BD00000900        mov ebp,0x90000
000000C4  89EC              mov esp,ebp
000000C6  E863000000        call 0x12e
000000CB  42                inc edx
000000CC  6974206D6F64653A  imul esi,[eax+0x6d],dword 0x3a65646f
000000D4  2031              and [ecx],dh
000000D6  362D62697400      ss sub eax,0x746962
000000DC  42                inc edx
000000DD  6974206D6F64653A  imul esi,[eax+0x6d],dword 0x3a65646f
000000E5  2033              and [ebx],dh
000000E7  322D62697400      xor ch,[dword 0x746962]
000000ED  4B                dec ebx
000000EE  65726E            gs jc 0x15f
000000F1  656C              gs insb
000000F3  206973            and [ecx+0x73],ch
000000F6  206C6F61          and [edi+ebp*2+0x61],ch
000000FA  64696E672E2E2E20  imul ebp,[fs:esi+0x67],dword 0x202e2e2e
00000102  50                push eax
00000103  6C                insb
00000104  6561              gs popa
00000106  7365              jnc 0x16d
00000108  207761            and [edi+0x61],dh
0000010B  69742E2E2E2E00B7  imul esi,[esi+ebp+0x2e],dword 0xb7002e2e
00000113  ED                in eax,dx
00000114  E843FFB402        call 0x2b5005c
00000119  B00F              mov al,0xf
0000011B  B500              mov ch,0x0
0000011D  B101              mov cl,0x1
0000011F  B600              mov dh,0x0
00000121  8A16              mov dl,[esi]
00000123  00C0              add al,al
00000125  BB00108EC3        mov ebx,0xc38e1000
0000012A  E80CFFC3BB        call 0xbbc4003b
0000012F  ED                in eax,dx
00000130  C00700            rol byte [edi],byte 0x0
00000133  E828FFFFFF        call 0x60
00000138  E8C34EF8FF        call 0xfff85000
0000013D  EBFE              jmp short 0x13d
0000013F  0000              add [eax],al
00000141  0000              add [eax],al
00000143  0000              add [eax],al
00000145  0000              add [eax],al
00000147  0000              add [eax],al
00000149  0000              add [eax],al
0000014B  0000              add [eax],al
0000014D  0000              add [eax],al
0000014F  0000              add [eax],al
00000151  0000              add [eax],al
00000153  0000              add [eax],al
00000155  0000              add [eax],al
00000157  0000              add [eax],al
00000159  0000              add [eax],al
0000015B  0000              add [eax],al
0000015D  0000              add [eax],al
0000015F  0000              add [eax],al
00000161  0000              add [eax],al
00000163  0000              add [eax],al
00000165  0000              add [eax],al
00000167  0000              add [eax],al
00000169  0000              add [eax],al
0000016B  0000              add [eax],al
0000016D  0000              add [eax],al
0000016F  0000              add [eax],al
00000171  0000              add [eax],al
00000173  0000              add [eax],al
00000175  0000              add [eax],al
00000177  0000              add [eax],al
00000179  0000              add [eax],al
0000017B  0000              add [eax],al
0000017D  0000              add [eax],al
0000017F  0000              add [eax],al
00000181  0000              add [eax],al
00000183  0000              add [eax],al
00000185  0000              add [eax],al
00000187  0000              add [eax],al
00000189  0000              add [eax],al
0000018B  0000              add [eax],al
0000018D  0000              add [eax],al
0000018F  0000              add [eax],al
00000191  0000              add [eax],al
00000193  0000              add [eax],al
00000195  0000              add [eax],al
00000197  0000              add [eax],al
00000199  0000              add [eax],al
0000019B  0000              add [eax],al
0000019D  0000              add [eax],al
0000019F  0000              add [eax],al
000001A1  0000              add [eax],al
000001A3  0000              add [eax],al
000001A5  0000              add [eax],al
000001A7  0000              add [eax],al
000001A9  0000              add [eax],al
000001AB  0000              add [eax],al
000001AD  0000              add [eax],al
000001AF  0000              add [eax],al
000001B1  0000              add [eax],al
000001B3  0000              add [eax],al
000001B5  0000              add [eax],al
000001B7  0000              add [eax],al
000001B9  0000              add [eax],al
000001BB  0000              add [eax],al
000001BD  0000              add [eax],al
000001BF  0000              add [eax],al
000001C1  0000              add [eax],al
000001C3  0000              add [eax],al
000001C5  0000              add [eax],al
000001C7  0000              add [eax],al
000001C9  0000              add [eax],al
000001CB  0000              add [eax],al
000001CD  0000              add [eax],al
000001CF  0000              add [eax],al
000001D1  0000              add [eax],al
000001D3  0000              add [eax],al
000001D5  0000              add [eax],al
000001D7  0000              add [eax],al
000001D9  0000              add [eax],al
000001DB  0000              add [eax],al
000001DD  0000              add [eax],al
000001DF  0000              add [eax],al
000001E1  0000              add [eax],al
000001E3  0000              add [eax],al
000001E5  0000              add [eax],al
000001E7  0000              add [eax],al
000001E9  0000              add [eax],al
000001EB  0000              add [eax],al
000001ED  0000              add [eax],al
000001EF  0000              add [eax],al
000001F1  0000              add [eax],al
000001F3  0000              add [eax],al
000001F5  0000              add [eax],al
000001F7  0000              add [eax],al
000001F9  0000              add [eax],al
000001FB  0000              add [eax],al
000001FD  0055AA            add [ebp-0x56],dl
00000200  E802000000        call 0x207
00000205  EBFE              jmp short 0x205
00000207  55                push ebp
00000208  48                dec eax
00000209  89E5              mov ebp,esp
0000020B  48                dec eax
0000020C  C745F800800B00    mov dword [ebp-0x8],0xb8000
00000213  48                dec eax
00000214  8B45F8            mov eax,[ebp-0x8]
00000217  C60053            mov byte [eax],0x53
0000021A  90                nop
0000021B  5D                pop ebp
0000021C  C3                ret
0000021D  0000              add [eax],al
0000021F  001400            add [eax+eax],dl
00000222  0000              add [eax],al
00000224  0000              add [eax],al
00000226  0000              add [eax],al
00000228  017A52            add [edx+0x52],edi
0000022B  0001              add [ecx],al
0000022D  7810              js 0x23f
0000022F  011B              add [ebx],ebx
00000231  0C07              or al,0x7
00000233  08900100001C      or [eax+0x1c000001],dl
00000239  0000              add [eax],al
0000023B  001C00            add [eax+eax],bl
0000023E  0000              add [eax],al
00000240  C7                db 0xc7
00000241  FF                db 0xff
00000242  FF                db 0xff
00000243  FF16              call [esi]
00000245  0000              add [eax],al
00000247  0000              add [eax],al
00000249  41                inc ecx
0000024A  0E                push cs
0000024B  108602430D06      adc [esi+0x60d4302],al
00000251  51                push ecx
00000252  0C07              or al,0x7
00000254  0800              or [eax],al
00000256  0000              add [eax],al

How I compile:

1. nasm link.asm -f elf64 -o llink.o
2. nasm start_boot.asm -f bin -o boot.bin
3. gcc -ffreestanding -c kernel.c -o krnl.o
4. ld -o krnlf.bin -Ttext 0x1000 link.o krnl.o -oformat binary
5. cat link.o krnl.o > osimg

Notes:

  1. I'll be compiling the "link.asm" as elf64 otherwise, GCC Linker will tell you that: "Link.asm is not compatible with 'i386:x86_64' output"
  2. sure, should be binary.
  3. I tried -fno-pie, -no-stdlib, -fno-builtin and combined them but still.
  4. This where I link "link.o" and "krnl.o".
  5. to combine/stitch them together?

UPDATE: Now I removed the header file; "get_sector.asm"

and edited my bootsector, compiled it. here's the code of my latest bootsector:

 [org 7c000h]

DRIVE_ID db 0
KERNEL_HEX equ 0x1000

    mov bp, 9000h
    mov sp, bp

    mov bh, [_realmode]
    call _print

    call prep_kernel
    call init_gdt

    jmp $

%include "print_str.asm"
%include "print_str_pm.asm"
%include "gdt_conf.asm"
%include "init_protm.asm"

_realmode       db    "Bit mode: 16-bit", 0
_pmmode         db    "Bit mode: 32-bit", 0
_kernel_lod     db    "Kernel is loading... Please wait....", 0

[bits 16]

prep_kernel:
    mov bh, _kernel_lod
    call _print

    mov ah, 02h
    mov al, 15
    mov ch, 0
    mov cl, 1
    mov dh, 0
    mov dl, [DRIVE_ID]

    mov bx, KERNEL_HEX
    mov es, bx
    
    int 13h

    ret

[bits 32]
launch_kernel:
    mov ebx, _kernel_lod
    call _print_pm

    call KERNEL_HEX

    jmp $

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

now QEmu produces the following output:

SeaBIOS bla bla...

iPXE bla bla...

Booting from Hard Disk...
 _

it made a space before the cursor... maybe it booted successfully? or what?

Sorry for the long Question... -_-

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
FryingRonald
  • 53
  • 1
  • 8
  • I think there's a typo: `VID_MEM* = 'S';` should be `*VID_MEM = 'S';` – Luca Polito Sep 22 '21 at 21:09
  • I'll try thanks for the comment! – FryingRonald Sep 22 '21 at 21:10
  • Unfortunately, It was a typo on my Question. But thank you so much for the helpful comment! – FryingRonald Sep 22 '21 at 21:12
  • `DRIVE_ID db 0` is the opcode for a memory-destination add instruction, and messes up decoding of the later instructions (since decoding will start wherever that `00 modrm` instruction ends. [Assembly (x86): db 'string',0 does not get executed unless there's a jump instruction](https://stackoverflow.com/q/30561366). That might not be the only bug, but until that's fixed and tested, it's probably not worth having having other people spend time looking at the rest of the code. – Peter Cordes Sep 22 '21 at 21:14
  • I'll try your comment and try to recompile! thank you btw! – FryingRonald Sep 22 '21 at 21:16
  • BTW, a debug technique that should let you spot bugs like this in future is to single-step your code in a debugger, e.g. the one built-in to Bochs. Seeing disassembly start with some weird `add` instruction should get your attention. You are using a debugger, I hope... otherwise it's like trying to build a robot blindfolded. – Peter Cordes Sep 22 '21 at 21:18
  • Finally I can sleep (jk :D) thank you for all the answers and Fortunately I was able to solve it! thanks stack overflow! – FryingRonald Sep 22 '21 at 21:21
  • sure I'll use your debugging technique next time – FryingRonald Sep 22 '21 at 21:21

0 Answers0