Bootloaders in c?

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.
Post Reply
RobertH
Posts: 12
Joined: Sun Feb 15, 2015 12:11 pm

Bootloaders in c?

Post by RobertH »

Im starting to write a bootloader for my operating system and I have a couple of questions. If I write the bootloader in c does it still need to have 2 stages to pass the 512mb mark? Also does anyone have any documentation on writing a bootloader in c? Thanks in advance!
Techel
Member
Member
Posts: 215
Joined: Fri Jan 30, 2015 4:57 pm
Location: Germany
Contact:

Re: Bootloaders in c?

Post by Techel »

Since your bootloader can only be 510 bytes in size and you need much hardware depend stuff you should write it in asm.
RobertH
Posts: 12
Joined: Sun Feb 15, 2015 12:11 pm

Re: Bootloaders in c?

Post by RobertH »

Roflo wrote:Since your bootloader can only be 510 bytes in size and you need much hardware depend stuff you should write it in asm.
I have written a multistage one in asm but I looked into how other loaders such as grub worked and relized they were written in c and asm.
Coomer69
Member
Member
Posts: 31
Joined: Thu Feb 20, 2014 4:49 am

Re: Bootloaders in c?

Post by Coomer69 »

I have written a multistage one in asm but I looked into how other loaders such as grub worked and relized they were written in c and asm.
You could have an assembly boot sector and a second stage file written in C and assembly. If you extracted the code and data from the executable file you could have the boot sector load stage 2 from fixed sectors on the disk and execute the code without the need to parse an ELF/PE/similar file.
User avatar
Muazzam
Member
Member
Posts: 543
Joined: Mon Jun 16, 2014 5:59 am
Location: Shahpur, Layyah, Pakistan

Re: Bootloaders in c?

Post by Muazzam »

Boot loaders are usually 16-bit, they call BIOS functions and some time they have need to reallocate themselves. You'll need 16-bit compiler and you'll need to embed Assembly within C. You are still limited to 512-byte whatever language you use as BIOS loads the the first sector from the disk (1 sector = 512 bytes (usually)). Here I am talking about BIOS. Using UEFI can simplify these things. As far as I know, it loads the file (not 512-bytes), and not in 16-bit.
RobertH
Posts: 12
Joined: Sun Feb 15, 2015 12:11 pm

Re: Bootloaders in c?

Post by RobertH »

SapphireBeauty wrote:
I have written a multistage one in asm but I looked into how other loaders such as grub worked and relized they were written in c and asm.
You could have an assembly boot sector and a second stage file written in C and assembly. If you extracted the code and data from the executable file you could have the boot sector load stage 2 from fixed sectors on the disk and execute the code without the need to parse an ELF/PE/similar file.
Alright Ill give it a shot thanks for the help!
Coomer69
Member
Member
Posts: 31
Joined: Thu Feb 20, 2014 4:49 am

Re: Bootloaders in c?

Post by Coomer69 »

RobertH wrote:
SapphireBeauty wrote: You could have an assembly boot sector and a second stage file written in C and assembly. If you extracted the code and data from the executable file you could have the boot sector load stage 2 from fixed sectors on the disk and execute the code without the need to parse an ELF/PE/similar file.
Alright Ill give it a shot thanks for the help!
Oh, and if you want to use 32-bit C code you will need to set up protected mode in your stage 2 file in an assembly bootstrap before you jump to the C code. The 32-bit code can then either read disks by returning to real mode or use ATA PIO mode
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Bootloaders in c?

Post by Brendan »

Hi,
RobertH wrote:Im starting to write a bootloader for my operating system and I have a couple of questions. If I write the bootloader in c does it still need to have 2 stages to pass the 512mb mark?
No.

For an example; my current bootloader is about 12 KiB. Code in the first 512 bytes of the file loads the next 512 bytes of the file, which loads the remainder of the file. The code in the remainder of the file mostly runs in 32-bit protected mode with paging (and with IRQs enabled), and includes a "BIOS wrapper" to do that. The BIOS wrapper is just a protected mode IDT with interrupt handlers that switch to real mode, pass control to the BIOS interrupt handler, then (when the BIOS interrupt handler returns) switches back to protected mode before doing "IRET".

I'm using assembly; but there's no reason you can't use C for the 32-bit protected mode parts. You would still need to do the BIOS wrapper and the "first sector loads second sector which loads the remainder" part with assembly; but that doesn't add up to much and the majority of the code could easily be C, sort of.

The other problem is that the BIOS functions don't use C calling conventions. This means that (even though the BIOS wrapper I mentioned above can allow you to do "int 0x13" in protected mode) you'd still need "C calling convention" assembly wrappers for the BIOS functions.

Taking that into account; you'd really have to consider how much is left that can be done in C. If your boot loader is a minimal thing that doesn't do much (e.g. loads a "stage 2" and nothing else) then you'd have almost nothing that can be done in C and it'd be a waste of time. If your boot loader does more (e.g. gets a full memory map and pounds it into a sane format, initialises a memory manager, supports a file system, decompresses whatever file/s it loads, checks checksums to ensure the file/s aren't corrupt, parses EDID and intelligently decides which video mode to use, etc) then you could easily end up with 90% of the code in C if you wanted.


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.
RobertH
Posts: 12
Joined: Sun Feb 15, 2015 12:11 pm

Re: Bootloaders in c?

Post by RobertH »

Brendan wrote:Hi,
RobertH wrote:Im starting to write a bootloader for my operating system and I have a couple of questions. If I write the bootloader in c does it still need to have 2 stages to pass the 512mb mark?
No.

For an example; my current bootloader is about 12 KiB. Code in the first 512 bytes of the file loads the next 512 bytes of the file, which loads the remainder of the file. The code in the remainder of the file mostly runs in 32-bit protected mode with paging (and with IRQs enabled), and includes a "BIOS wrapper" to do that. The BIOS wrapper is just a protected mode IDT with interrupt handlers that switch to real mode, pass control to the BIOS interrupt handler, then (when the BIOS interrupt handler returns) switches back to protected mode before doing "IRET".

I'm using assembly; but there's no reason you can't use C for the 32-bit protected mode parts. You would still need to do the BIOS wrapper and the "first sector loads second sector which loads the remainder" part with assembly; but that doesn't add up to much and the majority of the code could easily be C, sort of.

The other problem is that the BIOS functions don't use C calling conventions. This means that (even though the BIOS wrapper I mentioned above can allow you to do "int 0x13" in protected mode) you'd still need "C calling convention" assembly wrappers for the BIOS functions.

Taking that into account; you'd really have to consider how much is left that can be done in C. If your boot loader is a minimal thing that doesn't do much (e.g. loads a "stage 2" and nothing else) then you'd have almost nothing that can be done in C and it'd be a waste of time. If your boot loader does more (e.g. gets a full memory map and pounds it into a sane format, initialises a memory manager, supports a file system, decompresses whatever file/s it loads, checks checksums to ensure the file/s aren't corrupt, parses EDID and intelligently decides which video mode to use, etc) then you could easily end up with 90% of the code in C if you wanted.


Cheers,

Brendan
Interesting, whats the reason bootloaders such as grub use c? Is it just because it does more then just the minimums or is there another reason that has to do with efficency?
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: Bootloaders in c?

Post by Roman »

GRUB uses C to have some code portable.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
RobertH
Posts: 12
Joined: Sun Feb 15, 2015 12:11 pm

Re: Bootloaders in c?

Post by RobertH »

Roman wrote:GRUB uses C to have some code portable.
Oh ok thank you very much
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: Bootloaders in c?

Post by Kevin »

And because it's way too large to maintain it in assembly without losing your sanity.
Developer of tyndur - community OS of Lowlevel (German)
Post Reply