Index: /trunk/XTIDE_Universal_BIOS/Doc/changes.txt
===================================================================
--- /trunk/XTIDE_Universal_BIOS/Doc/changes.txt	(revision 26)
+++ /trunk/XTIDE_Universal_BIOS/Doc/changes.txt	(revision 27)
@@ -1,5 +1,7 @@
-XTIDE Universal BIOS v1.1.2 (26 July 2010)
-*Fixed a bug where Disk Parameter Table was accessed with wrong pointer register after writing last block
-*Cleaned AH=00h, Disk Controller Reset a bit
+XTIDE Universal BIOS v1.1.2 (28 July 2010)
+  * v1.1.1 broke booting from foreign drives, it is now fixed.
+  * Fixed a bug where Disk Parameter Table was accessed with wrong pointer register after writing last block.
+  * Cleaned AH=00h, Disk Controller Reset a bit.
+  * Timeout errors might now get translated for better error codes on certain situations.
 
 
Index: /trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h.asm
===================================================================
--- /trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h.asm	(revision 26)
+++ /trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h.asm	(revision 27)
@@ -88,5 +88,5 @@
 Int13h_UnsupportedFunction:
 Int13h_DirectCallToAnotherBios:
-	; Temporarily store original DI and DS to RAMVARS
+	; Temporarily store original DI and DS from stack to RAMVARS
 	pop		WORD [RAMVARS.wI13hDI]
 	pop		WORD [RAMVARS.wI13hDS]
Index: /trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/AH0h_HReset.asm
===================================================================
--- /trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/AH0h_HReset.asm	(revision 26)
+++ /trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/AH0h_HReset.asm	(revision 27)
@@ -2,5 +2,5 @@
 ; Project name	:	IDE BIOS
 ; Created date	:	27.9.2007
-; Last update	:	26.7.2010
+; Last update	:	28.7.2010
 ; Author		:	Tomi Tilli
 ; Description	:	Int 13h function AH=0h, Disk Controller Reset.
@@ -41,10 +41,10 @@
 	jz		SHORT .SkipHardDiskReset
 	call	ResetForeignHardDisks
-	call	ResetHardDisksHandledByOurBIOS
+	call	AH0h_ResetHardDisksHandledByOurBIOS
 ALIGN JUMP_ALIGN
 .SkipHardDiskReset:
 	mov		ah, bh						; Copy error code to AH
 	xor		al, al						; Zero AL...
-	sub		al, ah						; ...and set CF if error
+	cmp		al, bh						; ...and set CF if error
 	jmp		Int13h_PopXRegsAndReturn
 
@@ -54,14 +54,14 @@
 ;	Parameters:
 ;		BL:		Requested drive (DL when entering AH=00h)
-;		DL:		Drive number
 ;	Returns:
 ;		BH:		Error code from requested drive (if available)
 ;	Corrupts registers:
-;		AX, DL
+;		AX, DL, DI
 ;--------------------------------------------------------------------	
 ALIGN JUMP_ALIGN
 ResetFloppyDrivesWithInt40h:
+	call	GetDriveNumberForForeignBiosesToDL
+	and		dl, 7Fh						; Clear hard disk bit
 	xor		ah, ah						; Disk Controller Reset
-	and		dl, 7Fh						; Clear bit 7
 	int		INTV_FLOPPY_FUNC
 	jmp		SHORT BackupErrorCodeFromTheRequestedDriveToBH
@@ -76,17 +76,38 @@
 ;		BH:		Error code from requested drive (if available)
 ;	Corrupts registers:
-;		AX, DL
+;		AX, DL, DI
 ;--------------------------------------------------------------------	
 ALIGN JUMP_ALIGN
 ResetForeignHardDisks:
-	mov		dl, bl						; Drive to reset
-	mov		ah, 0Dh						; Reset Hard Disk (Alternate reset)
-
+	call	GetDriveNumberForForeignBiosesToDL
+	xor		ah, ah						; Disk Controller Reset
 	pushf								; Push flags to simulate INT
 	cli									; Disable interrupts since INT does that
 	call	FAR [RAMVARS.fpOldI13h]
 	sti									; Make sure interrupts are enabled again (some BIOSes fails to enable it)
+	jmp		SHORT BackupErrorCodeFromTheRequestedDriveToBH
 
-	jmp		SHORT BackupErrorCodeFromTheRequestedDriveToBH
+
+;--------------------------------------------------------------------
+; GetDriveNumberForForeignBiosesToDL
+;	Parameters:
+;		BL:		Requested drive (DL when entering AH=00h)
+;		DS:		RAMVARS segment
+;	Returns:
+;		DL:		BL if foreign drive
+;				80h if our drive
+;	Corrupts registers:
+;		DI
+;--------------------------------------------------------------------	
+ALIGN JUMP_ALIGN
+GetDriveNumberForForeignBiosesToDL:
+	mov		dl, bl
+	call	RamVars_IsDriveHandledByThisBIOS
+	jc		SHORT .GetFirstDriveForForeignBios
+	ret		; Return what was in BL unmodified
+ALIGN JUMP_ALIGN
+.GetFirstDriveForForeignBios:
+	mov		dl, 80h
+	ret
 
 
@@ -99,8 +120,8 @@
 ;		BH:		Error code from requested drive (if available)
 ;	Corrupts registers:
-;		AX, CX, DX
+;		AX, CX, DX, DI
 ;--------------------------------------------------------------------
 ALIGN JUMP_ALIGN
-ResetHardDisksHandledByOurBIOS:
+AH0h_ResetHardDisksHandledByOurBIOS:
 	mov		dh, [RAMVARS.bDrvCnt]		; Load drive count to DH
 	test	dh, dh
@@ -111,6 +132,5 @@
 .DriveResetLoop:
 	call	AHDh_ResetDrive
-	call	BackupErrorCodeFromTheRequestedDriveToBH
-	call	.SkipNextDriveIfItIsSlaveForThisController
+	call	.BackupErrorCodeFromMasterOrSlaveToBH
 	inc		dx
 	cmp		dl, dh						; All done?
@@ -120,52 +140,29 @@
 
 ;--------------------------------------------------------------------
-; .SkipNextDriveIfItIsSlaveForThisController
+; .BackupErrorCodeFromMasterOrSlaveToBH
 ;	Parameters:
+;		AH:		Error code for drive DL reset
+;		BL:		Requested drive (DL when entering AH=00h)
 ;		DL:		Drive just resetted
 ;		DS:		RAMVARS segment
 ;	Returns:
+;		BH:		Backuped error code
 ;		DL:		Incremented if next drive is slave drive
 ;				(=already resetted)
 ;	Corrupts registers:
-;		AX, CX
+;		CX, DI
 ;--------------------------------------------------------------------
 ALIGN JUMP_ALIGN
-.SkipNextDriveIfItIsSlaveForThisController:
-	push	di
+.BackupErrorCodeFromMasterOrSlaveToBH:
+	call	BackupErrorCodeFromTheRequestedDriveToBH
+	mov		cx, [RAMVARS.wIdeBase]		; Load base port for resetted drive
 
-	call	.GetBasePortToAXfromDriveInDL
-	xchg	cx, ax
-
-	inc		dx
-	call	.GetBasePortToAXfromDriveInDL
-	jnc		SHORT .SkipNextDrive
-
-	cmp		ax, cx
-	je		SHORT .SkipNextDrive		; Same controller so slave already reset
-
-	dec		dx							; Restore DX
-.SkipNextDrive:
-	pop		di
-	ret
-
-;--------------------------------------------------------------------
-; .GetBasePortToAXfromDriveInDL
-;	Parameters:
-;		DL:		Drive whose base port to find
-;		DS:		RAMVARS segment
-;	Returns:
-;		AX:		Base port (if drive found)
-;		CF:		Set if drive found
-;				Cleared if drive not found
-;	Corrupts registers:
-;		DI
-;--------------------------------------------------------------------
-ALIGN JUMP_ALIGN
-.GetBasePortToAXfromDriveInDL:
-	call	FindDPT_ForDriveNumber		; Get DPT to DS:DI
-	jnc		SHORT .DriveNotFound
-	eMOVZX	di, BYTE [di+DPT.bIdeOff]	; CS:DI now points to IDEVARS
-	mov		ax, [cs:di+IDEVARS.wPort]
-.DriveNotFound:
+	inc		dx							; DL to next drive
+	call	FindDPT_ForDriveNumber		; Get DPT to DS:DI, store port to RAMVARS
+	jnc		SHORT .NoMoreDrivesOrNoSlaveDrive
+	cmp		cx, [RAMVARS.wIdeBase]		; Next drive is from same controller?
+	je		SHORT BackupErrorCodeFromTheRequestedDriveToBH
+.NoMoreDrivesOrNoSlaveDrive:
+	dec		dx
 	ret
 
Index: /trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/AHDh_HReset.asm
===================================================================
--- /trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/AHDh_HReset.asm	(revision 26)
+++ /trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/AHDh_HReset.asm	(revision 27)
@@ -2,5 +2,5 @@
 ; Project name	:	IDE BIOS
 ; Created date	:	9.12.2007
-; Last update	:	26.7.2010
+; Last update	:	28.7.2010
 ; Author		:	Tomi Tilli
 ; Description	:	Int 13h function AH=Dh, Reset Hard Disk (Alternate reset).
@@ -40,9 +40,8 @@
 ;		CF:		0 if succesfull, 1 if error
 ;	Corrupts registers:
-;		Nothing
+;		DI
 ;--------------------------------------------------------------------
 ALIGN JUMP_ALIGN
 AHDh_ResetDrive:
-	push	di
 	push	dx
 	push	cx
@@ -64,5 +63,4 @@
 	pop		cx
 	pop		dx
-	pop		di
 	ret
 
Index: /trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/Common/HError.asm
===================================================================
--- /trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/Common/HError.asm	(revision 26)
+++ /trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/Common/HError.asm	(revision 27)
@@ -2,5 +2,5 @@
 ; Project name	:	IDE BIOS
 ; Created date	:	30.11.2007
-; Last update	:	8.4.2010
+; Last update	:	28.7.2010
 ; Author		:	Tomi Tilli
 ; Description	:	Error checking functions for BIOS Hard disk functions.
@@ -8,4 +8,24 @@
 ; Section containing code
 SECTION .text
+
+;--------------------------------------------------------------------
+; HError_GetErrorCodeToAHforBitPollingTimeout
+;	Parameters:
+;		AL:		IDE Status Register contents
+;		DX:		IDE Status Register Address
+;	Returns:
+;		AH:		Hard disk BIOS error code
+;		CF:		Set since error
+;	Corrupts registers:
+;		AL, CX
+;--------------------------------------------------------------------
+ALIGN JUMP_ALIGN
+HError_GetErrorCodeToAHforBitPollingTimeout:
+	test	al, FLG_IDE_ST_BSY						; Other bits undefined when BSY set
+	jnz		SHORT HError_GetErrorCodeForStatusReg	; Busy, normal timeout
+	test	al, FLG_IDE_ST_DF | FLG_IDE_ST_CORR | FLG_IDE_ST_ERR
+	jnz		SHORT HError_GetErrorCodeForStatusReg	; Not busy but some error
+	or		al, FLG_IDE_ST_BSY						; Polled bit got never set, force timeout
+	; Fall to HError_GetErrorCodeForStatusReg
 
 ;--------------------------------------------------------------------
@@ -37,6 +57,4 @@
 	mov		[HDBDA.wHDStAndErr], ax			; Store Status and Error to BDA
 	pop		ds
-
-	; Translate registers to BIOS error code
 	; Fall to HError_ConvertIdeErrorToBiosRet
 
@@ -58,8 +76,14 @@
 ALIGN JUMP_ALIGN
 HError_ConvertIdeErrorToBiosRet:
-	; Any error?
+	test	al, FLG_IDE_ST_BSY
+	jnz		SHORT .TimeoutError
 	test	al, FLG_IDE_ST_DF | FLG_IDE_ST_CORR | FLG_IDE_ST_ERR
 	jnz		SHORT .ReadErrorFromStatusReg
 	xor		ah, ah					; No errors, zero AH and CF
+	ret
+
+.TimeoutError:
+	mov		ah, RET_HD_TIMEOUT
+	stc
 	ret
 
Index: /trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/Common/HStatus.asm
===================================================================
--- /trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/Common/HStatus.asm	(revision 26)
+++ /trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/Common/HStatus.asm	(revision 27)
@@ -2,5 +2,5 @@
 ; Project name	:	IDE BIOS
 ; Created date	:	15.12.2009
-; Last update	:	26.6.2010
+; Last update	:	28.7.2010
 ; Author		:	Tomi Tilli
 ; Description	:	IDE Status Register polling functions.
@@ -189,6 +189,5 @@
 ; IDE Status register polling.
 ; This function first waits until controller is not busy.
-; When not busy, IDE Status Register is polled until wanted
-; flag (HBIT_ST_DRDY or HBIT_ST_DRQ) is set.
+; When not busy, IDE Status Register is polled until wanted flag is set.
 ;
 ; HStatus_PollBusyAndFlg
@@ -207,19 +206,17 @@
 ALIGN JUMP_ALIGN
 HStatus_PollBsyAndFlg:
-	call	SoftDelay_InitTimeout		; Initialize timeout counter
+	call	SoftDelay_InitTimeout				; Initialize timeout counter
 ALIGN JUMP_ALIGN
 .PollLoop:
-	in		al, dx						; Load IDE Status Register
-	test	al, FLG_IDE_ST_BSY			; Controller busy?
-	jnz		SHORT .UpdateTimeout		;  If so, jump to timeout update
-	test	al, ah						; Test secondary flag
-	jnz		SHORT HStatus_PollCompleted	; If set, break loop
+	in		al, dx								; Load IDE Status Register
+	test	al, FLG_IDE_ST_BSY					; Controller busy?
+	jnz		SHORT .UpdateTimeout				;  If so, jump to timeout update
+	test	al, ah								; Test secondary flag
+	jnz		SHORT GetErrorCodeFromPollingToAH	; If set, break loop
 ALIGN JUMP_ALIGN
 .UpdateTimeout:
-	call	SoftDelay_UpdTimeout		; Update timeout counter
-	jnc		SHORT .PollLoop				; Loop if time left (sets CF on timeout)
-	mov		ah, RET_HD_TIMEOUT			; Load error code for timeout
-	ret
-
+	call	SoftDelay_UpdTimeout				; Update timeout counter
+	jnc		SHORT .PollLoop						; Loop if time left (sets CF on timeout)
+	jmp		HError_GetErrorCodeToAHforBitPollingTimeout
 
 ;--------------------------------------------------------------------
@@ -241,21 +238,13 @@
 ALIGN JUMP_ALIGN
 HStatus_PollBsy:
-	call	SoftDelay_InitTimeout		; Initialize timeout counter
+	call	SoftDelay_InitTimeout				; Initialize timeout counter
 ALIGN JUMP_ALIGN
 .PollLoop:
-	in		al, dx						; Load IDE Status Reg
-	test	al, FLG_IDE_ST_BSY			; Controller busy? (clears CF)
-	jz		SHORT HStatus_PollCompleted	;  If not, jump to check errors
-	call	SoftDelay_UpdTimeout		; Update timeout counter
-	jnc		SHORT .PollLoop				; Loop if time left (sets CF on timeout)
-	mov		ah, RET_HD_TIMEOUT			; Load error code for timeout
-	ret
-
-ALIGN JUMP_ALIGN
-HStatus_PollCompleted:
-	test	al, FLG_IDE_ST_DF | FLG_IDE_ST_ERR
-	jnz		SHORT .GetErrorCode			;  If errors, jump to get error code
-	xor		ah, ah						; Zero AH and clear CF
-	ret
-.GetErrorCode:
+	in		al, dx								; Load IDE Status Reg
+	test	al, FLG_IDE_ST_BSY					; Controller busy?
+	jz		SHORT GetErrorCodeFromPollingToAH	;  If not, jump to check errors
+	call	SoftDelay_UpdTimeout				; Update timeout counter
+	jnc		SHORT .PollLoop						; Loop if time left (sets CF on timeout)
+ALIGN JUMP_ALIGN
+GetErrorCodeFromPollingToAH:
 	jmp		HError_GetErrorCodeForStatusReg
Index: /trunk/XTIDE_Universal_BIOS/Src/Initialization/FloppyDrive.asm
===================================================================
--- /trunk/XTIDE_Universal_BIOS/Src/Initialization/FloppyDrive.asm	(revision 26)
+++ /trunk/XTIDE_Universal_BIOS/Src/Initialization/FloppyDrive.asm	(revision 27)
@@ -2,5 +2,5 @@
 ; Project name	:	IDE BIOS
 ; Created date	:	25.3.2010
-; Last update	:	2.5.2010
+; Last update	:	28.7.2010
 ; Author		:	Tomi Tilli
 ; Description	:	Various floppy drive related functions that
@@ -25,10 +25,5 @@
 FloppyDrive_IsInt40hInstalled:
 	cmp		WORD [es:INTV_FLOPPY_FUNC*4+2], 0C000h	; Any ROM segment?
-	jae		SHORT .Int40hIsAlreadyInstalled
-	clc
-	ret
-ALIGN JUMP_ALIGN
-.Int40hIsAlreadyInstalled:
-	stc
+	cmc
 	ret
 
Index: /trunk/XTIDE_Universal_BIOS/Src/Initialization/Initialize.asm
===================================================================
--- /trunk/XTIDE_Universal_BIOS/Src/Initialization/Initialize.asm	(revision 26)
+++ /trunk/XTIDE_Universal_BIOS/Src/Initialization/Initialize.asm	(revision 27)
@@ -2,5 +2,5 @@
 ; Project name	:	IDE BIOS
 ; Created date	:	23.3.2010
-; Last update	:	1.7.2010
+; Last update	:	28.7.2010
 ; Author		:	Tomi Tilli
 ; Description	:	Functions for initializing the BIOS.
@@ -418,6 +418,7 @@
 ALIGN JUMP_ALIGN
 Initialize_ResetDetectedDrives:
-	xor		ah, ah				; Disk Controller Reset
-	mov		dl, 80h				; Reset all floppy drives and hard disks
-	int		INTV_DISK_FUNC
-	ret
+	xor		bx, bx
+	call	ResetFloppyDrivesWithInt40h
+	mov		bl, 80h
+	call	ResetForeignHardDisks
+	jmp		AH0h_ResetHardDisksHandledByOurBIOS
Index: /trunk/XTIDE_Universal_BIOS/Src/Main.asm
===================================================================
--- /trunk/XTIDE_Universal_BIOS/Src/Main.asm	(revision 26)
+++ /trunk/XTIDE_Universal_BIOS/Src/Main.asm	(revision 27)
@@ -38,5 +38,5 @@
 	at	ROMVARS.bRomSize,	db	CNT_ROM_BLOCKS	; ROM size in 512B blocks
 	at	ROMVARS.rgbJump, 	jmp	Initialize_FromMainBiosRomSearch
-	at	ROMVARS.rgbDate,	db	"07/26/10"		; Build data (mm/dd/yy)
+	at	ROMVARS.rgbDate,	db	"07/28/10"		; Build data (mm/dd/yy)
 	at	ROMVARS.rgbSign,	db	"XTIDE110"		; Signature for flash program
 	at	ROMVARS.szTitle
@@ -49,5 +49,5 @@
 		db	" (XT)=-",STOP
 %endif
-	at	ROMVARS.szVersion,	db	"v1.1.2 (07/26/10)",STOP
+	at	ROMVARS.szVersion,	db	"v1.1.2 (07/28/10)",STOP
 
 ;---------------------------;
