📄 v2osboot.asm
字号:
; V2_OS boot sector
; Copyright (C) 1999 V2_Lab
; Copyright (C) 2000 V2_OS Kernel Coders Team
;
; This program is free software; you can redistribute it and/or
; modify it under the terms of the GNU General Public License
; as published by the Free Software Foundation; either version 2
; of the License, or (at your option) any later version.
;
; More information is available at http://www.terbium.net/members/phil/
org 7c00h
cli
xor bx, bx
mov ds, bx
mov ss, bx
mov sp, 7bfeh
; A small CPU check to determine if we run on a 386
; see http://www.ddj.com/articles/1996/9611/9611n/9611n.htm for details
push sp
pop ax
cmp ax,sp
jz AtLeast286
WrongCPU:
mov si, WrongCPUMsg
jmp short WriteAndDie
AtLeast286:
mov word [bx + 24], WrongCPU ; Install an exception handler
mov [bx + 26], bx
cwde ; and try a 32 bit instruction
; Save boot disk
mov [BootDisk], dl
; Check for int 13h extensions
xor bp, bp ; bp=0 means LBA, bp=1 means CHS
mov ah, 41h
mov bx, 55aah
int 13h
jc NoExtensions
cmp bx, 0aa55h
jnz NoExtensions
test cl, 1
jz SkipCHS
NoExtensions:
inc bp
mov ah, 8
int 13h ; Get some information about the bootdisk
jc Error
and ecx, 00111111b
mov [SectorsPerHead], ecx
mov cl, dh
inc cx
mov [NumberOfHeads], ecx
SkipCHS mov ah, 0 ; Reste the bootdrive
mov dl, [BootDisk]
int 13h
jc Error
or dl,dl
jns near LoadKernel
call ReadSectors
mov si, 8200h - 2 - 64 ; Point to the partition table
mov cx, 4
PartLoop:
cmp byte [si + 4], 6fh
jz Found
add si, 16
loop PartLoop
mov si, BadPartTableMsg
WriteAndDie:
mov ah, 0eh ; Teletype output
xor bx, bx ; Page 0, color 0 (graphic only)
.Loop lodsb
or al, al
jz $
int 10h
jmp short .Loop
Error mov si, ErrorMsg
jmp short WriteAndDie
ReadSectors:
mov di, 4
RetryLoop:
or bp, bp
jz LBA
xor edx, edx
mov eax, [DAP.Sector]
div dword [SectorsPerHead]
mov cx, dx
inc cl
mov dl, 0
div dword [NumberOfHeads]
mov ch, al
shl ah, 6
or cl, ah
mov dh, dl
mov ax, 0201h
les bx, [DAP.Buffer]
jmp short Load
LBA mov byte [DAP.Count], 1
mov ah, 42h
mov si, DAP
Load mov dl,[BootDisk]
int 13h
jnc Next
mov ah, 0
int 13h
jc Error
dec di
jnz RetryLoop
jmp short Error
Next add word [DAP.BufferSeg], 20h
inc dword [DAP.Sector]
dec byte [SectorCount]
jnz ReadSectors
ret
Found cmp dword [si + 1], 06fffffffh
jne CHS
mov eax, [si + 8]
jmp short CalcPartition
CHS mov ax, [si + 2]
mov dl, al
and dl, 00111111b
add [DAP.Sector], dl
xchg ah,al
shr ah,0x6
mul dword [NumberOfHeads]
mov dl, [si + 1]
add eax,edx
mul dword [SectorsPerHead]
dec eax
CalcPartition:
dec eax ; because we loaded the MBR
add [DAP.Sector], eax
LoadKernel:
mov eax, [LoadPosition]
shr eax, 4
mov [DAP.BufferSeg], ax
mov eax, [KernelPosition]
add [DAP.Sector], eax
mov ax,[KernelSize]
mov [SectorCount], ax
call ReadSectors
cli ; BIOS likes to enable interrupts
; enable A20
; mov bx, output
; mov ah, 0adh ; disable keyboard
; call bx
; mov ah, 0d0h ; read status
; call bx
;.inloop in al, 64h ; wait for status data to read
; test al, 1
; jz .inloop
; in al, 60h ; read status byte
; mov cl, al
; mov ah, 0d1h ; write status
; call bx
; mov ah, cl
; or ah, 2 ; set the "A20 bit"
; call bx
; mov al, 2 ; set the "A20 bit" on PS/2
; out 92h, al
; mov ah, 0aeh ; enable keyboard
; call bx
mov al, 0d1h
out 64h, al
mov al, 3
out 60h, al
mov al, 2
out 92h, al
; switch to protected mode
lgdt [GDTvalue] ; load protected mode GDT
mov eax, cr0
or al, 1 ; set protected mode flag
mov cr0, eax
jmp 8:SetRegs ; jump to 32 bit code
;output in al, 64h ; wait for permission to write
; test al, 2
; jnz output
; mov al, ah
; out 64h, al ; write the command
; ret
; 32 bit protected mode code
SetRegs o32 ; toggle width (imm32 to imm16)
mov ax, 10h
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
a32 ; toggle address width (32 to 16)
jmp [LoadPosition]
; Initialized variables
; error messages
WrongCPUMsg db '32 bit CPU required.', 0
BadPartTableMsg db 'Boot partition not found.', 0
ErrorMsg db 'Error', 0
SectorCount db 1
; the disk access packet for int 13h extensions
DAP db 10h, 0
.Count dw 1
.Buffer dw 0
.BufferSeg dw 800h
.Sector dd 0
.SectorHigh dd 0
GDTvalue dw 17h, GDT - 8, 0
; Pad the sector to 512 bytes
times 1e4h - ($ - $$) db 0
LoadPosition dd 10000h
GDT dw 0ffffh, 0
db 0, 10011011b, 11001111b, 0
dw 0ffffh, 0
db 0, 10010011b, 11001111b, 0
KernelPosition dd 1
KernelSize dw 20
; Boot signature
dw 0aa55h
; Uninitialized variables
section .bss
SectorsPerHead resd 1
NumberOfHeads resd 1
BootDisk resb 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -