以下源代码来源于linux2.6.26.5的arch/x86/boot/header.S
- /*
- * header.S
- *
- * Copyright (C) 1991, 1992 Linus Torvalds
- *
- * Based on bootsect.S and setup.S
- * modified by more people than can be counted
- *
- * Rewritten as a common file by H. Peter Anvin (Apr 2007)
- *
- * 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/segment.h>
- #include <linux/utsrelease.h>
- #include <asm/boot.h>
- #include <asm/e820.h>
- #include <asm/page.h>
- #include <asm/setup.h>
- #include "boot.h"
- #include "offsets.h"
- SETUPSECTS = 4 /* default nr of setup-sectors */
- BOOTSEG = 0x07C0 /* original address of boot-sector */
- SYSSEG = DEF_SYSSEG /* system loaded at 0x10000 (65536) */
- SYSSIZE = DEF_SYSSIZE /* system size: # of 16-byte clicks */
- /* 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
- #从2.6内核开始linux不再支持从软盘引导系统,因此如果从这里开始引导系统
- #必然会出错,以下注释仅供学习之用,我会尽量努力把内核启动部分代码都加
- #上注释,但不保证正确性。
- .code16
- .section ".bstext", "ax"
- .global bootsect_start
- bootsect_start:
- # Normalize the start address
- ljmp $BOOTSEG, $start2 #跳转到start2,这个是一个远跳转,注意其语法为:ljmp $段,$段内偏移
- start2:
- movw %cs, %ax #%cs内容为0x07c0
- movw %ax, %ds
- movw %ax, %es
- movw %ax, %ss
- xorw %sp, %sp #%sp清零
- sti #开中断
- cld #清除eflags方向位,为msg_loop后的语句做准备
- movw $bugger_off_msg, %si #将bugger_of_msg标示所在内存地址放入%si
- msg_loop:
- lodsb #从%ds:%si处加载1字节到%al
- andb %al, %al #连同下一条语句判断%al是否为0
- jz bs_die #若%al为0则跳转到bs_die处
- movb $0xe, %ah #若%al不为0,则调用bios中断程序显示%al中的字节,0xe是功能码
- movw $7, %bx #%bh=0x00 则为page 0,%bl=0x07指明字体颜色
- int $0x10 #中断号0x10为显示字符中断
- jmp msg_loop #跳转到msg_loop处循环显示,每次显示一个字节
- bs_die:
- # Allow the user to press a key, then reboot
- xorw %ax, %ax #清零%ax
- int $0x16 #调用0x16号bios中断,该中断在%ax为零的时候等待用户按任意键
- int $0x19 #调用0x19号bios中断重启计算机
- # int 0x19 should never return. In case it does anyway,
- # invoke the BIOS reset code...
- ljmp $0xf000,$0xfff0 #这条语句应当不会执行,若执行到这里,则这条语句会模拟用户按下reset建
- .section ".bsdata", "a"
- 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