create a own operating system
What is an OS ?
Operating system, program that manages a computer’s resources, especially the allocation of those resources among other programs. Typical resources include the central processing unit, computer memory, file storage, input/output devices, and network connections.
I will put all implement steps in series of articles it’s helps you easy to understand the steps and you can easily develop your very own OS.
#1- Load OS in Bootloader
1. Tools
1.1 Quick setup
Once Ubuntu is installed, either physical or virtual, the following packages should be installed using apt-get
:
sudo apt-get install build-essential nasm genisoimage bochs bochs-sdl
1.2 Programming Languages
The operating system will be developed using the C programming language, using GCC. We use C because developing an OS requires very precise control of the generated code and direct memory access.
The code will make use of one type attribute that is specific for GCC:
__attribute__((packed))
2. Booting
Booting an operating system consists of transferring control along a chain of small programs, each one more “powerful” than the previous one, where the operating system is the last “program”. See the following figure for an example of the boot process:
2.1 BIOS
Basic Input Output System (BIOS) stored on a read-only memory chip on the motherboard of the computer. The task of this program is to start the computer system after its power on. It will load some basic function instructions for hardware. And then it will transfer control to the bootloader.
2.2 Bootloader
This program will transfer control to us, the operating system. This program has two parts. The first part of will transfer control to the second part and that gives control of the PC to the operating system.
In this development process, we use an existing bootloader (GNU Grand Unified Bootloader-GRUB) because writing it from the beginning could complex. This GRUB will load OS into a correct memory location. Then the control will transfer to the Operating System.
2.3 The Operating System
GRUB will transfer control to the operating system by jumping to a position in memory. Before the jump, GRUB will look for a magic number to ensure that it is actually jumping to an OS and not some random code. This magic number is part of the multiboot specification which GRUB adheres to. Once GRUB has made the jump, the OS has full control of the computer.
3. Implement OS
This section will describe how to implement of the smallest possible OS that can be used together with GRUB. The only thing the OS will do is write 0xCAFEBABE
to the eax
register .
3.1 Compiling the Operating System
First create a folder as your OS name, Now open your favorite text editor, and paste the following code, and save it as loader.s
:
global loader ; the entry symbol for ELF
MAGIC_NUMBER equ 0x1BADB002 ; define the magic number constant
FLAGS equ 0x0 ; multiboot flags
CHECKSUM equ -MAGIC_NUMBER ; calculate the checksum
; (magic number + checksum + flags should equal 0)
section .text: ; start of the text (code) section
align 4 ; the code must be 4 byte aligned
dd MAGIC_NUMBER ; write the magic number to the machine code,
dd FLAGS ; the flags,
dd CHECKSUM ; and the checksum
loader: ; the loader label (defined as entry point in linker script)
mov eax, 0xCAFEBABE ; place the number 0xCAFEBABE in the register eax
.loop:
jmp .loop ; loop forever
04. Compile assembly code into object file with following command
nasm -f elf32 loader.s
05.Create a linker script called link.ld
ENTRY(loader) /* the name of the entry label */
SECTIONS {
. = 0x00100000; /* the code should be loaded at 1 MB */
.text ALIGN (0x1000) : /* align at 4 KB */
{
*(.text) /* all text sections from all files */
}
.rodata ALIGN (0x1000) : /* align at 4 KB */
{
*(.rodata*) /* all read-only data sections from all files */
}
.data ALIGN (0x1000) : /* align at 4 KB */
{
*(.data) /* all data sections from all files */
}
.bss ALIGN (0x1000) : /* align at 4 KB */
{
*(COMMON) /* all COMMON sections from all files */
*(.bss) /* all bss sections from all files */
}
}
06. Generate a executable file called kernel.elf using loader.o and link.ld with following command
ld -T link.ld -melf_i386 loader.o -o kernel.elf
07.Building an ISO Image
We will create the kernel ISO image with the program genisoimage
. A folder must first be created that contains the files that will be on the ISO image. The following commands create the folder and copy the files to their correct places:(open your terminal in current folder location)
mkdir -p iso/boot/grub # create the folder structure
cp stage2_eltorito iso/boot/grub/ # copy the bootloader
cp kernel.elf iso/boot/ # copy the kernel
08.We will create an ISO image usinge “genisoimage” command
genisoimage -R \
-b boot/grub/stage2_eltorito \
-no-emul-boot \
-boot-load-size 4 \
-A os \
-input-charset utf8 \
-quiet \
-boot-info-table \
-o os.iso \
isoFor more information about the flags used in the command, see the manual for genisoimage.The ISO image os.iso (your_title.iso)now contains the kernel executable, the GRUB bootloader, and the configuration file.
09.Running bochs
megs: 32
display_library: sdl
romimage: file=/usr/share/bochs/BIOS-bochs-latest
vgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest
ata0-master: type=cdrom, path=os.iso, status=inserted
boot: cdrom
log: bochslog.txt
clock: sync=realtime, time0=local
cpu: count=1, ips=1000000
Bochs needs a configuration file to start called bochsrc.txt :after save the file we can run Bochs with the following command: bochs -f bochsrc.txt -q
then you can type “c” in the terminal then hit enter.you can see the following window::::::
.After quitting Bochs, display the log produced by Boch:
cat bochslog.txt
if you find EAX=cafebabe in the logfile your os is running sucessfully.
my Github repository for this:::::::::::::::::::::::::::::>>>>>>>>