Basics of a bootloader in c?
Basics of a bootloader in c?
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?
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
- Alan Kay
Re: Basics of a bootloader in c?
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?
Ok thanks. I guess I should look more into the structs and typedef struct.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?
Re: Basics of a bootloader in c?
Ok Thanks for that tip.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.
Re: Basics of a bootloader in c?
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
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
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
Re: Basics of a bootloader in c?
Hi,
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
Why?RobertH wrote:I have written a bootloader for my os in asm but now I want to make one in c.
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.
- Combuster
- 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?
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
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?
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.
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.
-
- Member
- Posts: 52
- Joined: Mon Oct 11, 2010 11:37 pm
- Location: Milwaukee, Wisconsin
Re: Basics of a bootloader in c?
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.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
Microsoft is over if you want it.