Page 1 of 3
linux versus multiboot in GRUB2
Posted: Tue Dec 15, 2020 1:51 pm
by jamesread
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?
Re: linux versus multiboot in GRUB2
Posted: Tue Dec 15, 2020 4:09 pm
by PeterX
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.
Re: linux versus multiboot in GRUB2
Posted: Tue Dec 15, 2020 5:25 pm
by jamesread
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.
Re: linux versus multiboot in GRUB2
Posted: Tue Dec 15, 2020 6:53 pm
by Octocontrabass
Yes, but you need that regardless of the boot protocol you choose.
Re: linux versus multiboot in GRUB2
Posted: Tue Dec 15, 2020 7:01 pm
by nexos
PeterX wrote:- Not sure if NexBoot is another one
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.
Re: linux versus multiboot in GRUB2
Posted: Tue Dec 15, 2020 8:11 pm
by jamesread
nexos wrote:
My current OS uses Multiboot 2 at the moment.
Is the code for your OS available?
Re: linux versus multiboot in GRUB2
Posted: Wed Dec 16, 2020 8:17 am
by bzt
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?
Yes. Not really an executable code, but a struct.
Here's the specification.
jamesread wrote:And with multiboot again is there any special code the kernel must have to make it multiboot compliant.
Same, you need a struct.
Here's the spec.
jamesread wrote:What would experienced kernel hackers advise? Which method of loading a kernel is best suited to a simple hello world kernel?
Doesn't matter. It only depends on which loader you prefer.
jamesread wrote:Is the code for your OS available?
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.
Cheers,
bzt
Re: linux versus multiboot in GRUB2
Posted: Thu Dec 17, 2020 5:32 am
by jamesread
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
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 `+'
Re: linux versus multiboot in GRUB2
Posted: Thu Dec 17, 2020 10:42 am
by Octocontrabass
Re: linux versus multiboot in GRUB2
Posted: Thu Dec 17, 2020 11:34 am
by jamesread
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}
I guess that's an improvement but still not getting an object file.
Re: linux versus multiboot in GRUB2
Posted: Thu Dec 17, 2020 12:00 pm
by Octocontrabass
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".)
Re: linux versus multiboot in GRUB2
Posted: Thu Dec 17, 2020 12:06 pm
by jamesread
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
Re: linux versus multiboot in GRUB2
Posted: Thu Dec 17, 2020 12:25 pm
by Octocontrabass
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.
Re: linux versus multiboot in GRUB2
Posted: Thu Dec 17, 2020 12:54 pm
by jamesread
With the following command it compiled without errors:
Then I ran:
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
so grub-file program confirmed that I a multiboot2 compliant image. Then when I run in qemu I get the following error:
Code: Select all
error: no multiboot header found
error: you need to load the kernel first
Here is my grub.cfg
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
}
So near but yet so far. Any ideas what's still wrong?
Re: linux versus multiboot in GRUB2
Posted: Thu Dec 17, 2020 1:05 pm
by Octocontrabass
In grub.cfg, the "multiboot" command doesn't support Multiboot2. Use the "multiboot2" command instead.
You really need a cross-compiler.