Detailed analysis of Linux kernel boot program

Source: Internet
Author: User
Article title: detailed analysis of Linux kernel boot programs. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
This program starts the boot program in the Linux operating system. for the principle, see the Linux kernel
  
Roaming.
  
Gohigh@shtdu.edu.cn www.freedove.com
  
! Bootsect. s (c) 1991,199 2 Linus Torvalds copyright
  
! Drew Eckhardt modified
  
! Bruce Evans (bde) modified
  
!
  
! Bootsect. s is loaded to 0x7c00 (31 k) by bios-start subroutine, and
  
! Moved to the address 0x90000 (576 k) and jumped to it.
  
!
  
! Bde-do not jump blindly. some systems may only have a 512k low
  
! Memory. Use interrupt 0x12 to obtain the maximum memory (system), and so on.
  
!
  
! It then uses BIOS interrupt to directly load setup to its own backend (0x90200) (576.5 k ),
  
! And load the system to the address 0x10000.
  
!
  
! Note! The maximum length of the current kernel system is (8*65536-4096) (508 k) bytes long, even in
  
! In the future, this is no problem. I want to keep it simple and clear. In this way, the maximum kernel length of KB should be
  
! It is sufficient, especially because there is no buffer high-speed buffer as in minix (and especially now
  
! The kernel is compressed :-)
  
!
  
! Loading programs is as simple as possible, so continuous read errors will lead to an endless loop. Only manual restart is allowed.
  
! As long as possible, the entire track can be acquired at a time, and the loading process can be very fast.
  
# Include/* obtain the CONFIG_ROOT_RDONLY parameter */
  
!! CONFIG_ROOT_RDONLY definition is not defined in config. h (that is, in autoconf. h !!!?
  
# Include
  
. Text
  
  
SETUPSECS = 4! Default value of the number of setup-sectors;
  
BOOTSEG = 0x7C0! Original address of bootsect;
  
INITSEG = DEF_INITSEG! Move the bootsect program to this segment (0x9000)-avoid;
  
SETUPSEG = DEF_SETUPSEG! Setup starts from here (0x9020 );
  
SYSSEG = DEF_SYSSEG! The system is loaded to the 0x1000 (65536) (64 k) segment;
  
SYSSIZE = DEF_SYSSIZE! System size (0x7F00): The number of 16 bytes to be loaded as one node;
  
!! The preceding four DEF _ parameters are defined in boot. h:
  
!! DEF_INITSEG 0x9000
  
!! DEF_SYSSEG 0x1000
  
!! DEF_SETUPSEG 0x9020
  
!! DEF_SYSSIZE 0x7F00 (= 32512 = 31.75 k) * 16 = 508 k
  
  
  
! ROOT_DEV & SWAP_DEV is now compiled by "build;
  
ROOT_DEV = 0
  
SWAP_DEV = 0
  
# Ifndef SVGA_MODE
  
# Define SVGA_MODE ASK_VGA
  
# Endif
  
# Ifndef RAMDISK
  
# Define RAMDISK 0
  
# Endif
  
# Ifndef CONFIG_ROOT_RDONLY
  
# Define CONFIG_ROOT_RDONLY 1
  
# Endif
  
! Ld86 requires an entry identifier, which is the same as usual;
  
. Globl _ main
  
_ Main:
  
# If 0/* the abnormal branch of the debugging program is harmless unless the BIOS is odd (such as the old HP machine */
  
Int 3
  
# Endif
  
Mov ax, # BOOTSEG !! Set the ds segment register to 0x7C0;
  
Mov ds, ax
  
Mov ax, # INITSEG !! Set the es segment register to 0x9000;
  
Mov es, ax
  
Mov cx, #256 !! Set the cx counter to 256 (256 characters to be moved, 512 bytes );
  
Sub si, si !! SOURCE address ds: si = 0x07C0: 0x0000;
  
Sub di, di !! Target address es: di = 0x9000: 0x0000;
  
Cld !! Clear direction flag;
  
Rep !! Move the program from 0x7C0: 0 (31 k) to 0x9000: 0 (576 k;
  
Movsw !! A total of 256 characters (512 bytes) (0x200 long );
  
Jmpi go, INITSEG !! Indirectly jump to the go area of the mobile program;
  
! Ax and es now have the INITSEG value (0x9000 );
  
  
  
Go: mov di, #0x4000-12! 0x4000 (16 k) Yes> = bootsect + setup length +
  
! + Any value of the stack length;
  
! 12 is the size of the disk parameter block es: di = 0x94000-12 = 592k-12;
  
! Bde-change 0xff00 to 0x4000 to use the debug program (bde) from 0x6400 ). If
  
! If we have detected the highest memory, we don't have to worry about this. Also, my BIOS can be configured to drive wini.
  
Table
  
! Put it in the memory rather than in the vector table. Old-fashioned stack areas may mess up the drive tables;
  
  
  
Mov ds, ax! Set the ds data segment to 0x9000;
  
Mov ss, ax! Set the stack segment to 0x9000;
  
Mov sp, di! Stack Pointer INITSEG: 0x4000-12;
  
/*
  
* Many default disk parameter tables of BIOS cannot
  
* The number of slices is greater than specified in the table.
  
* Maximum number of sectors (-in some cases
  
* This means 7 sectors.
  
*
  
* The Reading operation on a single sector is very slow and of course there is no problem,
  
* A new parameter table must be created in RAM (for the first disk.
  
* We will set the maximum number of sectors to 36-what we can do on an ED 2.88 drive
  
* Maximum value.
  
*
  
* If this value is too high, there is no harm, but it will be a problem if it is low.
  
*
  
* The segment register is like this: ds = es = ss = cs-INITSEG, (= 0X9000)
  
* Fs = 0, gs is not used.
  
*/
  
! After repeated operations (rep) are executed above, cx is 0;
  
  
  
Mov fs, cx !! Set fs segment register = 0;
  
Mov bx, #0x78! Fs: bx is the address of the disk parameter table;
  
Push ds
  
Seg fs
  
Lds si, (bx )! Ds: si is the source address;
  
!! Put the pointer value indicated by fs: bx address in ds: si;
  
Mov cl, #6! Copy 12 bytes to the beginning of 0x9000: 0x4000-12;
  
Cld
  
Push di !! Pointer 0x9000: 0x4000-12;
  
Rep
  
Movsw
  
Pop di !! Di still points to 0x9000: 0x4000-12 (beginning of the parameter table );
  
Pop si !! Ds => si = INITSEG (= 0X9000 );
  
Movb 4 (di), * 36! Corrected the slice count value;
  
Seg fs
  
Mov (bx), di !! Modify the address of the disk parameter table at fs: bx (0000: 0x0078) to 0x9000: 0x4000-12;
  
Seg fs
  
Mov 2 (bx), es
  
! Directly load the setup-sectors sector to the end of the boot block .!! Starting from 0x90200
  
;
  
! Note that es has been set.
  
! Similarly, after the rep loop, cx is 0.
  
Load_setup:
  
Xor ah, ah! Reset the soft Drive (FDC );
  
Xor dl, dl
  
Int 0x13
  
Xor dx, dx! Drive 0, Head 0;
  
Mov cl, #0x02! Starting from Sector 2, the track is 0;
  
Mov bx, #0x0200! Set data buffer address = es: bx = 0x9000: 0x200;
  
! In the INITSEG segment, that is, 0x90200;
  
Mov ah, #0x02! Function Number 2 (read operation) to be called );
  
Mov al, setup_sects! Number of sectors to be read SETUPSECS = 4;
  
! (Parole all data is on the head 0 and track 0 );
  
Int 0x13! Read operations;
  
Jnc OK _load_setup! OK to continue;
  
  
  
Push ax! Otherwise, the error message is displayed. Save the value of ah (feature number 2 );
  
Call print_nl !! Print line breaks;
  
Mov bp, sp !! Bp will be used as a parameter to call print_hex;
  
Call print_hex !! Print the data referred to by bp;
  
Pop ax
  
Jmp load_setup !! Retry!
  
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  
!! INT 13-DISK-read sector (S) INTO MEMORY
  
!! AH = 02 h
  
!! AL = number of sectors to read (must be nonzero)
  
!! CH = low eight bits of cylinder number
  
!! CL = sector number 1-63 (bits 0-5)
  
!! High two bits of cylinder (bits 6-7, hard disk only)
  
!! DH = head number
  
!! DL = drive number (bit 7 set for hard disk)
  
!! ES: BX-> data buffer
  
!! Return: CF set on error
  
!! If AH = 11 h (corrected ECC error), AL = burst length
  
!! CF clear if successful
  
!! AH = status (see #00234)
  
!! AL = number of sectors transferred (only valid if CF set for some
  
!! BIOSes)
  
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  
  
OK _load_setup:
  
! Obtain disk drive parameters, especially the number of sectors per track (nr of sectors/track );
  
  
# If 0
  
! Bde-Phoenix BIOS manual mentioned that feature 0x08 only works on hard disks.
  
! But it does not work for one of my BIOS (1987 Award.
  
! If you do not check the error code, it is a fatal error.
  
  

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.