Bootloader and Kernel development

advertisement
OS Dev
Step 1
Agenda:
Boot loader Development
Kernel Development
Lab Instructor
Mr. Mujtaba Aslam
Submitted By
Rida Fatima || 13f-8080 || A
Date
Thursday, March 19, 2015
Boot loader Development:
In this step we will be creating a boot loader. Instead of writing it on MBR of your hard drive we will be
creating a Virtual Floppy Drive (VFD) and write our boot loader to its
MBR.
Virtual Floppy Drive (VFD):
We can create an empty image and mount it using Linux’s loop devices.
Steps:
1. Switch to root mode
2. Create an empty image named as floppy.img
3. Create loop device named loop0
4. Define file system for loop0 device
5. Mount the device in /media/floppy directory
You need to mount, just to verify that your disk is created successfully.
Now un-mount the device so we can write our boot loader to its MBR.
My First boot loader:
Steps:
1. Setup the environment for development and deployment.
2. Make an empty assembly file named as bootloader.asm.
3. Write following assembly code.
This code basically uses infinite loop to hold blank screen.
4. Now compile this assembly file using NASM to a binary format file named as boot.bin.
5. Write this bin file to MRB sector of Virtual floppy drive that we created in previous section.
6. Boot this floppy drive on QEMU virtual manager.
7. If everything goes well, you will see the following output:
8. Edit the asm file.
Compile the asm file again, write it to floppy drive boot sector and boot it on QEMU, you will see
a string printed on the virtual machine as shown below.
Kernel Development in C language:
Steps:
1. Make a directory for all your code and compiled files (Don’t go for super user mode so you have
Rights to your file when you are using GUI Desktop as normal user).
2. Create an assembly file named as Kernel.asm by ‘gedit kernel.asm’.
3. Write following code in assembly file.
This code basically loads from grub and transfer control to our kmain() function in C code.
4. Create C file named Kernel.c
5. Write the following code in C file.
6. Create linker file name link.ld to join output of both C and ASM file.
7. Write following statement in linker file.
8. Now, we have all our files ready to build the kernel. But, since we like to boot our kernel with
the GRUB bootloader, there is one step left. There is a standard for loading various x86 kernels
using a boot loader; called as Multiboot specification.
Add following lines between Section.text and global start to define multiboot specification:
9. So by now we have 3 files ( Kernel.asm, Kernel.c and link.ld ).
10. Compile ASM file, Compile C file and Link them both using linker file.
11. Now we have 6 files.
kasm.o file is output of kernel.asm, kc.o is output of kernel.c and kernel file is formed by linking
kasm.o and kc.o using linker file.
12. Now rename the kernel to kernel-01 as Grub excepts this format: KernelName-Version
13. Test run your kernel using QEMU.
14. You will get the following output.
15. Now switch to super user mode and copy your kernel file to /boot directory.
16. Now add a menu entry in grub.cfg file.
17. Restart the Ubuntu and you will see your kernel entry along with other entries.
18. Select your kernel and you will see the following output.
Download