Linux-0.11 操作系统的引导实验

Linux-0.11 操作系统的引导实验

实验目的

  • 熟悉 hit-oslab 实验环境;
  • 建立对操作系统引导过程的深入认识;
  • 掌握操作系统的基本开发过程;
  • 能对操作系统代码进行简单的控制,揭开操作系统的神秘面纱。

实验内容

此次实验的基本内容是:

  1. 阅读《Linux 内核完全注释》的第 6 章,对计算机和 Linux 0.11 的引导过程进行初步的了解;
  2. 按照下面的要求改写 0.11 的引导程序 bootsect.s
  3. 有兴趣同学可以做做进入保护模式前的设置程序 setup.s。

改写 bootsect.s 主要完成如下功能:

  1. bootsect.s 能在屏幕上打印一段提示信息“XXX is booting…”,其中 XXX 是你给自己的操作系统起的名字,例如 LZJos、Sunix 等(可以上论坛上秀秀谁的 OS 名字最帅,也可以显示一个特色 logo,以表示自己操作系统的与众不同。)

改写 setup.s 主要完成如下功能:

  1. bootsect.s 能完成 setup.s 的载入,并跳转到 setup.s 开始地址执行。而 setup.s 向屏幕输出一行"Now we are in SETUP"。
  2. setup.s 能获取至少一个基本的硬件参数(如内存参数、显卡参数、硬盘参数等),将其存放在内存的特定地址,并输出到屏幕上。
  3. setup.s 不再加载 Linux 内核,保持上述信息显示在屏幕上即可。

参考资料:Linux内核完全注释 第六章

实验步骤

1.改写bootsect.s

只需要保留 输出字符串部分的代码

  1. 需要修改的是字符串长度,即用需要输出的字符串长度替换 mov cx,#24 中的 24。要注意:除了我们设置的字符串 msg1 之外,还有三个换行 + 回车,一共是 6 个字符。比如这里 Welcome to Beauty’s system … 的长度是 30,加上 6 后是 36,所以代码应该修改为 mov cx,#36。
  2. 输出我们想要的字符串 就要将msg1数据段的字符修改,我的修改是Welcome to Beauty’s system …
entry _start
_start:
    mov ah,#0x03
    xor bh,bh
    int 0x10
    mov cx,#36
    mov bx,#0x0007
    mov bp,#msg1
    mov ax,#0x07c0
    mov es,ax
    mov ax,#0x1301
    int 0x10
inf_loop:
    jmp inf_loop
msg1:
    .byte   13,10
    .ascii  "Welcome to Beauty's system ..."
    .byte   13,10,13,10
.org 510
boot_flag:
    .word   0xAA55

编译链接

$ as86 -0 -a -o bootsect.o bootsect.s
$ ld86 -0 -s -o bootsect bootsect.o

需要留意的文件是 bootsect 的文件大小是 544 字节,而引导程序必须要正好占用一个磁盘扇区,即 512 个字节。造成多了 32 个字节的原因是 ld86 产生的是 Minix 可执行文件格式,这样的可执行文件除了文本段、数据段等部分以外,还包括一个 Minix 可执行文件头部
要去掉这 32 个字节的文件头部

$ dd bs=1 if=bootsect of=Image skip=32

去掉这 32 个字节后,将生成的文件拷贝到 linux-0.11 目录

cp ./Image ../Image

运行

../../run

结果图

在这里插入图片描述

2.改写setup.s

2.1.setup.s 向屏幕输出一行"Now we are in SETUP"

这部分的功能跟bootsect.s的功能差不多,只需要copy过来代码,修改字符串和部分代码即可

entry _start
_start:
    mov ah,#0x03
    xor bh,bh
    int 0x10
    mov cx,#25
    mov bx,#0x0007
    mov bp,#msg2
    mov ax,cs !这里要修改es的值,可以用cs的值来代替
    mov es,ax
    mov ax,#0x1301
    int 0x10
inf_loop:
    jmp inf_loop
msg2:
    .byte   13,10
    .ascii "Now we are in SETUP"
    .byte   13,10,13,10
.org 510
boot_flag:
    .word   0xAA55

2.2.bootsect.s 能完成 setup.s 的载入

就是在原来的代码中添加一部分载入setup.s的代码 这一部分在原码中有实现的部分 直接拿来用即可

SETUPLEN=2
SETUPSEG=0x07e0
entry _start
_start:
    mov ah,#0x03
    xor bh,bh
    int 0x10
    mov cx,#36
    mov bx,#0x0007
    mov bp,#msg1
    mov ax,#0x07c0
    mov es,ax
    mov ax,#0x1301
    int 0x10

!其实修改 就是将之前的无限循环变为这部分的代码 而这部分就是载入setup
load_setup:
    mov dx,#0x0000
    mov cx,#0x0002
    mov bx,#0x0200
    mov ax,#0x0200+SETUPLEN
    int 0x13
    jnc ok_load_setup
    mov dx,#0x0000
    mov ax,#0x0000
    int 0x13
    jmp load_setup

ok_load_setup:
    jmpi    0,SETUPSEG

msg1:
    .byte   13,10
    .ascii "Welcome to Beauty's system ..."
    .byte   13,10,13,10

.org 510
boot_flag:
    .word   0xAA55

再次编译

#在Linux-0.11目录下
make BootImage

有 Error!这是因为 make 根据 Makefile 的指引执行了 tools/build.c,它是为生成整个内核的镜像文件而设计的,没考虑我们只需要 bootsect.s 和 setup.s 的情况。它在向我们要 “系统” 的核心代码。为完成实验,接下来给它打个小补丁。
修改工作主要集中在 build.c 的尾部,可以参考下面的方式,将圈起来的部分注释掉。

在这里插入图片描述
重新编译运行

make BootImage
../run
结果图

在这里插入图片描述

2.3.setup.s 能获取至少一个基本的硬件参数

复用原来的代码,显示基本硬件参数

entry _start
_start:
! Print "NOW we are in SETUP"
    mov ah,#0x03  
    xor bh,bh
    int 0x10
    mov cx,#25
    mov bx,#0x0007
    mov bp,#msg2
    mov ax,cs
    mov es,ax
    mov ax,#0x1301  
    int 0x10
    mov ax,cs
    mov es,ax

! Get memory size (extended mem, kB)
    mov ax,#INITSEG
    mov ds,ax
    mov ah,#0x88
    int 0x15
    mov [2],ax

! Get disk params
    mov ax,#0x0000
    mov ds,ax
    lds si,[4*0x41]
    mov ax,#INITSEG
    mov es,ax
    mov di,#0x0004
    mov cx,#0x10
    rep
    movsb

! Restore es and ds
    mov ax,cs
    mov es,ax
    mov ax,#INITSEG
    mov ds,ax

! Print "Memory Size:"
    mov ah,#0x03
    xor bh,bh
    int 0x10
    mov cx,#14
    mov bx,#0x0007
    mov bp,#msg_memory
    mov ax,#0x1301
    int 0x10

! Print memory size
    mov dx,[2]
    call print_hex

! Print "KB"
    call kb_print

! Cyles
    mov ah,#0x03  
    xor bh,bh
    int 0x10
    mov cx,#7
    mov bx,#0x0007
    mov bp,#msg_cyles
    mov ax,#0x1301
    int 0x10
    mov dx,[4]
    call print_hex

! Heads
    mov ah,#0x03
    xor bh,bh
    int 0x10
    mov cx,#8
    mov bx,#0x0007
    mov bp,#msg_heads
    mov ax,#0x1301  
    int 0x10
    mov dx,[6]
    call print_hex

! Secotrs
    mov ah,#0x03
    xor bh,bh
    int 0x10
    mov cx,#10
    mov bx,#0x0007
    mov bp,#msg_sectors
    mov ax,#0x1301
    int 0x10
    mov dx,[12]
    call print_hex

inf_loop:
    jmp inf_loop

kb_print:
    mov ah,#0x03
    xor bh,bh
    int 0x10
    mov cx,#2
    mov bx,#0x0007
    mov bp,#msg_kb
    mov ax,#0x1301  
    int 0x10
    ret

print_hex:
    mov    cx,#4
print_digit:
    rol    dx,#4
    mov    ax,#0xe0f  
    and    al,dl  
    add    al,#0x30
    cmp    al,#0x3a
    jl     outp  
    add    al,#0x07  
outp:
    int    0x10
    loop   print_digit
    ret
print_nl:
    mov    ax,#0xe0d
    int    0x10
    mov    al,#0xa
    int    0x10
    ret

msg2:
    .byte 13,10
    .ascii "NOW we are in SETUP"
    .byte 13,10,13,10
msg_memory:
    .byte 13,10
    .ascii "Memory Size:"
msg_cyles:
    .byte 13,10
    .ascii "Cyls:"
msg_heads:
    .byte 13,10
    .ascii "Heads:"
msg_sectors:
    .byte 13,10
    .ascii "Sectors:"
msg_kb:
    .ascii "KB"

结果图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值