Basics of a bootloader 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

Basics of a bootloader in c?

Post by RobertH »

I have written a bootloader for my os in asm but now I want to make one in c. I have made a basic Hello World one in c but I am struggling to understand how to convert things such as the OEM Block into c code. I have been looking through some other peoples code about enabling A20 line and such but I am not understanding how to convert it over. Any advice or help would be great thanks!
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: Basics of a bootloader in c?

Post by Roman »

Hi. Just learn C. Use structs (or typedef struct {...} typename) for data structures. Use inline assembly or separate assembly files for things possible only with assembly. What's the problem?
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Basics of a bootloader in c?

Post by iansjack »

One stumbling block that you need to watch out for is to ensure that structs are packed. Otherwise you are likely to have problems when referring to OS/filesystem/network, etc. data structures.
RobertH
Posts: 12
Joined: Sun Feb 15, 2015 12:11 pm

Re: Basics of a bootloader in c?

Post by RobertH »

Roman wrote:Hi. Just learn C. Use structs (or typedef struct {...} typename) for data structures. Use inline assembly or separate assembly files for things possible only with assembly. What's the problem?
Ok thanks. I guess I should look more into the structs and typedef struct.
RobertH
Posts: 12
Joined: Sun Feb 15, 2015 12:11 pm

Re: Basics of a bootloader in c?

Post by RobertH »

iansjack wrote:One stumbling block that you need to watch out for is to ensure that structs are packed. Otherwise you are likely to have problems when referring to OS/filesystem/network, etc. data structures.
Ok Thanks for that tip.
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Basics of a bootloader in c?

Post by SpyderTL »

I was under the impression that you couldn't really write a boot loader in C.

I found this blog post talking about using c to make a boot loader. A lot of inline assembly and linker tricks...

http://dc0d32.blogspot.in/2010/06/real- ... iting.html
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Basics of a bootloader in c?

Post by Brendan »

Hi,
RobertH wrote:I have written a bootloader for my os in asm but now I want to make one in c.
Why?

It's not like you're going to be able to port an "80x86, PC BIOS" boot loader to ARM or PowerPC or Sparc; and most of it will be setting up segments and stack (which C can't do), calling BIOS functions (which C can't do), switching between real mode and protected/long mode (which C can't do), diddling with IO ports (which C can't do), 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.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Basics of a bootloader in c?

Post by Combuster »

That tutorial example of a bootloader in C is essentially still written in assembly, just hidden under a load of macros. In essence writing bootloader code in this style is both bigger, less legible and far more error prone.

Especially for the small piece that is the bootsector, it's questionable if you can actually say it's a bootloader in C rather than a bootloader compiled by a C compiler. It certainly is not if you ask the official C standard. Then again, some things have to be done just because we can :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
dc0d32
Member
Member
Posts: 69
Joined: Thu Jun 09, 2005 11:00 pm
Location: Right here

Re: Basics of a bootloader in c?

Post by dc0d32 »

I agree with Brendan and Combuster.

Yes, indeed a lot of inline assembly and linker play. It was more of a "Why? Because I can!" exercise. It certainly is neither the most elegant nor even a recommended solution.
CelestialMechanic
Member
Member
Posts: 52
Joined: Mon Oct 11, 2010 11:37 pm
Location: Milwaukee, Wisconsin

Re: Basics of a bootloader in c?

Post by CelestialMechanic »

Brendan wrote:[snip!]
It's not like you're going to be able to port an "80x86, PC BIOS" boot loader to ARM or PowerPC or Sparc; and most of it will be setting up segments and stack (which C can't do), calling BIOS functions (which C can't do), switching between real mode and protected/long mode (which C can't do), diddling with IO ports (which C can't do), etc. [snip!]

Brendan
Actually, some C compilers do provide functions for working with registers (in a structure), calling BIOS functions, and functions such as inpw() and outpb() and the like. BUT, this stuff is hardware specific and thus non-portable. The early phases of the boot procedure are highly architecture-dependent and I concur with Brendan and others that this code really ought to be written in the appropriate assembly from the first. There are no royal roads to OS development.
Microsoft is over if you want it.
Post Reply