OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Apr 16, 2024 12:45 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: [SOLVED] Got wrong Grub Magic Number (Should be 0x2BADB002)
PostPosted: Sat Nov 04, 2017 11:32 am 
Offline

Joined: Sat Nov 04, 2017 11:14 am
Posts: 3
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:
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:

.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:
...
#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:
/* 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!


Last edited by farizluqman on Sat Nov 04, 2017 12:50 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Got wrong Grub Magic Number (Should be 0x2BADB002)
PostPosted: Sat Nov 04, 2017 12:49 pm 
Offline

Joined: Sat Nov 04, 2017 11:14 am
Posts: 3
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


Top
 Profile  
 
 Post subject: Re: [SOLVED] Got wrong Grub Magic Number (Should be 0x2BADB0
PostPosted: Sun Nov 05, 2017 2:10 am 
Offline
Member
Member

Joined: Sat Jan 21, 2017 7:35 am
Posts: 35
You should not post so much code, nobody would even look at your post.


Top
 Profile  
 
 Post subject: Re: [SOLVED] Got wrong Grub Magic Number (Should be 0x2BADB0
PostPosted: Tue Nov 07, 2017 3:58 am 
Offline

Joined: Sat Nov 04, 2017 11:14 am
Posts: 3
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


Top
 Profile  
 
 Post subject: Re: [SOLVED] Got wrong Grub Magic Number (Should be 0x2BADB0
PostPosted: Sat Nov 11, 2017 5:58 am 
Offline
Member
Member
User avatar

Joined: Sun Oct 22, 2006 7:01 am
Posts: 2646
Location: Devon, UK
TBH, code is fine, as long as it's inside code tags which yours is.

Cheers,
Adam


Top
 Profile  
 
 Post subject: Re: [SOLVED] Got wrong Grub Magic Number (Should be 0x2BADB0
PostPosted: Sat Nov 11, 2017 1:19 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 692
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:
.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:
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>


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 66 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group