Boot analysis of linux kernel version 2.6.16

See this blog post for details of the computer startup process Computer boot process Briefly, after pressing the power key, cs:ip is set to 0xffff0, which is fixed. The BIOS program is recorded on it. The BIOS performs basic hardware self-test, establishes an interrupt vector table, initializes BIOS interrupts, etc. next, the first sector (512Byte) of the first disk will be loaded to the memory position of 0x7c00, and then the kernel will take over, The implementation of different versions of kernel is also different
/The arch directory is architecture related code. Machines with different architectures have different characteristics and implementation. Therefore, there are different machine directories under the arch directory, as shown in the figure below. Since the startup code is still in real mode at the time of startup, it should be implemented by assembly, which is related to the architecture of the machine

Next, let's look at the directory structure of common x86 architectures

Originally, looking at this directory, I thought it should be BIOS loading bootect and bootect loading setup, but after reading bootect, I found that it was not. This bootect was abandoned!!! I checked and said that it was before version 2.4. After BIOS executes and plays its own code, it will load bootect. S to 0x7c00. During bootect execution, it will move itself to the beginning of 0x90000 of the absolute memory address and continue to execute. Bootect is mainly used as the setup module of the four sectors starting from the second sector of the disk (compiled by setup.S) Load it into the memory immediately after bootect (0x902000), and then load the system module behind the setup module on the disk to the beginning of memory 0x10000
After 2.4, I was studying... The following are the notes of bootect. Don't look, it's useless

#ifndef _LINUX_BOOT_H
#define _LINUX_BOOT_H

/* Don't touch these, unless you really know what you're doing. */
#define DEF_INITSEG 	 0x9000 / / the address of the memory segment to which bootect will move itself
#define DEF_SYSSEG 	 0x1000 / / segment address loaded by system module
#define DEF_SETUPSEG 	 0x9020 / / segment address loaded by setup module
#define DEF_SYSSIZE 	 0x7F00 / / default system module length

/* Internal svga startup constants */
#define NORMAL_VGA	0xffff		/* 80x25 mode */
#define EXTENDED_VGA	0xfffe		/* 80x50 mode */
#define ASK_VGA		0xfffd		/* ask for it at bootup */

#endif
/*
 *	bootsect.S		Copyright (C) 1991, 1992 Linus Torvalds
 *
 *	modified by Drew Eckhardt
 *	modified by Bruce Evans (bde)
 *	modified by Chris Noe (May 1999) (as86 -> gas)
 *	gutted by H. Peter Anvin (Jan 2003)
 *
 * BIG FAT NOTE: We're in real mode using 64k segments.  Therefore segment
 * addresses must be multiplied by 16 to obtain their respective linear
 * addresses. To avoid confusion, linear addresses are written using leading
 * hex while segment addresses are written as segment:offset.
 *
 */

#include <asm/boot.h>

SETUPSECTS	= 4			/* default nr of setup-sectors */ 
						/* setup Four sectors of the disk starting from the second sector */
BOOTSEG		= 0x07C0		/* original address of boot-sector */
							/* bootsect Will be loaded to 0x7c00 by BIOS */
INITSEG		= DEF_INITSEG		/* we move boot here - out of the way */
								/* Move the bootect position to position 0x90000 - avoid the use of the system module */
SETUPSEG	= DEF_SETUPSEG		/* setup starts here */
								/* setup The program starts at 0x90200 */
SYSSEG		= DEF_SYSSEG		/* system loaded at 0x10000 (65536) */
								/* system Module loaded at 0x10000(64KB) */
SYSSIZE		= DEF_SYSSIZE		/* system size: # of 16-byte clicks */
								/* system Length of module */
					/* to be loaded */
ROOT_DEV	= 0 			/* ROOT_DEV is now written by "build" */
SWAP_DEV	= 0			/* SWAP_DEV is now written by "build" */

#ifndef SVGA_MODE
#define SVGA_MODE ASK_VGA
#endif

#ifndef RAMDISK
#define RAMDISK 0
#endif

#ifndef ROOT_RDONLY
#define ROOT_RDONLY 1
#endif

.code16
.text

.global _start
_start:

	# Normalize the start address
	jmpl	$BOOTSEG, $start2

start2:
	/* Initialize the segment registers ds, es, ss so that they are equal to cs*/
	movw	%cs, %ax
	movw	%ax, %ds
	movw	%ax, %es
	movw	%ax, %ss
	movw	$0x7c00, %sp    /* Initialize stack pointer */
	sti						/* Set interrupt */
	cld						/* Clear direction sign */

	movw	$bugger_off_msg, %si  /* The register si points to the character to be displayed */

/* Display bug character by character_ off_ MSG content */
msg_loop:
	lodsb    /* lodsb The function of is to take the contents of ds:[si] into al, and then si = si + 1 */
	andb	%al, %al  
	jz	die  /* jz Is jmp if zero. If the value of al phase and is 0, skip to die */
	/* Display a character on the screen, advance the cursor and scroll, and display the screen as needed */
	movb	$0xe, %ah  /* BIOS Interrupt function number ah = 0x13 */
	movw	$7, %bx    /* [bh,bl]bh = Display page number, bl = character attribute */
	int	$0x10
	jmp	msg_loop

die:
	# Allow the user to press a key, then reboot
	xorw	%ax, %ax
	int	$0x16        /* Read a character from the keyboard, al = character code, ah = scan code */
	int	$0x19        /* Find and load the MBR to 0x7c00, and restart it */

	# int 0x19 should never return.  In case it does anyway,
	# invoke the BIOS reset code...
	ljmp	$0xf000,$0xfff0   /* To prevent the above from returning, jump to 0xffff0 and restart the BIOS program */


bugger_off_msg:
	.ascii	"Direct booting from floppy is no longer supported.\r\n"
	.ascii	"Please use a boot loader program instead.\r\n"
	.ascii	"\n"
	.ascii	"Remove disk and press any key to reboot . . .\r\n"
	.byte	0


	# Kernel attributes; used by setup

	.org 497
setup_sects:	.byte SETUPSECTS
root_flags:	.word ROOT_RDONLY
syssize:	.word SYSSIZE
swap_dev:	.word SWAP_DEV
ram_size:	.word RAMDISK
vid_mode:	.word SVGA_MODE
root_dev:	.word ROOT_DEV
boot_flag:	.word 0xAA55
# I cry!!!!!!!!! I have studied for a long time. Starting from 2.6, the system boot bootect function is no longer supported. If you boot from the bootect in this file, you will be prompted to press any key to restart the computer......... Ordinary genius, I emo

Keywords: Linux

Added by the_lynx123 on Tue, 19 Oct 2021 04:21:26 +0300