Linux Source Analysis Note 1 (BOOTSECT.S file analysis) __linux

Source: Internet
Author: User
linux0.11 Source Analysis 1 bootsect.s file Analysis from Power-on power-up to the process of performing the main functionSince the linux0.11 system was stored on a floppy disk, the main purpose of the power-adding process was to load the operating system from the boot disk to complete the preparation of the main function. Starting from boot to Mian function is divided into three parts:
The first part is to boot the BIOS and prepare Real ModeInterrupt vector table and interrupt service program. The second part is loading the operating system from the boot disk to memory the third part is to perform the transition work of 32-bit main function. Part I.From the hardware, Intel will all the 80X86 series of CPU hardware designed to power into the 16-bit real mode operation, after the battery point to the 0xffff0 location of the BIOS program in the first place in memory space (0X00400~0X004FF) to build the interrupt vector table. The BIOS data area (0X00400~0X004FF) is built in a 256-byte memory space next to each other, and the corresponding interrupt service program for the interrupt vector table at 8KB is loaded after 56kb (0X0E2CE). Part II

For linux0.11 systems, computers are divided into three batches of code that load the operating system successively: the first batch of BIOS interrupts int 0x19 loads the contents of the first sector bootsect into memory. In the second and third batches, the contents of the following four sectors and subsequent 240 sectors were loaded into memory under the command of Bootsect.

The Interrupt Service program pointed to by the int 0x19 interrupt vector copies the contents of the 0-track 1 sector of the diskette drive No. 0 head to the memory 0x07c00 (this address belongs to the DRAM area). The track sector stores the Bootsect code. The maximum addressable memory in real mode is only 1MB, so you need to plan for memory.

Bootsect copies its own code of operations as follows:

Entry Start                                                                  !  Represents the entry point for a program
start:
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  

First, the program enters the start entry point, then stores the boot sector in the DS register by the BIOS-loaded location (i.e. bootseg), the address of the new location to be moved (INITSEG) in the CX register, and the following 2 sub instructions are made to 0x0000 respectively, CX is 256 and the operation is MOVW, which indicates a word copy operation, with a total of 512 bytes moved. (Here you need to know how MOVW this instruction executes, see my other blog notes)

Next code to execute

    Jmpi    go,initseg                                       
go:mov ax,cs mov ds,ax mov! Put stack at Es,ax mov                                           
0x9ff00                                                           
mov SP, #0xFF00      ! arbitrary value >>512

After the copy completes, executes the jmpi instruction, the meaning is jumps to go:initseg, the CS value changes the INITSEG,IP the value to change from initseg to Go:mov ax,cs offset.
Then the DS, ES, SS are adjusted, the current value of the CS assigned to them, the top pointer SP point to the offset address of the 0xff00 place. Note: The SS and SP form the position of the stack data in memory.
-Load the Setup program into memory
The Interrupt service program that the BIOS provides to interrupt 0x13 interrupts is required to complete. The int 0x13 loads the code of the specified sector into the specified location in memory.
int 0x13 is a direct disk service, with specific parameters and usage references so
The code is as follows:

 mov dx, #0x0000! Drive 0, head 0 mov cx, #0x0002! Sector 2, Track 0 MOV bx, #0x0200! Address = A, in initseg storage offset addresses the Mov ax, #0x0200 +setuplen! Service 2, nr of Sectors Setuplen is the number of sectors loaded, corresponding to the parameter Al int 0x13! Read it points to the disk service program, according to the value of AH is corresponding to 02, is the read sector function, AL is the number of sectors Jnc Ok_load_setup! Ok-continue if the CF flag is 0, jump mov dx, #0x0000 mov ax, #0x0000!  Reset the diskette is not 0 to reset the int 0x13. Continue to interrupt J Load_setup. Return to the Load_setup location 

The front 4 mov is the first set of parameters, note that the 4th MOV, given the ah=02, indicating that the function of 02H read sector. Al is the number of sectors.
Then call int 0x13 Interrupt, enter interrupt service program, read the four sectors that start the floppy disk from the second sector to
ES:BX represents the buffer address, so BX offset is the 512,es value or the current segment. Load Part Three code

Bootsect through the BIOS interrupt int 0x13, load the system module of 240 sectors into the memory load work by Bootsect call read it subroutine complete, This subroutine loads the system module of the 240 sectors starting with the floppy 6th sector into the 120kb space in the back of the Memory sysseg (0x10000).
The code is as follows:

 Ok_load_setup:!                                                              Get disk drive parameters, specifically nr of Sectors/track mov dl, #0x00 。 dl= Drive mov ax, #0x0800!                                                              Ah=8 is get drive parameters ah=08 indicates read drive parameter int 0x13 。 Interrupt 13 vector mov ch, #0x00. Ch= Cylindrical SEG cs. Indicates that the next instruction will use segment beyond MOV sectors,cx mov ax, #INITSEG mov es,ax! Print some inane message mov ah, #0x03!                                                        Read Cursor pos ah=03 is the entry parameter, in the text coordinates, reads the cursor various information XOR BH,BH 。 BH is the display page number int 0x10 mov cx, #24! CH for the beginning of the cursor line, CL for the end of the cursor line MOV bx, #0x0007! Page 0, attribute 7 (normal).                BX is the display page mov bp, #msg1                                        。 es:bp= Displays the address of the string, MSG1 in the source code the last few lines of the file position mov ax, #0x1301! Write string, move cursor!
Ah=13 represents the display string, al=1 indicates that the string contains only characters, and the cursor position changes after display. int 0x10! Display interrupt for service, screen write operation

The

reads the drive parameters above and prints the text messages that load the system. Call int 0x10 interrupt.
Next, call the Read_it function to read the sector contents, as follows:

    Read_it:mov ax,es Test ax, #0x0fff. Test if a bit in ax is 0, if 0, ax is 0, ZF is 1, otherwise 0 die:jne die! Es must is at 64kB boundary ZF does not equal 0 Jump XOR BX,BX! BX is starting address within segment. XOR or directive Rp_read:mov ax,es cmp ax, #ENDSEG!                  Have we loaded all yet? 。 The two numbers were subtracted and compared to JB Ok1_read. Judge 2 number size RET OK1_READ:SEG CS mov ax,sectors su
    b ax,sread mov cx,ax SHL cx, #9 add cx,bx jnc ok2_read JE ok2_read XOR Ax,ax Sub ax,bx shr ax, #9 ok2_read:call read_track mov cx,ax add ax,sread SEG CS cmp ax,s Ectors jne ok3_read mov ax, #1 Sub Ax,head jne Ok4_read Inc track Ok4_read:mov Head,ax XOR ax, Ax Ok3_read:mov sread,ax SHL CX, #9 add BX,CX JnC Rp_read mov ax,es add ax, #0x1000 mov es,ax xor bx,bx jmp rp_read 

Calling the Kill_motor function

* * This procedure turns off the floppy drive motor, so
* We enter the kernel in a known state, and
* do N ' t have to worry about it later.
* *
kill_motor:
    push DX
    mov, #0x3f2
    mov al, #0
    outb
    pop dx
    ret

Then there is a section of code that determines the root file system number.

by executing Jmpi 0,setupseg
Will jump to 0x90200, where the setup program loads, cs:ip the first instruction to the Setup program.
Now, the entire Bootsect program is complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.