Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Problem with Kernel, Grub 13 Error

elf assembly c

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 Poe

Poe

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 81 posts

Posted 10 January 2015 - 08:18 PM

so I can now get a grub file to boot, however when it tries to load my kernel, here is the bootsector (i'm assuming from research that the multiboot header isn't loading in the correct space:

    global loader                   ; the entry symbol for ELF
    
    extern write    
    
    MAGIC_NUMBER equ 0x1BADB002     
    CHECKSUM     equ -MAGIC_NUMBER  
	KERNEL_STACK_SIZE equ 4096                  

    section .text:                  
    align 4                         
        dd MAGIC_NUMBER             
        dd CHECKSUM                 
 		mov esp, kernel_stack + KERNEL_STACK_SIZE

    section .bss:
    
    align 4                                     
    
    kernel_stack:                               
        resb KERNEL_STACK_SIZE                  

    loader:                         
        mov eax, 0xCAFEBABE         ; place the number 0xCAFEBABE in the register eax
        
    .loop:
        jmp .loop                   ; loop forever

here is the linker:

ENTRY(loader)

SECTIONS
{
    . = 0xC0100000;
    kernel_virtual_start = .;
    kernel_physical_start = . - 0xC0000000;

    .text ALIGN (0x1000) : AT(ADDR(.text)-0xC0000000)
    {
        *(.text)
    }

    .rodata ALIGN (0x1000) : AT(ADDR(.rodata)-0xC0000000)
    {
        *(.rodata*)
    }

    .data ALIGN (0x1000) : AT(ADDR(.data)-0xC0000000)
    {
        *(.data)
    }

    .bss ALIGN (0x1000) : AT(ADDR(.bss)-0xC0000000)
    {
        *(COMMON)
        *(.bss)
    }

    kernel_virtual_end = .;
    kernel_physical_end = . - 0xC0000000;
}

here is the makefile, i'm not sure if you'll need any of the others but if so just ask

CFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector \
             -nostartfiles -nodefaultlibs -c

all:
	nasm -f elf32 kernel/bootsect.s
	nasm -f elf32 kernel/io.s
	nasm -f elf32 kernel/in.s
	gcc $(CFLAGS) kernel/functions.c
	ld -T link.ld -melf_i386 kernel/bootsect.o kernel/io.o kernel/in.o functions.o -o kernel.elf
	cp kernel.elf iso/boot
              
clean:
	rm kernel/*.o
	rm kernel.elf
	
run:
	bash image.sh
	qemu-system-i386 --cdrom kern.iso

i'm sorry for more code than text, but i've been getting this working all night.


"Portability is for those who can't write new programs" - Linus Torvalds


#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 12 January 2015 - 12:20 PM

What does it do when it tries to load your kernel? You didn't say. However, this seems problematic to me:

section .text:                  
    align 4                         
    dd MAGIC_NUMBER             
    dd CHECKSUM                 
    mov esp, kernel_stack + KERNEL_STACK_SIZE

You're mixing code and data. Because the magic number and checksum are first, the processor is gonna interpret those as instructions, throwing everything off. If my calculations are correct, that code will execute as:

add     dh, [bx + si + 0x1bad]
dec     BYTE [bx + 0x52]
in      al, 0x66
mov     sp, 0x1010
add     [bx+si], al

You need to move those elsewhere.


sudo rm -rf / && echo $'Sanitize your inputs!'


#3 Poe

Poe

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 81 posts

Posted 12 January 2015 - 06:14 PM

ok, so I can get it now to an error 10: unsupported multiboot features requested

this is the make warning:

nasm -f elf32 kernel/bootsect.s
kernel/bootsect.s:16: warning: uninitialized space declared in non-BSS section `.text:': zeroing
nasm -f elf32 kernel/io.s
nasm -f elf32 kernel/in.s
gcc -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -c kernel/functions.c
ld -T link.ld -melf_i386 kernel/bootsect.o kernel/io.o kernel/in.o functions.o -o kernel.elf
cp kernel.elf iso/boot

here is the transformed code:

global loader                   ; the entry symbol for ELF
global kernel_stack
    
MAGIC_NUMBER equ 0x1BADB002     
CHECKSUM     equ -MAGIC_NUMBER  
KERNEL_STACK_SIZE equ 4096                  

section .text:                  

    loader:                         
        mov eax, 0xCAFEBABE         ; place the number 0xCAFEBABE in the register eax
         mov esp, kernel_stack + KERNEL_STACK_SIZE
        
    .loop:
        jmp .loop                   ; loop forever              

section .data:

    align 4                         
        dd MAGIC_NUMBER             
        dd CHECKSUM      

section .bss:                

    align 4
        kernel_stack:
           resb KERNEL_STACK_SIZE

Edited by Poe, 12 January 2015 - 06:39 PM.

"Portability is for those who can't write new programs" - Linus Torvalds


#4 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 12 January 2015 - 06:50 PM

So after doing a bit of reading I see that you were right the first time; I didn't know GRUB has a header. Good news is, according to the documentation here, it just seems that you just don't have the entire GRUB header set up properly, which should be easy to fix.


sudo rm -rf / && echo $'Sanitize your inputs!'