Page 1 of 1

GRUB Kernel Loading

Posted: Fri Feb 18, 2011 6:30 pm
by xfelix
I am new to the forums and kernel development. I am currently a student at the University of Maryland, and am taking a class on OS development where we are adding on to a pre-existing kernel GeekOS. I am having alot of fun in this class right now, but I would like to start writing one from scratch, because I'm really interested and I think it would be alot of fun.

I have been going over the bare bones basic kernel tutorial, and I'm getting the kernel to load properly, but when I attempt to boot the kernel nothing happens.

Here is Makefile which basically does everything in the wiki automatically

Code: Select all

#

.PHONY: all clean run 

NASM = nasm
CC = gcc
LD = ld

FLAGS = -nostdlib -nostartfiles -nodefaultlibs
OPT_FLAGS = -Wall -O2

KERNEL_SOURCE = ../src/kernel.c
MULTIBOOT_SOURCE = ../src/loader.s
LINK_SCRIPT = linker.ld

all: loader.o kernel.o kernel.bin fd.img

loader.o: $(MULTIBOOT_SOURCE)
	$(NASM) -f elf $< -o $@ 

kernel.o: $(KERNEL_SOURCE)
	$(CC) -c $< -o $@ $(FLAGS) $(OPT_FLAGS)

kernel.bin: loader.o kernel.o
	$(LD) -T $(LINK_SCRIPT) $< kernel.o -o $@

fd.img:
	cat stage1 stage2 pad kernel.bin > fd.img

clean:
	rm -r -f *~ *.o

run:
	qemu -vga std -fda fd.img
Also here is my kernel.c source code

Code: Select all

/*
 * NoobOS by xfelix
 */

void Kernel_Main(void* mb, int mn) {
  unsigned char* vram = (unsigned char*)0xB8000;
  vram[0] = 0x41;
  vram[1] = 0x07;
}
when I launch qemu with the floppy image, Grub starts up and after I type
grub> kernel 200+18 I get...
[Multiboot-elf, <0x100000:0x1014:0x4000>,shtab=0x106140,entry=0x10000c]
grub> boot

nothing happens after the boot command. I have an extern Kernel_Main in the loader.s file so it should be being called and the fact that the kernel 200+18 command is properly working tells me that there is nothing wrong with my loader anyway.

ok i got some questions. first can anyone tell me what they think the problem could be? Shouldn't this code be displaying the letter A in the qemu terminal. Second in the tutorial, the author said that everything has to be written in the kernel.c file, but I know that there has to be a way to compile and link seperate files into a flat binary file, does anyone know how to do this?

Any Help is greatly appreciated! :D

Re: GRUB Kernel Loading

Posted: Fri Feb 18, 2011 7:58 pm
by thepowersgang
Well, without seeing your loader.asm, it's hard to tell, but I suspect there's still a "jmp $" in there.

My suggestion for doing early boot testing, is running in Bochs, setting a breakpoint at the kernel entrypoint (as said by the mutiboot header, 0x10000c in this case) then single stepping through until something happens that you don't expect.

Re: GRUB Kernel Loading

Posted: Fri Feb 18, 2011 8:10 pm
by xfelix
thepowersgang wrote:Well, without seeing your loader.asm, it's hard to tell, but I suspect there's still a "jmp $" in there.

My suggestion for doing early boot testing, is running in Bochs, setting a breakpoint at the kernel entrypoint (as said by the mutiboot header, 0x10000c in this case) then single stepping through until something happens that you don't expect.
I do not have jmp $ at the moment, here is my loader.s file

Code: Select all

global loader
extern Kernel_Main

MODULEALIGN equ  1
MEMINFO     equ  1                   ; provide memory map
FLAGS       equ  MODULEALIGN | MEMINFO  ; this is the Multiboot 'flag' field
MAGIC       equ  0x1BADB002           
CHECKSUM    equ -(MAGIC + FLAGS)        ; checksum required
 
section .text
align 4
	jmp loader
MultiBootHeader:
   dd MAGIC
   dd FLAGS
   dd CHECKSUM
 
STACKSIZE equ 0x4000
 
loader:
   mov esp, stack+STACKSIZE           ; set up the stack
   push 0x0
   popf
   push eax                           ; pass Multiboot magic number
   push ebx                           ; pass Multiboot info structure
 
   call Kernel_Main                       ; call kernel proper
 
   cli
hang:
   hlt                                ; halt machine should kernel return
   jmp   hang
 
section .bss
align 4
stack:
   resb STACKSIZE
I just added the jmp loader line at the begging of the .text section. Right now i'm using qemu, but I will look into the Bochs emulator.

Re: GRUB Kernel Loading

Posted: Sat Feb 19, 2011 2:59 am
by davidv1992
When qemu executes a hlt with interrupts disabled it wont refresh the screen anymore, and anything written to the screen just before the hlt will not become visible.

Re: GRUB Kernel Loading

Posted: Sat Feb 19, 2011 7:52 am
by xfelix
davidv1992 wrote:When qemu executes a hlt with interrupts disabled it wont refresh the screen anymore, and anything written to the screen just before the hlt will not become visible.
I don't think that I have interrupts disabled, but this is extremely useful to know thank you! This is how I'm launching qemu...

qemu -vga std -fda fd.img

and I don't think that interrupts are disabled by default. I have also made some temporary changes to the Kernel_Main to have an infinite loop so it won't return and halt anymore. Still nothing is happening after running boot command in GRUB, Here is the kernel now...

Code: Select all

/*
 * NoobOS by xfelix
 */

void Clear_Screen(void);

unsigned char* vram = (unsigned char*)0xB8000;

void Kernel_Main(void* mb, int mn) {
  Clear_Screen();
  vram[0] = 0x41;
  vram[1] = 0x07;

  while(1) {
  }
}

void Clear_Screen() {
  int i;
  for(i=0; i < 80*24; i++) {
    vram[i] = 0x0;
  }
}


I have also been looking at the GRUB multi-boot manual on gnu, picking up things here and there. It's probably something simple and suddle that I'm just not picking up on. Thank You for all help so far!! :mrgreen:

Re: GRUB Kernel Loading

Posted: Sat Feb 19, 2011 8:25 am
by Graham
xfelix wrote:
davidv1992 wrote:When qemu executes a hlt with interrupts disabled it wont refresh the screen anymore, and anything written to the screen just before the hlt will not become visible.
I don't think that I have interrupts disabled, but this is extremely useful to know thank you! This is how I'm launching qemu...

qemu -vga std -fda fd.img

and I don't think that interrupts are disabled by default.
When GRUB calls your kernel interrupts are disabled. Also, your code disables interrupts with CLI right before the HLT instruction. Anyway if your infinite loop in C doesn't help then I would assume there is another problem or the problem was elsewhere...

Re: GRUB Kernel Loading

Posted: Sat Feb 19, 2011 4:11 pm
by xfelix
Problem Resolved! At first it was because of the halt instruction being called, and qemu being retarded.
Then the problem was the makefile wasn't updating the floppy image correctly -_-
Thanks everyone for you help :mrgreen: