Gos - BIOS and successor MBR

Written in front: homemade operating system Gos Chapter 2 Chapter 2: the main content is the transfer of power in CPU and how to control BIOS and MBR

BIOS

Students with operating system foundation know that when the operating system is powered on, the BIOS will be loaded first, and then the BIOS will load the operating system. This process seems very simple, but in fact, there are many details we don't understand, and these details officially master the Dragon killing technology of the operating system.

What is BIOS

The whole process of BIOS is the Base Input Output System, that is, the basic input system. You can see that there are several keywords: basic, input and output, system.

There are several questions:

  • What does basic mean?
  • What are the inputs and outputs?

Let's look at the basic meaning first.

8086 real mode memory layout focus!!!

There are a total of 20 addresses in Intel 8086, so the address range that the CPU can access is 220 = 1M. This valuable 1M space is divided into the following areas:

startendsizepurpose
FFFF0FFFFF16BBIOS entry address. The content here is mainly jmp f000:e05b
F0000FFFEF64KB-16BOther BIOS addresses
C8000EFFFF160KBMapped hardware adapter ROM or memory mapped I/O
C0000C7FFF32KBDisplay adapter BIOS
B8000BFFFF32KBAdapter for text mode display
B0000B7FFF32KBAdapter for black and white display
A0000AFFFF64KBAdapter for color display
9FC009FFFF1KBEBDA extended BIOS data area
7E007DFF512BMBR loading area
5004FF256BBIOS data area
0003FF1KBInterrupt vector table

This watch is very important. It is equivalent to the sword Manual of our dragon killing skill. We have to rely on him to kill the dragon.

Let's look at the area from 0xffff0 to 0xfffff at the beginning. The 64KB memory is ROM, which stores BIOS code. The main work of BIOS is to detect and initialize the hardware. This process is enlarged to see that BIOS calls the hardware driver function provided by the hardware driver. In addition, another thing BIOS does is initialize the interrupt vector table, so that the interrupt function can be called through the assembly instruction of int interrupt number.

Why is it called basic input and output? Because BIOS works in real mode, it doesn't need to do all the work. It only needs to complete the basic initialization. The really broad sea of stars is in protection mode!

BIOS boot

From the beginning of our whole article, we know that BIOS is the first software started by the whole computer, so who started it? Is it bootstrapping?

It's actually hardware. In real mode, x86 CPU accesses memory through segment base register cs and segment offset register ip. At the moment of startup, the cs:ip of the CPU is initialized to 0xF000:0xFFF0, which is the entry address of the BIOS.

Note:
In the x86 mode, there is a convention that in order for a 16 bit register to access a 20 bit memory space, the segment base address needs to be shifted by 4 bits to the left, so 0xF000:0xFFF0 here is actually equivalent to 0xFFFF0

After that, the BIOS executes the instruction jmp f000:e05b and jumps to the place where the BIOS really starts to execute. After that, the BIOS starts to detect the peripheral information such as memory and graphics card. When the detection passes and initializes the hardware, the BIOS will establish an interrupt vector table at 0x000~0x3FF and fill in the interrupt routine.

MBR

When the BIOS is initialized successfully, he completes his work. Then he will hand over the power to the MBR. He is the first program we can control.

What is MBR

MBR is the Main Boot Record. It is located in the first sector of the whole hard disk, that is, 0 disk, 0 channel and 1 sector. This sector is called MBR boot sector. The contents of this boot sector are as follows:

  • 446 byte boot program and parameters
  • 64 byte partition table
  • 2-byte end flags 0x55 and 0xaa

Note:
As for how the power is transferred, the BIOS detects the 0x55 and 0xaa marks from the beginning of the disk after completing the work. When they are detected, he thinks this is the successor MBR.
After that, the BIOS loads the 512 bytes to 0x7c00, and then jmp goes there. In this way, the handover of power is completed and the code in the MBR is executed.

Contents of MBR

Finally, the hard dish came. We began to control the operating system, starting with our MBR program. The following code is the content of the program. I can't understand the comments in the comment area.

;Master bootstrap
SECTION MBR vstart=0x7c00
    mov ax,cs           ;use cs Register to initialize other registers
    mov ds,ax
    mov es,ax
    mov ss,ax
    mov fs,ax
    mov sp,0x7c00       ;Initialize stack pointer

; Clear screen utilization 0 x06 No. function, roll up all lines and clear the screen
; int 0x10  Function No.: 0 x60    Function Description: roll up window
; Input:
; AH Function No.: 0 x06
; AL = Number of rows rolled up(0 Represent all)
; BH = Roll up row properties
; (CL,CH) = Upper left corner of window(x,y) Location of
; (DL,DH) = Lower right corner of window(x,y)Location of
; No return value!
    mov ax,0x600
    mov bx,0x700
    mov cx,0        ;top left corner(0,0)
    mov dx,0x184f   ;Lower right corner(80,25)
                    ;VAG In text mode, a line can only hold 80 characters, a total of 25 lines
                    ;The subscript starts at 0, so 0 x18=24,0x4f=79
    int 0x10        ;System call number 10

;The following three lines of assembly code get the cursor position
; .get_cursor Gets the position of the current cursor and prints characters at the cursor position
    mov ah,3        ;input:3 The sub function of No. is to obtain the cursor position, which needs to be saved ah register
    mov bh,0        ;bh The register stores the page number of the cursor to be obtained

    int 0x10        ;Output: ch=The cursor starts the line, cl=Cursor end line
                    ;dh=The line number of the cursor, dl=Column number of cursor

;Print string
;Or 10 h interrupt,However, this time, sub function 13 is called to print the string
    mov ax,message
    mov bp,ax       ;es:bp Is the first address of the string, es Same at this time cs Consistent, already at the beginning sreg initialization

;Registers are used for cursor positions dx Content in, cs Cursor position in can be ignored
    mov cx,12        ;cx Is the string length, excluding the number of characters with terminator 0
    mov ax,0x1301   ;The sub function number 13 is a real character and attribute to be stored ah register
                    ;al Set character writing mode al=01: The string is displayed and the cursor moves with it
    mov bx,0x2      ;bh Store the page number to display, here is page 0
                    ;bl In is the character attribute, and the attribute is green on a black background(bl=02h)
    int 0x10        ;implement BIOS 0x10 No. interrupt

;Program hovers here
    jmp $

    message db "hello,world!"
    times 510-($-$$) db 0		;Expanded to 510 bytes
    db 0x55,0xaa

After that, we call nasm to compile this assembly code:

# Compile command
nasm -o mbr.bin mbr.S

Then use the dd tool to write it into the first sector of the disk hd60M.img we created earlier:

# The generated executable file is written to disk 0, track 0 and sector 1 of hd60M.img virtual hard disk
# Remember to change your bochs address after of
sudo dd if=/home/ik/workspace/mbr.bin of=/bochs/bo_tmp/bin/hd60M.img bs=512 count=1 conv=notrunc

Then go to the place where you installed bochs and run the following command. Let's see the results:

reference

[1] Deep understanding of computer systems
[2] Operating system truth restore

Tags: Assembly Language Operating System bios

Posted on Mon, 27 Sep 2021 09:48:50 -0400 by solarith