Sunday, May 8, 2011

OSDEV Series - Episode 1.3: Short recap

The boot process:
When you turn on your computer, several things happen:
1. All the hardware is powered
2. When power is good, the processor starts, executing firmware
3. The firmware (also known as the BIOS = Basic Input/Output System) runs system checks (POST = Power On Self Test)
4. The BIOS looks for bootable devices, such as diskettes, hard disks, or DVDs
5. When a bootable device is found, the first sector is loaded in memory, and executed.
Inside the first sector, there is a part of our operating system. Now the operating system has full control over the computer.


Components of an operating system:
1. Bootloader
Description: It is a small program, that is located in the boot sector.
Role: Does hardware detection, prepares the computer for operating mode, and executes the kernel. Some bootloaders also give the possibility of booting multiple kernels, and show a menu to the user.
Why it exists: The space in the boot sector is very limited. For example, on a floppy disk there are only 512 bytes.


2. Kernel
Description: The core of all operating systems.
Role: It does essential stuff, such as:
- Hardware input and output
- Memory management
- Makes executing programs possible

3. Programs
Special programs:
Drivers: have special privileges in comparison with normal programs.
Shell: comes with most operating systems. It is like an 'add-on' that allows user interaction.
Normal programs:
Examples: text editor, browser, videogames etc.



Setting up GRUB:
There are 2 floppy images needed:
1. myos.img - will contain the operating system
2. aux.img - auxiliary image, used to install GRUB on image #1.

The following commands will be executed in the Linux terminal, or Cygwin.


Step 1: Generating the images:
dd bs=512 count=2880 if=/dev/zero of=myos.img
dd bs=512 count=2880 if=/dev/zero of=aux.img

Step 2: Format and mount myos.img:
Attention: Linux users need administrator priviledges for the following instructions. This is done by placing sudo before the commands, in Debian based distributions.
Format the image: mkfs.msdos myos.img
Create a directory: mkdir /media/floppy1
Mount the image: mount -o loop myos.img /media/floppy1

Step 3: Create configuration file for GRUB
Create a new file called menu.cfg, and using your favorite text editor, write the following lines:

  default 0
  timeout 0
  hiddenmenu

  title MyOS
  root (fd0)
  kernel /kernel.bin
  boot


Step 4: Put all necessary files on the mounted floppy image
The floppy disk will contain the following file structure:
/boot/stage1
/boot/stage2
/boot/menu.cfg
kernel.bin


kernel.bin is the kernel of the operating system. We will create this file in the next episode.

Now we will copy all the files on the disk:
Create boot folder: mkdir /media/floppy1/boot
Copy stage files: cp stage? /media/floppy1/boot
Copy configuration file: cp menu.cfg /media/floppy1/boot

Step 5: Unmount the image
Unmount it: umount /media/floppy1
Delete folder: rm -r /media/floppy1

Step 6: Copy stage1 and stage2 directly inside the auxiliary image
stage1: dd bs=512 count=1 if=stage1 of=aux.img conv=notrunc
stage2: dd bs=512 seek=1 if=stage2 of=aux.img conv=notrunc

Step 7: Install GRUB on disk #1 using the auxiliary image
Using VirtualBox, we will start a virtual machine, with the auxiliary image inserted.
After the GRUB command line appears, insert myos.img, and type the following command:
install (fd0)/boot/stage1 (fd0) (fd0)/boot/stage2 (fd0)/boot/menu.cfg




Finally, I organized the files a little bit, and this is the new folder structure:
Boot/grub/aux.img
Boot/grub/menu.cfg

Boot/grub/stage1
Boot/grub/stage2
Boot/myos.img

OSDEV Series - Episode 1.3 - Babysteps

Section 1: Introduction
Episode 3: Babysteps




Covered topics in this episode:

Theory: the boot process, components of an operating system
Practice: setting up GRUB

In the next episode, we start writing our kernel, and we will learn more about kernel designs.