所以,经过几个小时的胡言乱语,我现在完全迷路了。我所要做的基本上是将第一个根目录条目加载到内存中,但似乎在读取部分失败了。我在用虚拟盒来模仿!
这是洞码:
[BITS 16]
[ORG 0x7C00]
jmp START
nop
OEM_ID db "APACU-OS"
BytesPerSector dw 0x0200
SectorsPerCluster db 0x01
ReservedSectors dw 0x227E
TotalFATs db 0x02
MaxRootEntries dw 0x0000
NumberOfSectors dw 0x0000
MediaDescriptor db 0xF8
SectorsPerFAT dw 0x0000
SectorsPerTrack dw 0x003D
SectorsPerHead dw 0x0002
HiddenSectors dd 0x00000001
TotalSectors dd 0x00200000
BigSectorsPerFAT dd 0x00003EC1
Flags dw 0x0000
FSVersion dw 0x0000
RootDirectoryStart dd 0x00000002
FSInfoSector dw 0x0001
BackupBootSector dw 0x0006
times 12 db 0
DriveNumber db 0x80
db 0x00
Signature db 0x29
VolumeID dd 0xFFFFFFFF
VolumeLabel db "APACU BOOT "
SystemID db "FAT32 "
START:
mov Byte[DriveNumber], dl
; code located at 0000:7C00, adjust segment registers
cli
mov ax, 0x7C00
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
; create stack
mov ax, 0x7C00
mov ss, ax
mov sp, 0xFFFF
sti
mov ah, 0x41
mov dl, [DriveNumber]
mov bx, 0x55AA
int 0x13
jnc continue
mov ah, 0x0E
mov al, "N"
int 0x10
;int 0x18
; No Support For Extended Reading
continue:
; size of a cluster in sectors is stored in cx
mov cx, WORD[SectorsPerCluster]
; compute location of the begining of the Data area and store in ax
mov al, BYTE [TotalFATs] ; Total number of FATs
mul WORD[BigSectorsPerFAT] ; Number of sectors for a FAT
add ax, WORD [ReservedSectors] ; Find the start of the Data area
mov WORD [datasector], ax ; Store the begining of the Data area
; read 1st data cluster into memory (7C00:0200)
mov ax, WORD[RootDirectoryStart]
call ClusterLBA
mov bx, 0x0200 ; copy 1st data cluter above bootcode
call ReadSectors
; Point Index register to 1st File Entry
mov di, 0x0200 + 0x20
mov si, msgCRLF
call DisplayMessage
;Point to the offset where the file location information contains
mov dx, WORD [di + 0x001A]
mov WORD [cluster], dx
;Set up the segments where the kernel needs to be loaded
mov ax, 0100h ; set ES:BX = 0100:0000
mov es, ax
mov bx, 0
;Read the cluster which contains the kernel
mov cx, 0x0008
mov ax, WORD[cluster]
call ClusterLBA
call ReadSectors
mov si, msgCRLF
call DisplayMessage
;Jump to the location where the kernel was loded
push WORD 0x0100
push WORD 0x0000
retf
;An error has occured if this part is executed
mov si, msgFailure
call DisplayMessage
mov ah, 0x00
int 0x16 ; await keypress
int 0x19 ; warm boot computer
;*************************************************************************
; PROCEDURE ReadSectors
; reads cx sectors from disk starting at ax into
;memory location es:bx
;*************************************************************************
ReadSectors:
.MAIN:
mov di, 0x0005 ; five retries for error
.SECTORLOOP:
push ax
push bx
push cx
call LBACHS
mov ah, 0x02 ; BIOS read sector
mov al, 0x01 ; read one sector
mov ch, BYTE [absoluteTrack] ; track
mov cl, BYTE [absoluteSector] ; sector
mov dh, BYTE [absoluteHead] ; head
mov dl, BYTE [DriveNumber] ; drive
int 0x13 ; invoke BIOS
jnc .SUCCESS ; test for read error
xor ax, ax ; BIOS reset disk
int 0x13 ; invoke BIOS
dec di ; decrement error counter
pop cx
pop bx
pop ax
jnz .SECTORLOOP ; attempt to read again
int 0x18
.SUCCESS:
mov si, msgProgress
call DisplayMessage
pop cx
pop bx
pop ax
add bx, WORD [BytesPerSector] ; queue next buffer
inc ax ; queue next sector
loop .MAIN ; read next sector
ret
;*************************************************************************
; PROCEDURE DisplayMessage
; display ASCIIZ string at ds:si via BIOS
;*************************************************************************
DisplayMessage:
lodsb ; load next character
or al, al ; test for NUL character
jz .DONE
mov ah, 0x0E ; BIOS teletype
mov bh, 0x00 ; display page 0
mov bl, 0x07 ; text attribute
int 0x10 ; invoke BIOS
jmp DisplayMessage
.DONE:
ret
;*************************************************************************
;*************************************************************************
; PROCEDURE ClusterLBA
; convert FAT cluster into LBA addressing scheme
; FileStartSector = ((X - 2) * SectorsPerCluster(0x08))
;*************************************************************************
ClusterLBA:
sub ax, 0x0002 ; zero base cluster number
xor cx, cx
mov cl, BYTE [SectorsPerCluster] ; convert byte to word
mul cx
add ax, WORD [datasector] ; base data sector
ret
;*************************************************************************
; PROCEDURE LBACHS
; convert ax LBA addressing scheme to CHS addressing scheme
; absolute sector = (logical sector / sectors per track) + 1
; absolute head = (logical sector / sectors per track) MOD number of heads
; absolute track = logical sector / (sectors per track * number of heads)
;*************************************************************************
LBACHS:
mov word[SectorsPerTrack], 0x003D
mov word[SectorsPerHead], 0x0002
xor dx, dx ; prepare dx:ax for operation
div WORD [SectorsPerTrack] ; calculate
inc dl ; adjust for sector 0
mov BYTE [absoluteSector], dl
xor dx, dx ; prepare dx:ax for operation
div WORD [SectorsPerHead] ; calculate
mov BYTE [absoluteHead], dl
mov BYTE [absoluteTrack], al
ret
;************************************************************************
absoluteSector db 0x00
absoluteHead db 0x00
absoluteTrack db 0x00
cluster dw 0x0000
datasector dw 0x0000
;*******************************************************************************
;messages that needs to be shown
msgProgress db ".", 0x00
msgFailure db 0x0D, 0x0A, "Kernel loading failed...", 0x0D, 0x0A, 0x00
msgCRLF db 0x0D, 0x0A, 0x00
TIMES 510-($-$$) DB 0
DW 0xAA55
;*************************************************************************我知道它失败了,因为它正在触发读扇区循环中的int0x18。
所以我的问题是:为什么它失败了?我该怎么修理它呢?我还需要使用BIOS扩展读取驱动器吗?
发布于 2014-06-28 11:34:45
我认为您不能像this.And那样使用引导钩子中断(Int0x18)--我认为这不是您自己的code.If --您需要使用清楚的引导加载程序,您必须使用二进制文件(在其他操作系统中使用)--您想要制作自己的引导加载器--您必须浪费大量的时间来编写、编译和放置。
发布于 2014-08-06 05:13:09
分段:偏移量RealMemory =分段* 16 +偏移量
引导扇区在实际内存中以0x07C00加载。这等于07C0:0000或0000:7C00。因此,首先,您需要决定要在哪个空间引用变量。看起来您已经将原点设置为0x7C00。
但是,您要将段寄存器设置为7C00。对于您选择的源,段值将为0。但是,如果选择org 0,则段值为0x07C0。此外,您必须对齐CS:IP以对应您为代码选择的地址空间。
如果原产地为0x7c00:
xor ax, ax
mov ds, ax
push word RealignmentPoint
retf
RealignmentPoint:
;variable addresses are mapped into 'usable' memory locations.不过,这不是最好的做法。将原点设置为0,将段寄存器设置为0x07C0允许我们使用单个字节引用接近变量,重新对齐代码如下所示。
mov ax, 0x07C0
mov ds, ax
push ax
push word RealignmentPoint
retf
RealignmentPoint:
;variable addresses are mapped into acceptable memory locations这种技术是NTFS引导扇区拆卸的一部分(细打印该死)。但是,您需要做更多的研究并更好地规划事情,否则,您试图编写的其他98%的代码也将无法工作,因为它基于不推荐的功能或不完整的逻辑。
https://stackoverflow.com/questions/20306611
复制相似问题