CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

[Setting up a quick+basic environment -> Linux/*BSD]


[Replies] [Reply] [View by Thread] [Help]
[Back To CAIS Operating System Research]

Posted by Delta Gen int16h On 2008-03-01 03:14:22

Delta GenDelta Gen
Delta Gen int16h


I write this post with the assumption that you know how to find and install development tools and other utilities on your UNIX-like OS, as writing up each step for every distro/OS at this stage would be a little pointless as anyone developing for OSR/CAROS should have a basic/good knowledge of their OS||distro and will have at least compiled applications before.

Step 1 - Initial Tool-chain

Install GCC, Binutils, nasm, etc - basically all the usual development tools and libraries needed to compile+link asm and C++ code.

Step 2 - Emulator Installation

Install QEMU (kqemu, the kernel module is optional). You may also use Bochs, VMWare, VirtualBox etc but I will be working with and covering QEMU at this stage.

Step 3 - Initial Tree

Create the following directory structure somewhere:
CAROS/
    ->  src/
    ->  mnt/
    ->  doc/
Step 4 - The Floppy image with GRUB

Download caros.img, and place it in the CAROS directory. This is a floppy disk image with GRUB pre-installed and setup to load the file /kernel from the first floppy drive. This is where we will copy the initial kernel builds to for testing.

Step 5 - Booting the image

We will write a tiny script to boot our floppy image, for now - we will execute qemu with the following options:
  • -M pc (Emulate a 'normal' x86 PC)
  • -fda caros.img ('Insert' caros.img into the 1st floppy drive)
  • -boot a (Instruct QEMU to boot from a:)
  • -m 32 (Allocate 32MB of RAM to our virtual machine)
  • -std-vga (For now we will use basic VGA graphics rather than the supplied Cirrus Logic virtual-chipset)
  • -serial file:debug.txt (This will output all data on the virtual serial port to debug.txt*)
(start.sh)
#!/bin/sh

qemu -M pc -fda caros.img -boot a -m 32 -std-vga -serial file:debug.txt
Step 6 - Transferring the kernel to floppy image

The easiest way to do this is to mount the floppy image through the
loopback device, copy kernel to the root of the image and unmount it.

We can write another little script to automate this process:

(install.sh)
#!/bin/sh

sudo mount -oloop caros.img mnt
sudo cp src/kernel /mnt/kernel
sudo umount caros.img
This will mount the image, copy the kernel to it and then unmount it :)

Step 7 - The Makefile

In the 'src' directory, create a new text file named 'Makefile'. This file will basically inform 'make' how to compile and link our kernel/'OS'.
SORES = bootstrap.o kernel.o
CFLAGS = -nostdlib -nostdinc -fno-builtin -fno-stack-protector
ASFLAGS = -felf
LDFLAGS = -Tlinker.ld

all: $(SORES) link

link:
        ld $(LDFLAGS) -o kernel $(SORES)

.so.o:
        nasm $(ASFLAGS) $<

clean:
        -rm *.o kernel
Let's explain each line of this Makefile:
  • SORES = ~ --> The objects which (g)cc will produce, and ld will link
  • CFLAGS = ~ --> Options passed to GCC
  • ---> The options we passed will prevent standard libraries and includes to be searched/included in our kernel, as well as prevent built-in functions from being included. These are needed when building an OS where libraries / headers from your development environment and many built-in functions won't be available (we will need to write these from scratch!).
  • ASFLAGS = ~ --> Options passed to the Assembler (NASM), we want an Elf file so set -felf.
  • LDFLAGS = ~ --> Linker (ld) options are given here, we will be using an external linker script so use the -T flag followed by out script name.
The rest of our Makefile is pretty self-explanitory and non-OSDev specific, it should be noted that the 8 spaces used for indentation should be replaced by TABs as 'make' expects them.

Step 8 - linker.ld

The linker (ld) script is quite important for this project, it will tell 'ld' how to structure our kernel binary. I had already wrote notes at the top of this file before writing this, so I will just paste the whole file here:

(linker.ld)
/*
This file is used by the linker (ld) so that it knows how to structure(link)
our kernel binary.  Our linker script informs ld the following:

- The entry of our kernel should be 'start'

- The .text section (where the actual code is in Elfs) should be the first
  section, and should start at 1MB (0x100000).

- The .data section is for initialised, static data - this should be placed
  after our .text section and is page-aligned (more on memory paging later).

- You will notice within our .data section that .rodata is present... this
  is used to store read-only/static data such as constants. In memory, data
  stored in .rodata will either go into the process' code segment or data
  segment. This is a feature mostly unique to Operating Systems such as Linux 
  and may be discussed in a document about the Elf executable format.

- The .bss section is for initialised, static data and will be placed after
  .data, also being page-aligned.

*/

ENTRY(start)
SECTIONS
{
  .text 0x100000 :
  {
    code = .; _code = .; __code = .;
    *(.text)
    . = ALIGN(4096);
  }

  .data :
  {
     data = .; _data = .; __data = .;
     *(.data)
     *(.rodata)
     . = ALIGN(4096);
  }

  .bss :
  {
    bss = .; _bss = .; __bss = .;
    *(.bss)
    . = ALIGN(4096);
  }

  end = .; _end = .; __end = .;
}
That's it for now, I'll post the code for a basic mult-boot bootstrap and kernel later ;)

* -serial can also be setup to use a network socket or other devices for debugging, but for now outputting to a text file is most useful.


Replies:


Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.01685 seconds


About Us | Privacy Policy | Mission Statement | Help