Linux0.11 Kernel reading notes/boot/bootsect.s__linux

Source: Internet
Author: User

Jelly

qq:457283

Http://blog.csdn.net/jznsmail

! The main functions of this program
! 1.bootsect.s starts from 0X7C00.
! 2. Copy yourself to the 0x90000 place
! 3. Read the SETUP.S program from the disk 2nd sector to the 0X90200
! 4. Read system to 0x10000
! 5. Get root file system device number
! 6. Display information
!
! Program execution Sketch
! +---+---+---+ 0xa0000
!      |     |      | |
!      |     |      | |
! +---+---+---+
!      |     |      | |
!      |     |      | |
!      |     | | S |
!      |     |      | |
!      |     |      | |
! +---+---+---+ 0x90200
!      | | B | B |
! +---+---+---+ 0x90000
!      |     |      | |
!      |     |      | |
! +---+---+---+
!      |     |      | |
!      |     |      | |
!      |     |      | |
!      |     |      | |
!      |     | | K |
!      |     |      | |
!      |     |      | |
!      |     |      | |
! +---+---+---+ 0x10000
!      |     |      | |
!      |     |      | |
!      |     |      | |
!      |     |      | |
! +---+---+---+
!  |     B |      | |
! +---+---+---+ 0x7c00
!      |     |      | |
! +---+---+---+ 0x0000
! 1 2 3
! B-BOOTSECT.S Program
! S-SETUP.S Program
! K-system Module
!
! Sys_size is the number of clicks (bytes) to be loaded.
! 0x3000 is 0x30000 bytes = 196kB, more than enough to current
! Versions of Linux
!
! Size of the system module after compiling the connection
Syssize = 0x3000
!
! Bootsect.s (C) 1991 Linus Torvalds
!
! Bootsect.s is loaded in 0x7c00 by the Bios-startup routines, and moves
! Iself out of the way to address 0x90000, and jumps there.
!
! It then loads ' setup ' directly after itself (0X90200), and the system
! At 0x10000, using BIOS interrupts.
!
! note! Currently system is at most 8*65536 bytes long. This should is no
! Problem, even in the future. I want to keep it simple. This MB
! Kernel size should is enough, especially as this doesn ' t contain the
! Buffer cache as in Minix
!
! The loader has been made as simple as possible, and continuos
! Read errors'll result in a unbreakable loop. Reboot by hand. It
! Loads pretty fast by getting whole sectors in a time whenever possible.

! Defines 6 global identifier code snippets, data segments, uninitialized data segment start end address
. Globl Begtext, Begdata, BEGBSS, Endtext, Enddata, ENDBSS
! Code Snippets
. text
Begtext:
! Data segment
. Data
Begdata:
! Uninitialized data segment
. BSS
BEGBSS:
! Code Snippets
. text

Setuplen = 4! NR of Setup-sectors
! Number of sectors in the Setup program
BOOTSEG = 0x07c0! Original address of Boot-sector
! Boot-sector's original segment address
INITSEG = 0x9000! We move boot here-out of the way
! Move the boot-sector here.
SETUPSEG = 0x9020! Setup starts here
! The Setup program starts here.
SYSSEG = 0x1000! System loaded at 0x10000 (65536).
! System module loaded into 0x10000 (64K)
endseg = sysseg + syssize! Where to stop loading
! Stop the loaded segment address

! Root_dev:0x000-same type of floppy as boot.
! 0x301-first partition on the drive etc
Root_dev = 0x306! Device number, which means the root file system is the first partition on the 2nd hard drive
! Device number = Main Unit number *256 + sub-unit Number (= (major<<8) + minor)
! Main device Number: 1-memory, 2-disk, 3-hard drive, 4-ttyx,5-tty,6-, 7-Unnamed pipe
! 0x300 = 3*256 + 0 1th hard Drive
! 0x301 = 3*256 + 1 1th hard drive, 1th partition
! 0x306 = 3*256 + 6 2nd hard drive, 1th partition

Entry _start! Program Entry point
_start:
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
mov ax, #BOOTSEG! Set the data segment DS register to 0X07C0
MOV Ds,ax
mov ax, #INITSEG! Set the additional segment ES register to 0x9000
MOV Es,ax
MOV cx, #256! Move value cx=256 Word = 512 bytes
Sub Si,si! SOURCE Address Ds:si = 0x07c0:0x0000
Sub Di,di! Destination Address Es:di = 0x9000:0x0000
Rep! Repeat execution until cx=0
MOVW! Move a word
! The code above is to move bootsect itself from position 0x07c00 to 0x90000, a total of 512 bytes
Jmpi go,initseg! Indirect jump, initseg to jump to the segment address
! The following code is executed from the 0x90000
Go:mov Ax,cs! Will ds,es and SS set to 0x9000, at this time CS jump to 0x9000
MOV Ds,ax
MOV Es,ax
!  Put stack at 0x9ff00. ! Point the stack pointer to 0x9ff00 (0X9000:0XFF00)
MOV Ss,ax
mov sp, #0xFF00! Arbitrary value >>512
! Because 0x90000-0x90200 placed bootsect,0x90200 to start placing about 4 sectors of Setup
! program, so the stack pointer sp points to the 0x200 + 0x200 * 4 + stack size position

! Load the setup-sectors directly after the bootblock.
! Note which ' es ' is already set up.
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
Load_setup:
MOV dx, #0x0000! Drive 0, head 0.
! Drive 0, head 0
MOV cx, #0x0002! Sector 2, Track 0
! Sector 2, Track 0
MOV bx, #0x0200! Address = Initseg
! Buffer offset, es in the above code has been set to 0x9000
mov ax, #0x0200 +setuplen! Service 2, NR of sectors
! Read 4 disk Sectors to memory
int 0x13! Read it
! Call break
JNC Ok_load_setup! Ok-continue
! CF No bit indicates read success, jump to Ok_load_setup
MOV dx, #0x0000! Reset Drive and Head
mov ax, #0x0000! Reset the diskette
int 0x13! Reset
J Load_setup! Keep trying again
! The code above is to use the BIOS int 0x13 interrupt to read the Setup module from the 2nd sector of the disk
! 0X90200, read 4 sectors Altogether, reset the drive if read error, and try again
! INT 0x13
! Ah = 0x02 Read disk sector to memory Al = number of sectors that need to be read out
! CH = track (cylinder) Number Low 8 CL = Start sector (0-5 bits), track number 2 digits (6-7)
! DH = Magnetic Number One DL = drive letter (if hard drive bit 7 to set bit)
! ES:BX = point to data buffer if error CF flag position

! Successfully loaded Setup module
Ok_load_setup:

! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
! Get disk drive parameters, specifically nr of Sectors/track

mov dl, #0x00! Set drive letter to 0
mov ax, #0x0800! Ah=8 is get drive parameters
int 0x13! Call break
mov ch, #0x00
SEG CS! Indicates that the next statement operation is in the segment indicated by the CS segment Register,
MOV sectors,cx! Save number of sectors per track
mov ax, #INITSEG
MOV Es,ax! Because the value of ES is overwritten when reading disk parameters, this is changed back to
! The above code reads the disk drive parameters, especially the number of sectors
! INT 0x13
! Ah = 0x08 dl = Drive letter (if it is a hard drive, place 7 to 1)
! Return information:
! If error CF set bit, ah = status code
! Ah = 0, al = 0 bl = Drive type (AT/PS2)
! CH = max Track number low 8 bit CL = maximum number of sectors per track (bit 0-5), maximum track number 2 digits (bit 6-7)
! DH = maximum number of heads DL = number of drives
! Es:di = floppy disk parameter table

! Display information ' Loading system ... Carriage return Wrap ' a total of 24 characters
! Print some inane message

mov ah, #0x03! Read Cursor pos
XOR BH,BH! Read cursor position
int 0x10

MOV cx, #24! A total of 24 characters
MOV bx, #0x0007! Page 0, attribute 7 (normal)
mov bp, #msg1! Point to the string to display
mov ax, #0x1301! Write String, move cursor
int 0x10! Write a string and move the cursor

! OK, we ' ve written the message and now
! The following code loads the system module into the 0x10000
! We want to load the system (at 0x10000)

mov ax, #SYSSEG
MOV Es,ax! Segment of 0x010000
Call Read_it! Read system module, ES for input parameters
Call Kill_motor! Turn off the drive motor

! After this we check which root-device to use. If the device is
! defined (! = 0), what is done and the given device is used.
! Otherwise, EITHER/DEV/PS0 (2,28) or/dev/at0 (2,8), depending
! On the number of sectors "the BIOS reports current Ly.
! Linux Floppy drive main device number is 2, the secondary device = Type*4 + NR, of which NR is 0-3 respectively corresponding to the floppy drive a,b,c,d type is
! Floppy drive type 2-1.2m 7-1.44m
! Because 7 * 4 + 0 = 28, so/dev/ps0 (2, 28) refers to the 1.44M a drive, its device number is 0x21c
! Empathy/dev/at0 (2, 8) refers to 1.2M a drive, device number is 0x0208

SEG CS
MOV Ax,root_dev! Get root device number
CMP ax, #0! If you do not jump to root_defined for 0, you have given the root device number
Jne root_defined
! Detecting Root Device numbers
SEG CS! The following code in the CS refers to the China
MOV bx,sectors! Get the number of sectors per track 15-1.2MB drive 18-1.44MB Drive
mov ax, #0x0208! /dev/ps0-1.2mb
CMP BX, #15! Determine if the number of sectors per track is 15
Je root_defined! equals, the device number of the boot drive is the ax
mov ax, #0x021c! /dev/ps0-1.44mb
CMP BX, #18
Je root_defined
Undef_root:! If all is different, freezes
JMP Undef_root
Root_defined:
SEG CS
MOV Root_dev,ax! Save Device number

! After this (everyting loaded), we jump to
! The Setup-routine loaded directly after
! The Bootblock:

Jmpi 0,setupseg! Jump to 0x9020:0000 (SETUP.S program start) execution
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! This program ends!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

! This is routine loads the system at address 0x10000, making sure
! No 64kB boundaries are crossed. We try to load it as fast as
! Possible, loading whole tracks whenever we can.
!
! In:es-starting address segment (normally 0x1000)
!
Sread:. Word 1+setuplen! Sectors read of current track
! Number of read sectors in current track, starting to read 1 sectors Bootsect
Head:. Word 0! Current head
! Current magnetic Number One
Track:. Word 0! Current track
! Current track number

Read_it:
MOV ax,es
Test ax, #0x0fff! The value in ES and 0X0FFF phase, to see if the 0x1000 position
Die:jne die! Es must is at 64kB boundary is not in 0x1000 position to enter dead loop
XOR BX,BX! BX is starting address within segment
! BX is a paragraph in the offset address
Rp_read:
MOV ax,es! Compare ES values with endseg to see if the end of the segment is reached, if not then jump to ok1_read continue reading, otherwise return
CMP ax, #ENDSEG! Have we loaded all yet?
JB Ok1_read
Ret

Ok1_read:
SEG CS! Set one of the following instructions to operate in the China referred to by CS
MOV ax,sectors! Read sector number per track
Sub Ax,sread! Subtract current number of Read sectors
MOV Cx,ax! CX = Current number of unread sectors
SHL CX, #9! CX = CX << 9 = CX * 512 bytes = Total number of bytes not read
Add CX,BX! CX = The current offset within the CX + segment determines whether the number of unread bytes + The offset within the segment exceeds 64KB
JNC Ok2_read! No more than 64KB bytes, jump to Ok2_read execution
Je ok2_read
! The following is the number of unread bytes + The offset in the paragraph is over 64KB
XOR Ax,ax! Empty ax
Sub AX,BX! Ax = AX-BX Gets the maximum number of bytes to read
SHR AX, #9! Convert to number of sectors that also need to be read
Ok2_read:
Call Read_track
MOV Cx,ax! CX = number of slices that have been read since the above call
Add Ax,sread! Update the number of sectors that have been read
SEG CS! The next instruction operation in CS refers to the China
CMP ax,sectors! If there is a sector unread on the current track, jump to Ok3_read
Jne Ok3_read
mov ax, #1! Judge Magnetic Number One
Sub Ax,head! If 0 heads, then read the sector data on the 1 head surface
Jne Ok4_read
INC Track! Otherwise read the next track
Ok4_read:
MOV Head,ax! Save current magnetic Number One
XOR Ax,ax! Clears the number of read sectors for the current track
Ok3_read:
MOV Sread,ax! Save the number of read sectors for the current track
SHL CX, #9! Last read sector * 512 bytes
Add BX,CX! Adjusts the starting position of data within the current segment
JNC Rp_read! If less than 64KB boundary, jump to rp_read Continue reading data
MOV ax,es
Add ax, #0x1000! Adjust the Janki address to point to the next 64KB memory start
MOV Es,ax
XOR BX,BX! Clear the offset within a segment
JMP Rp_read! Jump to Rp_read Continue execution

! Reads the specified start sector and the number of required read sectors on the current track to the beginning of the ES:BX
Read_track:
! Ax,bx,cx,dx like stacks
Push AX
Push BX
Push CX
Push DX
MOV dx,track! Get current track number
MOV Cx,sread! Gets the number of read sectors on the current track
INC CX! CL = start reading sector
MOV ch,dl! CH = Current track number
MOV Dx,head! Take the current word number one
MOV dh,dl! DH = Magnetic Number One
mov dl, #0! DL = Drive letter (0 indicates current drive a)
and DX, #0x0100! The word number one is not greater than 1
mov ah, #2! Ah = 2, read disk sector function number
int 0x13! Call break
JC Bad_rt! Error, jump to Bad_rt
Pop DX! Eject DX, CX, BX, ax
Pop CX
Pop bx
Pop ax
Ret
! Drive reset operation (disk interrupt function number 0), and then jump to Read_track retry
Bad_rt:mov Ax, #0
MOV dx, #0
int 0x13
Pop DX
Pop CX
Pop bx
Pop ax
JMP Read_track

! /*
! * This procedure turns off the floppy drive motor
! * that we enter the kernel in a known state, and
! * don ' t have to worry about it later.
! */
Kill_motor:
Push DX
MOV dx, #0x3f2! Drive port for floppy drive control card, write only
mov al, #0! A drive, turn off FDC, static DMA and interrupt request, turn off the motor
Outb! Output the Al content to the DX-specified port
Pop DX
Ret

Sectors:! Used to hold the number of track sectors
. Word 0

MSG1:
. Byte 13,10! Enter, line-wrap ASCII
. ASCII "Loading system ..."
. Byte 13,10,13,10! A total of 24 ASCII characters

. org 508! Indicates that the following statement starts with 508 (0X1FC)
Root_dev:! Save the device number where the root file system resides (INIT/MAIN.C)
. Word Root_dev
Boot_flag:! Hard disk Valid identification
. Word 0xaa55

. text
Endtext:
. Data
Enddata:
. BSS
ENDBSS:

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.