I am following Bran's kernel development tutorial. In his assembly code, he has the following block of code that he describes to be not so important but something that is do with GRUB.
; 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)
EXTERN code, bss, end
; 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
dd code
dd bss
dd end
dd start
I would still like to know what this code is doing? Can someone give me or point me to where I can find a description about this. Second, why does this piece of code needs to be 4-byte aligned? Is this for efficiency reasons or has some special significance in context of GRUB?
This bit of assembly is not code, but a multiboot header. When you link your kernel this header will be placed at the beginning of the binary, so GRUB can see that your kernel is multiboot-compliant. This header contains a magic number (0x1badb002), a list of flags (in your case, that the kernel should be loaded on a page-aligned address, and that you would like GRUB to give you a memory map), and a checksum. If one of these entries does not follow the multiboot specification, GRUB will not load your kernel.
You can find more information about multiboot in GRUB's multiboot manual:
http://www.gnu.org/software/grub/manual/multiboot/multiboot.html
Quoting from the manual:
An OS image must contain an additional header called Multiboot header, besides the headers of the format used by the OS image. The Multiboot header must be contained completely within the first 8192 bytes of the OS image, and must be longword (32-bit) aligned. In general, it should come as early as possible, and may be embedded in the beginning of the text segment after the real executable header.
I'm not sure what the AOUT kludge does. I'm only using ELF in my OS. I followed this guide: http://wiki.osdev.org/Bare_Bones