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

📄 usbmisc.asm

📁 dos下的USB源码(包括UHCI
💻 ASM
📖 第 1 页 / 共 4 页
字号:
	call	USBMiscRestoreBaseAddress

	popf
	ret

USBMiscDwordReadMemReg	ENDP


;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure:	USBMiscDwordWriteMemReg
;
; Description:	This function writes a double word value from the host
;		controller memory space address
;
; Input: 	wMemOffs	Register offset to read
;		pHCStruc	HCStruc structure
;		dValue		Value to be written
;        	DS = ES		USB data area
;
; Output: 	Nothing
;
; Modified:	Nothing
;
; Referrals:	HCStruc
;
;----------------------------------------------------------------------------
;<AMI_PHDR_END>

USBMiscDwordWriteMemReg	PROC NEAR C PUBLIC USES ESI EBX DS,
				pHCStruc:NEAR, wMemOffs:DWORD, dValue:DWORD

	pushf
	cli
	mov	ebx, wMemOffs
	mov	si, pHCStruc
	add	ebx, (HCStruc PTR [si]).dBaseAddress

	push	ds
; The following routine modifies AX. If AX is non-zero then it modifies
; DS and EBX also.
; SI	HCStruc pointer
	call	USBMiscCheckAndUpdateBaseAddress
	or	ax, ax

	jz	ODWM_NotOS

	mov	eax, dValue
	add	ebx, wMemOffs
	mov	DWORD PTR ds:[bx], eax
	jmp	SHORT ODWM_DataWritten

ODWM_NotOS:		; Use CPU big real mode
	push	0
	pop	ds
	mov	eax, dValue
	mov	DWORD PTR ds:[ebx], eax

ODWM_DataWritten:

	pop	ds

; Restore the base address to the original value
; SI	HCStruc pointer
	call	USBMiscRestoreBaseAddress
	popf

	ret

USBMiscDwordWriteMemReg	ENDP


;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure:	USBMiscCheckAndUpdateBaseAddress
;
; Description:	This function changes the memory map base address for OHCI or
;		EHCI host controller to an address below 1MB if OS takes control
;		over over the BIOS
;
; Input: 	SI	HCStruc structure
;
; Output: 	AX		Value read from register
;		DS:BX
;
; Modified:	AX, DS, EBX
;
; Referrals:	HCStruc
;
;----------------------------------------------------------------------------
;<AMI_PHDR_END>

USBMiscCheckAndUpdateBaseAddress	PROC NEAR SYSCALL PUBLIC

	push	dx
	push	si

	xor	ax, ax			; POST not completed

IF	MKF_USB_MODE EQ 1

; Check if OS has control over USB
	cmp	USBAcquiredByOS, TRUE
	jnz	OCAUBA_NotOS

; USB is acquired by OS - make the memory mapped HC address below 1MB
; to be able to access it properly because OS may restrict any
; above 1MB memory access.

; Prepare new data area to be used as memory mapped HC registers
	call	USBPortPrepareMemoryMapBelow1MB

; DX:AX - Base address
	or	ax, ax
	jnz	OCAUBA_Continue

	or	dx, dx
	jnz	OCAUBA_Continue

; Routine not implemented. Stop the controller and exit.
; Invoke the stop routine in the HCDDriver
	mov	bx, (HCStruc PTR [si]).pHCDPointer
; SI - HCStruc pointer
	call	(HCDHEADER PTR cs:[bx]).pHCDStop

	jmp	OCAUBA_NotOS

OCAUBA_Continue:

	push	dx
	push	ax
	pop	ebx
; EBX - New memory mapped address (below 1MB)
; Program HC memory base register to the new value

; Write new base address
	mov	ah, USB_MEM_BASE_ADDRESS
	mov	dx, (HCStruc PTR [si]).wBusDevFuncNum
	call	write_pci_dword_far

;;	mov	esi, ebx

; Convert ESI into SEGMENT:OFFSET
	push	ebx
	shr	ebx, 4
	mov	ds, bx
	pop	ebx
	and	bx, 0Fh

	mov	ax, 0FFFFh
	jmp	SHORT OCAUBA_Exit

OCAUBA_NotOs:
	xor	ax, ax

OCAUBA_Exit:
ENDIF
	pop	si
	pop	dx

	ret

USBMiscCheckAndUpdateBaseAddress	ENDP


;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure:	USBMiscRestoreBaseAddress (pHCStruc)
;
; Description:	This function changes the memory map base address for the EHCI
;		or OHCI host controller to the original base address
;
; Input: 	SI	HCStruc structure
;
; Output: 	None
;
; Modified:	Nothing
;
; Referrals:	HCStruc
;
;----------------------------------------------------------------------------
;<AMI_PHDR_END>

USBMiscRestoreBaseAddress	PROC NEAR SYSCALL PUBLIC

	push	ax
	push	ebx
	push	si

; Check if OS has control over USB
	cmp	USBAcquiredByOS, TRUE
	jnz	ORBA_Exit

; USB is acquired by OS - restore the temp. memory map to its original
; state and restore the HC's memory map address to original address

	; Restore base address
	mov	ah, USB_MEM_BASE_ADDRESS
	mov	dx, (HCStruc PTR [si]).wBusDevFuncNum
	mov	ebx, (HCStruc PTR [si]).dBaseAddress
	call	write_pci_dword_far

; Restore the memory map state
	call	USBPortRestoreMemoryMapBelow1MB

ORBA_Exit:
	pop	si
	pop	ebx
	pop	ax
	ret

USBMiscRestoreBaseAddress	ENDP
ENDIF	; MKF_USB_MODE EQ 1
ENDIF	; MKF_USB_OHCI or MKF_USB_EHCI

;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure:	USBMiscInitFrameList
;
; Description:	This function will initialize the asynchronous frame list
;		pointers for the host controller with the value provided
;
; Input: 	SI	HCStruc strucuture
;		EAX	Value to initialize with
;
; Output: 	Nothing
;
; Modified:	Nothing
;
;----------------------------------------------------------------------------
;<AMI_PHDR_END>

USBMiscInitFrameList	PROC	NEAR SYSCALL PUBLIC

	push	ecx
	push	edi
	push	es

; Point data area to the frame list start
	push	0
	pop	es
	mov	edi, (HCStruc PTR [si]).dHcdDataArea
	movzx	ecx, (HCStruc PTR [si]).wAsyncListSize
	cld
	DB	67h	; use extended
	rep	stosd

	pop	es
	pop	edi
	pop	ecx
	ret
USBMiscInitFrameList	ENDP

;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure:	USBMiscGetFarBufferAddress
;
; Description:	This function will convert the far buffer address in segment:
;		offset form to 32bit absolute address. The buffer address
;		is taken from the DevInfo structure
;
; Input: 	BX	Device Info structure pointer
;
; Output: 	EDX	32bit physical address
;
; Modified:	EDX
;----------------------------------------------------------------------------
;<AMI_PHDR_END>

USBMiscGetFarBufferAddress	PROC	NEAR SYSCALL PUBLIC

	push	eax
; Change Seg:Off in fpBuffer to 32 bit absolute address
; Save the value in EDX
	mov	eax, (DeviceInfo PTR [bx]).CntrlXfer.fpBuffer
	movzx	edx, ax
	xor	ax, ax
	shr	eax, (16-4)
	add	edx, eax
	pop	eax
	ret

USBMiscGetFarBufferAddress	ENDP

;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure:	USBMiscFormDeviceRequest
;
; Description:	This function will convert the far buffer address in segment:
;		offset form to 32bit absolute address. The buffer address
;		is taken from the DevInfo structure
;
; Input: 	BX	Device Info structure pointer
;		DI	DeviceRequest structure pointer
;
; Output: 	Nothing
;
; Modified:	None
;----------------------------------------------------------------------------
;<AMI_PHDR_END>

USBMiscFormDeviceRequest	PROC	NEAR SYSCALL PUBLIC

	push	cx
	push	si
	push	di
	push	es

	push	ds
	pop	es
	mov	si, bx
	add	si, DeviceInfo.CntrlXfer
	mov	cx, SIZE DeviceRequest
	rep	movsb

	pop	es
	pop	di
	pop	si
	pop	cx
	ret
USBMiscFormDeviceRequest	ENDP

;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure:	USBMiscGetBulkEndPointInfo
;
; Description:	This function is used to return the bulk endpoint
;		information like endpoint number, max packet size and
;		data sync values for USB security device and mass storage
;		device.  This is used by UHCI & OHCI based HCs
;                                                                              ;
; Input: 	SI	DeviceInfo structure
;		DL	For identifying bulk-in or bulk-out
;
; Output: 	AX	Max bulk data packet value
;		BL	Endpoint number (Bulk IN/OUT)
;		BH	Data sync value
;
; Modified:	AX, BX
;
;----------------------------------------------------------------------------
;<AMI_PHDR_END>

USBMiscGetBulkEndPointInfo	PROC NEAR SYSCALL PUBLIC

	push	cx
	push	si
	push	di

; Get mass struc in SI
	mov	di, (DeviceInfo PTR [si]).pMassInfoPtr

; Get maximum packet size from device info structure
	mov	cx, (DeviceInfo PTR [si]).wBulkInMaxPkt
	mov	bl, (DeviceInfo PTR [si]).bBulkInEndpoint
	mov	bh, USB_BULK_IN_DATA_SYNC_SHIFT

	test	dl, BIT7
	jnz	UMGBEI_MaxPktSzFound

; It is an OUT transaction get appropriate size
	mov	cx, (DeviceInfo PTR [si]).wBulkOutMaxPkt
	mov	bl, (DeviceInfo PTR [si]).bBulkOutEndpoint
	mov	bh, USB_BULK_OUT_DATA_SYNC_SHIFT

UMGBEI_MaxPktSzFound:

; This may be a security device. In that case mass device info structure
; will be 0
	or	di, di
	jz	UMGBEI_NotALun

	cmp	(MassDeviceInfo PTR [di]).pLUN0DevInfoPtr, 0
	jz	UMGBEI_NotALun
	mov	si, (MassDeviceInfo PTR [di]).pLUN0DevInfoPtr
UMGBEI_NotALun:
	movzx	ax, (DeviceInfo PTR [si]).bDataSync

	push	bx
	push	cx
	movzx	bx, bh
	xor	cl, cl
	bt	ax, bx
	jnc	UMGBEI_ToggleBitObtained
	inc	cl
UMGBEI_ToggleBitObtained:
	pop	ax		; Max Packet
	pop	bx

	mov	bh, cl		; Data sync value
	pop	di
	pop	si
	pop	cx
	ret
USBMiscGetBulkEndPointInfo	ENDP

;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure:	USBMiscUpdateBulkDataSync
;
; Description:	This function is used to update the bulk data sync value
;		for the USB security sensor and mass storage device
;                                                                              ;
; Input: 	SI	DeviceInfo structure
;		DL	Transfer direction
;		CL	Data toggle value to be updated
;
; Output: 	Nothing
;
; Modified:	Nothing
;
;----------------------------------------------------------------------------
;<AMI_PHDR_END>

USBMiscUpdateBulkDataSync	PROC NEAR SYSCALL PUBLIC

	push	ax
	push	cx
	push	dx
	push	si
	push	di

	mov	ch, cl
; Update the data toggle value into the structure
	mov	dh, USB_BULK_IN_DATA_SYNC_SHIFT
	test	dl, BIT7
	jnz	UMUBDS_InPacket
; It is an OUT transaction get appropriate size
	mov	dh, USB_BULK_OUT_DATA_SYNC_SHIFT
UMUBDS_InPacket:

	mov	di, (DeviceInfo PTR [si]).pMassInfoPtr

; This may be a security device. In that case mass device info structure
; will be 0
	or	di, di
	jz	UMUBDS_ToggleBitDone

	cmp	(MassDeviceInfo PTR [di]).pLUN0DevInfoPtr, 0
	je	UMUBDS_ToggleBitDone
	mov	si, (MassDeviceInfo PTR [di]).pLUN0DevInfoPtr
UMUBDS_ToggleBitDone:


; Reset toggle bit
	mov	al, 1
	xchg	dh, cl
	shl	al, cl
	not	al
	and	(DeviceInfo PTR [si]).bDataSync, al

; Set toggle bit appropriately
	mov	al, ch
	shl	al, cl
	or	(DeviceInfo PTR [si]).bDataSync, al
	xchg	dh, cl

	pop	di
	pop	si
	pop	dx
	pop	cx
	pop	ax
	ret
USBMiscUpdateBulkDataSync	ENDP

IF	MKF_USB_MODE GE 2
;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure:	USBMiscConvertProtAddrToPhyAddr
;
; Description:	This function converts the address specified as SEG:OFFSET
;		into 32bit absolute address. If the SEG:OFFSET is SEL:OFFSET
;		then it parses through the page table and return the
;		correct 32bit absolute value
;
; Input: 	ES:DI	Address to convert
;
; Output: 	EDI	Converted address
;
; Modified:	EDI
;
;----------------------------------------------------------------------------
;<AMI_PHDR_END>

USBMiscConvertProtAddrToPhyAddr	PROC NEAR SYSCALL PUBLIC

	push	eax
	push	es
	push	fs

	xor	eax, eax

; Set FS to SMRAM dump area (of BSP)
	push	SMI_BSPDUMPSEG
	pop	fs

	test	BYTE PTR FS:[SMRAM_CPU_DUMP_CR0], BIT0	; Protected mode enabled?
	jz	UCPATP_Exit

; Protected mode and paging enabled. Convert the address
	mov	ax, es
	shl	eax, 4
	movzx	edi, di
	add	eax, edi

; Set ES to 0
	push	0
	pop	es

	call	USBMisc_ConvertLinearToPhysical
	mov	edi, eax		; EDI	Physical address

; Clear zero flag indicating translation done
	or	sp, sp

UCPATP_Exit:
	pop	fs
	pop	es
	pop	eax
        ret
USBMiscConvertProtAddrToPhyAddr	ENDP

;<AMI_PHDR_START>
;----------------------------------------------------------------------------
; Procedure:	USBMisc_ConvertLinearToPhysical
;
; Description:	This routine converts the 32bit linear address to 32 bit
;		physical address
;
; Input: 	FS	CPU dump segment
;		ES	0
;		EAX	Linear address to be converted
;
; Output: 	EAX	Converted physical address
;
; Modified:	EAX
;
; Notes:	This routine assumes the paging used as 4MByte other paging
;		sizes will not work with this code
;
;----------------------------------------------------------------------------
;<AMI_PHDR_END>

USBMisc_ConvertLinearToPhysical		PROC NEAR PUBLIC
	push	esi

; Check whether paging is enabled (CR0 bit31)
	test	BYTE PTR FS:[SMRAM_CPU_DUMP_CR0+3], BIT7
	jz	ukcltp_Exit			; No. So no need to convert

; Get the dump area version number
; If version number is below 30003h then we cannot access CR4
	cmp	DWORD PTR FS:[SMRAM_CPU_DUMP_VER], 30003h
	jb	ukcltp_PAENotEnabled		; Assume as PAE not enabled

; Check whether PAE(CR4) is enabled
	test	BYTE PTR FS:[SMRAM_CPU_DUMP_CR4], BIT5
	jz	ukcltp_PAENotEnabled

; Get the PDBR value from CR3
	mov	esi, DWORD PTR FS:[SMRAM_CPU_DUMP_CR3]
; Mask off lower 5 bits
	and	esi, 0FFFFFFE0h

; Get the directory table entry
	push	eax
	shr	eax, (30 - 3)	; Get bits 31-30 and multiply by 8

⌨️ 快捷键说明

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