📄 glblinit.asm
字号:
;/*
; * Microsoft Confidential
; * Copyright (C) Microsoft Corporation 1991
; * All Rights Reserved.
; */
;===========================================================================
;
; FILE: GLBLINIT.ASM
;
;===========================================================================
;===========================================================================
;Declaration of include files
;===========================================================================
;
;---------------------------------------------------------------------------
;
; M024 : B#5495. Added "Insufficient memory" message when FORMAT cannot
; allocate memory for FAT, Directory... etc
;
;---------------------------------------------------------------------------
;
debug equ 0
.xlist
INCLUDE BPB.INC
; INCLUDE VERSION.INC
; INCLUDE VERSIONA.INC
INCLUDE DOSMAC.INC
INCLUDE SYSCALL.INC
; INCLUDE DPB.INC
INCLUDE FOREQU.INC
INCLUDE FORMACRO.INC
INCLUDE IOCTL.INC
INCLUDE FORSWTCH.INC
INCLUDE SAFEDEF.INC
INCLUDE SYSVAR.INC
.list
;===========================================================================
; Declarations for all publics in other modules used by this module
;===========================================================================
;Bytes
EXTRN msgOutOfMemory :BYTE
EXTRN msgInsufficientMemory :BYTE ; M024
EXTRN Drive :BYTE
EXTRN ClustBound_Flag :BYTE
EXTRN FileStat :BYTE
EXTRN SystemDriveLetter :BYTE
;Words
EXTRN SwitchMap :WORD
EXTRN mSize :WORD
EXTRN mStart :WORD
EXTRN ClustBound_Buffer_Seg :WORD
EXTRN Paras_per_fat :WORD
;Pointers
EXTRN DirectorySector :DWORD
EXTRN FatSpace :DWORD
EXTRN FatSector :DWORD
;No more SAFE module
; EXTRN HeaderBuf :DWORD
EXTRN DirBuf :DWORD
;Functions
;Messages
EXTRN msgFormatNotSupported :BYTE
;Structures
EXTRN SavedParams :BYTE
EXTRN DeviceParameters :BYTE
EXTRN Bios :BYTE
EXTRN Dos :BYTE
EXTRN Command :BYTE
;Labels
EXTRN FatalExit :NEAR
EXTRN ReadDos :NEAR
EXTRN SysPrm :NEAR
;===========================================================================
; Data segment
;===========================================================================
DATA SEGMENT PUBLIC PARA 'DATA'
SECTORS_FOR_MIRROR EQU 7 ; # extra buffer sectors
; required by Mirror utility,
; apart from FAT & Root Dir
DATA ENDS
;===========================================================================
; Executable code segment
;===========================================================================
CODE SEGMENT PUBLIC PARA 'CODE'
ASSUME CS:CODE, DS:DATA, ES:DATA
;===========================================================================
; Declarations for all publics in this module
;===========================================================================
PUBLIC Global_Init
PUBLIC GetDeviceParameters
; for debug
PUBLIC Copy_Device_Parameters
PUBLIC Alloc_Dir_Buf
PUBLIC Alloc_Fat_Buf
PUBLIC Alloc_Fat_Sec_Buf
PUBLIC Alloc_DirBuf2
PUBLIC Alloc_Cluster_Buf
PUBLIC Do_Switch_S
;===========================================================================
;
; Global_Init : This procedure first gets the default drive parameters.
; It then allocates buffer space for the root directory
; sector, FAT,a fat sector, a file header and first
; root DIR sector based on these parameters. It
; then checks for the /s switch and if this is present,
; a buffer is allocated for the system files and these
; are read into memory. A prompt to insert the system
; disk will be given in the case of removable media.
;
;===========================================================================
Global_Init proc near
lea DX, DeviceParameters ; Get the default drive parameters
mov DeviceParameters.DP_SpecialFunctions, 0
call GetDeviceParameters
jnc GotDeviceParameters
Message msgFormatNotSupported
stc ; Let the jump to FatalExit be made
ret ; in the main routine, upon returning
GotDeviceParameters:
call Copy_Device_Parameters ; Save the device parameters
; for when we exit
call Alloc_Dir_Buf ; Allocate root directory buffer
jc gi_memerr ; M024
call Alloc_Fat_Buf ; Allocate FAT buffer
jc gi_memerr ; M024
call Alloc_Fat_Sec_Buf ; Allocate fat sector buffer
jc gi_memerr ; M024
;No more SAFE module
; call Alloc_Header_Buf ; Allocate buffer for restoration file
; retc
call Alloc_DirBuf2 ; Allocate 1-sector buffer DirBuf (general-
; purpose use)
jc gi_memerr ; M024
call Alloc_Cluster_Buf ; get room for retry buffer
call Do_Switch_S ; Load system files if needed
; carry flag determined by Do_Switch_S
; clc ; Signal no error
ret
gi_memerr: ; M024
Message msgInsufficientMemory ; M024
stc ; M024
ret ; M024
Global_Init endp
; =========================================================================
;
; GetDeviceParameters:
; Get the device parameters
;
; Input:
; Drive
; DX - pointer to device parameters
; =========================================================================
GetDeviceParameters proc near
mov AX, (IOCTL shl 8) or GENERIC_IOCTL
mov bl, Drive
inc bl
mov CX, (RAWIO shl 8) or GET_DEVICE_PARAMETERS
int 21H
return
GetDeviceParameters endp
;==========================================================================
;
; Copy_Device_Parameters : This procedure saves a copy of the original
; device parameters in the structure
; SavedParams.
;
;==========================================================================
Copy_Device_Parameters proc near
lea SI, DeviceParameters
lea DI, SavedParams
mov CX, size a_DeviceParameters
push DS
pop ES
rep movsb
ret
Copy_Device_Parameters endp
;==========================================================================
;
; Alloc_Dir_Buf : This procedure allocates a memory block for the root
; directory buffer, based on the device parameters only.
;
; Inputs : DeviceParameters.DP_BPB.BPB_BytesPerSector
; Outputs : CY CLEAR - DirectorySector pointer to buffer
; CY SET - failure
; Modifies : AX, BX, DirectorySector
;
;==========================================================================
Alloc_Dir_Buf proc near
; DirectorySector =
; malloc( Bytes Per Sector )
mov BX, DeviceParameters.DP_BPB.BPB_BytesPerSector
add BX, 0fH
shr BX, 1 ; Divide by 16 to get #paragraphs
shr BX, 1
shr BX, 1
shr BX, 1
mov AH, Alloc
int 21h
jc Exit_Alloc_Dir_Buf
; Base address of newly allocated
; block is AX:0000
mov WORD PTR DirectorySector+2,AX
xor AX,AX
mov WORD PTR DirectorySector,AX
Exit_Alloc_Dir_Buf:
ret
Alloc_Dir_Buf endp
;==========================================================================
;
; Alloc_Fat_Buf : This procedure allocates a memory block for the FAT
; buffer, based on the device parameters only. In order
; to ensure there is enough buffer space for the Mirror
; utility, the FatSpace buffer is initially allocated
; with size:
; FAT + RootDir + 6 sectors + 1 surplus sector
; which is all the buffer space required by Mirror.
;
; Inputs : DeviceParameters.DP_BPB.BPB_BytesPerSector
; DeviceParameters.DP_BPB.BPB_SectorsPerFat
; DeviceParameters.DP_BPB.BPB_RootEntries
;
; Outputs : CY CLEAR - FatSpace pointer to buffer
; CY SET - failure
;
; Modifies : AX, BX, DX, FatSpace
;
;==========================================================================
Alloc_Fat_Buf proc near
; FatSpace =
; malloc( BytesPerSec * SecPerFat +
; 32 * RootEntries + 6 * ByesPerSec)
mov AX, DeviceParameters.DP_BPB.BPB_BytesPerSector
add AX, 0fH ; round up for next para
shr AX, 1 ; convert to paras
shr AX, 1
shr AX, 1
shr AX, 1
mul DeviceParameters.DP_BPB.BPB_SectorsPerFat
mov BX,AX ; Save FAT size in paras in BX
;No more SAFE module
; Old Logic Was : Since this buffer is used in SAFE to hold the root
; directory from the disk, the size of the buffer is
; allocated as the larger of the drive defaults for
; the FAT or the whole root directory.
; The size of FatSpace must be the largest of
; 1) FAT
; 2) Root Dir
; 3) Cluster size needed for 1.5K
;
; mov AX,DeviceParameters.DP_BPB.BPB_RootEntries
; shl AX,1 ; Multiply by 2 to get total para size
; ; (Each entry is 32 bytes)
;
; cmp AX,BX ; now see which is bigger
; jna CheckClusters ; Use FAT size if BX >= AX
;
;UseDirSize:
; mov BX,AX ; Root directory is bigger
;
;CheckClusters: ; Now BX contains the larger of the
; ; FAT and RootDir size, in paras
; ; Calculate the para size of the #
; ; clusters needed for 1.5K
; mov AX, DeviceParameters.DP_BPB.BPB_BytesPerSector
; mov CL,deviceParameters.DP_BPB.BPB_SectorsPerCluster
; xor CH,CH ; Sectors per cluster in CX
; mul CX ; DX:AX= bytes per cluster
; mov CX,AX ; CX = #bytes per cluster
; mov AX,1536 ; AX = 1.5K
; div CX ; Calc. # clusters needed for 1.5K
; or DX,DX ; Non-zero remainder?
; jz Rounded ; No need to round up for zero remainder
; inc AX ; Increment #clusters needed, for non-zero remainder
;Rounded:
; mul CX ; Calculate byte size needed (#Clusters * BytesPerCluster)
; add AX, 0fH ; Convert to paras
; shr AX, 1
; shr AX, 1
; shr AX, 1
; shr AX, 1
;
; cmp AX,BX ; Now AX=para size for clusters in 1.5K
; jna AllocateAsIs ; Use larger of FAT,RootDir if not bigger
;
;UseClustSize:
; mov BX,AX ; Clusters in 1.5K is bigger
SaveFatSize:
mov Paras_per_fat,BX ; Set paras_per_fat here, to
; avoid having to calculate it later
; Now add on root dir + extra sectors
mov AX,DeviceParameters.DP_BPB.BPB_RootEntries
shl AX,1 ; AX = para size of root dir
add BX,AX ; BX = FAT + root dir
mov AX, DeviceParameters.DP_BPB.BPB_BytesPerSector
add AX, 0fH ; round up for next para
shr AX, 1 ; convert to paras
shr AX, 1
shr AX, 1
shr AX, 1 ; AX = sector size in paras
mov CX,SECTORS_FOR_MIRROR ; CX = # additional sectors needed by Mirror
mul CX ; AX = total extra sector size in paras
add BX,AX ; BX = FAT + root dir + extra sectors
; in paras
mov AH,Alloc
int 21h
jc Exit_Alloc_Fat_Buf
mov WORD PTR FatSpace+2,AX
xor AX,AX
mov WORD PTR FatSpace,AX
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -