Mojtaba Kamyabi
Mojtaba Kamyabi

Reputation: 3600

How to compile a simple operating system and make it bootable?

I'm interested in operating system concepts, so I downloaded the Hello world OS. I'd like to know how to compile and link the code and make a bootable image. I'm using an old version of Cygwin on Windows (Cygwin-b20) from 1999.

My code for main.c is:

#include "bloader.h"

int main();

unsigned int oldEBP;
struct boot_dir *viewableDirectory;
int totalMem;
char * passedParams;

void _start(int memSize, char *parms, struct boot_dir *loadedfiles)
{
    asm("mov %%ebp, %0":"=m"(oldEBP));
    viewableDirectory = loadedfiles; /*make file mem locations global*/
    totalMem = memSize; /*make mem of system global*/
    passedParams = parms; /*make paramaters passed to system global*/
    main();

    asm("hlt");     /* this halts the machine, solving the problem of triple-faults on 
                        some machines, but also making it impossible to return to DOS */
}

int main()
{
    char *vidmem = (char *) 0xb8000;

    /* "Hello " */
    vidmem[0] = 'H';
    vidmem[1] = 0x7;
    vidmem[2] = 'e';
    vidmem[3] = 0x7;
    vidmem[4] = 'l';
    vidmem[5] = 0x7;
    vidmem[6] = 'l';
    vidmem[7] = 0x7;
    vidmem[8] = 'o';
    vidmem[9] = 0x7;
    vidmem[10] = ' ';
    vidmem[11] = 0x7;

    /* "World " */
    vidmem[12] = 'W';
    vidmem[13] = 0x7;
    vidmem[14] = 'o';
    vidmem[15] = 0x7;
    vidmem[16] = 'r';
    vidmem[17] = 0x7;
    vidmem[18] = 'l';
    vidmem[19] = 0x7;
    vidmem[20] = 'd';
    vidmem[21] = 0x7;
    vidmem[22] = ' ';
    vidmem[23] = 0x7;

    /* "OS" */
    vidmem[24] = 'O';
    vidmem[25] = 0x7;
    vidmem[26] = 'S';
    vidmem[27] = 0x7;

    return 0;
}

I'm interested in instructions to:

  1. Create ISO file after compile and run it with VMware (or other virtual machines)
  2. Run this code after compiling in real machine when system boots and how to add it to bootloader (for example GRUB) entry

Upvotes: 2

Views: 1816

Answers (1)

user3692106
user3692106

Reputation: 11

Make an empty file and give it the extension .vmdk

open it up in your favorite hex editor

and place your compiled boot loader code

its exactly the same as a hard drive

also the last 2 bytes in the sector must be 55 and AA

your bootloader code starts from 00

it will look something like this

Sector    Offset    Hex Values                                           Ascii
x00000000 x000      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00      
          x010      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00       
          x020      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  
          x030      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
          x040      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
          x050      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
          x060      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
          x070      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
          x080      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
          x090      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
          x0A0      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
          x0B0      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
          x0C0      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
          x0D0      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
          x0E0      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
          x0F0      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
          -------------
          x1F0      00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA

here is a guide to make run a boot loader

http://www.codeproject.com/Articles/36907/How-to-develop-your-own-Boot-Loader#_Toc231383191

Upvotes: 2

Related Questions