My Code cann't detect Multiboot 2. Please Help.

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
Ananta96
Posts: 24
Joined: Mon Jul 01, 2019 10:46 am

My Code cann't detect Multiboot 2. Please Help.

Post by Ananta96 »

I've got troubled with the linker. My bootloader can't detect a multiboot header and fail. The message "no multiboot header found". My code has section multiboot but still error.

loader.asm

Code: Select all

section .multiboot_header
header_start:
    dd 0xe85250d6
    dd 0
    dd header_end - header_start
    dd 0x100000000 - (0xe85250d6 + 0 + (header_end - header_start))

    dw 0
    dw 0
    dd 0
header_end:

global start
extern long_mode_start

section .text
bits 32
start:
    mov esp, stack_top

    call check_multiboot
    call check_cpuid
    call check_long_mode

    call set_up_page_tables
    call enable_paging

    lgdt [gdt64.pointer]
    jmp gdt64.code:long_mode_start

    hlt

check_multiboot:
    cmp eax, 0x36d76289
    jne .no_multiboot
    ret
.no_multiboot:
    mov al,"0"
    jmp error

check_cpuid:
    pushfd
    pop eax
    mov ecx, eax
    xor eax, 1 << 21
    push eax
    popfd

    pushfd
    pop eax
    push ecx
    popfd

    xor eax, ecx
    jz .no_cpuid
    ret
.no_cpuid:
    mov al, "1"
    jmp error

check_long_mode:
    mov eax, 0x80000000 
    cpuid
    cmp eax, 0x80000001
    jb .no_longmode

    mov eax, 0x80000001
    cpuid
    test edx, 1 << 29
    jz .no_longmode
    ret
.no_longmode:
    mov al, "2"
    jmp error

set_up_page_tables:
    mov eax, p3_table
    or eax, 0b11
    mov [p4_table], eax

    mov eax, p2_table
    or eax, 0b11
    mov [p3_table], eax

    mov ecx, 0

.map_p2_table:
    mov eax, 0x200000
    mul ecx
    mov eax, 0b10000011
    mov [p2_table + ecx * 8],eax

    inc ecx
    cmp ecx, 512
    jne .map_p2_table

    ret

enable_paging:
    mov eax, p4_table
    mov cr3, eax

    mov eax, cr4
    or eax, 1 << 5
    mov cr4, eax

    mov ecx, 0xC0000080
    rdmsr
    or eax, 1 << 8
    wrmsr

    mov eax, cr0
    or eax, 1 << 31
    mov cr0, eax

    ret

error:
    mov dword [0xb8000], 0x4f524f45
    mov dword [0xb8004], 0x4f3a4f52
    mov dword [0xb8008], 0x4f204f20
    mov byte [0xb800a], al
    hlt

section .bss
align 4096
p4_table:
    resb 4096
p3_table:
    resb 4096
p2_table:
    resb 4096
stack_bottom:
    resb 64
stack_top:

section .rodata
gdt64:
    dq 0
.code: equ $ - gdt64
    dq (1 << 43) | (1 << 44) | (1 << 47) | (1 << 53)
.pointer:
    dw $ - gdt64 - 1
    dq gdt64
longmode.asm

Code: Select all

global long_mode_start

section .text
bits 64
long_mode_start:
    mov ax, 0
    mov ss, ax
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax

    extern kmain
    call kmain

    mov rax, 0x2f592f412f4b2f4f
    mov qword[0xb8000], rax
    hlt

Code: Select all

ENTRY(start)
SECTIONS
{
  . = 0x0100000;

  .text :
  {
    *(.multiboot_header)
    *(.text*)
    *(.rodata)
  }

  .data  :
  {
    start_ctors = .;
    KEEP(*( .init_array ));
    KEEP(*(SORT_BY_INIT_PRIORITY( .init_array.* )));
    end_ctors = .;

    *(.data)
  }

  .bss  :
  {
    *(.bss)
  }

  /DISCARD/ : 
  { 
    *(.fini_array*) 
    *(.comment) 
  }
}
Octocontrabass
Member
Member
Posts: 5578
Joined: Mon Mar 25, 2013 7:01 pm

Re: My Code cann't detect Multiboot 2. Please Help.

Post by Octocontrabass »

Ananta96 wrote:My bootloader can't detect a multiboot header and fail.
Which bootloader?

Code: Select all

    dw 0
    dw 0
    dd 0
header_end:
The size of a tag can never be less than 8. The final tag in your multiboot header must have a size of 8. Here's the relevant part of the multiboot specification if you need details.

You may need to specify alignment for your multiboot header; I'm not sure if the default alignment is good enough.
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: My Code cann't detect Multiboot 2. Please Help.

Post by Schol-R-LEA »

@Ananta96: Have you read the sticky post "Links and Advice for New Members" yet? It gives some helpful suggestions (and links to information sources, mostly in the wiki) which you may want to see.

Do you have an hosted source code repository which you could give a link to? It would allow us to see what you have done so far, which would make it easier for us to help you effectively.

If you don't have one, I strongly recommend setting one up, if only because accidents happen and being able to trace a bug back to its origin point is immensely useful. It is both a backup for your code, a means of sharing your code with others, a collaboration tool, and an audit trail for your changes. I know that I and some of others here really harp on this topic a lot, but trust me, it makes a huge difference in any sort of software development.

Also, if you are following any tutorials, please let us know which ones, so we can look through them ourselves and see what you recommendations you've read and compare your code to them, to see if there were any parts you seem to have misunderstood, or any places where the tutorial itself is flawed (pretty much all tutorials on this topic have some problems, so we may be able to point those out to you if we know which one you are using).
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
Ananta96
Posts: 24
Joined: Mon Jul 01, 2019 10:46 am

Re: My Code cann't detect Multiboot 2. Please Help.

Post by Ananta96 »

Finally, I've posted it in Github. But now I've a new problem that in kstdio (folder lib/kstdio.cpp) can't detect terminal text. So I cann't print using kprintf. My Github https://github.com/Ananta98/PetraOS
Octocontrabass
Member
Member
Posts: 5578
Joined: Mon Mar 25, 2013 7:01 pm

Re: My Code cann't detect Multiboot 2. Please Help.

Post by Octocontrabass »

Ananta96 wrote:lib/kstdio.cpp
This file doesn't exist.
Ananta96
Posts: 24
Joined: Mon Jul 01, 2019 10:46 am

Re: My Code cann't detect Multiboot 2. Please Help.

Post by Ananta96 »

I've updated sorry for trouble uploading. "kstdio" file didn't upload. Lib in outside forget it
Octocontrabass
Member
Member
Posts: 5578
Joined: Mon Mar 25, 2013 7:01 pm

Re: My Code cann't detect Multiboot 2. Please Help.

Post by Octocontrabass »

There are still files missing. Have you tried downloading your source from Github and compiling it?
Ananta96
Posts: 24
Joined: Mon Jul 01, 2019 10:46 am

Re: My Code cann't detect Multiboot 2. Please Help.

Post by Ananta96 »

I've finished kstdio problem. But now I have got a new problem. IDT doesn't work and double fault. I've decided using bitbucket as Version Control System https://bitbucket.org/Ananta99/petraos/src/master/
Octocontrabass
Member
Member
Posts: 5578
Joined: Mon Mar 25, 2013 7:01 pm

Re: My Code cann't detect Multiboot 2. Please Help.

Post by Octocontrabass »

I can't access that link without an account.
Ananta96
Posts: 24
Joined: Mon Jul 01, 2019 10:46 am

Re: My Code cann't detect Multiboot 2. Please Help.

Post by Ananta96 »

I'm sorry so for this disappointment. I've changed code to 32-bit format. I've got a problem with GDT, I take 4 hours and no solution. I've hoped the community can fix this. This is a new repository web https://github.com/Ananta98/PetraOS. I've got a problem with this code :

gdt.hpp

Code: Select all

#include <lib/types.hpp>

extern "C" {
    struct GDT_entry {
        uint16_t limit_low;
        uint16_t base_low;
        uint8_t base_middle;
        uint8_t access;
        uint8_t granularity;
        uint8_t base_high;
    }__attribute__((packed)); 

    struct GDT_ptr {
        uint16_t base;
        uint32_t limit;
    }__attribute__((packed));

    void initialize_gdt();
    void set_gdt_gate(int32_t number,uint32_t base, uint32_t limit,uint8_t access,uint8_t granularity);
};

gdt.cpp

Code: Select all

#include <arch/gdt.hpp>
#include <lib/kstdio.hpp>

GDT_entry gdt_entries[5];
GDT_ptr gdt_pointer;
extern "C" void load_gdt(uint32_t);

void initialize_gdt() {
    gdt_pointer.limit = (sizeof(struct GDT_entry) * 5) - 1;
    gdt_pointer.base = (uintptr_t)&gdt_entries;
    set_gdt_gate(0, 0, 0, 0, 0);                
    set_gdt_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); 
    set_gdt_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); 
    set_gdt_gate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF); 
    set_gdt_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF);
    load_gdt((uintptr_t)&gdt_pointer);
}

void set_gdt_gate(int32_t number,uint32_t base, uint32_t limit,uint8_t access,uint8_t granularity) {
    gdt_entries[number].base_low    = (base & 0xFFFF);
    gdt_entries[number].base_middle = (base >> 16) & 0xFF;
    gdt_entries[number].base_high   = (base >> 24) & 0xFF;

    gdt_entries[number].limit_low   = (limit & 0xFFFF);
    gdt_entries[number].granularity = ((limit >> 16) & 0x0F);
    gdt_entries[number].granularity |= (granularity & 0xF0);
    gdt_entries[number].access      = access;
}
load_gdt.asm

Code: Select all

section .text
[GLOBAL load_gdt]
load_gdt:
    mov eax, [esp+4]
    lgdt [eax]
    mov ax, 0x10
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    jmp 0x08:flush
flush:
    ret
all codes already in github https://github.com/Ananta98/PetraOS
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: My Code cann't detect Multiboot 2. Please Help.

Post by iansjack »

Ananta96 wrote:I take 4 hours and no solution. I've hoped the community can fix this.
Are you really saying that after 4 hours of trying to find this bug you now want the community to fix it for you?

You're going to have to try a little harder than that if you want to progress.
Octocontrabass
Member
Member
Posts: 5578
Joined: Mon Mar 25, 2013 7:01 pm

Re: My Code cann't detect Multiboot 2. Please Help.

Post by Octocontrabass »

Ananta96 wrote:I've got a problem with GDT, I take 4 hours and no solution.
It looks like you're copying code from James Molloy's tutorial. You should read this list of known bugs in the tutorial.
Post Reply