How to create 64bits multiboot2 graphics os with GRUB2 ?

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
Filadelphila
Posts: 2
Joined: Sat Jan 02, 2021 8:50 am

How to create 64bits multiboot2 graphics os with GRUB2 ?

Post by Filadelphila »

Hi everybody!
I'd like to create a little operating system for fun who can works on my personal computer basically to understand computer mecanics.
Until now I just worked on a VGA Text mode "kernel" (not really, just some c functions for wrtiting on the screen).
It's really easy to work with in qemu or real hardware but graphics scared me.
It seem difficult to find some resources who can help.
I understand that I need to get the video framebuffer address to work with but I don't see anything who can help me on internet.
I used this multiboot2 header for now with nasm:

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))

align 8
    dw 5
    dw 1
    dd 20
    dd 1024 
    dd 768
    dd 32

align 8
    dw 0
    dw 0
    dd 8
header_end:
On the gnu multiboot2 specification webpage I can't understand how used the multiboot2 header file.
I thought I could find it but really nothing about seem existing on internet.
Anyway, thanks a lot for taking time reading. :)
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: How to create 64bits multiboot2 graphics os with GRUB2 ?

Post by Octocontrabass »

I'm a bit confused. Where exactly are you getting stuck?
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: How to create 64bits multiboot2 graphics os with GRUB2 ?

Post by kzinti »

Here is how I parse the multiboot2 information:

https://github.com/kiznit/rainbow-os/bl ... #L219-L351
Filadelphila
Posts: 2
Joined: Sat Jan 02, 2021 8:50 am

Re: How to create 64bits multiboot2 graphics os with GRUB2 ?

Post by Filadelphila »

Thank for your answers!
For be clear, I'm confused by how to use the multiboot2.h header file suggested on the gnu website.
I want to get the framebuffer address for working in graphics mode. I think I have to get some others information too.
If I understand I can do this by implement a multiboot header.
My confused is that I can't find how to properly implement the multiboot2 specification in my project.
At the moment I use the grub2 bootloader.
Here is my kernel.c code:

Code: Select all

// multiboot2 file found on the gnu website
// https://www.gnu.org/software/grub/manual/multiboot2/multiboot.html#multiboot2_002eh
#include "multiboot2.h"
// .multiboot_header is defined in the linker.ld file below
struct multiboot_header test __attribute__((section(".multiboot_header"))) = { 
    MULTIBOOT2_HEADER_MAGIC,
    MULTIBOOT_ARCHITECTURE_I386,
    // don't know how to get header size by c code
    0,  
    0x100000000 - (MULTIBOOT2_HEADER_MAGIC + MULTIBOOT_ARCHITECTURE_I386 + 0)
};

struct multiboot_header_tag test_2 __attribute__((section(".multiboot_header"))) = { 
    .type = 0,
    .flags = 0,
    .size = 8 
};

void kernel_start () {
    // 0xa0000 address is used for example
    // the above code is incorrect
    unsigned int *buffer = (unsigned int *) 0xa0000;
    for (int i = 0; i < 640 * 480; i++) {
        buffer[i] = 0x4398DE19;
    }   
    while (1) {}
}
Here is my linker.ld file if it can help to understand:

Code: Select all

ENTRY(kernel_start)

SECTIONS {
    . = 1M; 

    .boot : ALIGN(0x1000) 
    {   
        *(.multiboot_header)
    }   
    .text : ALIGN(0x1000) 
    {   
        *(.text)
    }   
}
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: How to create 64bits multiboot2 graphics os with GRUB2 ?

Post by Octocontrabass »

Filadelphila wrote:For be clear, I'm confused by how to use the multiboot2.h header file suggested on the gnu website.
You don't have to use it. It's an example that shows one way you might define the things you need in order to make your OS compatible with multiboot2. You can
Filadelphila wrote:My confused is that I can't find how to properly implement the multiboot2 specification in my project.
I suggest writing the multiboot2 header in assembly, like you already have, and linking it together with the rest of your kernel.

Since you want 64-bit code, you also need to write assembly code to switch the CPU from 32-bit mode to 64-bit mode, set up the stack, and pass the multiboot2 information pointer to your C function. Getting all of that to work together correctly can be a challenge, so you might want to stick to 32-bit mode for now. (But you still need to set up the stack and pass the multiboot2 information pointer.)

Once your C code has access to the multiboot2 information pointer, you can use it to find the address of the framebuffer.
Post Reply