Saturday, August 13, 2011

OSDEV Series - Episode 1.4: Short recap

In this episode, we set up a proper environment for Windows. Unlike Linux, where things are much easier, on Windows we have to build ourselves the tools that we have to use.

Download list:

Creating GRUB floppy image
Same as under Linux (Episode 1.3), except that we use Virtual Floppy Drive (VFD) instead of mount.

Building binutils and GCC
Step 1: System requirements
Make sure you have enough disk space (my installation of Cygwin had 1.6 GB after building and installing everything), and enough RAM memory (at least 512 MB).


Step 2: Download necessary tools
There are some additional tools you need for the build. To install them, you need to run the Cygwin installation. If you deleted the file, you can download it here. Install the following packages:
  • Devel > bison
  • Devel > flex
  • Devel > gcc
  • Devel > gcc-core
  • Devel > gcc-g++
  • Devel > make
  • Libs > libgmp-devel 
  • Libs > libmpfr-devel 
  • Libs > libmpc-devel

Step 3: Remove other GCC ports
If you have MinGW or DJGPP installed, you will need to rename the folder they are located in. For example, you can add an underscore at the end of the folder name. This way, GCC and binutils won't find them, and won't use them. If you don't, the build could fail.


Step 4: Download the source code
Use the links provided above to download binutils and GCC source code. Unpack them in the /usr/src folder, this is usually found in [cygwin_installation]\usr\src (default is C:\cygwin\usr\src). You should use 7zip, WinRar, or the tar command in cygwin to do this, otherwise the build could fail.



Step 5: Preparation
Launch Cygwin, and type the following commands:

export PREFIX=/usr/local/cross
export TARGET=i586-elf
cd /usr/src
mkdir build-binutils build-gcc

The first two commands will create two environment variables. PREFIX is where binutils and GCC will be installed, and TARGET is what platform they will create executables for.
Then we create two directories in /usr/src, which will be used to build these tools.


Step 6: Build and install binutils
Type the following commands:

cd /usr/src/build-binutils
../binutils-x.xx/configure --target=$TARGET --prefix=$PREFIX --disable-nls
make all
make install

Replace x.xx with the version number (you can press TAB to autocomplete in Cygwin).
So we configure binutils, build it, and then install it.


Step 7: Build and install GCC
Type the following commands:

cd /usr/src/build-gcc
export PATH=$PATH:$PREFIX/bin
../gcc-x.x.x/configure --target=$TARGET --prefix=$PREFIX --disable-nls --enable-languages=c,c++ --without-headers
make all-gcc
make install-gcc

Replace x.x.x with the version number (you can press TAB to autocomplete).
Now we append to the PATH environment variable (which already exists) the location where binutils was installed.
Then, configure gcc, build and install it.


Step 8: Build and install libgcc
As a useful addition, you may also like to build libgcc, the GCC low-level runtime library. Linking against libgcc provides integer, floating point, decimal, stack unwinding (useful for exception handling) and other support functions. Once you have built and installed the GCC Cross-Compiler, keep Cygwin opened, and type:

make all-target-libgcc
make install-target-libgcc


Step 9: Edit PATH environment variable
Right click on My Computer, and go to Properties. If you have Windows 7, look for where it says "Computer name". There should be a link there which says Change Settings.
Go to the Advanced tab, and click Environment Variables. Look for the PATH variable, and edit it. Put a semicolon at the end, and add the following path:
[cygwin installation]\usr\local\cross\bin 
(default is C:\cygwin\usr\local\cross\bin)


Step 10: Cleanup
If you renamed any folder at Step 3, you may restore the original name. You can now delete the source code for GCC and binutils, as well as the build-gcc and build-binutils folders.

Troubleshooting: You can visit this link for more information on how to build binutils and GCC, as well as troubleshooting information.


Building our operating system
Now we can go back to our operating system and build it.

Open Cygwin, and create two folders:
mkdir /mnt
mkdir /mnt/floppy

Now edit build.sh:
Add two variables:
compiler=i586-elf-gcc
linker=i586-elf-ld

Now replace gcc and ld, with $compiler, respectively $linker. This way, if somehow the compiler or linker may change, we only have to edit one single line instead of a thousand.

Also, replace all the code that copies kernel.bin to the floppy image, with these two lines:

mount A: /mnt/floppy
cp kernel.bin /mnt/floppy/

You can save and close the file.


The last step is to open the myos.img image, using VFD, and launching the build.
To launch the build, navigate to your work directory (/cygdrive/[drive]/[path]), and execute ./build.sh

No comments:

Post a Comment