Anyone tried to code their own bootloader ?

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.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Coldberg wrote:Wow awesome :) anyone ever tried PXE ?
It's on the massive todo list :)
indiocolifa
Member
Member
Posts: 41
Joined: Sat May 24, 2008 12:41 pm
Location: La Plata, Argentina

Re: Anyone tried to code their own bootloader ?

Post by indiocolifa »

I'm developing FAT12/FAT16/FAT32 code in my assembler bootloader stage 2. My question is how do you load your kernel after setting up a proper environment (A20/PMode/HW detection,etc). My kernel executable will be ELF, so I must write a minimal ELF loader that's it?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Anyone tried to code their own bootloader ?

Post by AJ »

Yes - pretty much. My stage 2 switches between PMode and RMode so that in effect it supports any device which is bootable by the BIOS, without coding separate drivers for PATA/FDD/SATA/ATAPI etc...

Cheers,
Adam
User avatar
Telgin
Member
Member
Posts: 72
Joined: Thu Dec 20, 2007 1:45 pm

Re: Anyone tried to code their own bootloader ?

Post by Telgin »

I'm in the process of writing one for my OS at the moment, actually.

Probably not the best project to learn x86 assembly on, that's for sure, but I've managed to get by so far. Having experience with MIPS assembly helps a little.

I've probably done plenty wrong in the code, but in 512 bytes I managed to get enough system information to load a second stage boot loader from an image of the filesystem I developed for my OS. I'm currently writing the second stage in real mode assembly as well, with the goal of it being able to load boot-time drivers (which I have certainly not written yet nor would the kernel actualyl look for such a thing for quite some time in the future) as well as the kernel before setting the requested VBE video mode and entering protected (64-bit) mode.

So far all it does is actually start the second stage and read a data table set up by the first stage to get drive geometry and volume information without having to do the BIOS call or file system parsing again.

Also, can I ask why using memory above 0x7FFF is bad? I'm pretty certain that my first stage loaded something starting at 0x8000 without any trouble, and that should be only half of the addressable memory. I'm concerned now though, it sounds like I've done something stupid that I shouldn't have... all of the intricacies of real mode x86 can be a little tiring.
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: Anyone tried to code their own bootloader ?

Post by bewing »

You may have dropped an 'F' or '0' when you were being told what was bad. 0x8000 is fine. 0x80000 (=512K) is bad -- unless you use INT12h to make sure that you have some available memory between 0x80000 and 0xA0000.
indiocolifa
Member
Member
Posts: 41
Joined: Sat May 24, 2008 12:41 pm
Location: La Plata, Argentina

Re: Anyone tried to code their own bootloader ?

Post by indiocolifa »

I'm thinking of my "ELF" kernel decision... mmm.. Do I need the kernel to be in an executable format such as ELF? I don't know if there is an advantage of using that at this point, may be it's better to output a flat binary C kernel and load it at the physical address I want (2 MB).
User avatar
Telgin
Member
Member
Posts: 72
Joined: Thu Dec 20, 2007 1:45 pm

Re: Anyone tried to code their own bootloader ?

Post by Telgin »

bewing wrote:You may have dropped an 'F' or '0' when you were being told what was bad. 0x8000 is fine. 0x80000 (=512K) is bad -- unless you use INT12h to make sure that you have some available memory between 0x80000 and 0xA0000.
That's reassuring. Thought I wasn't crazy. :)
I'm thinking of my "ELF" kernel decision... mmm.. Do I need the kernel to be in an executable format such as ELF? I don't know if there is an advantage of using that at this point, may be it's better to output a flat binary C kernel and load it at the physical address I want (2 MB).
Unless you need to take advantage of something offered by an executable format such as exporting functions or code relocation. I suppose if you know where the code is going to be in memory and originate it there from the assembler, it should work fine.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Anyone tried to code their own bootloader ?

Post by AJ »

Another good reason to use ELF is size on disk - especially if you have a large BSS. Also, where a flat binary has to pad between aligned sections an ELF simply specifies the "memsz" of a given section.

For example, if your code section ends at 0x1001 and your sections are all page aligned, your data section may start at 0x2000. In a flat binary, 0x1002-0x1FFF will be zero-padding. In an elf, you do not have 0x0FFD bytes of superfluous data sitting on disk (which has to be loaded via a slow disk driver, rather than quickly set in memory).

Cheers,
Adam
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Re: Anyone tried to code their own bootloader ?

Post by inflater »

In the "PortixOS 0.4" days I've used a small bootloader that would set unreal mode, since I couldn't do that in inline assembly of Borland pascal. It could be done using external compilation (TASM etc) and then linkage to the whole kernel, but I've used this method only for loading fonts. :)
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Anyone tried to code their own bootloader ?

Post by Troy Martin »

I'm using one based off of the MikeOS bootloader for my OS (which is based off of MikeOS as well :P).

I mainly write my own. The last OS project I did didn't get too far, and I don't have anything but the bootloader anymore.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
naiksidd_85
Member
Member
Posts: 76
Joined: Thu Jan 17, 2008 1:15 am

Re: Anyone tried to code their own bootloader ?

Post by naiksidd_85 »

I started writing bootloader, but with my knowledge and the requirement it started becoming complex and time consuming.
So i started using GRUB insted.
But yes this on my list once am through with some basic things.
may be the next version.. (as if I am going to publish any :D )
Learning a lot these days THANKS to OSdev users
User avatar
AndrewAPrice
Member
Member
Posts: 2327
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: Anyone tried to code their own bootloader ?

Post by AndrewAPrice »

I want to know what happened to that guy who was writing his boot loader in C. I'd like to see his code (but I think his OS was closed source), and know if he was successful or not.
My OS is Perception.
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Re: Anyone tried to code their own bootloader ?

Post by inflater »

Anyways I think a bootloader in C is possible (but not entirely in C though.) Just create a working C kernel with basic screen and disk I/O functions and voila. :)
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
User avatar
Masterkiller
Member
Member
Posts: 153
Joined: Sat May 05, 2007 6:20 pm

Re: Anyone tried to code their own bootloader ?

Post by Masterkiller »

I'm using my own bootloader since CD structure is a little bit different. It loads only binary files, but I'm currently working on parsing .ini files.
I'm still confused in what to do then. The GRUB finished loading of the image in protected-mode. I suppose it doesn't load any drivers. Then kernel must start the system in protected mode and load the drivers from disk (cd-rom in my case). How could kernel do that without cd-rom driver? So do I must first load the cdrom driver then the kernel or I should write a function for switching back to real-mode and use the BIOS functions.
ALCA OS: Project temporarity suspended!
Current state: real-mode kernel-FS reader...
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Anyone tried to code their own bootloader ?

Post by Brendan »

Hi,
Masterkiller wrote:The GRUB finished loading of the image in protected-mode. I suppose it doesn't load any drivers. Then kernel must start the system in protected mode and load the drivers from disk (cd-rom in my case). How could kernel do that without cd-rom driver? So do I must first load the cdrom driver then the kernel or I should write a function for switching back to real-mode and use the BIOS functions.
Don't let the terminology used in the GRUB/multi-boot documentation confuse you. GRUB loads a file that it tries to execute (that they call "the kernel") and (optionally) several other files. There's no reason why the initial executable file can't be something like a second stage, and no reason why the other files can't be kernel/s, device drivers and/or a boot image (containing lots of stuff).

Also note that the multi-boot specification clearly states that the boot loader (GRUB) must leave the "boot environment" in a working state, which means you can disable protected mode (return to real mode) and use the BIOS, etc (then enable protected mode again when you actually want protected mode). Basically the multi-boot specification doesn't allow a boot loader (e.g. GRUB) to stuff up things like the BIOS's IVT, data area, extended data area, PIC settings, etc.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply