Page 1 of 1
linux hello world kernel with GRUB2
Posted: Tue Dec 22, 2020 8:18 am
by jamesread
I'm trying to build a simple linux hello world kernel that will boot with GRUB2. To this end I have isolated the files linux-5.10.1/arch/x86/boot/header.S linux-5.10.1/arch/x86/boot/main.c and linux-5.10.1/arch/x86/boot/setup.ld as a good starting point. After doing a complete make on the linux kernel source I removed header.o and main.o to see what make would do to remake and link these files. Here's what I got:
Code: Select all
make
CALL scripts/checksyscalls.sh
CALL scripts/atomic/check-atomics.sh
DESCEND objtool
DESCEND bpf/resolve_btfids
CHK include/generated/compile.h
CHK kernel/kheaders_data.tar.xz
AS arch/x86/boot/header.o
CC arch/x86/boot/main.o
LD arch/x86/boot/setup.elf
OBJCOPY arch/x86/boot/setup.bin
BUILD arch/x86/boot/bzImage
Kernel: arch/x86/boot/bzImage is ready (#2)
Does anybody know how I can force make to give better output so that I can get the actual full gcc commands to build and link these files?
Re: linux hello world kernel with GRUB2
Posted: Tue Dec 22, 2020 10:33 am
by nullplan
"make V=1" makes it show the full command. "make help" displays a help text that would have told you that.
Re: linux hello world kernel with GRUB2
Posted: Tue Dec 22, 2020 12:18 pm
by jamesread
Thanks, that did the trick.
Re: linux hello world kernel with GRUB2
Posted: Tue Dec 22, 2020 3:02 pm
by jamesread
I managed to build a bootable linux image that boots with GRUB2. However, when I modify main.c to print a Hello linux world message I just get a blank screen for some reason. The good news is it seems to be booting both in legacy and UEFI mode just no output to screen.
Re: linux hello world kernel with GRUB2
Posted: Tue Dec 22, 2020 3:04 pm
by nexos
Is the system in graphical mode?
Re: linux hello world kernel with GRUB2
Posted: Tue Dec 22, 2020 3:16 pm
by jamesread
Not sure. How would I know that?
Re: linux hello world kernel with GRUB2
Posted: Thu Dec 24, 2020 11:30 pm
by jamesread
Here is my grub.cfg file
Code: Select all
set timeout=15
set default=0 # Set the default menu entry
menuentry "MYOS" {
linux /boot/kernel-file
}
So I don't think it's booting in graphical mode. There must be some other reason why I'm not getting any output to screen.
Re: linux hello world kernel with GRUB2
Posted: Fri Dec 25, 2020 7:15 am
by nexos
On EFI, it is definitley booting into graphical mode. On BIOS, it may not, but your linux bootstrap code may be putting it in graphical mode. Personally, I would recommend following the Bare Bones tutorial on the Wiki, and using Multiboot 1, then changing to Multiboot 2 later.
Re: linux hello world kernel with GRUB2
Posted: Fri Dec 25, 2020 7:39 am
by jamesread
What kind of code could be putting it in graphical mode? What am I looking for?
Re: linux hello world kernel with GRUB2
Posted: Sun Dec 27, 2020 1:22 am
by jamesread
I thought I found the code that sets graphical mode. There is a function set_mode in video-mode.c which is called by set_video in video.c which is called by main in main.c. I commented out the call and I still get a blank screen.
I then commented out all the function calls in main (just in case) and put a while loop which prints out my message continuously. My new main function looks like this:
Code: Select all
void main(void)
{
while (1) {
printf("Hello linux world\n");
}
}
Still getting a blank screen.
Re: linux hello world kernel with GRUB2
Posted: Tue Dec 29, 2020 10:09 am
by nexos
IMO it is better just to write it from scratch. If you are not extremely familliar with Linux, its best not to copy it
.