Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query BIOS using GRUB?

Tags:

c++

c

kernel

bios

grub

I am trying to make a small kernel for 80386 processor mainly for learning purpose and want to get the full memory map of the available RAM.

I have read that it is possible and better to do so with the help of GRUB than directly querying the BIOS.

Can anybody tell me how do I do it ?

Particularly, for using bios functionality in real mode we use bios interrupts and get the desired values in some registers , what is the actual equivalent way when we want to use GRUB provided functions ?

like image 970
Pratik Singhal Avatar asked Nov 01 '22 14:11

Pratik Singhal


1 Answers

Here is the process I use in my kernel (note that this is 32bit). In my bootstrap assembly file, I tell GRUB to provide me with a memory map:

.set MEMINFO,  1 << 1                   # Get memory map from GRUB

Then, GRUB loads the address of the multiboot info structure into ebx for you (this structure contains the address of the memory map). Then I call into C code to handle the actual iteration and processing of the memory map. I do something like this to iterate over the map:

/* Macro to get next entry in memory map */

#define MMAP_NEXT(m) \
            (multiboot_memory_map_t*)((uint32_t)m + m->size + sizeof(uint32_t))

void read_mmap(multiboot_info_t* mbt){

    multiboot_memory_map_t* mmap = (multiboot_memory_map_t*) mbt->mmap_addr;


    /* Iterate over memory map */

    while((uint32_t)mmap < mbt->mmap_addr + mbt->mmap_length) {

        // process the current memory map entry

        mmap = MMAP_NEXT(mmap);
    }
}

where multiboot_info_t and multiboot_memory_map_t are defined as in the Gnu multiboot.h file. As Andrew Medico posted in the comments, here is a great link for getting started with this.

like image 141
Joel Avatar answered Nov 15 '22 05:11

Joel