Page 1 of 1
Basics of a bootloader in c?
Posted: Thu Jul 09, 2015 11:18 pm
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!
Re: Basics of a bootloader in c?
Posted: Fri Jul 10, 2015 12:12 am
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?
Re: Basics of a bootloader in c?
Posted: Fri Jul 10, 2015 1:37 am
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.
Re: Basics of a bootloader in c?
Posted: Fri Jul 10, 2015 3:10 pm
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.
Re: Basics of a bootloader in c?
Posted: Fri Jul 10, 2015 3:11 pm
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.
Re: Basics of a bootloader in c?
Posted: Fri Jul 10, 2015 7:59 pm
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
Re: Basics of a bootloader in c?
Posted: Sat Jul 11, 2015 6:45 am
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
Re: Basics of a bootloader in c?
Posted: Mon Jul 13, 2015 5:51 am
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
Re: Basics of a bootloader in c?
Posted: Mon Aug 31, 2015 12:05 am
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.
Re: Basics of a bootloader in c?
Posted: Mon Aug 31, 2015 6:51 pm
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.