Minimal bootloader

A bootloader is a program which loads the prerequisites for the operating system. This tutorial will explain about bootloader design and how it gets fitted in the system. For creating a bootloader  first we must setup the development environment system so that we can assemble the code and then we can test our working bootloader. Therefore, we will first lay down our minimal bootloader objectives:

  1. Our first objective is to boot the system.
  2. and second is to print a string on the screen.

Now since, we have setup our bootloader functionalities, we will now setup the development environment. To do this we will install a couple of software in our system. I have previously mentioned that I will use Linux operating system to do so. So all those using windows, please follow this link. The article will explain you about installing the development environment on windows operating system.

Development Environment:-

  1. Linux operating system, preferably Ubuntu operating system. You can download the operating system from here and to setup with Virtualbox, if you are windows users or Ubuntu user.
  2. Install packages like bcc, bin86, gcc, nasm and hexedit.
  3. A system that will be used to test your custom operating system Virtualbox.

So if you are new to Linux operating system, follow this link, this link will help you in understanding some of the basic commands of linux os.  To start with our bootloader type the following code in the text editor:

Minimal bootloader

;This is a boot loader program which hangs after printing Welcome Message
;Run bochs using the following command :==> bochs -q 'boot:a' 'floppya:1_44=floppy.img, status=inserted'
[BITS 16]             ;Tells the assembler that its a 16 bit code
[ORG 0x7C00]          ;Origin, tell the assembler that where the code will be in memory after it is loaded

MOV SI, WelcomeString                 ;Store string pointer to SI
CALL _Video_Mode
CALL _Cursor_Position
CALL _PrintString                ;Call print string procedure

JMP $                         ;Infinite loop, hang it here.

_Video_Mode:
        MOV AH,0X00
        MOV AL,0X13
        INT 0X10
        RET
_Cursor_Position:
        MOV AH,0x02      ;Function Code for setting up cursor position
        MOV BH,0x00      ;Page Number
        MOV DH,0x06      ;Row
        MOV DL,0x00      ;Column
        INT 0x10         ;Call video interrupt
        RET              ;Return to calling procedure

_PrintCharacter:         ;Procedure to print character on screen
                        ;Assume that ASCII value is in register AL
        MOV AH, 0x0E     ;Tell BIOS that we need to print one character on screen.
        MOV BH, 0x00    ;Page no.
        MOV BL, 0x07    ;Text attribute 0x07 is light grey font on black background
        INT 0x10        ;Call video interrupt
        RET             ;Return to calling procedure

_PrintString:           ;Procedure to print string on screen
                        ;Assume that string starting pointer is in register SI
next_character:         ;Label to fetch next character from string
        MOV AL, [SI]    ;Get a byte from string and store in AL register
        INC SI                ;Increment SI pointer
        OR AL, AL            ;Check if value in AL is zero (end of string)
        JZ exit_function         ;If end then return
        CALL _PrintCharacter   ;Else print the character which is in AL register
        JMP next_character      ;Fetch next character from string

        exit_function:         ;End label
        RET                ;Return from procedure

;Data
WelcomeString db 'Welcome to AD OS....', 0    ;ADwita parashar Operating System string which ends with 0

TIMES 510 - ($ - $$) db 0    ;Fill the rest of sector with 0
DW 0xAA55            ;Add boot signature at the end of bootloader

and save it with a file name bootloader.asm.

Now, I hope you quickly want to run the code to see its working so I will now explain to assemble and make it running.

To assemble the code follow the instructions:

  1. You will need an assemble the bootloader.asm with an assembler called as “nasm”. nasm
  2. After it assembles it will create a binary file. Type the command “ls” on the command prompt to see the created binary file.
  3. Next step is to copy it in a virtual floppy disk. So we will now copy the file on our virtual floppy disk. Follow the commands mentioned.dd
  4. After creating the the virtual floppy as floppy.img. Open virtualbox and create a machine as shown in the tutorial. virtualbox
  5. After creating the virtual machine. Right click on the virtual machine you created. virtualbox settings
  6. Click on settings and then click on storage.Afterwards click on floppy icon.virtualbox floppy
  7. Now click on choose disk button to add the floppy image, you created.imsert floppy imageresult
  8. Since now you have added the floppy disk to the system. You can now start the machine to see you minimal bootloader boot.ADOS

With this I now assume that you have understood the boot loader and how it can be used to boot the system.

Remember one thing that this bootloader must not be copied to hard disk. Since we have not included any of the hard disk parameters to make disk boot.

My next article will focus on explanation of source code and theory behind the code.


Creative Commons License
Design and Development of Operating System for teaching operating system course by Gaurav Parashar is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Be the first to comment

Leave a Reply

Your email address will not be published.


*


Comment moderation is enabled. Your comment may take some time to appear.