開發操作系統一直被認爲是高不可攀的事,的確,開發一個安全的,完整的,健全的OS是非常複雜的工作,不是一兩個人能完成的。但是一個簡易的操作系統是可以由一個人在很短的時間開發出來的。我將陸續發表開發簡易操作系統的全過程,盡力提供完整的源代碼,參考資料和文字說明,我也是OS開發的初學者,希望能得到各位讀者的技術支持。該簡易操作系統我稱爲Colimas Simple OS,它包括引導程序,圖形界面,鼠標和鍵盤驅動,內存管理,計時器,多任務處理,控制檯,Shell命令,API。
1. 開發環境
本文使用Qemu虛擬機,可以在Windows XP內虛擬軟盤鏡像文件。Qemu是開源軟件,可以在http://fabrice.bellard.free.fr/qemu/下載。C編譯器使用Cygwin下的GCC,彙編編譯器使用Nasm,可以在http://sources.redhat.com/cygwin/下載。
操作系統開發在FAT12文件系統的軟盤裏,FAT12文件系統格式參考http://en.wikipedia.org/wiki/FAT32#FAT12的Design部分。
2. 引導程序1
;boot.s
;Colimas Simple OS
org 0x
;Fat12文件系統格式參考 http://en.wikipedia.org/wiki/FAT32#FAT12
; |offset|Length|Descripton
jmp entry
db 0x90 ; 0x00 3 Jump instruction(to skip over header on boot)
db "COLIMAS " ; 0X03 8 OEM Name
db 512 ; 0x0b 2 Bytes per sector. The BIOS Parmeter Block starts here.
db 1 ; 0x0d 1 Sectors per cluster
db 1 ; 0x0e 2 Reserved sector count(including boot sector)
db 2 ; 0x10 1 Number of file allocation tables
db 224 ; 0x11 2 Maximum number o root directory entries
db 2880 ; 0x13 2 Total sector:80 tracks * 18 sectors * 2 sides=2880
db 0xf0 ; 0x15 1 Media descriptor
db 9 ; 0x16 2 Sectors per File Allocation Table
db 18 ; 0x18 2 Sectors per track
db 2 ; 0x
db 0 ; 0x
db 2880 ; 0x20 4 Total sectors again
db 0 ; 0x24 1 Physical drive number
db 0 ; 0x25 1 Reserved("current head")
db 0x29 ; 0x26 1 Signature
db 0xffffffff ; 0x27 4 ID(serial number)
db "Colimas OS "; 0x2b 11 Volume Label
db "FAT12 " ; 0x36 8 FAT file system type, FAT12
resb 18 ; 爲了安全添加18 bytes個0
entry:
mov ax,0 ;寄存器初始化
mov ss,ax
mov sp,0x
mov ds,ax
mov es,ax
mov si,msg ;source index爲msg第一個字符地址
putloop:
mov al,[si] ;第一個字符->al
add si,1 ;si+1
cmp al,0 ;比較0尋找最後一個字符,msg之後的byte是0
je fin ;如果等於0則,調到fin
;video bios參考http://en.wikipedia.org/wiki/BIOS_interrupt_call
mov ah,0x0e ;顯示字符
mov bx,15 ;黑色
int 0x10 ;調用video bios中斷
jmp putloop
fin:
hlt ;cpu停止
jmp fin ;死循環
msg:
db 0x
db "Colimas Simple OS Initialize..."
db 0x
db 0
resb 0x1fe-($-$$) ;510 bytes爲1止均設爲0
db 0x55,0xaa ;sector結束
編譯與運行:
$ nasm boot.s -o boot.bin
$ cp boot.bin ../qemu
$ ./qemu-win.bat
其中qemu-win.bat的內容是
@set SDL_VIDEODRIVER=windib
@set QEMU_AUDIO_DRV=none
@set QEMU_AUDIO_LOG_TO_MONITOR=0
qemu.exe -L . -m 32 -localtime -std-vga -fda boot.bin
運行結果
3.引導程序2
上文已經作了簡單的引導程序,引導程序利用了軟盤的第一個Sector作爲引導sector,下面開始讀取軟盤第2個Sector。
讀取磁盤需要使用Disk Bios,int 13中斷,參考http://en.wikipedia.org/wiki/BIOS_interrupt_call#INT_13h_AH.3D02h:_Read_Sectors_From_Drive
;boot.s
;Colimas Simple OS
org 0x
;Fat12文件系統格式參考 http://en.wikipedia.org/wiki/FAT32#FAT12
; |offset|Length|Descripton
jmp entry
db 0x90 ; 0x00 3 Jump instruction(to skip over header on boot)
db "COLIMAS " ; 0X03 8 OEM Name
db 512 ; 0x0b 2 Bytes per sector. The BIOS Parmeter Block starts here.
db 1 ; 0x0d 1 Sectors per cluster
db 1 ; 0x0e 2 Reserved sector count(including boot sector)
db 2 ; 0x10 1 Number of file allocation tables
db 224 ; 0x11 2 Maximum number o root directory entries
db 2880 ; 0x13 2 Total sector:80 tracks * 18 sectors * 2 sides=2880
db 0xf0 ; 0x15 1 Media descriptor
db 9 ; 0x16 2 Sectors per File Allocation Table
db 18 ; 0x18 2 Sectors per track
db 2 ; 0x
db 0 ; 0x
db 2880 ; 0x20 4 Total sectors again
db 0 ; 0x24 1 Physical drive number
db 0 ; 0x25 1 Reserved("current head")
db 0x29 ; 0x26 1 Signature
db 0xffffffff ; 0x27 4 ID(serial number)
db "Colimas OS "; 0x2b 11 Volume Label
db "FAT12 " ; 0x36 8 FAT file system type, FAT12
resb 18 ; 爲了安全添加18 bytes個0
entry:
mov ax,0 ;寄存器初始化
mov ss,ax
mov sp,0x
mov ds,ax
mov es,ax
mov si,msg ;source index爲msg第一個字符地址
putloop:
mov al,[si] ;第一個字符->al
add si,1 ;si+1
cmp al,0 ;比較0尋找最後一個字符,msg之後的byte是0
je fin ;如果等於0則,調到fin
;video bios參考http://en.wikipedia.org/wiki/BIOS_interrupt_call
mov ah,0x0e ;顯示字符
mov bx,15 ;灰色
int 0x10 ;調用video bios中斷
jmp putloop
;讀取磁盤第2個Sector數據
reading:
mov ax,0x0820
mov es,ax ;0x0820(es) * 16=0x8200
mov ch,0 ;track/cylinder number
mov dh,0 ;head number
mov cl,2 ;sector number
mov ah,0x02 ;status of reading disk sector
mov al,1 ;number of sectors read
mov bx,0 ;0x0820(es) * 16 + 0(bx)=0x8200, 0x7e00~0x9fbff之間
mov dl,0x00 ;A drive
int 0x13 ;Read
jc error ;on error goto label error
mov si,msg2 ;source index msg2第一個字符地址
putloop2:
mov al,[si] ;第一個字符->al
add si,1 ;si+1
cmp al,0 ;比較0找最後一個字符,msg之後的byte是0
je fin ;
mov ah,0x0e ;顯示字符
mov bx,15 ;灰色
int 0x10 ;調用video bios中斷
jmp putloop2
error:
mov ax,0
mov es,ax
mov si,errmsg
putloop3:
mov al,[si]
add si,1
cmp al,0
je fin
mov ah,0x0e
mov bx,15
int 0x10
jmp putloop3
;讀完後,休眠
fin:
hlt ;cpu停止
jmp fin ;死循環
msg:
db 0x
db "Colimas Simple OS Initialize..."
db 0x
db "
db 0x
db 0
msg2:
db "1 sector was read."
db 0x
db 0
errmsg:
db "load error"
db 0x
db 0
resb 0x1fe-($-$$)
db 0x55,0xaa ;
編譯與運行:
$ nasm boot.s -o boot.bin
$ cp boot.bin ../qemu
$ ./qemu-win.bat
笑
(未完)-C語言的開始,編寫圖形顯示程序。