linux versus multiboot in GRUB2
linux versus multiboot in GRUB2
Hi,
as far as I can see there are several ways to load a kernel from GRUB2. For example there is the linux command to load a linux kernel and the multiboot command to load a multiboot compliant kernel like Hurd. But in terms of coding the kernel does anybody know what difference it makes. If I decide to for example load a hello world kernel with the linux command is there any special code that makes the kernel recognised as a 'linux' kernel? And with multiboot again is there any special code the kernel must have to make it multiboot compliant.
What would experienced kernel hackers advise? Which method of loading a kernel is best suited to a simple hello world kernel?
as far as I can see there are several ways to load a kernel from GRUB2. For example there is the linux command to load a linux kernel and the multiboot command to load a multiboot compliant kernel like Hurd. But in terms of coding the kernel does anybody know what difference it makes. If I decide to for example load a hello world kernel with the linux command is there any special code that makes the kernel recognised as a 'linux' kernel? And with multiboot again is there any special code the kernel must have to make it multiboot compliant.
What would experienced kernel hackers advise? Which method of loading a kernel is best suited to a simple hello world kernel?
Re: linux versus multiboot in GRUB2
Here some info for quite old Linux 2.4, but you will perhaps get an idea what's going on:
https://tldp.org/LDP/lki/lki-1.html
And here some info on the Multiboot protocol (old version and version 2) :
https://www.gnu.org/software/grub/manua ... cification
https://www.gnu.org/software/grub/manua ... cification
I personally find the Linux boot protocol very complicated and kind of crap. But you can decide yourself with the links I gave above.
Note that there are also other boot protocols (but not for Grub) :
- Bootboot
- Stivale
- Not sure if NexBoot is another one
A hello world kernel is best done with Grub Legacy or Grub 2 (read: Multiboot or Multiboot2). Or with you own tiny bootsector, but that's a lot of work for a newbie kernel developer and reinvents the wheel.
https://tldp.org/LDP/lki/lki-1.html
And here some info on the Multiboot protocol (old version and version 2) :
https://www.gnu.org/software/grub/manua ... cification
https://www.gnu.org/software/grub/manua ... cification
I personally find the Linux boot protocol very complicated and kind of crap. But you can decide yourself with the links I gave above.
Note that there are also other boot protocols (but not for Grub) :
- Bootboot
- Stivale
- Not sure if NexBoot is another one
A hello world kernel is best done with Grub Legacy or Grub 2 (read: Multiboot or Multiboot2). Or with you own tiny bootsector, but that's a lot of work for a newbie kernel developer and reinvents the wheel.
Re: linux versus multiboot in GRUB2
The multiboot links you posted have some example code but no compilation instructions. I'm guessing I will need a cross compiler of some description and need to compile with some special flags.
-
- Member
- Posts: 5563
- Joined: Mon Mar 25, 2013 7:01 pm
Re: linux versus multiboot in GRUB2
Yes, but you need that regardless of the boot protocol you choose.
Re: linux versus multiboot in GRUB2
In theory, yes, but I no longer maintain the reference implementation of it. I do plan on re coding it, and the spec is there for anyone to make there own implementation of . As for the question, I would recommend Multiboot 2 in general. For Raspberry Pi, use BOOTBOOT. For a lightweight boot protocol, I would use Stivale. My current OS uses Multiboot 2 at the moment.PeterX wrote:- Not sure if NexBoot is another one
Re: linux versus multiboot in GRUB2
Is the code for your OS available?nexos wrote: My current OS uses Multiboot 2 at the moment.
Re: linux versus multiboot in GRUB2
Yes. Not really an executable code, but a struct. Here's the specification.jamesread wrote:If I decide to for example load a hello world kernel with the linux command is there any special code that makes the kernel recognised as a 'linux' kernel?
Same, you need a struct. Here's the spec.jamesread wrote:And with multiboot again is there any special code the kernel must have to make it multiboot compliant.
Doesn't matter. It only depends on which loader you prefer.jamesread wrote:What would experienced kernel hackers advise? Which method of loading a kernel is best suited to a simple hello world kernel?
For example, take a look at my loader, it contains multiple structs to support many boot protocols (Multiboot, Linux boot, BIOS Boot Spec Expansion ROM among others). It's written in Assembly though, but it has an example "Hello World" kernel that you can use as a skeleton for your own kernel.jamesread wrote:Is the code for your OS available?
Cheers,
bzt
Re: linux versus multiboot in GRUB2
I tried to compile the boot.S code from the multiboot2 spec with the following command:
Here's what I got as output:
Code: Select all
as --32 boot.S -o boot.o
Code: Select all
boot.S: Assembler messages:
boot.S:106: Error: junk `(cmain)' after expression
boot.S:110: Error: junk `(printf)' after expression
boot.S:119: Error: bad or irreducible absolute expression
boot.S:94: Error: invalid operands (*COM* and *UND* sections) for `+'
boot.S:57: Error: invalid operands (*UND* and *UND* sections) for `+'
-
- Member
- Posts: 5563
- Joined: Mon Mar 25, 2013 7:01 pm
Re: linux versus multiboot in GRUB2
Code: Select all
gcc -I ~/multiboot -c -save-temps boot.S
boot.S: Assembler messages:
boot.S:97: Error: invalid instruction suffix for `push'
boot.S:101: Error: invalid instruction suffix for `push'
boot.S:103: Error: invalid instruction suffix for `push'
boot.S:109: Error: invalid instruction suffix for `push'
boot.S:57: Error: can't resolve `0' {.text section} - `L0' {*UND* section}
ls
boot.s boot.S kernel.c kernel.o multiboot2.h
as --32 boot.s -o boot.o
boot.S: Assembler messages:
boot.S:57: Error: can't resolve `0' {.text section} - `L0' {*UND* section}
-
- Member
- Posts: 5563
- Joined: Mon Mar 25, 2013 7:01 pm
Re: linux versus multiboot in GRUB2
I don't see why you're calling AS directly instead of using "gcc -m32".
There appears to be a typo: the identifier "GRUB_MULTIBOOT_ARCHITECTURE_I386" does not exist. (It should be "MULTIBOOT_ARCHITECTURE_I386".)
There appears to be a typo: the identifier "GRUB_MULTIBOOT_ARCHITECTURE_I386" does not exist. (It should be "MULTIBOOT_ARCHITECTURE_I386".)
Re: linux versus multiboot in GRUB2
OK. I made the suggested change and now I get:
Code: Select all
gcc -m32 boot.s -o boot.o
/usr/bin/ld: /tmp/ccyLKdfJ.o: in function `start':
(.text+0x0): multiple definition of `_start'; /usr/lib/gcc/x86_64-linux-gnu/10/../../../../lib32/Scrt1.o:(.text+0x0): first defined here
/usr/bin/ld: /tmp/ccyLKdfJ.o: warning: relocation in read-only section `.text'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/10/../../../../lib32/Scrt1.o: in function `_start':
(.text+0x2c): undefined reference to `main'
/usr/bin/ld: /tmp/ccyLKdfJ.o: in function `multiboot_entry':
(.text+0x3f): undefined reference to `cmain'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
-
- Member
- Posts: 5563
- Joined: Mon Mar 25, 2013 7:01 pm
Re: linux versus multiboot in GRUB2
Some of those errors are because you're not passing the correct flags to your compiler. Now would be a good time to get a proper cross-compiler set up.
The other errors are because you're telling GCC to produce a complete executable, but there are references to functions you haven't written yet.
The other errors are because you're telling GCC to produce a complete executable, but there are references to functions you haven't written yet.
Re: linux versus multiboot in GRUB2
With the following command it compiled without errors:
Then I ran:
so grub-file program confirmed that I a multiboot2 compliant image. Then when I run in qemu I get the following error:
Here is my grub.cfg
So near but yet so far. Any ideas what's still wrong?
Code: Select all
as --32 boot.s -o boot.o
Code: Select all
gcc -m32 -o myos.bin -ffreestanding -O2 -nostdlib boot.o kernel.o -lgcc
file myos.bin
myos.bin: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, BuildID[sha1]=373f1fc364dc8677afa6f288ef2a6c6316595082, not stripped
if grub-file --is-x86-multiboot2 myos.bin; then echo multiboot confirmed; else echo the file is not multiboot; fi
multiboot confirmed
Code: Select all
error: no multiboot header found
error: you need to load the kernel first
Code: Select all
set timeout=15
set default=0 # Set the default menu entry
menuentry "MYOS" {
multiboot /boot/kernel-file # The multiboot command replaces the kernel command
boot
}
-
- Member
- Posts: 5563
- Joined: Mon Mar 25, 2013 7:01 pm
Re: linux versus multiboot in GRUB2
In grub.cfg, the "multiboot" command doesn't support Multiboot2. Use the "multiboot2" command instead.
You really need a cross-compiler.
You really need a cross-compiler.