[SOLVED] Got wrong Grub Magic Number (Should be 0x2BADB002)
Posted: Sat Nov 04, 2017 11:32 am
Hi, was busy building up my operating system, then I decided to make my own implementation of paging and memory management. When I try to get the magic number (as the second argument on my kernel_main function)
I got this 0x2BADB0FF instead: 0x2BADB002. That's a few bytes off, maybe because of misalignments or other things. My entry_point.s or the boot loader is as follow
on my kernel's main function:
and my link.ld file
Any thoughts on the magic number not matching? Is it safe to ignore in this case? I was at the glance of coding my own paging and memory management implementations before seeing this problem.
Thanks in advance!
Code: Select all
void kernel_main(struct multiboot_info *mbt, unsigned int magic) {
Code: Select all
.set ALIGN, 1<<0 # align loaded modules on page boundaries
.set MEMINFO, 1<<1 # provide memory map
.set FLAGS, ALIGN | MEMINFO # this is the Multiboot 'flag' field
.set MAGIC, 0x1BADB002 # 'magic number' lets bootloader find the header
.set CHECKSUM, -(MAGIC + FLAGS) # checksum of above, to prove we are multiboot
.section .multiboot
.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM
.section .bootstrap_stack, "aw", @nobits
stack_bottom:
.skip 16384 #16 KiB
stack_top:
.section .text
.global _start
.type _start, @function
_start:
movl $stack_top, %esp
sti
pushl %eax # EAX contains the Magic Number returned by Grub <-- THIS IS WHERE IT GOT 0x2BADB0FF
pushl %ebx # EBX contains a pointer to the multiboot info structure.
call kernel_main
halt:
cli
hlt
.Lhang:
jmp .Lhang
.section .text
.global pause
.type pause @function
pause:
hlt
ret
.section .text
.global sys_cli
.type sys_cli @function
sys_cli:
hlt
ret
.section .text
.global sys_sti
.type sys_sti @function
sys_sti:
hlt
ret
.size _start, . - _start
.section .kend
.global end_of_kernel
end_of_kernel:
Code: Select all
...
#define MULTIBOOT_MAGIC_NUMBER 0x2BADB002
...
void kernel_main(struct multiboot_info *mbt, unsigned int magic) {
...
printf("magic number fail. expected: %x got: %x\n", MULTIBOOT_MAGIC_NUMBER, magic); # magic = 0x2BADB0FF
...
}
...
Code: Select all
/* The bootloader will look at this image and start execution at the symbol
designated as the entry point. */
ENTRY(start)
/* Tell where the various sections of the object files will be put in the final
kernel image. */
SECTIONS
{
/* First put the multiboot header, as it is required to be put very early
early in the image or the bootloader won't recognize the file format.
Next we'll put the .text section. */
.text BLOCK(4K) : ALIGN(4K)
{
*(.multiboot)
*(.text)
}
/* Read-only data. */
.rodata BLOCK(4K) : ALIGN(4K)
{
*(.rodata)
}
/* Read-write data (initialized) */
.data BLOCK(4K) : ALIGN(4K)
{
*(.data)
}
/* Read-write data (uninitialized) and stack */
.bss BLOCK(4K) : ALIGN(4K)
{
*(COMMON)
*(.bss)
*(.bootstrap_stack)
}
/* The compiler may produce other sections, by default it will put them in
a segment with the same name. Simply add stuff here as needed. */
.kend BLOCK(4K) : ALIGN(4K)
{
*(.kend)
}
}
Thanks in advance!