Error booting with GRUB

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
cg123

Error booting with GRUB

Post by cg123 »

I've been having trouble getting my OS to boot with GRUB - it's giving me error #7 (loading below 1 mb) with all of the linker scripts I've tried. Here's my linker script:

Code: Select all

OUTPUT_FORMAT(binary)
ENTRY(start)
phys = 0x100000;     /* 1 meg */
SECTIONS
{
   . = 0x100000;
    .data phys :
    {  data = .;
        *(.data)
        . = ALIGN(4096);
    }
    .text . :
    {  code = .;
        *(.text)
        . = ALIGN(4096);
    }
    .bss . :
    {  bss = .;
        *(.bss)
        . = ALIGN(4096);
    }
    .rodata . :
    {  rodata = .;
        *(.rodata)
        . = ALIGN(4096);
    }
    end = .;
}
Here's my boot.asm:

Code: Select all

; Boot.asm
; OS loader
[BITS 32]
[global start]
start:
    mov esp, _sys_stack     ; This points the stack to our new stack area
    jmp stublet

MULTIBOOT_PAGE_ALIGN   equ 1<<0
MULTIBOOT_MEMORY_INFO  equ 1<<1
MULTIBOOT_AOUT_KLUDGE  equ 1<<16

MULTIBOOT_HEADER_MAGIC equ 0x1BADB002
MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
CHECKSUM               equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)

; The Multiboot header
   align 4
mboot:
   dd MULTIBOOT_HEADER_MAGIC
   dd MULTIBOOT_HEADER_FLAGS
   dd CHECKSUM
; fields used if MULTIBOOT_AOUT_KLUDGE is set in MULTIBOOT_HEADER_FLAGS
   dd mboot ; these are PHYSICAL addresses
   dd code  ; start of kernel .text (code) section
   dd bss ; end of kernel .data section
   dd end   ; end of kernel BSS
   dd start ; kernel entry point (initial EIP)

[global _mbinfo]
[global _mbmagic]
[extern code]
[extern bss]
[extern end]
;   align 4
;mboot:
;   dd MULTIBOOT_HEADER_MAGIC
;   dd MULTIBOOT_HEADER_FLAGS
;   dd MULTIBOOT_CHECKSUM
   
;   dd mboot
;    dd code
;    dd bss
;    dd end
;    dd start
   ;dd _mboot      ; Location of this descriptor
   ;dd code         ; Start of kernel '.text' (code) section.
   ;dd bss         ; End of kernel '.data' section.
   ;dd end         ; End of kernel.
   ;dd start      ; Kernel entry point (initial EIP).
;_mbinfo:
;   dd 0
;_mbmagic:
;   dd 0

extern _main ; located in Kernel.cpp
call _main ; call int main(void) in Kernel.cpp
cli ; interrupts could disturb the halt
hlt ; halt the CPU
stublet:
    jmp $
   
   global _gdt_flush     ; Allows the C code to link to this
;extern _gp 
extern _gp            ; Says that '_gp' is in another file
_gdt_flush:
    lgdt [_gp]        ; Load the GDT with our '_gp' which is a special pointer
    mov ax, 0x10      ; 0x10 is the offset in the GDT to our data segment
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    jmp 0x08:flush2   ; 0x08 is the offset to our code segment: Far jump!
flush2:
    ret               ; Returns back to the C++ code
global _idt_load
extern _idtp
_idt_load:
    lidt [_idtp]
    ret
;really long IRQ/ISR section removed
;bss:
SECTION .bss
    resb 8192
_sys_stack:
(yes, thrown together from tutorials, but that's why I'm using c++ for the rest :P)

Anyone have any idea why it's doing this?
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Re:Error booting with GRUB

Post by Kevin McGuire »

Good GOD! What a bundle of hay with a one needle in it..

See:
"Try to identify the minimum environment necessary to reproduce the problem."
http://www.mega-tokyo.com/osfaq/HowToAskQuestions

boot.asm

Code: Select all

Here is the minimum enviroment nessary to possibly fix the problem:
; Boot.asm
; OS loader
[BITS 32]
[extern code]
[extern bss]
[extern end]
[extern main]
[global start]
start:
   mov eax, 0xb8000
   mov byte [eax], 65
   jmp main

MULTIBOOT_PAGE_ALIGN  equ 1<<0
MULTIBOOT_MEMORY_INFO  equ 1<<1
MULTIBOOT_AOUT_KLUDGE  equ 1<<16

MULTIBOOT_HEADER_MAGIC equ 0x1BADB002
MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
CHECKSUM              equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)

; The Multiboot header
  align 4
mboot:
  dd MULTIBOOT_HEADER_MAGIC
  dd MULTIBOOT_HEADER_FLAGS
  dd CHECKSUM
; fields used if MULTIBOOT_AOUT_KLUDGE is set in MULTIBOOT_HEADER_FLAGS
  dd mboot ; these are PHYSICAL addresses
  dd code  ; start of kernel .text (code) section
  dd bss ; end of kernel .data section
  dd end  ; end of kernel BSS
  dd start ; kernel entry point (initial EIP)
kernel.c

Code: Select all

void main(void){
   unsigned char *vmem = (unsigned char*)0xB8000;
   char *str = "hello";
   for(;;){
      vmem[0]++;
      vmem[1]++;
      vmem[2] = str[0];
      vmem[3] = 1;
   }
   return;
}
Here are some build commands:
gcc -ffreestanding -Wall -c kernel.c -o kernel.o
nasm -o boot.o -f elf boot.asm
ld -T elf.ld -o kernel boot.o kernel.o

GRUB COMMAND: kernel /kernel
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Re:Error booting with GRUB

Post by Kevin McGuire »

And, try this to fix the where the linker expects the data section to be in the boot.asm (I was using ELF32, and realized that the binary format did not work completely), also your linker script works.

Code: Select all

  dd mboot ; these are PHYSICAL addresses
  dd start  ; start of kernel .text (code) section
  dd end   ;   end of kernel .data section
  dd end   ; end of kernel BSS
  dd start ; kernel entry point (initial EIP)
cg123

Re:Error booting with GRUB

Post by cg123 »

Ok, I've reduced the code to just boot up and print 'hello.' It still gives me the error.

(same linker as above)

kernel.cpp:

Code: Select all

int main(void)
{
   unsigned char *vmem = (unsigned char*)0xB8000;
   char *str = "hello";
   for(;;){
      vmem[0]++;
      vmem[1]++;
      vmem[2] = str[0];
      vmem[3] = 1;
   }
   return false;
}
boot.asm:

Code: Select all

; Boot.asm
; OS loader
[BITS 32]
[global start]
start:
    mov esp, _sys_stack     ; This points the stack to our new stack area
    jmp stublet

MULTIBOOT_PAGE_ALIGN   equ 1<<0
MULTIBOOT_MEMORY_INFO  equ 1<<1
MULTIBOOT_AOUT_KLUDGE  equ 1<<16

MULTIBOOT_HEADER_MAGIC equ 0x1BADB002
MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
CHECKSUM               equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)

; The Multiboot header
   align 4
mboot:
   dd MULTIBOOT_HEADER_MAGIC
   dd MULTIBOOT_HEADER_FLAGS
   dd CHECKSUM
; fields used if MULTIBOOT_AOUT_KLUDGE is set in MULTIBOOT_HEADER_FLAGS
   dd mboot ; these are PHYSICAL addresses
   dd start  ; start of kernel .text (code) section
   dd end ; end of kernel .data section
   dd end   ; end of kernel BSS
   dd start ; kernel entry point (initial EIP)

[global _mbinfo]
[global _mbmagic]
[extern code]
[extern bss]
[extern end]

extern _main ; located in Kernel.cpp
call _main ; call int main(void) in Kernel.cpp
cli ; interrupts could disturb the halt
hlt ; halt the CPU
stublet:
    jmp $

;bss:
SECTION .bss
    resb 8192
_sys_stack:
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Re:Error booting with GRUB

Post by Kevin McGuire »

I fixed it, and it the problem is really obvious. Reduce the code back to what I gave you, and slowly add the stuff you changed until it starts giving unexpected results, or errors then you should have found what I found. Once you find it, I can give you a hint that it has to do witht the order of things.

There is also another problem that might cause you problems, but it should not for the moment - if you can find that one too.
cg123

Re:Error booting with GRUB

Post by cg123 »

Well, the code you gave me appears to be giving rather unexpected results. GRUB doesn't give any errors, but it just continually changes the first character on the screen to a random character and background color, even when it's just this:

Code: Select all

void main(void){
   for (;;)
   {
   
   }
   return;
}
I am now completely baffled.
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Re:Error booting with GRUB

Post by Kevin McGuire »

Welcome to more than just operating system development. ::)
Enjoy the stay, but make sure to stay relaxed and think outside the box.

How could "for(;;)" compile into code making the character change, if you can answer it you could be a genius..
cg123

Re:Error booting with GRUB

Post by cg123 »

I just got it to work... thanks for all the help. :D
Post Reply