OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 7:15 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 31 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: linux versus multiboot in GRUB2
PostPosted: Tue Dec 15, 2020 1:51 pm 
Offline
Member
Member

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


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Tue Dec 15, 2020 4:09 pm 
Offline
Member
Member

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


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Tue Dec 15, 2020 5:25 pm 
Offline
Member
Member

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


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Tue Dec 15, 2020 6:53 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
Yes, but you need that regardless of the boot protocol you choose.


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Tue Dec 15, 2020 7:01 pm 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
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


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Tue Dec 15, 2020 8:11 pm 
Offline
Member
Member

Joined: Wed Dec 09, 2020 11:32 am
Posts: 49
nexos wrote:
My current OS uses Multiboot 2 at the moment.


Is the code for your OS available?


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Wed Dec 16, 2020 8:17 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
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


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Thu Dec 17, 2020 5:32 am 
Offline
Member
Member

Joined: Wed Dec 09, 2020 11:32 am
Posts: 49
I tried to compile the boot.S code from the multiboot2 spec with the following command:

Code:
as --32 boot.S -o boot.o


Here's what I got as output:

Code:
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 `+'


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Thu Dec 17, 2020 10:42 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
The uppercase .S extension indicates that the code needs to be preprocessed by a C preprocessor before it can be assembled.


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Thu Dec 17, 2020 11:34 am 
Offline
Member
Member

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


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Thu Dec 17, 2020 12:00 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
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".)


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Thu Dec 17, 2020 12:06 pm 
Offline
Member
Member

Joined: Wed Dec 09, 2020 11:32 am
Posts: 49
OK. I made the suggested change and now I get:

Code:
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


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Thu Dec 17, 2020 12:25 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
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.


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Thu Dec 17, 2020 12:54 pm 
Offline
Member
Member

Joined: Wed Dec 09, 2020 11:32 am
Posts: 49
With the following command it compiled without errors:

Code:
as --32 boot.s -o boot.o


Then I ran:

Code:
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:
error: no multiboot header found
error: you need to load the kernel first


Here is my grub.cfg

Code:
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?


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Thu Dec 17, 2020 1:05 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
In grub.cfg, the "multiboot" command doesn't support Multiboot2. Use the "multiboot2" command instead.

You really need a cross-compiler.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 31 posts ]  Go to page 1, 2, 3  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot], Sa41848 and 59 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group