!转载于哈工大学生制作的gitbook以及源码。https://hoverwinter.gitbooks.io/hit-oslab-manual https://github.com/hoverwinter/HIT-OSLab
-
操作系统的引导
按下电源,运行引导程序(由BIOS加载运行),此时操作系统(操作系统也是软件)还不存在,计算机由引导程序进行资源管理,他可以利用的功能只有BIOS中断调用。
-
Linux 0.11 文件夹中的 boot/bootsect.s的修改
.globl begtext, begdata, begbss, endtext, enddata, endbss
.text
begtext:
.data
begdata:
.bss
begbss:
.text
SETUPLEN = 4 ! setup程序代码占用扇区数
BOOTSEG = 0x07c0 ! bootsect程序代码所在内存原始地址
INITSEG = 0x9000 ! 将bootsect移动到0x9000处
SETUPSEG = 0x9020 ! setup程序开始的地址
entry _start
_start:
! 下面这段代码将自身复制到0x9000处
mov ax,#BOOTSEG
mov ds,ax
mov ax,#INITSEG
mov es,ax
mov cx,#256
sub si,si
sub di,di
rep
movw
! 复制完成从0x9000的go标号处开始执行
jmpi go,INITSEG
go: mov ax,cs
mov ds,ax !设置ds=es=cs
mov es,ax
! 加载setup.s程序
load_setup:
mov dx,#0x0000 ! drive 0, head 0
mov cx,#0x0002 ! sector 2, track 0
mov bx,#0x0200 ! address = 512, in INITSEG
mov ax,#0x0200+SETUPLEN ! service 2, nr of sectors
int 0x13 ! read it
jnc ok_load_setup ! ok - continue
!加载错误
mov dx,#0x0000
mov ax,#0x0000 ! reset the diskette
int 0x13
j load_setup
ok_load_setup:
!输出一些信息
mov ah,#0x03 ! read cursor pos
xor bh,bh
int 0x10
mov cx,#27
mov bx,#0x000c ! page 0, attribute c
mov bp,#msg1 ! es:bp 指向待显示 字符串
mov ax,#0x1301 ! write string, move cursor
int 0x10
!开始执行setup代码
jmpi 0,SETUPSEG
msg1:
.byte 13,10
.ascii "Tonatus is booting..."
.byte 13,10,13,10
.org 510
boot_flag:
.word 0xAA55
.text
endtext:
.data
enddata:
.bss
endbss:
再次编译运行就可以执行了。
编译运行:
Ubuntu上先从终端进入~/oslab/linux-0.11/boot/目录。
运行
as86 -0 -a -o bootsect.o bootsect.s
ld86 -0 -s -o bootsect bootsect.o
其中-0(注意:这是数字0,不是字母O)表示生成8086的16位目标程序,-a表示生成与GNU as和ld部分兼容的代码,-s告诉链接器ld86去除最后生成的可执行文件中的符号信息。
如果这两个命令没有任何输出,说明编译与链接都通过了。
需要留意的文件是bootsect的文件大小是544字节,而引导程序必须要正好占用一个磁盘扇区,即512个字节。造成多了32个字节的原因是ld86产生的是Minix可执行文件格式,这样的可执行文件处理文本段、数据段等部分以外,还包括一个Minix可执行文件头部,它的结构如下:
struct exec {
unsigned char a_magic[2]; //执行文件魔数
unsigned char a_flags;
unsigned char a_cpu; //CPU标识号
unsigned char a_hdrlen; //头部长度,32字节或48字节
unsigned char a_unused;
unsigned short a_version;
long a_text; long a_data; long a_bss; //代码段长度、数据段长度、堆长度
long a_entry; //执行入口地址
long a_total; //分配的内存总量
long a_syms; //符号表大小
};
因此,要去除这32个字节的文件头部
dd bs=1 if=bootsect of=Image skip=32
生成的Image就是去掉文件头的bootsect。
- bootsect.s读入setup.s
首先编写一个setup.s,该setup.s可以就直接拷贝前面的bootsect.s(可能还需要简单的调整),然后将其中的显示的信息改为:“Now we are in SETUP”。
接下来需要编写bootsect.s中载入setup.s的关键代码。原版bootsect.s中下面的代码就是做这个的。
.globl begtext, begdata, begbss, endtext, enddata, endbss
.text
begtext:
.data
begdata:
.bss
begbss:
.text
BOOTSEG = 0x07c0 ! original address of boot-sector
INITSEG = 0x9000 ! we move boot here - out of the way
SETUPSEG = 0x9020 ! setup starts here
entry _start
_start:
!设置cs=ds=es
mov ax,cs
mov ds,ax
mov es,ax
mov ah,#0x03 ! read cursor pos
xor bh,bh
int 0x10
mov cx,#28
mov bx,#0x000c ! page 0, attribute c
mov bp,#msg1
mov ax,#0x1301 ! write string, move cursor
int 0x10
! ok, the read went well so we get current cursor position and save it for
! posterity.
! 获取光标位置 => 0x9000:0
mov ax,#INITSEG ! this is done in bootsect already, but...
mov ds,ax
mov ah,#0x03 ! read cursor pos
xor bh,bh
int 0x10 ! save it in known place, con_init fetches
mov [0],dx ! it from 0x90000.
! Get memory size (extended mem, kB)
! 获取拓展内存大小 => 0x9000:2
mov ah,#0x88
int 0x15
mov [2],ax
! Get hd0 data
! 获取硬盘参数 => 0x9000:80 大小:16B
mov ax,#0x0000
mov ds,ax
lds si,[4*0x41]
mov ax,#INITSEG
mov es,ax
mov di,#0x0080
mov cx,#0x10
rep
movsb
! 前面修改了ds寄存器,这里将其设置为0x9000
mov ax,#INITSEG
mov ds,ax
mov ax,#SETUPSEG
mov es,ax
!显示 Cursor POS: 字符串
mov ah,#0x03 ! read cursor pos
xor bh,bh
int 0x10
mov cx,#11
mov bx,#0x0007 ! page 0, attribute c
mov bp,#cur
mov ax,#0x1301 ! write string, move cursor
int 0x10
!调用 print_hex 显示具体信息
mov ax,[0]
call print_hex
call print_nl
!显示 Memory SIZE: 字符串
mov ah,#0x03 ! read cursor pos
xor bh,bh
int 0x10
mov cx,#12
mov bx,#0x0007 ! page 0, attribute c
mov bp,#mem
mov ax,#0x1301 ! write string, move cursor
int 0x10
!显示 具体信息
mov ax,[2]
call print_hex
!显示相应 提示信息
mov ah,#0x03 ! read cursor pos
xor bh,bh
int 0x10
mov cx,#25
mov bx,#0x0007 ! page 0, attribute c
mov bp,#cyl
mov ax,#0x1301 ! write string, move cursor
int 0x10
!显示具体信息
mov ax,[0x80]
call print_hex
call print_nl
!显示 提示信息
mov ah,#0x03 ! read cursor pos
xor bh,bh
int 0x10
mov cx,#8
mov bx,#0x0007 ! page 0, attribute c
mov bp,#head
mov ax,#0x1301 ! write string, move cursor
int 0x10
!显示 具体信息
mov ax,[0x80+0x02]
call print_hex
call print_nl
!显示 提示信息
mov ah,#0x03 ! read cursor pos
xor bh,bh
int 0x10
mov cx,#8
mov bx,#0x0007 ! page 0, attribute c
mov bp,#sect
mov ax,#0x1301 ! write string, move cursor
int 0x10
!显示 具体信息
mov ax,[0x80+0x0e]
call print_hex
call print_nl
!死循环
l: jmp l
!以16进制方式打印ax寄存器里的16位数
print_hex:
mov cx,#4 ! 4个十六进制数字
mov dx,ax ! 将ax所指的值放入dx中,ax作为参数传递寄存器
print_digit:
rol dx,#4 ! 循环以使低4比特用上 !! 取dx的高4比特移到低4比特处。
mov ax,#0xe0f ! ah = 请求的功能值,al = 半字节(4个比特)掩码。
and al,dl ! 取dl的低4比特值。
add al,#0x30 ! 给al数字加上十六进制0x30
cmp al,#0x3a
jl outp !是一个不大于十的数字
add al,#0x07 !是a~f,要多加7
outp:
int 0x10
loop print_digit
ret
!打印回车换行
print_nl:
mov ax,#0xe0d
int 0x10
mov al,#0xa
int 0x10
ret
msg1:
.byte 13,10
.ascii "Now we are in setup..."
.byte 13,10,13,10
cur:
.ascii "Cursor POS:"
mem:
.ascii "Memory SIZE:"
cyl:
.ascii "KB"
.byte 13,10,13,10
.ascii "HD Info"
.byte 13,10
.ascii "Cylinders:"
head:
.ascii "Headers:"
sect:
.ascii "Secotrs:"
.text
endtext:
.data
enddata:
.bss
endbss:
编译运行
现在有两个文件都要编译、链接。一个个手工编译,效率低下,所以借助Makefile是最佳方式。
在Ubuntu下,进入linux-0.11目录后,使用下面命令(注意大小写):
$ make BootImage
Windows下,在命令行方式,进入Linux-0.11目录后,使用同样的命令(不需注意大小写):
make BootImage
无论哪种系统,都会看到:
Unable to open 'system'
make: *** [BootImage] Error 1
有Error!这是因为make根据Makefile的指引执行了tools/build.c,它是为生成整个内核的镜像文件而设计的,没考虑我们只需要bootsect.s和setup.s的情况。它在向我们要“系统”的核心代码。为完成实验,接下来给它打个小补丁。
build.c从命令行参数得到bootsect、setup和system内核的文件名,将三者做简单的整理后一起写入Image。其中system是第三个参数(argv[3])。当“make all”或者“makeall”的时候,这个参数传过来的是正确的文件名,build.c会打开它,将内容写入Image。而“make BootImage”时,传过来的是字符串"none"。所以,改造build.c的思路就是当argv[3]是"none"的时候,只写bootsect和setup,忽略所有与system有关的工作,或者在该写system的位置都写上“0”。
build.c
#include <stdio.h> /* fprintf */
#include <string.h>
#include <stdlib.h> /* contains exit */
#include <sys/types.h> /* unistd.h needs this */
#include <sys/stat.h>
#include <linux/fs.h>
#include <unistd.h> /* contains read/write */
#include <fcntl.h>
/*
* Changes by falcon<zhangjinw@gmail.com> to define MAJOR and MINOR for they
* are not defined in current linux header file linux/fs.h,I copy it from
* include/linux/fs.h directly.
*/
#ifndef MAJOR
#define MAJOR(a) (((unsigned)(a))>>8)
#endif
#ifndef MINOR
#define MINOR(a) ((a)&0xff)
#endif
#define MINIX_HEADER 32
#define GCC_HEADER 1024
#define SYS_SIZE 0x3000
/*
* Changes by falcon<zhangjinw@gmail.com> to let this kernel Image file boot
* with a root image file on the first hardware device /dev/hd1, hence, you
* should prepare a root image file, and configure the bochs with
* the following lines(please set the ... as suitable info):
* ...
* floppya: 1_44="Image", status=inserted
* ata0-master: type=disk, path="/path/to/rootimage.img", mode=flat ...
* ...
*/
#define DEFAULT_MAJOR_ROOT 3
#define DEFAULT_MINOR_ROOT 1
/* max nr of sectors of setup: don't change unless you also change
* bootsect etc */
#define SETUP_SECTS 4
#define STRINGIFY(x) #x
void die(char * str)
{
fprintf(stderr,"%s\n",str);
exit(1);
}
void usage(void)
{
die("Usage: build bootsect setup system [rootdev] [> image]");
}
int main(int argc, char ** argv)
{
int i,c,id;
char buf[1024];
char major_root, minor_root;
struct stat sb;
if ((argc != 4) && (argc != 5))
usage();
if (argc == 5) {
if (strcmp(argv[4], "FLOPPY")) {
if (stat(argv[4], &sb)) {
perror(argv[4]);
die("Couldn't stat root device.");
}
major_root = MAJOR(sb.st_rdev);
minor_root = MINOR(sb.st_rdev);
} else {
major_root = 0;
minor_root = 0;
}
} else {
major_root = DEFAULT_MAJOR_ROOT;
minor_root = DEFAULT_MINOR_ROOT;
}
fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
if ((major_root != 2) && (major_root != 3) &&
(major_root != 0)) {
fprintf(stderr, "Illegal root device (major = %d)\n",
major_root);
die("Bad root device --- major #");
}
for (i=0;i<sizeof buf; i++) buf[i]=0;
if ((id=open(argv[1],O_RDONLY,0))<0)
die("Unable to open 'boot'");
if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
die("Unable to read header of 'boot'");
if (((long *) buf)[0]!=0x04100301)
die("Non-Minix header of 'boot'");
if (((long *) buf)[1]!=MINIX_HEADER)
die("Non-Minix header of 'boot'");
if (((long *) buf)[3]!=0)
die("Illegal data segment in 'boot'");
if (((long *) buf)[4]!=0)
die("Illegal bss in 'boot'");
if (((long *) buf)[5] != 0)
die("Non-Minix header of 'boot'");
if (((long *) buf)[7] != 0)
die("Illegal symbol table in 'boot'");
i=read(id,buf,sizeof buf);
fprintf(stderr,"Boot sector %d bytes.\n",i);
if (i != 512)
die("Boot block must be exactly 512 bytes");
if ((*(unsigned short *)(buf+510)) != 0xAA55)
die("Boot block hasn't got boot flag (0xAA55)");
buf[508] = (char) minor_root;
buf[509] = (char) major_root;
i=write(1,buf,512);
if (i!=512)
die("Write call failed");
close (id);
if ((id=open(argv[2],O_RDONLY,0))<0)
die("Unable to open 'setup'");
if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
die("Unable to read header of 'setup'");
if (((long *) buf)[0]!=0x04100301)
die("Non-Minix header of 'setup'");
if (((long *) buf)[1]!=MINIX_HEADER)
die("Non-Minix header of 'setup'");
if (((long *) buf)[3]!=0)
die("Illegal data segment in 'setup'");
if (((long *) buf)[4]!=0)
die("Illegal bss in 'setup'");
if (((long *) buf)[5] != 0)
die("Non-Minix header of 'setup'");
if (((long *) buf)[7] != 0)
die("Illegal symbol table in 'setup'");
for (i=0 ; (c=read(id,buf,sizeof buf))>0 ; i+=c )
if (write(1,buf,c)!=c)
die("Write call failed");
close (id);
if (i > SETUP_SECTS*512)
die("Setup exceeds " STRINGIFY(SETUP_SECTS)
" sectors - rewrite build/boot/setup");
fprintf(stderr,"Setup is %d bytes.\n",i);
for (c=0 ; c<sizeof(buf) ; c++)
buf[c] = '\0';
while (i<SETUP_SECTS*512) {
c = SETUP_SECTS*512-i;
if (c > sizeof(buf))
c = sizeof(buf);
if (write(1,buf,c) != c)
die("Write call failed");
i += c;
}
if(strcmp(argv[3],"none")==0) return 0;
if ((id=open(argv[3],O_RDONLY,0))<0)
die("Unable to open 'system'");
// if (read(id,buf,GCC_HEADER) != GCC_HEADER)
// die("Unable to read header of 'system'");
// if (((long *) buf)[5] != 0)
// die("Non-GCC header of 'system'");
for (i=0 ; (c=read(id,buf,sizeof buf))>0 ; i+=c )
if (write(1,buf,c)!=c)
die("Write call failed");
close(id);
fprintf(stderr,"System is %d bytes.\n",i);
if (i > SYS_SIZE*16)
die("System is too big");
return(0);
}
- setup.s获取基本硬件参数
setup.s将获得硬件参数放在内存的0x90000处。原版setup.s中已经完成了光标位置、内存大小、显存大小、显卡参数、第一和第二硬盘参数的保存。
用ah=#0x03调用0x10中断可以读出光标的位置,用ah=#0x88调用0x15中断可以读出内存的大小。有些硬件参数的获取要稍微复杂一些,如磁盘参数表。在PC机中BIOS设定的中断向量表中int 0x41的中断向量位置(4*0x41 = 0x0000:0x0104)存放的并不是中断程序的地址,而是第一个硬盘的基本参数表。第二个硬盘的基本参数表入口地址存于int 0x46中断向量位置处。每个硬盘参数表有16个字节大小。下表给出了硬盘基本参数表的内容:
表1 磁盘基本参数表
位移 大小 说明
0x00 字 柱面数
0x02 字节 磁头数
… … …
0x0E 字节 每磁道扇区数
0x0F 字节 保留
所以获得磁盘参数的方法就是复制数据。
下面是将硬件参数取出来放在内存0x90000的关键代码。
mov ax,#INITSEG
mov ds,ax !设置ds=0x9000
mov ah,#0x03 !读入光标位置
xor bh,bh
int 0x10 !调用0x10中断
mov [0],dx !将光标位置写入0x90000.
!读入内存大小位置
mov ah,#0x88
int 0x15
mov [2],ax
!从0x41处拷贝16个字节(磁盘参数表)
mov ax,#0x0000
mov ds,ax
lds si,[4*0x41]
mov ax,#INITSEG
mov es,ax
mov di,#0x0004
mov cx,#0x10
rep !重复16次
movsb
现在已经将硬件参数(只包括光标位置、内存大小和硬盘参数,其他硬件参数取出的方法基本相同,此处略去)取出来放在了0x90000处,接下来的工作是将这些参数显示在屏幕上。这些参数都是一些无符号整数,所以需要做的主要工作是用汇编程序在屏幕上将这些整数显示出来。
以十六进制方式显示比较简单。这是因为十六进制与二进制有很好的对应关系(每4位二进制数和1位十六进制数存在一一对应关系),显示时只需将原二进制数每4位划成一组,按组求对应的ASCII码送显示器即可。ASCII码与十六进制数字的对应关系为:0x30~0x39对应数字0~9,0x41~0x46对应数字a~f。从数字9到a,其ASCII码间隔了7h,这一点在转换时要特别注意。为使一个十六进制数能按高位到低位依次显示,实际编程中,需对bx中的数每次循环左移一组(4位二进制),然后屏蔽掉当前高12位,对当前余下的4位(即1位十六进制数)求其ASCII码,要判断它是0~9还是a~f,是前者则加0x30得对应的ASCII码,后者则要加0x37才行,最后送显示器输出。以上步骤重复4次,就可以完成bx中数以4位十六进制的形式显示出来。
下面是完成显示16进制数的汇编语言程序的关键代码,其中用到的BIOS中断为INT 0x10,功能号0x0E(显示一个字符),即AH=0x0E,AL=要显示字符的ASCII码。
!以16进制方式打印栈顶的16位数
print_hex:
mov cx,#4 ! 4个十六进制数字
mov dx,(bp) ! 将(bp)所指的值放入dx中,如果bp是指向栈顶的话
print_digit:
rol dx,#4 ! 循环以使低4比特用上 !! 取dx的高4比特移到低4比特处。
mov ax,#0xe0f ! ah = 请求的功能值,al = 半字节(4个比特)掩码。
and al,dl ! 取dl的低4比特值。
add al,#0x30 ! 给al数字加上十六进制0x30
cmp al,#0x3a
jl outp !是一个不大于十的数字
add al,#0x07 !是a~f,要多加7
outp:
int 0x10
loop print_digit
ret
这里用到了一个loop指令,每次执行loop指令,cx减1,然后判断cx是否等于0。如果不为0则转移到loop指令后的标号处,实现循环;如果为0顺序执行。另外还有一个非常相似的指令:rep指令,每次执行rep指令,cx减1,然后判断cx是否等于0,如果不为0则继续执行rep指令后的串操作指令,直到cx为0,实现重复。
!打印回车换行
print_nl:
mov ax,#0xe0d ! CR
int 0x10
mov al,#0xa ! LF
int 0x10
ret