Multiboot For Flat Exe

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
User avatar
os.hacker64
Member
Member
Posts: 149
Joined: Mon Feb 11, 2008 4:43 pm
Location: Limbo City,Afterlife

Multiboot For Flat Exe

Post by os.hacker64 »

This is probably the most noobish question on the planet, but how might I set up the multiboot header for a flat binary file to be loaded by grub? Some of the address fields confuse me a bit. Are there any examples out there?
User avatar
os.hacker64
Member
Member
Posts: 149
Joined: Mon Feb 11, 2008 4:43 pm
Location: Limbo City,Afterlife

Post by os.hacker64 »

Never mind, I told you it was a noob Q. While I'm at it does anyone know of a hex editor for linux.
User avatar
~
Member
Member
Posts: 1228
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Post by ~ »

Here is a sample 32-bit kernel to boot with GRUB.

It loads at the first physical Megabyte. If you want to change it you will have to adjust ORG and every 1048576 occurrence.

The stack is placed at the start of the second physical Megabyte.

It will just boot immediately, the kernel will just write a purple character at the top of the 80x25 text screen and will halt.


Attached there is the floppy image and the source code to this sample kernel, useful if you want to make your own flat binary kernel. Note that it's a floppy image and you should copy it with rawwrite, dd, cat, etc.

Code: Select all

bits 32
org 1048576

kernelstart:

    mov esp,1048576*2     ; This points the stack to our new stack area
    jmp stublet

; This part MUST be 4byte aligned, so we solve that issue using 'ALIGN 4'
ALIGN 4
mboot:
    ; Multiboot macros to make a few lines later more readable
    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
    MULTIBOOT_CHECKSUM	equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)


    ; This is the GRUB Multiboot header. A boot signature
    dd MULTIBOOT_HEADER_MAGIC
    dd MULTIBOOT_HEADER_FLAGS
    dd MULTIBOOT_CHECKSUM
    
    ; AOUT kludge - must be physical addresses. Make a note of these:
    ; The linker script fills in the data for these ones!
    dd mboot              ;GRUB Multiboot Header
    dd 1048576            ;Kernel Base Address
    dd 1048576+kernelsize ;Kernel End Address
    dd 1048576+kernelsize ;BSS End Address (can be same as Kernel End Address)
    dd stublet            ;Kernel Entry Point

; This is an endless loop here. Make a note of this: Later on, we
; will insert an 'extern _main', followed by 'call _main', right
; before the 'jmp $'.


stublet:
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  cli
  mov word[0xB8001],"X "
  hlt

 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



kernelsize equ $-kernelstart
Attachments
grub_flat_image.zip
(43.81 KiB) Downloaded 13 times
User avatar
os.hacker64
Member
Member
Posts: 149
Joined: Mon Feb 11, 2008 4:43 pm
Location: Limbo City,Afterlife

Post by os.hacker64 »

I tried changing that code so that it would work in FASM. Here it is:
Attachments
kernel.asm
Here's what I have in FASM
(487 Bytes) Downloaded 57 times
Post Reply