Page 1 of 1
grub module problem
Posted: Mon Oct 01, 2007 5:16 am
by lukem95
i have tried in vain for a while to get grub to load a module for my os.
the process seems very easy: get grub to load it, get the starting address from multiboot, relocate elf etc.
it seems to load it fine, and multiboot says theres one module loaded, and has a starting offset. when i try to relocate this, my driver gives all sorts of errors, saying its not an elf etc etc
i checked thru my code, and wrote a debugging function which printed everything remotly to do with modules onto the screen and found that the contents of the module where that of my gets() function...
i will post code as soon as i get home for you to look at, but i was wondering why the f*** this would happen, i havnt done anything remotly like each other in the code, and i certainly dont cross over the variables :S
Posted: Mon Oct 01, 2007 5:34 am
by JamesM
and i certainly dont cross over the variables :S
I really hope this quote still stands up when you've found the bug!
It sounds a lot like you're allocating something statically at 0x<bleh> and clobbering the module.
Posted: Mon Oct 01, 2007 4:08 pm
by lukem95
i think i may haved missed something, i have been over my code countless times and cannot see whats wrong with it.
heres what it looks like debugging:
heres my code:
Code: Select all
module_t* mods = (module_t*) mbd->mods_addr;
printf("Balls... multiboot doesn't seem to be working. Lets do some checks:\n");
printf("\n++++++++++++++DEBUG++++++++++++++\n");
printf("mods_count: %d\n", mbd->mods_count);
printf("mods_addr: 0x%x\n", mbd->mods_addr);
printf("mod_start: 0x%x\n",mods->mod_start);
printf("+++++++++++++++++++++++++++++++++\n\n");
openModule_elf("Shell.bin",mods->mod_start); /* mods[1].mod_start */
printf("hmm...\n");
so you can see what goes on in main
Code: Select all
int openModule_elf(char* name, int offset)
{
elfHeader* header;
/* Put header information into header struct */
header = (elfHeader*) offset;
/* DEBUGING STUFF */
printf("Opening %s from 0x%x!\n",name,offset);
printf("Magic Bits: 0x%x %s%s%s\n",header->ident[0],header->ident[1],header->ident[2],header->ident[3]);
/* !DEBUGING STUFF */
/* Check for any errors */
if(header->ident[0] != 0x7f/* || header->ident[1] != 'E' || header->ident[2] != 'L' || header->ident[3] != 'F'*/)
{
printf("Error opening %s! %s is not ELF\n",name,name);
return -1;
}
if(header->ident[4] != 1)
{
printf("Error opening %s! ELF not designed for x86-32 architecture!\n",name);
return -1;
}
if(header->ident[5] != 1)
{
printf("Error opening %s! ELF uses wrong encoding type. LSB ONLY supported!\n",name);
return -1;
}
if(header->machine != 7)
{
printf("Error opening %s! ELF designed for a different architecture!\n",name);
return -1;
}
if(header->flags)
{
printf("Error opening %s! ELF must not contain flags in header!\n",name);
return -1;
}
if(relocModule_elf(header, header->entry) != 1)
{
printf("Error opening %s! Cannot relocate ELF image!\n",name);
return -1;
}
/* Call the program */
__asm__ __volatile("call %0" : : "a" (header->entry));
return 1;
}
int relocModule_elf(elfHeader* header, int start_addr)
{
int i;
programHeader* pHeader = (programHeader*) (header->phoff + start_addr);
for(i = 0; i < header->phnum; i++)
{
/* Check if its loadable */
if( pHeader[i].type == 1 ) /* PT_LOAD == 1 */
{
if(!malloc(pHeader->memSize))
return -1;
}
}
/* We rule! */
return 1;
}
thats my module/elf relocation code (duh :p)
Code: Select all
void gets(char *string)
{
shell_bit = 1; /* Tell kbd handler to gather input */
while(shell_bit)
printf("");
for(int i=0; i<= kb_count; i++)
{
string[i] = shell_data_buffer[i];
}
kb_count = 0;
shell_bit = 0;
shell_data_buffer = "";
}
void keyboard_handler(struct regs *r)
{
/* Read from the keyboard's data buffer */
scancode = inportb(0x60);
if(getch_flag)
getch_bit++;
else
getch_bit=0;
/* Special Keys */
switch (scancode)
{
case KRCAPS_LOCK:
if(caps)
caps = 0;
else
caps = 1;
toggle_caps();
break;
case KRNUM_LOCK:
toggle_num();
break;
case KRSCROLL_LOCK:
toggle_scroll();
break;
case 88: /* F12 */
reboot();
break;
default:
break;
}
if (scancode & 0x80)
{
/* Held Keys */
}
else
{
if( shell_bit )
{
/* Was it enter? */
if(kbduk[scancode]=='\n')
{
/* Yes.. Clear all buffers */
shell_data_buffer[kb_count] = '\0';
putch('\n');
shell_bit = 0;
kb_count = 0;
} else {
/* No... so we append this key to our string */
if(caps)
shell_data_buffer[kb_count] = kbduk_shift[scancode];
else
shell_data_buffer[kb_count] = kbduk[scancode];
putch(shell_data_buffer[kb_count]);
kb_count++;
}
}
}
}
if anybody has 5 minutes and feels like reading through my code (:roll:) i would very much appreciate it!
Luke
edit: ignore the random curses in my code.. its when things (drivers) im testing don't do as they're told lol
Posted: Mon Oct 01, 2007 8:10 pm
by pcmattman
Your code appears to be alright.
I'd suggest loading up your final binary in a hex editor to find out exactly what's in it - and also load up a file that you know is a valid ELF binary and find out what's different.
It might also be that your kernel overwrites the module (I avoid this in my kernel by marking the pages that each module takes as "allocated").
Posted: Tue Oct 02, 2007 4:53 am
by lukem95
i dont think its been overwritten, because i too have a malloc system which marks used chunks as such...
ill have a little look when i get access to it again, maybe fiddle with my gets...
BTW: i typed hello[enter] in my gets function, before i tried to load the module, and the magic bits apear as you can see in the image :S
if i dont use a gets function the magic bits dont show elf either... maybe it is the reloc code..
Posted: Thu Oct 04, 2007 6:51 am
by lukem95
i fiddled and fiddled with no joy. I dont think its my gets code because even after commenting EVERYTHING to do with it (its got bits in 5 files... oddly, i may have to neaten my code
), it fails.
If any one has any suggestions i'd be very appreciative.
Posted: Thu Oct 04, 2007 4:56 pm
by pcmattman
Code: Select all
printf("Magic Bits: 0x%x %s%s%s\n",header->ident[0],header->ident[1],header->ident[2],header->ident[3]);
Should be
Code: Select all
printf("Magic Bits: 0x%x %c%c%c\n",header->ident[0],header->ident[1],header->ident[2],header->ident[3]);
The identification bytes are characters, not pointers to strings...
Posted: Fri Oct 05, 2007 3:54 am
by lukem95
thankyou for your repeated help, ill try this when i get home and post the results (if it doesn't enlighten me in some way that i can fix it myself).
lukem