2

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
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Mojtaba Kamyabi
  • 3,440
  • 3
  • 29
  • 50
  • 2
    Did you try the Makefile provided by the website? It should output a `img` file. With this file you should be able to mount it inside a virtual machine software, or `dd` it to a USB key – z̫͋ May 30 '14 at 14:36
  • thanks but I get the following error when run `make`: `gcc -g -O -I. -c main.c -o main.o [sig] E:\softwares\cygnus\cygwin-b20\H-i586-cygwin32\bin\make.exe 1000 (0) call_ handler: couldn't get context of main thread, error 998` – Mojtaba Kamyabi May 30 '14 at 14:56
  • Did you try this? http://stackoverflow.com/questions/11817747/error-cygwin-gcc-that-is-unsupported-on-this-system – Ruud Helderman May 30 '14 at 15:10
  • After googling the error, it has something to do with your bash. You might want to try Msys, or maybe compile it on a *nix platform if you have access to one – z̫͋ May 30 '14 at 15:23
  • @Ruud yes.I installed gcc correctly from sygwin – Mojtaba Kamyabi May 30 '14 at 15:23
  • @z̫͋ thanks for your comment but I have a problem when make boot from `kernel.elf` file with `bootmaker` command: `error: cannot load kernel.elf` I google it but nothing found :( – mojibuntu May 30 '14 at 15:40

1 Answers1

2

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