How do you access arguments from GRUB in kernel?

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
Techflash
Member
Member
Posts: 70
Joined: Sun May 08, 2022 2:10 am

How do you access arguments from GRUB in kernel?

Post by Techflash »

How would you go about accessing arguments that GRUB passed to the kernel, for example like this:

Code: Select all

menuentry "Example" {
    mutliboot /boot/os.elf argument=value
}
How would you be able to access the "argument"?
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

Re: How do you access arguments from GRUB in kernel?

Post by klange »

In Multiboot 1, the "cmdline" struct member is a 32-bit pointer to a null-terminated string that is passed along with the rest of the multiboot data.
User avatar
Demindiro
Member
Member
Posts: 96
Joined: Fri Jun 11, 2021 6:02 am
Libera.chat IRC: demindiro
Location: Belgium
Contact:

Re: How do you access arguments from GRUB in kernel?

Post by Demindiro »

https://ftp.gnu.org/old-gnu/Manuals/gru ... iboot.html

I would also consider using Multiboot2 instead.
My OS is Norost B (website, Github, sourcehut)
My filesystem is NRFS (Github, sourcehut)
Techflash
Member
Member
Posts: 70
Joined: Sun May 08, 2022 2:10 am

Re: How do you access arguments from GRUB in kernel?

Post by Techflash »

Hmmm, I checked using grub-file, my kernel is multiboot 1, but not multiboot 2 compliant. But I don't believe that I know how I could access the args. Unless the

Code: Select all

.long FLAGS
line in the muliboot header that's back in boot.S is the arguments? Even then, how would I be able to access that all the way over in the Kernel? Since I really would rather not be trying to do it with asm. If that's the ONLY possible way to do it then I will, but if I could pass it to the kernel and check it from there, I would much rather do that.
User avatar
Demindiro
Member
Member
Posts: 96
Joined: Fri Jun 11, 2021 6:02 am
Libera.chat IRC: demindiro
Location: Belgium
Contact:

Re: How do you access arguments from GRUB in kernel?

Post by Demindiro »

Techflash wrote:Since I really would rather not be trying to do it with asm. If that's the ONLY possible way to do it then I will, but if I could pass it to the kernel and check it from there, I would much rather do that.
You will have to use assembly. It's the only way to get the values out of the registers before they get clobbered. Besides, you should have some assembly already anyways to set up the stack.
My OS is Norost B (website, Github, sourcehut)
My filesystem is NRFS (Github, sourcehut)
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: How do you access arguments from GRUB in kernel?

Post by Octocontrabass »

Techflash wrote:Even then, how would I be able to access that all the way over in the Kernel?
When the bootloader jumps to your kernel entry point, EBX contains a pointer to the Multiboot information structure. You can pass that pointer to your main C function and then access all of the Multiboot information from C, including the kernel command line.

If you copied your code from a tutorial, it may already pass the pointer in EBX to your main C function.
Demindiro wrote:https://ftp.gnu.org/old-gnu/Manuals/grub-0.92/html_mono/multiboot.html
Huh, why not link the latest version of the spec?
User avatar
Demindiro
Member
Member
Posts: 96
Joined: Fri Jun 11, 2021 6:02 am
Libera.chat IRC: demindiro
Location: Belgium
Contact:

Re: How do you access arguments from GRUB in kernel?

Post by Demindiro »

Octocontrabass wrote:Huh, why not link the latest version of the spec?
I picked whatever from GNU showed up first when searching for it. I admittedly didn't check the version.
My OS is Norost B (website, Github, sourcehut)
My filesystem is NRFS (Github, sourcehut)
Post Reply