Page 1 of 1

pe32 msvc kernel grub multiboot msdos stub

Posted: Thu Mar 08, 2007 12:02 am
by Kevin McGuire
TomB@?.com

This is a lot of help thanks. I was almost going to finish the elf version of my kernel because there wasn't much about regarding PE kernels. But the main reason for trying PE is that it's different, and it's a challenge.

Thanks for the help, I'll work on this for the time being.

On 3/7/07, Leonard McGuire <[email protected] > wrote:

>..So this stub only has the contain the code for the multiboot
>header...

(short reply):
Yes. You can make a quick and dirty kernel and work from that. Just do
the multi boot header, and call the kernel's main function. You may very
well have to transverse the PE32 header.. (possibly)

(long reply):

Almost. If you are talking about the MSDOS stub then it actually does
two things.
1. Holds the multi boot header.
2. Performs any processing needed on the image it is contained inside in.

The first one is easy. The second one is also easy, but takes some work.

We know the stub is attached to the beginning of the linkers output
which should be you're kernel image. I can not remember how long the
MSDOS stub is required to be, but I can be sure it was a fixed size.

Since GRUB will load our image to a certain place in memory. We can
simply make the STUB use <LOADED-ADDRESS>+<MSDOS-STUB-SIZE> to acquire
the first byte of the PE32 header.

If I remember correctly I generated a DLL, not a EXE for my kernel. So
if you do a little research you will find the PE32 header (and
headers!). You will also need to try producing a DLL with MSVC and using
objdump or a similar program to view its contents.

The main objective is to use the STUB to read the PE32 image, and find
the entry point. Then setup the stack, and call or jump to the entry
point. This is _really_ easy I promise.

Once you get loaded try writing to video memory for a test:
((unsigned char*)0x8B000)[0] = 'a';
((unsigned char*)0x8B000)[1] = 0x01;

Note: I used '' instead of a string. Using a string will create a .DATA
section in the DLL image which the STUB you create will need to load
propery *hint hint* later on.

You pretty much need to overview these topics at the _very_ least!
- google: pe32 header
- google: msdos stub
- google: vga memory osdev
- google: grub multi boot header
- google: .DATA .TEXT .BSS

Let me know if I can help.

Tom Bell wrote:
>Hey,
>
>Thanks for replying. So this stub only has the contain the code for the
>multiboot header, and calling the main function? Or does it need some
>extra code adding?
>
>Thanks
>
>On 3/5/07, *Leonard McGuire* <[email protected]
><mailto:[email protected]>> wrote:
>
>The entire process I performed was the write a MSDOS
>stub. Then inside the MSVC linker options find the option /STUB and
>replace it with your own stub which is simply a boot loader program with
>a multi boot compliant header inserted below the MZ.. bytes that a MSDOS
>stub must have at the beginning... then use grub to load it. The whole
>reason of making a stub is to let MSVC append the kernel to the end of
>it. The stub then loads the kernel sections into memory appropriately.
>
>Its not all that hard, but fairly involved let me know if the Wiki does
>not help. My method is a little harder than the one the Wiki will
>give you.
>>TomB
>

>>Hey,
>>I was wondering if you still had anything document or code wise
>>regarding kernels written in MSVC.
>>Thanks
>>TomB
[wiki][/wiki]