type
Post
status
Published
slug
2023/07/11/Booting-of-the-HIT-OSLab-LAB1-operating-system
summary
操作系统的引导
tags
Linux
开发
category
Linux
icon
password
new update day
Property
Oct 22, 2023 01:31 PM
created days
Last edited time
Oct 22, 2023 01:31 PM

0 实验内容

此次实验的基本内容是:
  1. 阅读《Linux 内核完全注释》的第 6 章,对计算机和 Linux 0.11 的引导过程进行初步的了解;
  1. 按照下面的要求改写 0.11 的引导程序 bootsect.s
  1. 有兴趣同学可以做做进入保护模式前的设置程序 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"。
  1. setup.s 能获取至少一个基本的硬件参数(如内存参数、显卡参数、硬盘参数等),将其存放在内存的特定地址,并输出到屏幕上。
  1. setup.s 不再加载 Linux 内核,保持上述信息显示在屏幕上即可。

1 实验要求一

1.1 bootsect.s 代码编写

.globl begtext, begdata, begbss, endtext, enddata, endbss .text begtext: .data begdata: .bss begbss: .text SETUPLEN = 4 ! nr of setup-sectors 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: mov ax,#BOOTSEG mov ds,ax mov ax,#INITSEG mov es,ax mov cx,#256 sub si,si sub di,di rep movw jmpi go,INITSEG go: mov ax,cs mov ds,ax mov es,ax ok_load_setup: ! Print some inane message mov ah,#0x03 ! read cursor pos xor bh,bh int 0x10 mov cx,#27 mov bx,#0x0007 ! page 0, attribute 7 (normal) mov bp,#msg1 mov ax,#0x1301 ! write string, move cursor int 0x10 jmpi 0,SETUPSEG inf_loop: jmp inf_loop msg1: .byte 13,10 .ascii "FishOS is booting ..." !21 .byte 13,10,13,10 .org 510 boot_flag: .word 0xAA55 .text endtext: .data enddata: .bss endbss:

1.2 执行测试

notion image

2 实验要求二

2.1 bootsect.s 代码编写

.globl begtext, begdata, begbss, endtext, enddata, endbss .text begtext: .data begdata: .bss begbss: .text SETUPLEN = 4 ! nr of setup-sectors 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: mov ax,#BOOTSEG mov ds,ax mov ax,#INITSEG mov es,ax mov cx,#256 sub si,si sub di,di rep movw jmpi go,INITSEG go: mov ax,cs mov ds,ax mov es,ax ! load the setup-sectors directly after the bootblock. ! Note that 'es' is already set up. 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: ! Print some inane message mov ah,#0x03 ! read cursor pos xor bh,bh int 0x10 mov cx,#27 mov bx,#0x0007 ! page 0, attribute 7 (normal) mov bp,#msg1 mov ax,#0x1301 ! write string, move cursor int 0x10 jmpi 0,SETUPSEG #inf_loop: # jmp inf_loop msg1: .byte 13,10 .ascii "FishOS is booting ..." !21 .byte 13,10,13,10 .org 510 boot_flag: .word 0xAA55 .text endtext: .data enddata: .bss endbss:

2.2 setup.s 代码编写

INITSEG = 0x9000 ! we move boot here - out of the way SETUPSEG = 0x9020 ! this is the current segment .globl begtext, begdata, begbss, endtext, enddata, endbss .text begtext: .data begdata: .bss begbss: .text entry start start: ! ok, the read went well so we get current cursor position and save it for ! posterity. 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) mov ah,#0x88 int 0x15 mov [2],ax ! Get video-card data: mov ah,#0x0f int 0x10 mov [4],bx ! bh = display page mov [6],ax ! al = video mode, ah = window width ! check for EGA/VGA and some config parameters mov ah,#0x12 mov bl,#0x10 int 0x10 mov [8],ax mov [10],bx mov [12],cx ! Get hd0 data mov ax,#0x0000 mov ds,ax lds si,[4*0x41] mov ax,#INITSEG mov es,ax mov di,#0x0080 mov cx,#0x10 rep movsb ! Get hd1 data mov ax,#0x0000 mov ds,ax lds si,[4*0x46] mov ax,#INITSEG mov es,ax mov di,#0x0090 mov cx,#0x10 rep movsb ! 前面修改了ds寄存器,这里将其设置为0x9000 mov ax,#INITSEG mov ds,ax mov ax,#SETUPSEG mov es,ax print_msg: mov ah,#0x03 xor bh,bh int 0x10 ! 显示字符串“LZJos is running...” mov cx,#25 ! 要显示的字符串长度 mov bx,#0x0007 ! page 0, attribute 7 (normal) mov bp,#msg1 mov ax,#0x1301 ! write string, move cursor int 0x10 mov ah,#0x03 xor bh,bh int 0x10 mov cx,#11 ! 要显示的字符串长度 mov bx,#0x0007 ! page 0, attribute 7 (normal) mov bp,#cur_msg mov ax,#0x1301 ! write string, move cursor int 0x10 mov bp,[0] call print_hex call print_nl ! Memory SIZE: mov ah,#0x03 xor bh,bh int 0x10 mov cx,#12 ! 要显示的字符串长度 mov bx,#0x0007 ! page 0, attribute 7 (normal) mov bp,#mem_msg mov ax,#0x1301 ! write string, move cursor int 0x10 mov bp,[2] call print_hex mov ah,#0x03 xor bh,bh int 0x10 mov cx,#25 ! 要显示的字符串长度 mov bx,#0x0007 ! page 0, attribute 7 (normal) mov bp,#cyl_msg mov ax,#0x1301 ! write string, move cursor int 0x10 mov bp,[0x0080] call print_hex call print_nl mov ah,#0x03 xor bh,bh int 0x10 mov cx,#6 ! 要显示的字符串长度 mov bx,#0x0007 ! page 0, attribute 7 (normal) mov bp,#head_msg mov ax,#0x1301 ! write string, move cursor int 0x10 !显示 具体信息 mov ax,[0x80+0x02] call print_hex call print_nl !显示 提示信息 mov ah,#0x03 xor bh,bh int 0x10 mov ah,#0x03 ! read cursor pos xor bh,bh int 0x10 mov cx,#8 mov bx,#0x0007 ! page 0, attribute c mov bp,#sect_msg mov ax,#0x1301 ! write string, move cursor int 0x10 !显示 具体信息 mov ax,[0x80+0x0e] call print_hex call print_nl inf_loop: jmp inf_loop ! 后面都不是正经代码了,得往回跳呀 ! msg1处放置字符串 !以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 !打印回车换行 print_nl: mov ax,#0xe0d ! CR int 0x10 mov al,#0xa ! LF int 0x10 ret msg1: .byte 13,10 ! 换行+回车 .ascii "Now we are in SETUP" .byte 13,10,13,10 call print_nl cur_msg: .ascii "Cursor POS:" mem_msg: .ascii "Memory SIZE:" cyl_msg: .ascii "KB" .byte 13,10,13,10 .ascii "HD Info" .byte 13,10 .ascii "Cylinders:" head_msg: .ascii "Heads:" sect_msg: .ascii "Sectors:" .text endtext: .data enddata: .bss endbss:

2.3 运行测试

notion image

参考资料

 
欢迎加入喵星计算机技术研究院,原创技术文章第一时间推送。
notion image
 
linux 0.11 修改时间片大小后的表现make INSTALL_MOD_STRIP=1 modules_install