linux versus multiboot in GRUB2

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.
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

linux versus multiboot in GRUB2

Post 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?
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: linux versus multiboot in GRUB2

Post 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.
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: linux versus multiboot in GRUB2

Post 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.
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: linux versus multiboot in GRUB2

Post by Octocontrabass »

Yes, but you need that regardless of the boot protocol you choose.
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: linux versus multiboot in GRUB2

Post 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.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: linux versus multiboot in GRUB2

Post by jamesread »

nexos wrote: My current OS uses Multiboot 2 at the moment.
Is the code for your OS available?
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: linux versus multiboot in GRUB2

Post 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
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: linux versus multiboot in GRUB2

Post by jamesread »

I tried to compile the boot.S code from the multiboot2 spec with the following command:

Code: Select all

as --32 boot.S -o boot.o
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 `+'
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: linux versus multiboot in GRUB2

Post 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.
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: linux versus multiboot in GRUB2

Post 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".)
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: linux versus multiboot in GRUB2

Post 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
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: linux versus multiboot in GRUB2

Post 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.
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: linux versus multiboot in GRUB2

Post by jamesread »

With the following command it compiled without errors:

Code: Select all

as --32 boot.s -o boot.o
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?
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: linux versus multiboot in GRUB2

Post by Octocontrabass »

In grub.cfg, the "multiboot" command doesn't support Multiboot2. Use the "multiboot2" command instead.

You really need a cross-compiler.
Post Reply