Boot Loader的特點:
1. 長度必須是 512 bytes
2. 最後兩個byte(byte 511及512)必須是 "0x55 0xAA" (舊的BIOS會檢查這個)
3. 16 bit binary code (Real mode)
多數的BIOS會把Boot Loader複製到0x0000:0x7c00(segment 0, offset 0x7c00),也有一些BIOS會複製到0x07c0:0x0000。那為什麼是0x7c00呢? 因為之前的都已經被BIOS用了~
以下介紹如何製造一個Boot Loader,首先說明,此乃高危活動,閣下所做的事的一切後果,在下不會負責。
首先,我們需要這些工具:
1. PC模擬器 Bochs (我用的是2.4),直接下載
2. 機械語言編譯器 (assembler) NASM (我用的是2.06rc10),Window version快速下載
安裝工具的方法就不說了。
為了方便,我開了一個新資料夾 d:\os\
開始製作Boot Loader,我在d:\os\ 裡開了一個新檔案 boot.asm,加入了下面的程序:
---------- boot.asm ----------
[BITS 16] ;tell the assembler that its a 16 bit code
[ORG 0x7C00] ;Origin, tell the assembler that where the code will
;be in memory after it is been loaded
MOV AL, 10
CALL PrintCharacter
MOV AL, 72
CALL PrintCharacter
MOV AL, 101
CALL PrintCharacter
MOV AL, 108
CALL PrintCharacter
MOV AL, 108
CALL PrintCharacter
MOV AL, 111
CALL PrintCharacter
MOV AL, 32
CALL PrintCharacter
MOV AL, 87
CALL PrintCharacter
MOV AL, 111
CALL PrintCharacter
MOV AL, 114
CALL PrintCharacter
MOV AL, 108
CALL PrintCharacter
MOV AL, 100
CALL PrintCharacter
JMP $ ;infinite loop
PrintCharacter: ;Procedure to print character on screen
;Assume that ASCII value is in register AL
MOV AH, 0x0E ;Tell BIOS that we need to print one charater on screen.
MOV BH, 0x00 ;Page no.
MOV BL, 0x07 ;Text attribute 0x07 is lightgrey font on black background
INT 0x10 ;Call video interrupt
RET ;Return to calling procedure
TIMES 510 - ($ - $$) db 0 ;fill the rest of sector with 0
DW 0xAA55 ; add boot signature at the end of bootloader
---------- boot.asm ----------
[BITS 16] ;tell the assembler that its a 16 bit code
[ORG 0x7C00] ;Origin, tell the assembler that where the code will
;be in memory after it is been loaded
MOV AL, 10
CALL PrintCharacter
MOV AL, 72
CALL PrintCharacter
MOV AL, 101
CALL PrintCharacter
MOV AL, 108
CALL PrintCharacter
MOV AL, 108
CALL PrintCharacter
MOV AL, 111
CALL PrintCharacter
MOV AL, 32
CALL PrintCharacter
MOV AL, 87
CALL PrintCharacter
MOV AL, 111
CALL PrintCharacter
MOV AL, 114
CALL PrintCharacter
MOV AL, 108
CALL PrintCharacter
MOV AL, 100
CALL PrintCharacter
JMP $ ;infinite loop
PrintCharacter: ;Procedure to print character on screen
;Assume that ASCII value is in register AL
MOV AH, 0x0E ;Tell BIOS that we need to print one charater on screen.
MOV BH, 0x00 ;Page no.
MOV BL, 0x07 ;Text attribute 0x07 is lightgrey font on black background
INT 0x10 ;Call video interrupt
RET ;Return to calling procedure
TIMES 510 - ($ - $$) db 0 ;fill the rest of sector with 0
DW 0xAA55 ; add boot signature at the end of bootloader
---------- boot.asm ----------
這段code會在螢幕上寫上Hello World字串。如果想詳細了解code的內容,請看這裡 (英文)
好了,現在要把code變成binary,打開NASM Shell,行以下的指令:
> nasm d:\os\boot.asm -f bin -o d:\os\boot.img
這會拿著 d:\os\boot.asm,編譯,得到d:\os\boot.img,當中 -f bin 用來指定format是flat-form binary (怎說好呢? 這是一種非常純正的binary,常用於DOS系統的.com及.sys)
boot.img就是我們的Boot Loader了,現在我們會在Bochs模擬器上測試她。
Bochs需要一個config file,下面是我用的,位置及名稱是 d:\os\bochsrc.bxrc
---------- bochsrc.bxrc ----------
# how much memory the emulated machine will have
# if you have less than 64MB of memory in your actuall computer, set this lower(16 is good)
megs: 32
# filename of ROM images
# note that if before a filename we put "../" then the file is looked
# for in the directory above that the current directory(the one the configuration
# file is in).
romimage: file="C:/Program Files/Bochs-2.4/BIOS-bochs-latest"
vgaromimage: file="C:/Program Files/Bochs-2.4/VGABIOS-elpin-2.40"
# we want a floppy drive(called a), the disk image that we are using is called "1.44"
floppya: 1_44=d:/os/boot.img, status=inserted
# choose the boot disk.
boot: a
# disable the mouse unless your OS uses it
mouse: enabled=0
---------- bochsrc.bxrc ----------
# how much memory the emulated machine will have
# if you have less than 64MB of memory in your actuall computer, set this lower(16 is good)
megs: 32
# filename of ROM images
# note that if before a filename we put "../" then the file is looked
# for in the directory above that the current directory(the one the configuration
# file is in).
romimage: file="C:/Program Files/Bochs-2.4/BIOS-bochs-latest"
vgaromimage: file="C:/Program Files/Bochs-2.4/VGABIOS-elpin-2.40"
# we want a floppy drive(called a), the disk image that we are using is called "1.44"
floppya: 1_44=d:/os/boot.img, status=inserted
# choose the boot disk.
boot: a
# disable the mouse unless your OS uses it
mouse: enabled=0
---------- bochsrc.bxrc ----------
如果一切正常,您可以用滑鼠雙擊 d:\os\bochsrc.bxrc,之後就會彈出2個視窗,如果您能看到Hello World字樣,那就要恭喜您了,您剛剛製作了您第一個Boot Loader!!! (嚴格來說,這也是一個kernel啊 ^^)
3 則留言:
開機管理程式,如果骨仔大大可以說明,設定後,開機最快是多久?那麼相信大家一定會一窩蜂嘗試,如:原本開機要五分鐘,骨仔大大教導後,開機只要二分鐘,那麼,肯定這堂課一定會爆滿。
^^~
珍妮佛麥走
詳細的不容易說清楚,可是如果只是我這般的低手,搞兩三天就能做得比Windows好,他們早就消失了吧 ^^"
基礎之下,加快速度,是開機分秒必爭的關鍵。
^^~
珍妮佛麥走
發佈留言