littleosbook / littleosbook

Source for the little book about OS development
http://littleosbook.github.io/
2.2k stars 240 forks source link

loader missing multiboot header flags #11

Open Avidanborisov opened 9 years ago

Avidanborisov commented 9 years ago

The loader is missing the flags field after the magic number, making it an invalid executable for GRUB.

  Booting 'os'

kernel /boot/kernel.elf

Error 13: Invalid or unsupported executable format

Adding the flags field should make the loader start correctly:

global loader                 ; the entry symbol for ELF

ALIGN    equ 1 << 0           ; align loaded modules on page boundaries
MEMINFO  equ 1 << 1           ; provide memory map
FLAGS    equ ALIGN | MEMINFO  ; the multiboot flags field
MAGIC    equ 0x1BADB002       ; define the magic number constant
CHECKSUM equ -(MAGIC + FLAGS) ; calculate the checksum (magic number + flags + checksum should equal 0)

section .text:                ; start of the text (code) section
align 4                       ; the code must be 4 byte aligned
    dd MAGIC                  ; write the magic number to the machine code
    dd FLAGS                     
    dd CHECKSUM               ; and the checksum

loader:                       ; the loader label (defined as entry point in linker script)
    mov eax, 0xCAFEBABE       ; place the number 0xCAFEBABE in the register eax
.loop:
    jmp .loop                 ; loop forever
fcremo commented 9 years ago

The use of ALIGN as a name for a constant is throwing an error for me:

loader.s:3: error: expecting `)'

I don't know if this is supposed to happen, I think ALIGN is a reserved keyword. This works for me:

global loader                 ; the entry symbol for ELF

ALI    equ 1 << 0           ; align loaded modules on page boundaries
MEMINFO  equ 1 << 1           ; provide memory map
FLAGS    equ ALI | MEMINFO  ; the multiboot flags field
MAGIC    equ 0x1BADB002       ; define the magic number constant
CHECKSUM equ -(MAGIC + FLAGS) ; calculate the checksum (magic number + flags + checksum should equal 0)

section .text:                ; start of the text (code) section
align 4                       ; the code must be 4 byte aligned
    dd MAGIC                  ; write the magic number to the machine code
    dd FLAGS                     
    dd CHECKSUM               ; and the checksum

loader:                       ; the loader label (defined as entry point in linker script)
    mov eax, 0xCAFEBABE       ; place the number 0xCAFEBABE in the register eax
.loop:
    jmp .loop                 ; loop forever
Avidanborisov commented 9 years ago

Probably :) My bad for not actually testing the code with NASM (I'm using gas with .intel_syntax noprefix).

Layke commented 9 years ago

Ahh, this has been super helpful! I have been stuck here for days and after fixing this code it now works great! How often does this source get updated? It would save countless others much time and effort to have this pasted on the code section that is linked too.

Phlosioneer commented 9 years ago

Super helpful that I found this. Was getting stuck, ended up starting to read through the multiboot spec. Everything finally works! :D

andrewnguyen commented 9 years ago

I also ran into this issue but solved it via http://wiki.osdev.org/Grub_Error_13.

drysdam commented 9 years ago

Thanks for this! I was banging my head on it for an hour or two. I found the osdev link so I knew something was wrong with loader.s, but this is my first time getting so down and dirty so I had no idea what to do. Works now! (Someone should fork, fix and issue pull request. I would, but I don't know enough to write the explanatory text.)