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

linux hello world kernel with GRUB2

Post 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?
nullplan
Member
Member
Posts: 1790
Joined: Wed Aug 30, 2017 8:24 am

Re: linux hello world kernel with GRUB2

Post by nullplan »

"make V=1" makes it show the full command. "make help" displays a help text that would have told you that.
Carpe diem!
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: linux hello world kernel with GRUB2

Post by jamesread »

Thanks, that did the trick.
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: linux hello world kernel with GRUB2

Post 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.
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: linux hello world kernel with GRUB2

Post by nexos »

Is the system in graphical mode?
"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 hello world kernel with GRUB2

Post by jamesread »

:oops:

Not sure. How would I know that?
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: linux hello world kernel with GRUB2

Post 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.
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: linux hello world kernel with GRUB2

Post 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.
"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 hello world kernel with GRUB2

Post by jamesread »

What kind of code could be putting it in graphical mode? What am I looking for?
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: linux hello world kernel with GRUB2

Post 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.
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: linux hello world kernel with GRUB2

Post 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 :) .
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
Post Reply