我写的bootloader代码,用nasm编写

来源:互联网 发布:软件可靠性分析方法 编辑:程序博客网 时间:2024/05/21 10:31

启动系统,并进入pro模式!
如果软盘有kernerl.bin,就执行!
系统使用fat格式,并用winimage写入数据
kernerl.bin的读取有bug只能读连续扇区,部分代码引自网络


[bits 16]
[org 0x7c00] ; 告诉编译器程序加载到7c00处

CATALOG equ 0x7E00 ;catalog loaded at 0x7e00
KERNEL equ 0x500 ;kernel.bin loaded at 0x500.
jmp Start
; 引导区文件系统数据
;----------------------------------------------------------------------------
brOEM DB " T's OS " ; 0003h - 引导程序的名字
brBPS DW 0x200 ; 000Bh - 每扇区的字节数 512
brSPC DB 0x01 ; 000Dh - 每簇扇区数
brResCount DW 0x0001 ; 000Eh - 保留扇区数
brFATs DB 0x02 ; 0010h - FAT 备份数
brRootEntries DW 0x00e0 ; 0011h - 根目录文件数
brSectorCount
DW 2880 ; 0013h - 磁盘容量扇区数< 32MB
brMedia DB 240 ; 0015h - 媒体描述符
brSPF DW 9 ; 0016h - 每FAT扇区数
brSPH DW 18 ; 0018h - 每磁道扇区数
brHPC DW 2 ; 001Ah - 盘面数
brHidden DD 0 ; 001Ch - 隐藏扇区数
brSectors DD 0 ; 0020h - 如果大于32m的扇区总数
DB 0 ; 0024h - 物理驱动器号
DB 0 ; 0025h - 系统保留
DB 29H ; 0026h - 扩展扇区标记(包含29h)
brSerialNum DD 00000006H ; 0027h - 卷ID
brLabel DB 'NO NAME ' ; 002Bh - 卷标
brFSID DB 'FAT12 ' ; 0036h - 系统保留
;------------------------------------------------------------------------
Start:
mov ax, cs
mov ds, ax
mov es, ax
mov ax,0x8000 ;栈放在0x1F00段里,这里栈有问题(栈放在0x1F00段里)!!!!!!!!!!!!!!!
mov ss,ax
mov sp,0xffff ; 堆栈入口
call DispStr ; 调用显示字符串例程
call LoadFile ; 把目录区读入到200的地方
call FindFile ; 在200的地方找把kernel.bin,读出来并显示读取成功

;=============================================
;SwitchPro建立GDT,进入保护模式
;=============================================
SwitchPro;
; 打开A20
in al, 92h
or al, 00000010b
out 92h, al
; end 打开A20
; 设置GDT
cli
;mov ax,KERNEL ; lgdt 指令加载 gdtr 是以ds为数据段加载
;mov ds,ax
;lea si, [dword gdtr]
lgdt [gdtr]
;lea si, [dword idtr]
;lidt [dword gdtr]

mov eax, cr0
or eax,1
mov cr0, eax
jmp dword selcode:CODE32 ;
;jmp KERNEL ;跳到kenel.bin开始执行内核

[bits 32]
CODE32:
sti
mov eax,codesel_gdt
mov ebx,datasel_gdt
mov ax,ProMessage
mov bp,ax
mov cx,ProMsglength

jmp $ ; 到此停止!!!!!!!!!!!!!!!!!!!!!!!

[bits 16]

;===================================================================
;初始化gdtr和gdt
;===================================================================

gdtr:
dw gdt_end - gdt-1 ; gdt的长度--16位(800H)GDT界限gdt limit=2048, 256 GDT entries
;这里应该是伪长度7,15,23,31,因为从0开始计算的
dd gdt 

;===================================================================
;初始化gdtr和gdt
;===================================================================

gdtr:
dw gdt_end - gdt-1 ; gdt的长度--16位(800H)GDT界限gdt limit=2048, 256 GDT entries
;这里应该是伪长度7,15,23,31,因为从0开始计算的
dd gdt ; gdt的物理地址--32位GDT基地址
;0x00007c930017!!!!!!!!
gdt:
gdt0:
dw 0,0,0,0 ; 一定要为0
codesel_gdt:
dw 0xffff ; 界限Limit值 = 0x100000 * 0x1000 = 4GB
dw 0x0000 ; 基地址 = 0
db 0x00
db 0x9A ; 表示 存在 可执行可读代码段
db 0xCF ; 粒度以及32位代码1100=0XC
db 0x00
datasel_gdt:
dw 0xffff ; 界限4GB
dw 0x0000 ; 基地址
db 0x00
db 0x92 ; 表示 存在 可读写数据段
db 0xCF ; 粒度以及数据锻大小4G,1100=0XC
db 0x00
gdt_end:

selcode equ codesel_gdt - gdt ;索引值1,2,3....
seldata equ datasel_gdt - gdt


;=============================================
;DispStr 显示boot已经启动!
;=============================================
DispStr:
mov ax, BootMessage
mov bp, ax ; es:bp = 串地址
mov cx, Msglength ; cx = 串长度
call PrintMsg
ret

;=============================================
;LoadFile 把目录从软盘中读出来!
;=============================================
LoadFile:
mov ax,19 ;开始的扇区
mov bx,CATALOG ;目录放在7E00H的地方!!!!!!!!!!!!!!!!!!!!!!!!!!!
mov cx,14 ;目录扇区个数
loopreadsec:
push ax
push bx
push cx
call LBACHS ; 调用转换
mov dl, 0 ; 因为是a:所以为0!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
mov ah, 0x02 ; BIOS 读取扇区命令
mov al, 0x01 ; 一个扇区
int 0x13 ; 调用中断
nop
pop cx
pop bx
pop ax
add bx,200
inc ax
loop loopreadsec
ret
;=============================================
; 转换逻辑块访问为读取磁盘所使用的磁道,盘面,扇区
; 相对扇区 = (逻辑扇区 / 每磁道扇区数) + 1
; 相对盘面 = (逻辑扇区 / 每磁道扇区数) MOD 盘面数
; 相对磁道 = 逻辑扇区 / (每磁道扇区数 * 盘面数)
;=============================================
LBACHS:
xor dx, dx ; dx = 0
mov cx,18
div cx ; div: ax/18 -> 商:ax 余数:dx
inc dl ;sec ;
mov cl, dl ;sector
xor dx, dx ; dx = 0
push bx
mov bx,2
div bx ; ax/2 -> 商:ax 余数:dx
pop bx
mov ch, al ; track
mov dh, dl ; head
ret
;=============================================
;FindFile 查找文件名,并且装到0x500,显示Load Kernel.bin Success!
;=============================================
FindFile:
cld ;地址自动增加
mov cx,224 ;根目录共有224个文件
mov di,CATALOG ;目录放在7E00H的地方!!!!!!!!!!!!!!!!!!!!!
.l_findfile:
push cx
lea si,[Kernel] ;?
push di
mov cx,11 ;8+3=0xb文件名和后缀长度
repe cmpsb
pop di
je findfile_ok
add di,32 ;开始找下1个文件
pop cx
loop .l_findfile
mov ax, LoadKerFail ;失败
mov bp, ax ; es:bp = 串地址
mov cx, LoadKerFaillength ; cx = 串长度
call PrintMsg
ret

findfile_ok:
pop cx
add di,26 ;取文件占用的第一个簇号,即起始簇
mov ax,[di]
add ax,31 ;簇转成扇区
mov bx,KERNEL ;文件放在0x500!!!!!!!!!!!!!!!!!!!!!!!!!!!
call LBACHS ; 调用转换
mov dl, 0 ; 因为是a:所以为!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
mov ah, 0x02 ; BIOS 读取扇区命令
mov al, 0x03 ; 一个扇区,这里应该根据文件大小来决定!!!!!
int 0x13 ; 调用中断,内核放在0x1e00!
mov ax, LoadKerSuess ;成功
mov bp, ax ; es:bp = 串地址
mov cx, LoadKerlength ; cx = 串长度
call PrintMsg
ret

;=============================================
;显示信息
;=============================================
PrintMsg:
mov ax, 01301h ; ah = 13, al = 01h
mov bx, 000ch ; 页号为0(bh = 0) 黑底红字(bl = 0Ch,高亮)
mov dl, 0
mov dh, BYTE [NoLine]
int 10h ; 10h 号中断
add dh,1
mov BYTE [NoLine],dh
ret

BootMessage: db "Welcome to T's OS!",0x0D,0x0A
Msglength equ $-BootMessage
DB 0x00
Kernel: db 'KERNEL BIN' ;8+3=11位;将来这里改成内核的文件名!!!!!!!!!!!!!
LoadKerSuess: db 'Load Kernel.bin Success!',0x0D,0x0A
LoadKerlength equ $-LoadKerSuess
DB 0x00
LoadKerFail: db 'Load Kernel.bin Fail!',0x0D,0x0A
LoadKerFaillength equ $-LoadKerFail
DB 0x00
ProMessage: db "I'm in the Pro Mode!",0x0D,0x0A
ProMsglength equ $-ProMessage
DB 0x00
NoLine DB 0x00
times 510-($-$$) db 0 ; 填充剩下的空间,使生成的二进制代码恰好为512字节

dw 0xaa55 ; 结束标志

;把这段代码用NASM编译一下:
;nasm boot.asm –o boot.bin
 

原创粉丝点击