Learning notes of operating system true image restoration: Chapter II writing MBR master boot record

Posted by jeffgman on Fri, 29 Oct 2021 12:00:37 +0200

1. Computer startup process

  • Why load into memory
    (1) The hardware circuit of CPU is designed to run only programs in memory
    (2) The memory is fast and has large capacity,
  • What is load memory
    (1) The program is loaded into an area of memory by the loader (software or hardware).
    (2) The cs:ip register of the CPU is pointed to the starting address of the program.
  • After pressing the host power key, the first running software is bios

2. The first baton of software relay, BIOS

The full name of BIOS is base input & output system, that is, basic input and output system
BIOS works in 16 bit real mode

2.1 memory layout in real mode

startendsizeUse
FFFF0FFFFF16BBIOS entry address, which also belongs to BIOS code and also belongs to the top 64KB bytes. Only in order to emphasize its entry address, it is posted separately. Here 16 byte content jump instruction jmp f000: e05b
F0000FFFEF64KB-16BThe system BIOS range is F0000 ~ FFFFF, with a total of 64KB. In order to explain the entry address, the top 16 bytes are removed from here, so the termination address here is 0xffef
C8000EFFFF160KBMapped ROM or memory mapped I/O for hardware adapters
C0000C7FFF32KBDisplay adapter BIOS
B8000BFFFF32KBAdapter for text mode display
B0000B7FFF32KBAdapter for black and white display
A0000AFFFF64KBAdapter for color display
9FC009FFFF1KBEBDA (Extended BIOS Data Area)
7E009FBFF≈608KBAvailable area
7C007DFF512BMBR is loaded here by BIOS, with a total of 512 bytes
5007BFF≈30KBAvailable area
4004FF256BBIOS Data Area
0003FF1KBInterrupt Vector Table
  • Why is the physical memory inserted on the motherboard not "all memory" in the eyes of the CPU?
    In the computer, not only the memory module inserted on the motherboard needs to be accessed through the address bus, but also some peripherals need to be accessed through the address bus. There are many such devices. If all address buses are directed to physical memory, how can other devices access it? For this reason, we have to reserve some address space on the address bus in advance for these peripherals.

2.2 how does BIOS wake up

Because BIOS is the first software running on the computer, it cannot load itself by itself. It is loaded by hardware - ROM read-only memory. ROM is also a piece of memory, and the memory needs to be accessed. This ROM is mapped to the top of 1MB Memory, i.e. address 0xf0000 ~ 0xFFFF.

  • How does the BIOS start?
  1. At the moment of pressing power, the cs: ip register of the CPU is forcibly initialized to 0xF000:0xFFF0, that is, 0xff0. This address is the entry address of the BIOS.
  2. When the CPU executes the first instruction, jmp far f000: e05b jumps to address 0xfe05b, which is where the BIOS code really starts.
  3. Next, the BIOS constantly detects the peripheral information such as memory and graphics card. When the detection passes and initializes the hardware, it starts to establish the data structure at the memory 0x000~0x3FF, interrupt the vector table IVT and fill in the interrupt routine.
  4. Finally, jmp 0:0x7c00 gives the control of CPU to MBR

3. Next player MBR

MBR The size of must be 512 bytes to ensure 0 x55 And 0 xaa These two magic numbers appear exactly at the last two bytes of the sector, i.e. 510th byte and 511 byte.
;Master bootstrap 
;------------------------------------------------------------
SECTION MBR vstart=0x7c00         
   mov ax,0      
   mov ds,ax
   mov es,ax
   mov ss,ax
   mov fs,ax            ;take ax,dx,es,ss,fs Initialize to 0
   mov sp,0x7c00

; Clear screen utilization 0 x06 No. function, scroll up all lines to clear the screen.
; -----------------------------------------------------------
;INT 0x10   Function number:0x06	   Function description:Roll up window
;------------------------------------------------------
;Input:
;AH Function number= 0x06
;AL = Number of rows rolled up(If 0,Indicates all)
;BH = Roll up row attribute
;(CL,CH) = In the upper left corner of the window(X,Y)position
;(DL,DH) = In the lower right corner of the window(X,Y)position
;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),
			   ; VGA In text mode,A line can only hold 80 characters,25 lines in total.
			   ; Subscript starts at 0,So 0 x18=24,0x4f=79
   int     0x10            ; int 0x10

;;;;;;;;;    The following three lines of code are to obtain the cursor position    ;;;;;;;;;
;.get_cursor Gets the current cursor position,Print characters at cursor position.
   mov ah, 3		; input: 3 The number sub function is to obtain the cursor position,Need to deposit ah register
   mov bh, 0		; bh The register stores the page number of the cursor to be obtained

   int 0x10		; output: ch=Cursor start line,cl=Cursor end line
			; dh=Line number of cursor,dl=Column number of cursor

;;;;;;;;;    Get cursor position end    ;;;;;;;;;;;;;;;;

;;;;;;;;;     Print string    ;;;;;;;;;;;
   ;Or 10 h interrupt,However, this time it is to call sub function 13 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 agreement,
			; Already at the beginning sreg initialization

   ; Cursor position to be used dx Contents of register,cx Cursor position in is ignored
   mov cx, 5		; cx Is the string length,Number of characters excluding terminator 0
   mov ax, 0x1301	; The sub function number 13 is the display character and attribute,To deposit ah register,
			; al Set character writing mode ah=01: display string,The cursor follows
   mov bx, 0x2		; bh Stores the page number to display,This is page 0,
			; bl Character attribute in, Attribute green on black background(bl = 02h)
   int 0x10		; implement BIOS 0x10 No. interrupt
;;;;;;;;;      End of typing string	 ;;;;;;;;;;;;;;;

   jmp $		; Hover the program here

   message db "1 MBR"
   times 510-($-$$) db 0
   db 0x55,0xaa
  • Compile mbr.s and write mbr.bin to the virtual hard disk
#Install nasm
yum install -y nasm
#Compile mbr.s
nasm -o mbr.bin mbr.s
#Write mbr.bin to the virtual hard disk hd60m.img. your_ Replace path with your actual path
dd if=/your_path/mbr.bin of=/your_path/bochs/hd60m.img bs=512 count=1 conv=notrunc
dd Command parameter parsing
if=file      Specifies the file to read
of=FILE	     Specifies which file to export the data to
bs=BYTES     Specifies the size of the input and output blocks(byte)
count=BLOCKS Specifies the number of blocks to copy
seek=BLOCKS  Specifies how many blocks you want to skip when exporting blocks to a file
conv=CONVS	 Specify how to convert files, notrunc Do not break files

4. Start bochs

bin/bochs -f bochs.disk
 enter
c

Topics: Operating System