MSVC Grub - A new way
Posted: Sun Apr 18, 2010 12:20 am
Hello everyone,
Ive been working on experimenting with booting GRUB using Microsoft Visual C++ 2008 and developed an easy method of allowing one to boot a PE kernel built with MSVC with GRUB. The method seems too easy to warrant a tutorial on it, so I decided to create a post describing the method in hopes that it will be helpful for other developers using MSVC.
To get it booting, all we need to do is define a multiboot structure. Here is the declaration:Easy enough With our structure declared, we just define the multiboot structure in the beginning of the .text section. The .text section is always the first section of the image after the PE header structures. PE data (such as resources and export/import tables etc) are always at the end of the binary so the PE headers are always the same size (0x400). The multiboot spec says that this structure must be defined within the first 8k in the image, aligned on a 32-bit boundary. By placing this structure at the beginning of a segment in the file, it will always be at 0x400 aligned to the set section alignment, which are always 32 bit aligned:
Thats all that there is to it. KeStartup is your entry point function, LOADBASE is the base address of your kernel (like 1MB for example), HEADER_ADDRESS is the address of the multiboot header (which happens to be LOADBASE+0x400 do to .text always starting at 0x400), magic is 0x1BADB002, tested flags of 0x00010003 and the checksum being -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS).
Here is the complete example:Thats all that there is to it. Assuming GRUB is configured to boot your kernel, this should make your kernel bootable by GRUB. Tested and works with two separate projects. Please post comments if there are any
*Wiki: I never used the Wiki so dont know how to add pages. Please feel free to use this content if you like.
Ive been working on experimenting with booting GRUB using Microsoft Visual C++ 2008 and developed an easy method of allowing one to boot a PE kernel built with MSVC with GRUB. The method seems too easy to warrant a tutorial on it, so I decided to create a post describing the method in hopes that it will be helpful for other developers using MSVC.
To get it booting, all we need to do is define a multiboot structure. Here is the declaration:
Code: Select all
#pragma pack (push, 1)
/**
* Multiboot structure
*/
typedef struct _MULTIBOOT_INFO {
uint32_t magic;
uint32_t flags;
uint32_t checksum;
uint32_t headerAddr;
uint32_t loadAddr;
uint32_t loadEndAddr;
uint32_t bssEndAddr;
uint32_t entryPoint;
uint32_t modType;
uint32_t width;
uint32_t height;
uint32_t depth;
}MULTIBOOT_INFO, *PMULTIBOOT_INFO;
#pragma pack(pop,1)
Code: Select all
#pragma section(".text")
__declspec(allocate(".text"))
MULTIBOOT_INFO _MultibootInfo = {
MULTIBOOT_HEADER_MAGIC,
MULTIBOOT_HEADER_FLAGS,
CHECKSUM,
HEADER_ADDRESS,
LOADBASE,
0, //load end address
0, //bss end address
KeStartup
};
Here is the complete example:
Code: Select all
#pragma pack (push, 1)
/**
* Multiboot structure
*/
typedef struct _MULTIBOOT_INFO {
uint32_t magic;
uint32_t flags;
uint32_t checksum;
uint32_t headerAddr;
uint32_t loadAddr;
uint32_t loadEndAddr;
uint32_t bssEndAddr;
uint32_t entryPoint;
uint32_t modType;
uint32_t width;
uint32_t height;
uint32_t depth;
}MULTIBOOT_INFO, *PMULTIBOOT_INFO;
#pragma pack(pop,1)
/**
* Kernel entry
*/
void KeStartup ( PMULTIBOOT_INFO* loaderBlock ) {
__halt ();
}
//! loading address
#define LOADBASE 0x100000
//! header offset will always be this
#define ALIGN 0x400
#define HEADER_ADDRESS LOADBASE+ALIGN
#define MULTIBOOT_HEADER_MAGIC 0x1BADB002
#define MULTIBOOT_HEADER_FLAGS 0x00010003
#define STACK_SIZE 0x4000
#define CHECKSUM -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
#pragma section(".text")
__declspec(allocate(".text"))
MULTIBOOT_INFO _MultibootInfo = {
MULTIBOOT_HEADER_MAGIC,
MULTIBOOT_HEADER_FLAGS,
CHECKSUM,
HEADER_ADDRESS,
LOADBASE,
0, //load end address
0, //bss end address
KeStartup
};
*Wiki: I never used the Wiki so dont know how to add pages. Please feel free to use this content if you like.