⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fat32.asm

📁 这是一个开放源代码的与WINNT/WIN2K/WIN2003兼容的操作系统
💻 ASM
📖 第 1 页 / 共 2 页
字号:
; FAT32.ASM
; FAT32 Boot Sector
; Copyright (c) 1998, 2000, 2001, 2002 Brian Palmer

org 7c00h

segment .text

bits 16

start:
        jmp short main
        nop

OEMName				db 'FrLdr1.0'
BytesPerSector		dw 512
SectsPerCluster		db 0
ReservedSectors		dw 32
NumberOfFats		db 2
MaxRootEntries		dw 0			; Always zero for FAT32 volumes
TotalSectors		dw 0			; Always zero for FAT32 volumes
MediaDescriptor		db 0f8h
SectorsPerFat		dw 0			; Always zero for FAT32 volumes
SectorsPerTrack		dw 0
NumberOfHeads		dw 0
HiddenSectors		dd 0
TotalSectorsBig		dd 0
; FAT32 Inserted Info
SectorsPerFatBig	dd	0
ExtendedFlags		dw	0
FSVersion			dw	0
RootDirStartCluster	dd	0
FSInfoSector		dw	0
BackupBootSector	dw	6
Reserved1			times 12 db 0
; End FAT32 Inserted Info
BootDrive			db 0
Reserved			db 0
ExtendSig			db 29h
SerialNumber		dd 00000000h
VolumeLabel			db 'NO NAME    '
FileSystem			db 'FAT32   '

main:
        xor ax,ax               ; Setup segment registers
        mov ds,ax               ; Make DS correct
        mov es,ax               ; Make ES correct
        mov ss,ax				; Make SS correct
		mov bp,7c00h
        mov sp,7c00h            ; Setup a stack



		cmp BYTE [BYTE bp+BootDrive],BYTE 0xff	; If they have specified a boot drive then use it
		jne CheckSectorsPerFat

        mov [BYTE bp+BootDrive],dl				; Save the boot drive



CheckSectorsPerFat:
        cmp	WORD [BYTE bp+SectorsPerFat],byte 0x00	; Check the old 16-bit value of SectorsPerFat
		jnz CheckFailed								; If it is non-zero then exit with an error
CheckTotalSectors:									; Check the old 16-bit value of TotalSectors & MaxRootEntries
        cmp	DWORD [BYTE bp+MaxRootEntries],byte 0x00; by comparing the DWORD at offset MaxRootEntries to zero
		jnz CheckFailed								; If it is non-zero then exit with an error
CheckFileSystemVersion:
        cmp WORD [BYTE bp+FSVersion],byte 0x00		; Check the file system version word
		jna GetDriveParameters						; It is zero, so continue
CheckFailed:
        jmp PrintFileSystemError					; If it is not zero then exit with an error


GetDriveParameters:
		mov  ax,0800h
		mov  dl,[BYTE bp+BootDrive]					; Get boot drive in dl
		int  13h									; Request drive parameters from the bios
		jnc  CalcDriveSize							; If the call succeeded then calculate the drive size

		; If we get here then the call to the BIOS failed
		; so just set CHS equal to the maximum addressable
		; size
		mov  cx,0ffffh
		mov  dh,cl

CalcDriveSize:
		; Now that we have the drive geometry
		; lets calculate the drive size
		mov  bl,ch			; Put the low 8-bits of the cylinder count into BL
		mov  bh,cl			; Put the high 2-bits in BH
		shr  bh,6			; Shift them into position, now BX contains the cylinder count
		and  cl,3fh			; Mask off cylinder bits from sector count
		; CL now contains sectors per track and DH contains head count
		movzx eax,dh		; Move the heads into EAX
		movzx ebx,bx		; Move the cylinders into EBX
		movzx ecx,cl		; Move the sectors per track into ECX
		inc   eax			; Make it one based because the bios returns it zero based
		inc   ebx			; Make the cylinder count one based also
		mul   ecx			; Multiply heads with the sectors per track, result in edx:eax
		mul   ebx			; Multiply the cylinders with (heads * sectors) [stored in edx:eax already]

		; We now have the total number of sectors as reported
		; by the bios in eax, so store it in our variable
		mov   [BiosCHSDriveSize],eax


LoadExtraBootCode:
		; First we have to load our extra boot code at
		; sector 14 into memory at [0000:7e00h]
		mov  eax,0eh
        add  eax,DWORD [BYTE bp+HiddenSectors]	; Add the number of hidden sectors 
		mov  cx,1
        xor  bx,bx
        mov  es,bx								; Read sector to [0000:7e00h]
		mov  bx,7e00h
		call ReadSectors
		jmp  StartSearch



; Reads logical sectors into [ES:BX]
; EAX has logical sector number to read
; CX has number of sectors to read
ReadSectors:
		cmp  eax,DWORD [BiosCHSDriveSize]		; Check if they are reading a sector outside CHS range
		jae  ReadSectorsLBA						; Yes - go to the LBA routine
												; If at all possible we want to use LBA routines because
												; They are optimized to read more than 1 sector per read

		pushad									; Save logical sector number & sector count

CheckInt13hExtensions:							; Now check if this computer supports extended reads
		mov  ah,0x41							; AH = 41h
		mov  bx,0x55aa							; BX = 55AAh
		mov  dl,[BYTE bp+BootDrive]				; DL = drive (80h-FFh)
		int  13h								; IBM/MS INT 13 Extensions - INSTALLATION CHECK
		jc   ReadSectorsCHS						; CF set on error (extensions not supported)
		cmp  bx,0xaa55							; BX = AA55h if installed
		jne  ReadSectorsCHS
		test cl,1								; CX = API subset support bitmap
		jz   ReadSectorsCHS						; Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported

		popad									; Restore sector count & logical sector number

ReadSectorsLBA:
		pushad									; Save logical sector number & sector count

		cmp  cx,64								; Since the LBA calls only support 0x7F sectors at a time we will limit ourselves to 64
		jbe  ReadSectorsSetupDiskAddressPacket	; If we are reading less than 65 sectors then just do the read
		mov  cx,64								; Otherwise read only 64 sectors on this loop iteration

ReadSectorsSetupDiskAddressPacket:
		mov  [LBASectorsRead],cx
		o32 push byte 0
		push eax								; Put 64-bit logical block address on stack
		push es									; Put transfer segment on stack
		push bx									; Put transfer offset on stack
		push cx									; Set transfer count
		push byte 0x10							; Set size of packet to 10h
		mov  si,sp								; Setup disk address packet on stack


        mov  dl,[BYTE bp+BootDrive]				; Drive number
		mov  ah,42h								; Int 13h, AH = 42h - Extended Read
		int  13h								; Call BIOS
		jc   PrintDiskError						; If the read failed then abort

		add  sp,byte 0x10							; Remove disk address packet from stack

		popad									; Restore sector count & logical sector number

		push bx
		mov  ebx,DWORD [LBASectorsRead]
        add  eax,ebx							; Increment sector to read
		shl  ebx,5
        mov  dx,es
        add  dx,bx								; Setup read buffer for next sector
        mov  es,dx
		pop  bx

		sub  cx,[LBASectorsRead]
        jnz  ReadSectorsLBA						; Read next sector

        ret

LBASectorsRead:
	dd	0


; Reads logical sectors into [ES:BX]
; EAX has logical sector number to read
; CX has number of sectors to read
ReadSectorsCHS:
		popad										; Get logical sector number & sector count off stack

ReadSectorsCHSLoop:
        pushad
        xor   edx,edx
		movzx ecx,WORD [BYTE bp+SectorsPerTrack]
		div   ecx									; Divide logical by SectorsPerTrack
        inc   dl									; Sectors numbering starts at 1 not 0
		mov   cl,dl									; Sector in CL
		mov   edx,eax
		shr   edx,16
        div   WORD [BYTE bp+NumberOfHeads]			; Divide logical by number of heads
        mov   dh,dl									; Head in DH
        mov   dl,[BYTE bp+BootDrive]				; Drive number in DL
        mov   ch,al									; Cylinder in CX
        ror   ah,1									; Low 8 bits of cylinder in CH, high 2 bits
        ror   ah,1									;  in CL shifted to bits 6 & 7
        or    cl,ah									; Or with sector number
        mov   ax,0201h
        int   13h    ; DISK - READ SECTORS INTO MEMORY
                     ; AL = number of sectors to read, CH = track, CL = sector
                     ; DH = head, DL    = drive, ES:BX -> buffer to fill
                     ; Return: CF set on error, AH =    status (see AH=01h), AL    = number of sectors read

        jc    PrintDiskError						; If the read failed then abort

        popad

        inc   eax									; Increment Sector to Read

        mov   dx,es
        add   dx,byte 20h							; Increment read buffer for next sector
        mov   es,dx

        loop  ReadSectorsCHSLoop					; Read next sector

        ret   




; Displays a disk error message
; And reboots
PrintDiskError:
        mov  si,msgDiskError			; Bad boot disk message
        call PutChars					; Display it

		jmp  Reboot

; Displays a file system error message
; And reboots
PrintFileSystemError:
        mov  si,msgFileSystemError		; FreeLdr not found message
        call PutChars					; Display it

Reboot:
        mov  si,msgAnyKey				; Press any key message
        call PutChars					; Display it
        xor ax,ax       
        int 16h							; Wait for a keypress

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -