Page 1 of 1

How do you access arguments from GRUB in kernel?

Posted: Sat May 14, 2022 6:49 pm
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"?

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

Posted: Sat May 14, 2022 7:40 pm
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.

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

Posted: Sun May 15, 2022 5:26 am
by Demindiro
https://ftp.gnu.org/old-gnu/Manuals/gru ... iboot.html

I would also consider using Multiboot2 instead.

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

Posted: Sun May 15, 2022 1:22 pm
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.

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

Posted: Sun May 15, 2022 2:16 pm
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.

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

Posted: Sun May 15, 2022 4:06 pm
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?

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

Posted: Sun May 15, 2022 4:14 pm
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.