Writing a GRUB kernel
Writing a GRUB kernel
I'm using Cygwin to get access to GCC/LD/AS etc.
I've had a go using the code here:
http://www.osdev.org/osfaq2/index.php/BareBones
But get a number of errors:
1.) - In Loader.s
.comm stack, STACKSIZE, 32 # reserve 16k stack on a quadword boundary
.comm only works with 2 parameters - not three, you have to remove the ', 32'.
2.) - In Loader.s
call _main # call kernel proper
That line only assembles if I put in another underscore before the main.
Then, if it works - I get an EXE! I was a bit dubious, I'm fairly happy I'm after a *.BIN file, however I tried it anyway. The error that I get from GRUB is:
Booting 'MyOS'
root (fd0)
Filesystem type is fat, using whole disk
kernel /boot/a.exe
Error 13: Invalid or unsupported executable format
Press any key to continue...
Yes, I need to change the 'MyOS' bit, but that's not the major thing.
Sorry for all the questions, but I keep trying code of the net, to get the feel for it, and none of it's worked so far. I just want a base that works so that I can build something of my own and learn as I go.
I've had a go using the code here:
http://www.osdev.org/osfaq2/index.php/BareBones
But get a number of errors:
1.) - In Loader.s
.comm stack, STACKSIZE, 32 # reserve 16k stack on a quadword boundary
.comm only works with 2 parameters - not three, you have to remove the ', 32'.
2.) - In Loader.s
call _main # call kernel proper
That line only assembles if I put in another underscore before the main.
Then, if it works - I get an EXE! I was a bit dubious, I'm fairly happy I'm after a *.BIN file, however I tried it anyway. The error that I get from GRUB is:
Booting 'MyOS'
root (fd0)
Filesystem type is fat, using whole disk
kernel /boot/a.exe
Error 13: Invalid or unsupported executable format
Press any key to continue...
Yes, I need to change the 'MyOS' bit, but that's not the major thing.
Sorry for all the questions, but I keep trying code of the net, to get the feel for it, and none of it's worked so far. I just want a base that works so that I can build something of my own and learn as I go.
Re:Writing a GRUB kernel
Now that's really odd. It works great for me, but then again, I'm using as natively. You should check that ld is linking to a ELF exacutable and not windows PE or something else obscure. I find the linux program entitled 'file' very usefull for finding out stuff about a file. Also, get a copy of 'chkmb' (should come with grub source). This utility verifys the header's presence and other great stuff ;D. You might not be using the linker script as well
I also have a question. Where on earth is the magic number?? I did push it to the stack before going to the kernel proper, but it's ALWAYS 0! I'm using BareBones (just modified the ordering. now the header is in a C file, but it never worked before either!) so the regiester is right (and I compared it to spoon). Thanks ahead
Cheers, DH.
I also have a question. Where on earth is the magic number?? I did push it to the stack before going to the kernel proper, but it's ALWAYS 0! I'm using BareBones (just modified the ordering. now the header is in a C file, but it never worked before either!) so the regiester is right (and I compared it to spoon). Thanks ahead
Cheers, DH.
Re:Writing a GRUB kernel
Thanks, I've not used that one before. It was indeed linking to PE. I've now changed the linker command to:
ld --oformat elf32-i386 -T linker.ld kernel.o loader.o
And end up getting:
Administrator@al /cygdrive/g/Assembly/Operating System/OmnOS/code/kernel
$ ld --oformat elf32-i386 -T linker.ld kernel.o loader.o
ld: PE operations on non PE file.
On the upside, now I'm getting somewhere.
ld --oformat elf32-i386 -T linker.ld kernel.o loader.o
And end up getting:
Administrator@al /cygdrive/g/Assembly/Operating System/OmnOS/code/kernel
$ ld --oformat elf32-i386 -T linker.ld kernel.o loader.o
ld: PE operations on non PE file.
On the upside, now I'm getting somewhere.
Re:Writing a GRUB kernel
Classic sign that you are using the default toolset that comes with Cygwin. Follow the cross-compiler section of the Wiki to get rid of that error.TheUnbeliever wrote: $ ld --oformat elf32-i386 -T linker.ld kernel.o loader.o
ld: PE operations on non PE file.
If you want a slightly more useful barebones kernel then just grab the GRUB sourcecode from your favourite GNU mirror. You'll find an example kernel in that package written in GAS assembly and C (IIRC it shows a limited printf and how to access the memory map GRUB provides).
Re:Writing a GRUB kernel
>>>You should check that ld is linking to a ELF exacutable and not windows PE or something else obscure.
GRUB gives special treatment to ELF (a HUGE design mistake, IMHO), but it will load a kernel in any file format.
>>>Also, get a copy of 'chkmb' (should come with grub source).
Unless they've changed the name, it's called "mbchk". Very useful! There's a slightly modified version here:
http://my.execpc.com/~geezer/osd/code/boot/grub/
>>$ ld --oformat elf32-i386 -T linker.ld kernel.o loader.o
>>ld: PE operations on non PE file.
>>
>Classic sign that you are using the default toolset that
>comes with Cygwin. Follow the cross-compiler section of the
>Wiki to get rid of that error.
I wish the FAQ would not steer the newcomers toward re-compiling GCC, because that's the difficult solution. The simple solution is to add the "kludge" fields to the Multiboot header. Then GRUB will load your kernel whatever it's format (yes, even PE .EXE)
Here are some other Multiboot kernels. They should all build and run correctly with DJGPP (COFF), MinGW/CygWin (Win32 PE COFF), or GCC for Linux (ELF).
http://my.execpc.com/~geezer/os/boreal11.zip
http://my.execpc.com/~geezer/osd/code/osd3/
http://my.execpc.com/~geezer/osd/code/osd4/
http://my.execpc.com/~geezer/osd/code/osd5/
http://my.execpc.com/~geezer/osd/code/
http://my.execpc.com/~geezer/temp/mbtest.zip
GRUB gives special treatment to ELF (a HUGE design mistake, IMHO), but it will load a kernel in any file format.
>>>Also, get a copy of 'chkmb' (should come with grub source).
Unless they've changed the name, it's called "mbchk". Very useful! There's a slightly modified version here:
http://my.execpc.com/~geezer/osd/code/boot/grub/
>>$ ld --oformat elf32-i386 -T linker.ld kernel.o loader.o
>>ld: PE operations on non PE file.
>>
>Classic sign that you are using the default toolset that
>comes with Cygwin. Follow the cross-compiler section of the
>Wiki to get rid of that error.
I wish the FAQ would not steer the newcomers toward re-compiling GCC, because that's the difficult solution. The simple solution is to add the "kludge" fields to the Multiboot header. Then GRUB will load your kernel whatever it's format (yes, even PE .EXE)
Here are some other Multiboot kernels. They should all build and run correctly with DJGPP (COFF), MinGW/CygWin (Win32 PE COFF), or GCC for Linux (ELF).
http://my.execpc.com/~geezer/os/boreal11.zip
http://my.execpc.com/~geezer/osd/code/osd3/
http://my.execpc.com/~geezer/osd/code/osd4/
http://my.execpc.com/~geezer/osd/code/osd5/
http://my.execpc.com/~geezer/osd/code/
http://my.execpc.com/~geezer/temp/mbtest.zip
Re:Writing a GRUB kernel
It was me who did the "steering" there, and for a reason: The cross-compiler not only resolves this one problem, but many others too which had been reappearing every other day on the forum (PE operation on non-PE files, alloca(), people including standard headers from their host OS) - most of which haven't been seen around the forum for quite some time as a result.Chris Giese wrote:
I wish the FAQ would not steer the newcomers toward re-compiling GCC, because that's the difficult solution. The simple solution is to add the "kludge" fields to the Multiboot header. Then GRUB will load your kernel whatever it's format (yes, even PE .EXE)
Starting with a cross-compiler also is the first step towards a logical conclusion (bootstrapping a GCC running on your new OS natively), and once I'm done with PDCLib, I'll happily add instructions on how to add it to the cross-compiler environment.
I still think it's a good, clean, and not at all that difficult a solution. (Since once you got to that page, you have a step-by-step how-to right in front of you.) But I'd be happy to hear a different approach that would serve both Linux and Cygwin users, and resolves all the aforementioned problems.
Every good solution is obvious once you've found it.
Re:Writing a GRUB kernel
Is it possible to compile the cross-compiler in Cygwin, or will I need to move this lot completely to a *nix machine?
Re:Writing a GRUB kernel
http://users.skynet.be/towp/cygwin-elf.rar
If you want to compile gcc and the binutils yourself, get bison and flex for cygwin. For the rest follow the steps in the faq.
If you want to compile gcc and the binutils yourself, get bison and flex for cygwin. For the rest follow the steps in the faq.
Re:Writing a GRUB kernel
...and since flex and bison are both available from the Cygwin setup, it's a piece of cake even without having a Unix machine.
Actually I am surprised - I thought it was possible to build the cross-compiler with nothing but the basic Cygwin setup. Once I confirmed this at home, I'll extend the Wiki with a hint as to these dependencies.
Actually I am surprised - I thought it was possible to build the cross-compiler with nothing but the basic Cygwin setup. Once I confirmed this at home, I'll extend the Wiki with a hint as to these dependencies.
Every good solution is obvious once you've found it.
Re:Writing a GRUB kernel
actually...if else fails, don't use cygwin
Use Linux and company
Use Linux and company
Re:Writing a GRUB kernel
Which is fine, except that I need to use Microsoft applications. I'm a hearty supporter of Linux/GNU and open source, but I dislike people that say to use Linux just because it isn't Microsoft. I'm building a Linux machine for use, have plenty of shell accounts, but my main computers will be Microsoft for the foreseeable future.DruG5t0r3 wrote: actually...if else fails, don't use cygwin
Use Linux and company
Re:Writing a GRUB kernel
I'm thankfull for how red hat comes with it precompiled. ;D
Back on topic: does anybody know a solution to my problem, or is it GRUBs fault/my faulty code?
Back on topic: does anybody know a solution to my problem, or is it GRUBs fault/my faulty code?
Re:Writing a GRUB kernel
Solar wrote:
>I still think it's a good, clean, and not at all that difficult a solution.
I respectfully disagree. Rebuilding GCC would be difficult for me...especially the part where I download the GCC sources over dial-up :) (Or can you buy CygWin binaries and source on a CD-ROM?)
On the other hand, with a modest amount of Cygwin- or DJGPP- or ELF-specific "glue" in the libraries and makefiles, you can use your existing MinGW, CygWin, or DJGPP installation for OS dev.
PS - how big is a CygWin installation that contains everything you need to re-compile GCC? (gcc, binutils, Make, m4, bash, fileutils, shellutils, etc.)
Dragon_Hilord wrote:
>Back on topic: does anybody know a solution to my problem, or is it GRUBs fault/my faulty code?
Executive summary for building a Multiboot kernel with unmodified MinGW/CygWin:
- Add this dummy function to the C source: void __main(void) { }
- Make sure the Multiboot header contains correct "aout kludge" fields
- Align sections on 4096-byte boundaries in the linker script
- gcc -fno-leading-underscore -mno-stack-arg-probe -nostdinc -fno-builtin ...
- ld --file-align=4096 --image-base=0 ...
- Don't use 'strip'
- Run 'mbchk' on the kernel before trying to boot it
>I still think it's a good, clean, and not at all that difficult a solution.
I respectfully disagree. Rebuilding GCC would be difficult for me...especially the part where I download the GCC sources over dial-up :) (Or can you buy CygWin binaries and source on a CD-ROM?)
On the other hand, with a modest amount of Cygwin- or DJGPP- or ELF-specific "glue" in the libraries and makefiles, you can use your existing MinGW, CygWin, or DJGPP installation for OS dev.
PS - how big is a CygWin installation that contains everything you need to re-compile GCC? (gcc, binutils, Make, m4, bash, fileutils, shellutils, etc.)
Dragon_Hilord wrote:
>Back on topic: does anybody know a solution to my problem, or is it GRUBs fault/my faulty code?
Executive summary for building a Multiboot kernel with unmodified MinGW/CygWin:
- Add this dummy function to the C source: void __main(void) { }
- Make sure the Multiboot header contains correct "aout kludge" fields
- Align sections on 4096-byte boundaries in the linker script
- gcc -fno-leading-underscore -mno-stack-arg-probe -nostdinc -fno-builtin ...
- ld --file-align=4096 --image-base=0 ...
- Don't use 'strip'
- Run 'mbchk' on the kernel before trying to boot it
Re:Writing a GRUB kernel
He... ouch. I admit I have stopped thinking in 56k a long time ago. :-[ I think I have to post-it that as a reminder to my screen, because I do tend to forget that not everyone has DSL or better.Chris Giese wrote: Solar wrote:
>I still think it's a good, clean, and not at all that difficult a solution.
I respectfully disagree. Rebuilding GCC would be difficult for me...especially the part where I download the GCC sources over dial-up
Cygwin is not (any longer) for commercial sale. But it shouldn't be that much of a problem to find someone with broadband access and a CD-R to get you the 33 MB of sources that are GCC and binutils - and you would have to have some kind of a compiler environment to start with anyhow, no?(Or can you buy CygWin binaries and source on a CD-ROM?)
I'm not sure whether you are talking about a hypothetical case, or yourself. In the latter case, I'd be quite willing to send you a CD-R with Cygwin and necessary sources...
I'd daresay that, in the process of doing OS development, you will be doing so much downloading of documentation, web research, forum posting etc. etc. that you won't really feel those thirty-something MB in the long run. You are right, there are valid reasons not to go for a cross-GCC - but in that case you're pretty much borked when it comes to OS dev'ing, anyway IMHO.On the other hand, with a modest amount of Cygwin- or DJGPP- or ELF-specific "glue" in the libraries and makefiles, you can use your existing MinGW, CygWin, or DJGPP installation for OS dev.
You will probably laugh your head off when I tell you that I can't check that before Monday. The only Windows machines I still have access to are in the office, my home is 100% Gentoo Linux by now...PS - how big is a CygWin installation that contains everything you need to re-compile GCC? (gcc, binutils, Make, m4, bash, fileutils, shellutils, etc.)
Every good solution is obvious once you've found it.