Page 1 of 1

[SOLVED] Got wrong Grub Magic Number (Should be 0x2BADB002)

Posted: Sat Nov 04, 2017 11:32 am
by farizluqman
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)

Code: Select all

void kernel_main(struct multiboot_info *mbt, unsigned int magic) {
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

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:

on my kernel's main function:

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
  ...
}
...
and my link.ld file

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)
    }
}
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!

Re: Got wrong Grub Magic Number (Should be 0x2BADB002)

Posted: Sat Nov 04, 2017 12:49 pm
by farizluqman
Oh, silly me, I make the wrong entry, should be

ENTRY(_start)

instead of ENTRY(start) on the old link.ld

sorry for the little inconvenience :oops:
If you would love to try out my os, it is up on Github https://github.com/farizluqman/little-os

Re: [SOLVED] Got wrong Grub Magic Number (Should be 0x2BADB0

Posted: Sun Nov 05, 2017 2:10 am
by SukantPal
You should not post so much code, nobody would even look at your post.

Re: [SOLVED] Got wrong Grub Magic Number (Should be 0x2BADB0

Posted: Tue Nov 07, 2017 3:58 am
by farizluqman
SukantPal wrote:You should not post so much code, nobody would even look at your post.
Noted with thanks.

Hopefully this help when someone else encounter the same thing. Wrongfully defining the entry in the linker still boots your OS but will give wrong magic number. I'm not quite sure why

Re: [SOLVED] Got wrong Grub Magic Number (Should be 0x2BADB0

Posted: Sat Nov 11, 2017 5:58 am
by AJ
TBH, code is fine, as long as it's inside code tags which yours is.

Cheers,
Adam

Re: [SOLVED] Got wrong Grub Magic Number (Should be 0x2BADB0

Posted: Sat Nov 11, 2017 1:19 pm
by MichaelPetch
AJ wrote:TBH, code is fine, as long as it's inside code tags which yours is.
In this case not quite. Since his linker script has this:

Code: Select all

 .text BLOCK(4K) : ALIGN(4K)
    {
        *(.multiboot)
        *(.text)
    }
He's added the multiboot header and the .text section together. Since he didn't correctly specify an entry label it will default to the VMA of the text section (I assume he set it on the linker command line) of 0x100000. This would actually be the multibootheader itself. I noticed that when his multiboot header is executed as code it reads from one of the ports which seems to return the value 0xff and places it in AL. The end result is that EAX now has the wrong value in it (lower 8 bits now overwritten), the stack doesn't get set up correctly but decoding will eventually call his kernel_main function. The code I saw decoded was:

Code: Select all

0x100000                add    0x31bad(%eax),%dh                    ; Start of Multiboot header      
0x100006                add    %al,(%eax)                          
0x100008                sti                                                
0x100009                dec    %edi                                        
0x10000a                push   %edx                                        
0x10000b                in     $0xbc,%al                           ; <------- this sets AL to 0xFF in QEMU.     
0x10000d <_start+1>     add    %dl,0x10(%eax)                              
0x100010 <_start+4>     add    %bh,%bl                                     
0x100012 <_start+6>     push   %eax                                        
0x100013 <_start+7>     push   %ebx                                        
0x100014 <_start+8>     call   0x100030 <kernel_main>