Bad magic with GRUB.

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
cacao
Posts: 8
Joined: Tue Dec 12, 2006 5:32 pm

Bad magic with GRUB.

Post by cacao »

Hello,

I used GRUB as a bootloader for my kernel. I follow the specs on grub
website, I used exactly the same boot.s and multiboot.h but I have a
problem.
After grub load my kernel I do this test:

Code: Select all

if (magic != MULTIBOOT_BOOTLOADER_MAGIC)
{
 ...
}
magic is never equal to MULTIBOOT_BOOTLOADER_MAGIC is equal to
"0xa9c1".

I don't know what can I check to found the problem, do you have any
ideas ?

Regards,
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

I think that in that same tutorial, you push the values of eax and ebx to pass them to main().

Just checking - have you definitely pushed them in the right order? If not, you would be getting a pointer to the multiboot info structure rather than the magic number.

HTH
Adam
cacao
Posts: 8
Joined: Tue Dec 12, 2006 5:32 pm

Post by cacao »

I follow this specification : http://orgs.man.ac.uk/documentation/gru ... oot_4.html

%ebx is pushed before %eax and my main prototype is :

Code: Select all

int cmain(unsigned long magic, unsigned long addr)
{
 ...
}
I tried to display the value of addr and it seems to be the real grub
multiboot structure so I think that the value is pushed in a right order.

However I tried to debug it with bochs setting a logical break at the entry
point. The value of %eax was 0x2BADB002 (The good magic). Next, I step
into but I do not arrive to interpret the result.

Regards
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

Post by nick8325 »

That is odd...you could try breaking at the entry point and then using si to step one instruction at a time, to see what goes wrong. Have you changed boot.s at all?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

I wonder if you could be somehow using eax as a pointer rather than a value? Also, have you checked that your stack pointer and segment are in order?

Adam
User avatar
Walling
Member
Member
Posts: 158
Joined: Mon Dec 04, 2006 6:06 am
Location: Berlin, Germany

Post by Walling »

When I first read the subject I thought it said "Black magic with GRUB". Indeed GRUB can be black magic sometimes :wink:

Still... when debugging using Bochs try to see if the stack contains the expected words, ie. when breaking at the entry of cmain, you could write:

Code: Select all

x /3wx esp
The result should be something like: <Address of cmain> <Magic value> <Pointer to multiboot structure>. Also try "info regs" to see if the value of ESP is expected.
cacao
Posts: 8
Joined: Tue Dec 12, 2006 5:32 pm

Post by cacao »

Ok thanks to all, I will try to follow your advice this night and I keep you
inform.
cacao
Posts: 8
Joined: Tue Dec 12, 2006 5:32 pm

Post by cacao »

Hello,

It is really really weird. I set a breakpoint at the entry point. After
the break I typed "info reg" and %eax, %ebx, cs, ss, ds seems ok.

When I display three words the value of %eax and %ebx change.

Hmm...I'm lost. Do you know what is going on ?

Code: Select all

(0) Breakpoint 1, 0x002017a9 in ?? ()
Next at t=21755028
(0) [0x002017a9] 0008:002017a9 (unk. ctxt): push ebp                  ; 55
<bochs:4> info reg
eax: 0x2badb002 732803074
ecx: 0x1        1
edx: 0x1de00    122368
ebx: 0x2b920    178464
esp: 0x67eb4    425652
ebp: 0x67ec4    425668
esi: 0x2ba83    178819
edi: 0x2ba84    178820
eip: 0x2017a9  
eflags 0x46      
cs:  0x8       
ss:  0x10      
ds:  0x10      
es:  0x10      
fs:  0x10      
gs:  0x10      
<bochs:5> x/3wx esp
[bochs]:
0x00067eb4 <bogus+       0>:    0x00008c54      0x0000a9c1      0x002017a9
<bochs:6> 
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

Post by nick8325 »

cacao wrote:

Code: Select all

<bochs:5> x/3wx esp
[bochs]:
0x00067eb4 <bogus+       0>:    0x00008c54      0x0000a9c1      0x002017a9
<bochs:6> 
The stack grows downwards, doesn't it? So Bochs is displaying [esp], [esp+4] and [esp+8], when you want to see [esp-12], [esp-8] and [esp-4]. What does x/3wx esp-12 show?...
cacao
Posts: 8
Joined: Tue Dec 12, 2006 5:32 pm

Post by cacao »

Hmm...yes the stack grows downward but if I set a breakpoint at the
entry point, Where is the parameters of main located ? On the stack ?

Code: Select all

(0) Breakpoint 1, 0x002017a9 in ?? ()
Next at t=21755028
(0) [0x002017a9] 0008:002017a9 (unk. ctxt): push ebp                  ; 55
<bochs:3> info reg
eax: 0x2badb002 732803074
ecx: 0x1        1
edx: 0x1de00    122368
ebx: 0x2b920    178464
esp: 0x67eb4    425652
ebp: 0x67ec4    425668
esi: 0x2ba83    178819
edi: 0x2ba84    178820
eip: 0x2017a9  
eflags 0x46      
cs:  0x8       
ss:  0x10      
ds:  0x10      
es:  0x10      
fs:  0x10      
gs:  0x10      
<bochs:4> x/3wx esp-12
[bochs]:
0x00067ea8 <bogus+       0>:    0x0001de00      0x00000001      0x00000000
<bochs:5> 
[bochs]:
<bochs:5> disasm /5
002017a9: (                    ): push ebp                  ; 55
002017aa: (                    ): mov ebp, esp              ; 89e5
002017ac: (                    ): sub esp, 0x00000008       ; 83ec08
002017af: (                    ): cmp dword ptr ss:[ebp+0x8], 0x2badb002 ; 817d0802b0ad2b
002017b6: (                    ): jz .+0x00000034           ; 7434
<bochs:6> 
After the entry point there is the prolog and the test (002017af:), this
is what I'm doing in main. In the assembly we see that the value of
magic is located to "ss:[ebp+0x8]". So, I put a breakpoint to 0x002017af
and I did this :

Code: Select all

<bochs:17> x/1wx ss:ebp+0x8
[bochs]:
0x00067ecc <bogus+       0>:    0x0002ba96
Hmm...this is the true value of MULTIBOOT_BOOTLOADER_MAGIC...very
weird. Any ideas ?
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

Post by nick8325 »

cacao wrote:Hmm...yes the stack grows downward but if I set a breakpoint at the
entry point, Where is the parameters of main located ? On the stack ?
Um. Yes, of course, you're right. I was getting confused...

I just noticed, though: why is ESP so low? According to boot.s, the stack is after the end of the kernel, which (according to Bochs) is loaded at 2MB or so. How did you link the kernel? Perhaps you should inspect the kernel with a hex editor to see if the multiboot header looks right...

EDIT: also, try setting a breakpoint at multiboot_entry and stepping from there.
cacao
Posts: 8
Joined: Tue Dec 12, 2006 5:32 pm

[SOLVED] Bad magic with GRUB.

Post by cacao »

YES !!!! The linker script. I check it and I saw that my "ENTRY" value was
"cmain" instead of "_start" (defined in boot.s). The problem is solved.

Thanks to all guy ;)
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

Re: [SOLVED] Bad magic with GRUB.

Post by nick8325 »

cacao wrote:YES !!!! The linker script. I check it and I saw that my "ENTRY" value was
"cmain" instead of "_start" (defined in boot.s). The problem is solved.

Thanks to all guy ;)
Aha! :)
Post Reply