Howto "boot" a C/C++ Kernel

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.
yomega

Re:Howto "boot" a C/C++ Kernel

Post by yomega »

Hi ppl,

thanks Solar you got me on the right track ;) It was both: my tools and my kernel loader.

I finally managed to boot the example kernel. But in fact, the tutorial I posted contains some errors. The kernel image created by this page is not runnable with grub, even they told me so.

So now i will start reading reading and reading :)

thanks again.

Greetings,
Stephan
mr anderson

Re:Howto "boot" a C/C++ Kernel

Post by mr anderson »

I also tried the website solar gave, but i seem to have a problem:
when assembling the loader (loader.asm), i encounter the follwoing error:

"loader.asm:15 Error: junk at end of line, first unrecognized charachter is ','"

I am not really very good with assembler, and just want to get to the c part of kernel programming...

Can someone please help me out?

Just for reference, i'll paste the file loader.asm too:

.global _loader

.set ALIGN, 1<<0
.set MEMINFO, 1<<1
.set FLAGS, ALIGN | MEMINFO
.set MAGIC, 0x1BADB002
.set CHECKSUM, -(MAGIC + FLAGS)

.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM

.set STACKSIZE, 0x4000
.comm stack, STACKSIZE, 32

_loader: mov $(stack + STACKSIZE), %esp
push %eax
push %ebx

call _main
hlt
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Howto "boot" a C/C++ Kernel

Post by Solar »

I smell something here...

"loader.asm:15 Error..."

So the error is on line 15. That would be:

".comm stack, STACKSIZE, 32"

The error said: "junk at end of line, first unrecognized charachter is ','" - it doesn't seem to like the number of parameters here.

Looking up .comm in the binutils online manual gives:

"When using ELF, the .comm directive takes an optional third argument."

When using ELF. I'd daresay your toolchain is not configured to an ELF target.

The solution, of course, is to build a GCC Cross-Compiler...
Every good solution is obvious once you've found it.
Dreamsmith

Re:Howto "boot" a C/C++ Kernel

Post by Dreamsmith »

Solar wrote:Haven't tried to do it, but GRUB does offer a flag to demand VESA modes to be set, and the Multiboot data structure has fields to report back the actual screen mode.
The above statement is false. A true statement would be: GRUB does offer a flag to request VESA modes to be set. Sorry to split hairs, but this is a vitally important distinction in this case, as you code must handle properly the situation where your bootloader simply ignores the request. Indeed, last time I checked, GRUB always ignores it, which is allowed under the Multiboot specification.
Mr. Anderson

Re:Howto "boot" a C/C++ Kernel

Post by Mr. Anderson »

Thanks for your help solar

I have made now made a cross compiler, that compiles to i386-elf.
Everything compiles very smoothly, but still, grub refuses to load my kernel. I always get error 13:

"Error 13: Invalid or unsupported executable format"

I'm sure that the kernel is compiled to the right target now, and it has the multiboot header. I used the example given in the 'barebones' site you mentioned earlier. Do you or anyone else have any idea about what'd going wrong here??
Therx

Re:Howto "boot" a C/C++ Kernel

Post by Therx »

I doubt it's the problem as you'll have a very small kernel but the multiboot header has to be within the first 8kb of the kernel

Pete
Mr. Anderson

Re:Howto "boot" a C/C++ Kernel

Post by Mr. Anderson »

The multiboot header was the first file i passed to the header, so i think it should be under the 8kb limit. :-s
Mr. Anderson

Re:Howto "boot" a C/C++ Kernel

Post by Mr. Anderson »

sorry, i passed it to the LINKER ofcourse
Mr. Anderson

Re:Howto "boot" a C/C++ Kernel

Post by Mr. Anderson »

My kernel still won't run, but i have noticed something weird:

as i told you: grub won't boot my kernel, even when it is compiled to an i386-elf target, and shows an error 13 (invalid or unsupported kernel format).

BUT now i've seen that, if i set the type of the kernel to 'openbsd' or 'netbsd' (kernel --type=netbsd mykernel.bin), i DONT get this error. Grub shows me some memory information ( i think some data about the multiboot header) Afer that, i just have the grub shell back.

Does anyone know what is going on here, and how i can run my kernel :-s?
Mr. Anderson

Re:Howto "boot" a C/C++ Kernel

Post by Mr. Anderson »

again, i made a mistake...
I had forgotten to isseu the boot command (it has been a while since i last used grub :-p) grub now boots my kernel, as long as i specify that its type is 'netbsd' or 'openbsd'...BUT when grub loads it, i get, as Vmware calls it, a 'Machine kernel stack fault'.

Something is going DEFINITELY WRONG here

Even if you don't have an exact sollution, but a clue, please tell me....

thanx already :-D
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Howto "boot" a C/C++ Kernel

Post by Solar »

I honestly have no idea. I'm more or less the "maintainer" for the FAQ pages "GCC Cross-Compiler" and "BareBones", and I routinely follow the instructions on those very FAQ pages to - successfully - construct toolchain and "hello world" kernel...
Every good solution is obvious once you've found it.
Mr. Anderson

Re:Howto "boot" a C/C++ Kernel

Post by Mr. Anderson »

well, then i'll just start all over again...i'm going to read the instructions even more carefully than last time...fingers crossed...:-s

thanx anyway :-)
Mr. Anderson

Re:Howto "boot" a C/C++ Kernel

Post by Mr. Anderson »

It worked :-D

my kernel now boots, and i don't need to set types to netbsd and stuff like that.

I tried it all over again 2 times, and the second time it worked...I don't know what i did different, but i'm quite happy now.

Thanx for all the assistance
If i ever can return the favor i will :-D
movaxes
Posts: 5
Joined: Mon Jan 29, 2007 3:16 pm
Contact:

Re:Howto "boot" a C/C++ Kernel

Post by movaxes »

Pype.Clicker wrote:hmm ... i can see
vbeprobe [mode] Command
Probe VESA BIOS EXTENSION information. If the mode mode is specified, show only the information about mode. Otherwise, this command lists up available VBE modes on the screen. See also testvbe
but that's a grub-shell command and will be quite useless for the booted program :(
+-------------------+
72 | vbe_control_info | (present if flags[11] is set)
76 | vbe_mode_info |
80 | vbe_mode |
82 | vbe_interface_seg |
84 | vbe_interface_off |
86 | vbe_interface_len |
+-------------------+
The fields vbe_control_info and vbe_mode_info contain the physical addresses of VBE control information returned by the VBE Function 00h and VBE mode information returned by the VBE Function 01h, respectively.

The field vbe_mode indicates current video mode in the format specified in VBE 3.0.

The rest fields vbe_interface_seg, vbe_interface_off, and vbe_interface_len contain the table of a protected mode interface defined in VBE 2.0+. If this information is not available, those fields contain zero. Note that VBE 3.0 defines another protected mode interface which is incompatible with the old one. If you want to use the new protected mode interface, you will have to find the table yourself.

The fields for the graphics table are designed for VBE, but Multiboot boot loaders may simulate VBE on non-VBE modes, as if they were VBE modes.

from the Multiboot standard :P
The graphics fields of Multiboot header

All of the graphics fields are enabled by flag bit 2. They specify the preferred graphics mode. Note that that is only a recommended mode by the OS image. If the mode exists, the boot loader should set it, when the user doesn't specify a mode explicitly. Otherwise, the boot loader should fall back to a similar mode, if available.

The meaning of each is as follows:

mode_type
Contains 0 for linear graphics mode or 1 for EGA-standard text mode. Everything else is reserved for future expansion. Note that the boot loader may set a text mode, even if this field contains 0.
width
Contains the number of the columns. This is specified in pixels in a graphics mode, and in characters in a text mode. The value zero indicates that the OS image has no preference.
height
Contains the number of the lines. This is specified in pixels in a graphics mode, and in characters in a text mode. The value zero indicates that the OS image has no preference.
depth
Contains the number of bits per pixel in a graphics mode, and zero in a text mode. The value zero indicates that the OS image has no preference.
Okay, so basically, you tell GRUB which mode you request as 1600x1200x32, and grubs does what needed to locate that mode's number and setup the mode. And then you check the mode info reported by the vbe_info block of GRUB to know if it worked or not ...
Do you about some code implementing this??
Thx in advance if you do ;)
Post Reply