📄 usbmbb.asm
字号:
TITLE USBMBB.ASM -- Boot Block USB Mass Storage Device Functions
;***************************************************************************;
;***************************************************************************;
;** **;
;** (C)Copyright 1985-2002, American Megatrends, Inc. **;
;** **;
;** All Rights Reserved. **;
;** **;
;** 6145-F Northbelt Pkwy, Norcross, GA 30071 **;
;** **;
;** Phone (770)-246-8600 **;
;** **;
;***************************************************************************;
;***************************************************************************;
;***************************************************************************;
; $Header: /BIOS/Corebin/800/Modules/USB2/Template/Core/USBMBB.ASM 1 10/14/02 8:40p Sivagarn $
;
; $Revision: 1 $
;
; $Date: 10/14/02 8:40p $
;***************************************************************************;
; Revision History
; ----------------
; $Log: /BIOS/Corebin/800/Modules/USB2/Template/Core/USBMBB.ASM $
;
; 1 10/14/02 8:40p Sivagarn
; * USB boot block related files are added
;
;***************************************************************************;
;----------------------------------------------------------------------------
; Global options are defined here
;----------------------------------------------------------------------------
OPTION PROC:PRIVATE
;----------------------------------------------------------------------------
;----------------------------------------------------------------------------
; Macro and equate files are included here
;----------------------------------------------------------------------------
INCLUDE equates.equ
INCLUDE usb.equ
INCLUDE mbiosequ.equ
USB_MASS_COMMAND_BUFFER_SIZE EQU 32
USB_MASS_DATA_BUFFER_SIZE EQU 100h
USB_CMDSTS_MODE_SENSE_DONE EQU BIT0
USB_CMDSTS_READ_CAPACITY_DONE EQU BIT1
;<AMI_SHDR_START>
;----------------------------------------------------------------------------
; Name: USBI13RegAccess
;
; Description: Structure defining the local (stack) storage structure of
; the USB INT13h local store. These fields can be accessed
; with reference to SS.
;
; Fields: Name Type Description
; ------------------------------------------------------------
; dRegEDI DWORD EDI register value (PUSHAD)
; dRegESI DWORD ESI register value (PUSHAD)
; dRegEBP DWORD EBP register value (PUSHAD)
; dRegESP DWORD ESP register value (PUSHAD)
; dRegEBX DWORD EBX register value (PUSHAD)
; dRegEDX DWORD EDX register value (PUSHAD)
; dRegECX DWORD ECX register value (PUSHAD)
; dRegEAX DWORD EAX register value (PUSHAD)
; wRegDS WORD DS register value (PUSH DS)
; wRegES WORD ES register value (PUSH ES)
;
;----------------------------------------------------------------------------
;<AMI_SHDR_END>
USBI13RegAccess STRUC
dRegEDI DD ?
dRegESI DD ?
dRegEBP DD ?
dRegESP DD ?
dRegEBX DD ?
dRegEDX DD ?
dRegECX DD ?
dRegEAX DD ?
wRegDS DW ?
wRegES DW ?
USBI13RegAccess ENDS
;----------------------------------------------------------------------------
;----------------------------------------------------------------------------
; External data definitions are defined here
;----------------------------------------------------------------------------
EXTERN CurrentDevice:DeviceInfo
EXTERN CurrentHC:HCStruc
EXTERN bCurrentDeviceType:BYTE
EXTERN pDeviceInitializer:WORD
EXTERN ControlDataBuffer:BYTE
EXTERN Recovery_Flag:BYTE
EXTERN RECOVERY_FILE_FOUND_BIT:ABS
EXTERN RECOVERY_DEVICE_FOUND_BIT:ABS
EXTERN Int13_Vector:DWORD
EXTERN Mbr_Offset:WORD
EXTERN Mbr_Segment:WORD
EXTERN Recovery_Drive_Number:BYTE
;----------------------------------------------------------------------------
;----------------------------------------------------------------------------
; External function definitions are defined here
;----------------------------------------------------------------------------
USBBB_EnumerateUSBDevice PROTO NEAR SYSCALL
USBBB_IssueControlXferWithoutData PROTO NEAR SYSCALL
fixed_delay_far PROTO FAR SYSCALL
TryToReadMBR PROTO NEAR SYSCALL
ReadRomFileFromInt13Device PROTO NEAR SYSCALL
;----------------------------------------------------------------------------
;----------------------------------------------------------------------------
; D A T A S E G M E N T
;----------------------------------------------------------------------------
USB_DSEG SEGMENT PARA PUBLIC 'DATA'
PUBLIC bLastBulkCommandStalled
bLastBulkCommandStalled BYTE ?
wUSBCommandStatus WORD ?
BOTCommandTag DWORD ?
wMassTempData WORD ?
PUBLIC stMassDeviceInfo
stMassDeviceInfo MassDeviceInfo < ? >
stMassXactStruc MASS_XACT_STRUC < ? >
stMassRead MASS_READ < ? >
dLastSenseData DWORD ?
ALIGN 16
aMassCommandBuffer BYTE USB_MASS_COMMAND_BUFFER_SIZE DUP (?)
aMassDataBuffer BYTE USB_MASS_DATA_BUFFER_SIZE DUP (?)
;;ALIGN 16
PUBLIC USB_DSEG_DATA_END
USB_DSEG_DATA_END LABEL BYTE
USB_DSEG ENDS
;----------------------------------------------------------------------------
;----------------------------------------------------------------------------
; IVT_DSEG S E G M E N T STARTS
;----------------------------------------------------------------------------
IVT_DSEG SEGMENT PARA COMMON 'DATA'
IVT_DSEG ENDS
;----------------------------------------------------------------------------
; C O D E S E G M E N T
;----------------------------------------------------------------------------
RECOVERY_CSEG SEGMENT PUBLIC 'CODE'
ASSUME CS:RECOVERY_CSEG
.586p
;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure: farInitCheckUSBMedia
;
; Description: This eProcedure will search for the USB storage device and
; initialize it as A: for the recovery code to get the
; BIOS recovery image
;
; Input: None
;
; Output: None
;
; Modified: Nothing
;
;----------------------------------------------------------------------------
;<AMI_PHDR_END>
farInitCheckUSBMedia PROC FAR PUBLIC
push si
push ds
push es
; Check whether recovery device is already found
test CS:Recovery_Flag, RECOVERY_DEVICE_FOUND_BIT
jnz fICUM_Exit ; If recovery device already found,
call farUSBM_EnumerateStorageDevice
jz fICUM_Exit
; Recovery device found
push IVT_DSEG
pop es
; Set INT13 vector to USB Mass storage routines
push USB_CSEG
push OFFSET USBINT13Handler ; INT13 ISR
pop DWORD PTR ES:int13_vector
;; mov ax, cs
;; shl eax, 16
;; mov ax, OFFSET USBINT13Handler ; INT13 ISR
;; mov es:int13_vector, eax
call TryToReadMBR ; read from floppy
jc fICUM_Exit
or CS:Recovery_Flag, RECOVERY_DEVICE_FOUND_BIT
mov CS:bUSBRecoveryFlag, 1
fICUM_Exit:
pop es
pop ds
pop si
ret
farInitCheckUSBMedia ENDP
bUSBRecoveryFlag BYTE ?
;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure: farReadFileFromUSBMedia
;
; Description: This eProcedure will read the recovery file from the USB
; storage media
;
; Input: None
;
; Output: None
;
; Modified: Nothing
;
;----------------------------------------------------------------------------
;<AMI_PHDR_END>
farReadFileFromUSBMedia PROC FAR PUBLIC
test CS:Recovery_Flag, RECOVERY_FILE_FOUND_BIT
jnz fRFFUM_Exit
test CS:bUSBRecoveryFlag, 1
jz fRFFUM_Exit
push ds
push es
pushad
mov ds, CS:Mbr_Segment
mov es, CS:Mbr_Segment
mov bx, CS:Mbr_Offset
mov si, bx
mov dl, CS:Recovery_Drive_Number
call ReadRomFileFromInt13Device
mov CS:bUSBRecoveryFlag, 0
popad
pop es
pop ds
fRFFUM_Exit:
ret
farReadFileFromUSBMedia ENDP
RECOVERY_CSEG ENDS
;----------------------------------------------------------------------------
; C O D E S E G M E N T
;----------------------------------------------------------------------------
USB_CSEG SEGMENT PARA USE16 PUBLIC 'CODE'
ASSUME CS:USB_CSEG
ASSUME DS:USB_DSEG
.586p
PUBLIC _USBMBB_ASM_START
_USBMBB_ASM_START LABEL BYTE
;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure: farUSBM_EnumerateStorageDevice
;
; Description: This far Procedure will search for the USB storage device and
; initialize it as A: for the recovery code to get the
; BIOS recovery image
;
; Input: None
;
; Output: ZR On error (No device found)
; NZ On success (Recovery device found)
;
; Modified: Nothing
;
;----------------------------------------------------------------------------
;<AMI_PHDR_END>
farUSBM_EnumerateStorageDevice PROC FAR PUBLIC
push si
push ds
push es
; Set current device type that we are looking as storage
mov CS:bCurrentDeviceType, BIOS_DEV_TYPE_STORAGE
mov ax, OFFSET USBM_ConfigureStorageDevice
mov CS:pDeviceInitializer, ax
call USBBB_EnumerateUSBDevice ; Changes DS, ES
pop es
pop ds
pop si
ret
farUSBM_EnumerateStorageDevice ENDP
;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure: USBM_ConfigureStorageDevice
;
; Description: This function will configure the USB mass storage device
;
; Input: DS USB data area
; ES USB data area
;
; Output: ZR Error
; NZ Successful completion
;
; Modified: None
;
;----------------------------------------------------------------------------
;<AMI_PHDR_END>
USBM_ConfigureStorageDevice PROC NEAR PUBLIC
push ax
push bx
push cx
push si
; Set SI to the current devices device info structure
mov si, OFFSET CurrentDevice
; Clear the USB command flag
mov wUSBCommandStatus, 0
;; movzx cx, bUSBStorageDeviceDelayCount
mov cx, 20
UBCSD_TryAgain:
; Device is coming up give some delay
push cx
mov cx, ((500 * 1000) / 15) ; 500ms delay
call fixed_delay_far
pop cx
; Issue start unit command
call USBM_StartUnitCommand
; Check device ready
call USBM_TestUnitReady
jnz UBCSD_DeviceReady
mov eax, dLastSenseData
; AL - Sense Code
; AH - Additional Sense Code (ASC)
; AL+ - Additional Sense Code Qualifier (ASCQ)
cmp ax, 02806h
jne UBCSD_CheckForMedia
; Issue start unit command
call USBM_StartUnitCommand
jmp SHORT UBCSD_DeviceReady
UBCSD_CheckForMedia:
cmp eax, 003A02h
je UBCSD_DeviceReady ; No media !
; Check whether we can recover from this error condition
; Currently only recoverable error condition are
; 1. Device is getting ready (010402)
; 2. Device reset occurred (002906)
cmp eax, 010402h
je UBCSD_PrepareForNextIteration
; Check for write protected command
cmp al, 07h
je UBCSD_DeviceReady
cmp eax, 002906h
jne UBCSD_DeviceReady
UBCSD_PrepareForNextIteration:
loop UBCSD_TryAgain
UBCSD_DeviceReady:
; Issue read capacity command
call USBM_ReadCapacityCommand
jz UBCK_TryModeSense
jmp SHORT UBCK_GetBootRecord
;;; For CDROM (any device with block size more than 512 bytes) skip mode sense
;; cmp stMassDeviceInfo.wBlockSize, 200h
;; jb
UBCK_TryModeSense:
; Issue mode sense command
call USBM_ModeSense
jz UBCK_Exit ; We cannot support this device
UBCK_GetBootRecord:
call USBM_GetFormatType
; Return status in zero flag
UBCK_Exit:
pop si
pop cx
pop bx
pop ax
ret
USBM_ConfigureStorageDevice ENDP
;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure: USBM_ReadDevice
;
; Description: This function a sector at the LBA specified
;
; Input: EAX LBA to read
; CX Transfer length
; BX Pre-skip
; BX+ Post-skip
; EDI Data buffer
;
; Output: ZR If read failed
; NZ If read is successful
;
; Modified: Nothing
;
;----------------------------------------------------------------------------
;<AMI_PHDR_END>
USBM_ReadDevice PROC NEAR PUBLIC
push eax
push si
push edx
mov edx, eax
mov si, OFFSET aMassCommandBuffer
; Set opcode to read command
mov (CommonRWVCommand PTR [si]).OpCode, COMMON_READ_10_OPCODE
; Number of blocks (sectors to read)
xchg ch, cl
mov (CommonRWVCommand PTR [si]).TransferLength, cx
xchg ch, cl ; Better than save and restore CX
; Little endian
mov eax, edx
xchg ah, al ; Big endian to little endian
rol eax, 16
xchg ah, al
mov (CommonRWVCommand PTR [si]).Lba, eax
xor dx, dx ; DX = 0
; Fill the common bulk transaction structure
; Fill Command buffer address & size
mov stMassXactStruc.pCmdBuffer, si
mov stMassXactStruc.bCmdSize, SIZE CommonRWVCommand
mov stMassXactStruc.bXferDir, BIT7 ; IN
;; push es
;; push di
;; pop eax
mov stMassXactStruc.fpBuffer, edi
mov ax, cx
; AX = Number of blocks to read
mul stMassDeviceInfo.wBlockSize ; * block/sector size
push dx
push ax
pop eax ; EAX = #of bytes to read
mov stMassXactStruc.dwLength, eax
mov stMassXactStruc.wPreSkip, bx
ror ebx, 16
mov stMassXactStruc.wPostSkip, bx
ror ebx, 16 ; Better than save and restore
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -