In the previous section, we mentioned that the memory interrupt vector table and interrupt service program are built by the BIOS in real mode, then it is necessary to use these interrupts to load the operating system kernel, this step has three steps: 1, bootloader bootsect loading, 2, setup program loading; 3, system kernel loading.
After the interrupt vector table and the interrupt service program are built, the BIOS generates an int 0x19 interrupt, which is responsible for copying the Bootsect program from the first sector (512B) of the system disk to the 0X07C00 (bootseg) location of the memory. Here int 0x19 Interrupt Service Program is independent of the operating system, operating system vendors and hardware manufacturers have a convention, that is, the operating system's boot disk in the first sector, int 0x19 will copy the Bootsect program from the sector, and then placed in the memory bootseg location.
After loading the bootsect boot program, the first thing Bootsect do is to plan the memory, in fact, to specify where each program will be placed in the future. Setuplen=4 is the size of the setup program, in sectors, BOOTSEG=0X07C0 is the initial position of the Bootsect program, and initseg=0x9000 is the new location where the Bootsect program will be copied; setupseg= 0X9020 is the location where the setup program is placed, close to the Bootsect program, sysseg=0x1000 is where the kernel is loaded, and endseg=sysseg+syssize is where the kernel ends.
Then the Bootsect program first copies itself from the BOOTSEG location to the initseg location, the specific code is as follows:
mov ax, #BOOTSEG
MOV ds, ax
mov ax, #INITSEG
MOV es, ax
MOV cx, #256
Sub Si, si
Sub Di, di
Rep
Movw
Jmpi go, initseg
Go:mov Ax, CS
MOV ds, ax
MOV es, ax
mov ss, ax
mov sp, #0XFF00
In the above code, Ds:si constitute the source address 0x07c00,es:si constitute the destination address 0x90000, the loop at this time is 256, one word at a time, in real mode, the length of 16 bits, so 256 times moved 512B is the entire bootsect program. At the end of the copy, at this time the value of CS is 0x07c0, but when the execution of Jmpi go, initseg instruction, the value of CS becomes initseg, and the subsequent DS,ES,SS register values are rewritten, and the stack is set. So far, Bootsect has finished copying itself, and then it is loading the setup program.
When the setup program is loaded, Bootsect takes advantage of the int 0x13 interrupt provided by the BIOS, which needs to pass in the register some parameters, the code is as follows:
Load_setup:
MOV dx, #0x0000
MOV cx, #0x0002
MOV bx, #0x0200
mov ax, #0x0200 +setuplen
int 0x13
Jnc Ok_load_setup
MOV dx #0x0000
MOV ax #0x0000
int 0x13
J Load_setup
Ok_load_setup:
Starting from the second sector, copy 4 sectors, copy to 0X0200, because CS is 0x9000 at this time, so the destination address is 0x90200. After the setup program is loaded, the kernel of the system will be loaded.
As with the load setup, the load kernel also takes an int 0x13 interrupt, just needs to load 240 sectors starting from the sixth sector to SYSSEG, and then the entire operating system code has been loaded into memory, Bootsect task completed. Below, jump to the Setup program with Jmpi 0,setupseg to start execution.
The Setup program uses the interrupt vectors and interrupt service programs provided by the BIOS to obtain the operational needs of the machine system data volunteer cores, which are placed at 0X90000-0X901FC. This data will overwrite the BOOTSECT program because the mission of the BOOTSECT program has been completed.
At this point, the operating system kernel load is fully completed, and then, by leveraging in-memory code, Linux will implement the conversion from the actual mode to the protected mode, and will continue to learn later!
This article is from the "Quest" blog, please be sure to keep this source http://niji1990.blog.51cto.com/7706365/1709868
Linux kernel system boot (ii)