hi,
i want to boot my C Kernel from GRUB. I have installed grub on my floppy. at this stage my C Kernel is just a 'hello world' (typical) kernel .
Since i am booting my kernel from grub i have to include 'multiboot header' in my kernel so that grub recognizes it as valid kernel.
So where in my kernel m i suppose to keep this header????
In what format should i compile and link my C file????
I don't want to write a small assembly function which typically loads the C Kernel! So can i load my C Kernel from GRUB without any initial assembly code???
Following is my C Kernel code with Multiboot header :
int main(void)
{
//*******Multiboot Header for Kernel*********
long unsigned int Magic = 0x1BADB002;
long unsigned int Flags = 0x00000000;
long unsigned int Checksum = 0xE4524FFE;
//*******Multiboot Header for Kernel*********
char *str = "IOS v1.0",*ch;
unsigned short *vidmem = (unsigned short*) 0xb8000;
unsigned i;
for(ch = str, i = 0; *ch; ch++,i++)
{
vidmem = (unsigned char) *ch | 0x0700;
}
for(; ;
return 0;
}
One more question what sould be my checksum value? how m i suppose to calculate it??
my parameters to gcc are :
gcc -c kernel.c -o kernel.o
my parameters to ld are :
ld -Ttext=0x1000000 --oformat coff-go32-exe -o kernel.exe kernel.o
I really don't wanna use any assembly to load my C Kernel main function!!!!
And at last : if i want to use intel syntax assembly in my C code inline assembly is there any way i can do this in gcc???[/quote]
Booting C Kernel from GRUB
Booting C Kernel from GRUB
Nothings Impossible
-
- Member
- Posts: 134
- Joined: Sun Oct 24, 2004 11:00 pm
- Location: North Dakota, where the buffalo roam
Re: Booting C Kernel from GRUB
Well, for starters, you can't make the header out of local variables. The header needs to be present in the binary when GRUB reads it, and local variables are not initialized until the function is run. Try making them global.
Also, you may need to do a few more special things to use coff format. You should read the documentation for the multiboot specification. <a href="http://www.gnu.org/software/grub/manual/multiboot/multiboot.html">http://www.gnu.org/software/grub/manual/multiboot/multiboot.html</a>
Also, you may need to do a few more special things to use coff format. You should read the documentation for the multiboot specification. <a href="http://www.gnu.org/software/grub/manual/multiboot/multiboot.html">http://www.gnu.org/software/grub/manual/multiboot/multiboot.html</a>
Last edited by rexlunae on Sun Feb 06, 2005 12:00 am, edited 2 times in total.
Re: Booting C Kernel from GRUB
You might be able to use the variables from C with a few changes, but I don't know how; it's actually pretty easy to do in assembly. The Grub source code comes with an example "stub kernel" to show you how to do this; with a few fixes and a Makefile, you can use it directly. Get the source code if you don't have it already. Once you've got it unpacked, look at:
grub/doc/boot.S
grub/doc/kernel.c
grub/doc/multiboot.h
The variables you're asking about are defined in boot.S. Warning -- boot.S is written in AT&T assembly syntax; operand order is "src, dest", not "dest, src" like it is with MASM/NASM code.
I'm using the Grub stub kernel myself as a starting point. I've posted the "fixed" versions of the files, plus a Makefile, on my website. If you're doing your work from a Unixy system (Linux, DJGPP, ...), try using these.
http://matt.ioioio.net/kernel/
If you get "undefined symbol" errors when building, look at multiboot.h; change
#define HAVE_ASM_USCORE 0
to
#define HAVE_ASM_USCORE 1
That should fix things.
Last, you can use inline assembly with GCC, but the format depends on the assembler you're using. If you're using GAS (the Gnu ASsembler), then you *might* be able to use Intel syntax by giving a special flag. You'll have to look through the assembler documentation (info page, probably) to see what the flag is; I've never used it before. If you can't find out about the flag, then you can use AT&T syntax (the default). It's not much different from Intel; look over boot.S from the Grub stub kernel for an example.
grub/doc/boot.S
grub/doc/kernel.c
grub/doc/multiboot.h
The variables you're asking about are defined in boot.S. Warning -- boot.S is written in AT&T assembly syntax; operand order is "src, dest", not "dest, src" like it is with MASM/NASM code.
I'm using the Grub stub kernel myself as a starting point. I've posted the "fixed" versions of the files, plus a Makefile, on my website. If you're doing your work from a Unixy system (Linux, DJGPP, ...), try using these.
http://matt.ioioio.net/kernel/
If you get "undefined symbol" errors when building, look at multiboot.h; change
#define HAVE_ASM_USCORE 0
to
#define HAVE_ASM_USCORE 1
That should fix things.
Last, you can use inline assembly with GCC, but the format depends on the assembler you're using. If you're using GAS (the Gnu ASsembler), then you *might* be able to use Intel syntax by giving a special flag. You'll have to look through the assembler documentation (info page, probably) to see what the flag is; I've never used it before. If you can't find out about the flag, then you can use AT&T syntax (the default). It's not much different from Intel; look over boot.S from the Grub stub kernel for an example.
Last edited by __matt__ on Mon Feb 07, 2005 12:00 am, edited 2 times in total.
-
- Posts: 7
- Joined: Fri Feb 04, 2005 12:00 am
Re: Booting C Kernel from GRUB
Im trying something similar, having Grub call some assembly which invokes the C kernel, and im testing with bochs.
However, I have not figured out how to create the proper disk image.
I know I need to write the stage1 and stage2 files from grub to the disk, but what about my kernel? Should I be formatting it with some known filesystem and copying the files?
I followed this tutorial to get started, but he doesnt explain how to do the image.
http://www.mega-tokyo.com/osfaq2/index.php/BareBones
If it matters, the development environment is FreeBSD.
However, I have not figured out how to create the proper disk image.
I know I need to write the stage1 and stage2 files from grub to the disk, but what about my kernel? Should I be formatting it with some known filesystem and copying the files?
I followed this tutorial to get started, but he doesnt explain how to do the image.
http://www.mega-tokyo.com/osfaq2/index.php/BareBones
If it matters, the development environment is FreeBSD.
---
Unfortunately, while all answers are replies, not all replies are answers
Unfortunately, while all answers are replies, not all replies are answers
Re: Booting C Kernel from GRUB
hi,
THANK GOD! After spending whole night playing with assembly(nasm) and GRUB and C code i was finally able to boot my C Kernel From GRUB.
But i *had* to use assembly one way or the other to boot my C Kernel.
One of the simplest way to boot ur C Kernel is to call ur 'main' function of C Kernel from assembly code. In assembly put the 'multiboot header' and just call ur 'main' function.
hey desjardins:
as it seems u have same prob as i had! Abt the format of the file, in my case it was FAT. Look its actually very simple, u need two floppies
Combine Stage1 and Stage2 in one file and write that file to 1st sector of ur first floppy using 'Rawrite' or some other software of that kind.
Then format ur second floppy (format should be the one which GRUB understands. To be on safer side use 'fat') and put stage1 and stage2 file on it.
Boot from floppy 1 , on grubs cmd line , type installation command after placing floppy2 in drive.
Then place ur Kernel file on formatted floppy. U r ready to load ur Kernel.
Just at grub cmd line type this command
kernel (fd0)/name_of_ur_kernel
I know this sounds simple, but mind u it isn't:)
I m really bad at explaning things.
But the best way is to read the documentation provided for Multiboot kernel , again and again and again until u succeed.
If u want i can email u my code and detailed steps involved:)
THANK GOD! After spending whole night playing with assembly(nasm) and GRUB and C code i was finally able to boot my C Kernel From GRUB.
But i *had* to use assembly one way or the other to boot my C Kernel.
One of the simplest way to boot ur C Kernel is to call ur 'main' function of C Kernel from assembly code. In assembly put the 'multiboot header' and just call ur 'main' function.
hey desjardins:
as it seems u have same prob as i had! Abt the format of the file, in my case it was FAT. Look its actually very simple, u need two floppies
Combine Stage1 and Stage2 in one file and write that file to 1st sector of ur first floppy using 'Rawrite' or some other software of that kind.
Then format ur second floppy (format should be the one which GRUB understands. To be on safer side use 'fat') and put stage1 and stage2 file on it.
Boot from floppy 1 , on grubs cmd line , type installation command after placing floppy2 in drive.
Then place ur Kernel file on formatted floppy. U r ready to load ur Kernel.
Just at grub cmd line type this command
kernel (fd0)/name_of_ur_kernel
I know this sounds simple, but mind u it isn't:)
I m really bad at explaning things.
But the best way is to read the documentation provided for Multiboot kernel , again and again and again until u succeed.
If u want i can email u my code and detailed steps involved:)
Nothings Impossible
-
- Posts: 7
- Joined: Fri Feb 04, 2005 12:00 am
Re: Booting C Kernel from GRUB
Thanks, I found this article which describes how to setup the dual disks for GRUB. Id prefer to do my testing under Bochs however, and I dont think you can start bochs with 1 disk, and then switch to another while its running...
http://www.mega-tokyo.com/osfaq2/index.php/GRUB
http://www.mega-tokyo.com/osfaq2/index.php/GRUB
---
Unfortunately, while all answers are replies, not all replies are answers
Unfortunately, while all answers are replies, not all replies are answers
Re: Booting C Kernel from GRUB
But you can configure bochs to run with more than one floppy drive..desjardins wrote:Thanks, I found this article which describes how to setup the dual disks for GRUB. Id prefer to do my testing under Bochs however, and I dont think you can start bochs with 1 disk, and then switch to another while its running...
Just load up your "grub disk" as the first flopppy and your kernel disk as the second floppy and refer to it as (fd1) instead of (fd0).
~rmg
-
- Member
- Posts: 134
- Joined: Sun Oct 24, 2004 11:00 pm
- Location: North Dakota, where the buffalo roam
Re: Booting C Kernel from GRUB
That's not entirely true. Try this:kan wrote: But i *had* to use assembly one way or the other to boot my C Kernel.
<i>Filename: example.c</i>
Code: Select all
#define BOOT_MAGIC 0x1BADB002
static unsigned int boot_magic =BOOT_MAGIC;
static unsigned int boot_flags =0;
static unsigned int boot_check =-(BOOT_MAGIC);
char *location = (void *)0xb8000;
int offset = 0;
void print(const char *input)
{
while(*input) switch(*input)
{
case '\n':
offset += (160 - (offset % 160));
input++;
break;
default:
location[offset++] = *input++;
location[offset++] = 7;
}
}
void clear()
{
location = (void *)0xb8000;
char *p = location;
while((int)p < 0xc0000) *p++ = 0;
}
_start()
{
clear();
print("This kernel is only half-a-screen and can print.\nHave fun.\n");
while(1);
}
Code: Select all
example: example.c Makefile
gcc -ffreestanding example.c -Ttext 0x100000 -nostdlib -oformat=elf32-i386 -o example