Thread: How to write a simple boot program in C ?

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    29

    How to write a simple boot program in C ?

    Hi guys. Can anyone show me a way with How to Write a program which can be executed at the time of Booting ?
    Simply I want to write a C program which I can use to display my Name at the Time of Booting before loading the main Operating System.
    Any Suggestions ?

    Thanks in advance

    OS: Windows 7

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    If you don't have the support of an Operating System, you are in freestanding zone.
    A freestanding C99 implementation is only required to support the functions and macros declared in <float.h>, <iso646.h>, <limits.h>, <stdarg.h>, <stdbool.h>, <stddef.h>, and <stdint.h>.

    You need a special way to display things: putchar(), printf() and similar functions will (probably) not work.
    I think you need direct access to the display device, but I don't know how to do that.

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    "Before loading the main operating system" - that means you'd need to install it in place of the bootloader, or chained from the bootloader, and then execute Windows afterwards.

    You might be able to get GRUB or similar to boot a program of your choice but you will have to know deep internals of the PC, be able to pass off to a further bootloader, not interfere with anything else, and have some deep knowledge of BIOS interrupt services and compile towards quite a low target (which may not even be feasible - but you *might* be able to target GCC to produce a raw output for a "real-mode" processor before the OS kicks it into 386+ protected mode.

    I could *probably* write something in C, or assembler, myself but the chances are I would never bother - so much restriction on what you can and can't do and you're basically writing the equivalent of an old DOS program without the benefit of DOS services around. Hardly any memory accessible, hardly any facilities available (I'm guessing you would have to call a BIOS interrupt service routine to clear the screen, print a string, and then more to try to pass control back to the "real" bootloader - Ralf Brown's Interrupt List will help you there, but you're almost certainly going to need to do assembly code calls), hardly any protections available, hardly any point.

    I'll just say now - technically possible. Practically so infeasible tricky that it probably won't ever happen just so you can display a name there using C.

    If you just want to put your name on the bootloader, use GRUB and the customisations available for it. I personally do it with TrueCrypt to warn people that the PC is encrypted so they don't bother to try to access it - it's just a custom message facility available on top of TrueCrypt / GRUB's (hugely complicated) bootloader. But writing it yourself in C? Go read up on the "toy OS" article, syslinux might be a good resource for quick chain bootloaders, etc. but you're really playing with the raw guts of the system and that means YOU have to do all the work.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Could someone please write a simple program for me?!
    By danjer242 in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2010, 10:43 PM
  2. Can any one write a simple C Program 4 this??
    By sriki in forum C Programming
    Replies: 30
    Last Post: 12-03-2007, 03:48 PM
  3. Idiot Trying To Write A Simple Program
    By verd in forum C Programming
    Replies: 4
    Last Post: 02-05-2005, 07:06 AM
  4. I want to write a simple program in C++ to...
    By Danhash in forum Windows Programming
    Replies: 3
    Last Post: 03-07-2003, 01:38 PM
  5. how to write simple C program?
    By edwardkys in forum C Programming
    Replies: 3
    Last Post: 11-22-2002, 05:50 PM