📄 setup.asm
字号:
;
; setup.asm
;
; All rights reserved
;
; MPL--mmmmpl@126.com
; 2007.9.21
;
; Compile with nasm.
; This block will be loaded by boot.bin
[bits 16]
[org 0000h]
jmp start
;---------------------------------------
;setup.asm start here,
;1.find system image file
;2.load system image file 0000:0400h
;3.entry prorected mode
;4.print notice message
;5.jmp 0000:0400h=1kb
start:
mov ax,SETUPSEG
mov ds,ax
mov ss,ax
mov es,ax
mov fs,ax
mov gs,ax
mov sp,7000h
;-----------------------------------------
; find file
mov ax,ROOTDIRSEG
mov es,ax
mov bx,ROOTDIR ; ax = pointer to current dir-entry
find_sys: ;find the kernel.bin
mov di,bx ; point si to current filename
mov si,sysImage ; point di to the filename we need
mov cx,11 ; compare max 11 bytes 8 filename + 3 extendName
cld
repe cmpsb
je found_sys
add bx, 32 ; done yet?
cmp bx, ROOTDIR+224*32
jne find_sys
; print error and halt
mov dx,0001h
mov ah,0ch
mov si,system_not_found
call print
jmp $
found_sys: ;setup.bin be found
; save cluster
mov ax,ROOTDIRSEG
mov ds,ax
mov dx, word [bx+26] ; load cluster of file from the directory entry
mov ax,SETUPSEG
mov ds,ax
mov word [cluster],dx ;26~27 is the start cluster number of the file
mov ah,0ch
mov dx,0001h
mov si,system_found
call print
;jmp $
;-----------------------------------------
;load syste image at :(0040h:0000h)
;SYSSEG=0000h
;SYSOFF=0400h
load_sys:
mov ax,SYSSEG
mov es,ax
mov bx,SYSOFF
mov ax,word [cluster]
load_loop:
; calculate next cluster
mov si,ax
shl si,01h
mov dx,FATDECODEDSEG
mov ds,dx
mov cx,word [FATDECODED+si]
mov dx,SETUPSEG
mov ds,dx
mov word [cluster],cx
; calculate track, head and sector
add ax,31 ; ax = logical sector
xor dx,dx
mov di,18
div di
mov cl,dl
inc cl ; cl = sector = (logical % 18 + 1)
xor dx,dx
mov di,02h
div di
mov ch,al ; ch = track = ((logical / 18) / 2)
mov dh,dl ; dh = head = ((logical / 18) % 2)
mov dl,byte [bootdev]
; read data
mov ax,0201h
int 13h
jc print_io_error ; out of range crap :(
add bx, 512
; done?
mov ax,word [cluster]
cmp ax,0ff7h
jb load_loop
mov si,load_sys_ok
mov dx,0002h
mov ah,0ch
call print
goto_pm:
;that was painless,now we enable a20
cli
call empty_8042
mov al,0d1h
out 64h,al
call empty_8042
mov al,0dfh
out 60h,al
call empty_8042
;well that went ok,I hope.Now we have to reprogrem the interrupts
;(20-27h is master ,28-2fh is slover
mov al,11h
out 20h,al
wait
out 0a0h,al
wait
mov al,20h
out 21h,al
wait
mov al,28h
out 0a1h,al
wait
mov al,04h
out 21h,al
wait
mov al,02h
out 0a1h,al
wait
mov al,01h ;normal EOI method for both
;mov al,03h ;automatica EOI method for both
out 21h,al
wait
out 0a1h,al
wait
mov al,0ffh
out 21h,al
wait
out 0a1h,al
wait
;prepare for protected mode
mov ax,SETUPSEG
mov ds,ax
mov es,ax
lidt [idtr]
lgdt [gdtr]
mov eax,cr0
or al,01h
mov cr0,eax
jmp CODESEL:dword go_pm+10000h
;........................................................
;printf i/o fault message
print_io_error:
mov ax,SETUPSEG
mov ds,ax
mov si,io_error
call print
jmp $
; print zero-terminated string (without formatting)
; ds:si = string
; es:di = position on screen
print:
;(dh,dl)=(x,y) position
;ah=color
;ds:si=String address
push es
push di
push ax
;destination EA and seg
push dx
and dx,00ffh
mov ax,160
mul dx
pop dx
shr dx,08h
add ax,dx
mov di,ax
mov ax,0b800h
mov es,ax
;source EA and seg
pop ax
cld
pr:
lodsb
or al,al
jz prEnd
stosw
jmp pr
prEnd:
pop di
pop es
ret
;---------------------------------------
empty_8042:
wait
in al,64h
test al,02h
jnz empty_8042
ret
;>>>>>>>>>>>>>>>entry Protected mode>>>>>>>>>>>>>>>>
[BITS 32]
go_pm:
mov ax,DATASEL
mov ds,ax
mov ss,ax
mov sp,7000h
mov ax,VIDEOSEL
mov es,ax
mov esi,welcome
add esi,SETUPSEG*10h
mov edi,160*3
cld
mov ah,0ch
nextChar:
lodsb
cmp al,'.'
je nextChar2
stosw
jmp nextChar
nextChar2:
lodsb
or al,al
jz finish
stosw
mov ecx,0fffffh
l:
loop l
jmp nextChar2
finish:
jmp CODESEL:dword 17000h ;system image was loaded at :17000h,jmp to execute
;-----------------------------------------
align 4
gdtr: dw gdt_end-gdt
dd SETUPSEG*10h+gdt
idtr: dw 00h ;no interrupt
dd 00h
;-----------------------------------------
align 4
gdt:
gdt0: dd 00000000h,00000000h ;dummy
code_gdt: dd 00000fffh,00c09a00h ;4096*4096=16M
data_gdt: dd 00000fffh,00c09200h ;4096*4096=16M
vgdt: dd 8000ffffh,0000920bh ;64k
CODESEL equ 08h
DATASEL equ 10h
VIDEOSEL equ 18h
gdt_end:
;----------------------------------------
;system constants
SETUPSEG equ 1000h
SETUPOFF equ 0000h
FATBUFSEG equ 1100h
FATBUF equ 0000h ;11000h-12fffh
ROOTDIRSEG equ 1300h
ROOTDIR equ 0000h ;13000h-14fffh
FATDECODEDSEG equ 1500h
FATDECODED equ 0000h ;15000h-16fffh
SYSSEG equ 1700h
SYSOFF equ 0000h
;----------------------------------------
;two variables
cluster dw 00h
bootdev db 00h
;system image filename
sysImage db "SYSTEM",20h,20h,"BIN",00h
;message
io_error db "IO error...",00h
system_not_found db "Oh,No system image file...",00h
system_found db "System Image file found...",00h
load_sys_ok db "System Image loaded...",00h
welcome db "Hi,Welcome to you.............",00h
;;;;;;;;;;;;;;;;;;;END;;;;;;;;;;;;;;;;;;;;;;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -