Index: trunk/XTIDE_Universal_BIOS/Inc/ATA_ID.inc
===================================================================
--- trunk/XTIDE_Universal_BIOS/Inc/ATA_ID.inc	(revision 439)
+++ trunk/XTIDE_Universal_BIOS/Inc/ATA_ID.inc	(revision 441)
@@ -20,4 +20,10 @@
 %ifndef ATA_ID_INC
 %define ATA_ID_INC
+
+; Maximum valid P-CHS parameters
+MAX_VALID_PCHS_CYLINDERS			EQU		16383
+MAX_VALID_PCHS_HEADS				EQU		16
+MAX_VALID_PCHS_SECTORS_PER_TRACK	EQU		63
+
 
 ; PIO Minimum Cycle Times (t0)
@@ -316,4 +322,5 @@
 	.strMediaSr	resw 206-176	; 176...205V, Current media serial number
 				resw 255-206	; 206...254R
+	.wIntegrityWord:
 	.bSignature	resb 1	; 255[0-7]X, Signature
 	.bChecksum	resb 1	; 255[8-15]X, Checksum
@@ -472,4 +479,7 @@
 A6_wCFAPower_MASK_mA	EQU	0FFFh	; Maximum current in mA
 
+; ATA-6 WORD 255, Integrity Word (Signature and Checksum)
+A6_wIntegrity_SIGNATURE	EQU	0A5h
+
 
 %endif ; ATA_ID_INC
Index: trunk/XTIDE_Universal_BIOS/Src/Initialization/AtaID.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS/Src/Initialization/AtaID.asm	(revision 439)
+++ trunk/XTIDE_Universal_BIOS/Src/Initialization/AtaID.asm	(revision 441)
@@ -22,4 +22,85 @@
 SECTION .text
 
+;--------------------------------------------------------------------
+; AtaID_VerifyFromESSI
+;	Parameters:
+;		ES:SI:	Ptr to 512-byte ATA information read from the drive
+;	Returns:
+;		CF:		Set if failed to verify ATA-ID
+;				Cleared if ATA-ID verified succesfully
+;	Corrupts registers:
+;		AX, BX, CX
+;--------------------------------------------------------------------
+AtaID_VerifyFromESSI:
+	; We cannot start by reading ATA version since the ID might be
+	; corrupted. We start by making sure P-CHS values are valid.
+	; If they are, we assume the ATA ID to be valid. Fortunately we can do
+	; futher checking for ATA-5 and later since they contain signature and
+	; checksum bytes. Those are not available for ATA-4 and older.
+
+	; Verify P-CHS cylinders
+	mov		bx, ATA1.wCylCnt
+	mov		cx, MAX_VALID_PCHS_CYLINDERS
+	call	.CompareCHorSfromOffsetBXtoMaxValueInCX
+
+	add		bx, BYTE ATA1.wHeadCnt - ATA1.wCylCnt
+	mov		cl, MAX_VALID_PCHS_HEADS
+	call	.CompareCHorSfromOffsetBXtoMaxValueInCX
+
+	add		bx, BYTE ATA1.wSPT - ATA1.wHeadCnt
+	mov		cl, MAX_VALID_PCHS_SECTORS_PER_TRACK
+	call	.CompareCHorSfromOffsetBXtoMaxValueInCX
+
+	; We now verified P-CHS parameters so we assume ATA ID to be valid
+	; for ATA-4 and older. For ATA-5 and later we check signature
+	; and checksum.
+	mov		ax, [es:si+ATA6.wMajorVer]			; ATA-3 and later have this word
+	inc		ax
+	jz		SHORT .AtaIDverifiedSuccessfully	; FFFFh means no version info available
+	dec		ax
+	jz		SHORT .AtaIDverifiedSuccessfully	; Zero means no version info available
+	cmp		ax, A6_wMajorVer_ATA5
+	jb		SHORT .AtaIDverifiedSuccessfully	; ATA-3 and ATA-4 do not have checksum
+
+	; Check signature byte
+	cmp		BYTE [es:si+ATA6.bSignature], A6_wIntegrity_SIGNATURE
+	jne		SHORT .FailedToVerifyAtaID
+
+	; Check checksum byte
+	mov		cx, ATA6_size
+	call	Memory_SumCXbytesFromESSItoAL
+	test	al, al
+	jnz		SHORT .FailedToVerifyAtaID
+
+	; ATA-ID is now verified to be valid
+.AtaIDverifiedSuccessfully:
+	clc
+	ret
+
+;--------------------------------------------------------------------
+; .CompareCHorSfromOffsetBXtoMaxValueInCX
+;	Parameters:
+;		BX:		C, H or S offset to ATA-ID
+;		CX:		Maximum valid C, H or S value
+;		ES:SI:	Ptr to 512-byte ATA information read from the drive
+;	Returns:
+;		Exits from AtaID_VerifyFromESSI with CF set if invalid value
+;	Corrupts registers:
+;		AX
+;--------------------------------------------------------------------
+.CompareCHorSfromOffsetBXtoMaxValueInCX:
+	mov		ax, [es:bx+si]
+	test	ax, ax
+	jz		SHORT .InvalidPCHorSinOffsetBX
+	cmp		ax, cx			; Compare to max valid value
+	jbe		SHORT .ValidPCHorSinOffsetBX
+.InvalidPCHorSinOffsetBX:
+	add		sp, 2			; Clear return address for this function
+.FailedToVerifyAtaID:
+	stc						; Set carry to indicate invalid ATA-ID
+.ValidPCHorSinOffsetBX:
+	ret
+
+
 %ifdef MODULE_ADVANCED_ATA
 ;--------------------------------------------------------------------
Index: trunk/XTIDE_Universal_BIOS/Src/Initialization/DetectDrives.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS/Src/Initialization/DetectDrives.asm	(revision 439)
+++ trunk/XTIDE_Universal_BIOS/Src/Initialization/DetectDrives.asm	(revision 441)
@@ -218,4 +218,8 @@
 ;--------------------------------------------------------------------
 CreateBiosTablesForHardDisk:
+	push	bx
+	call	AtaID_VerifyFromESSI
+	pop		bx
+	jc		SHORT DetectDrives_DriveNotFound
 	call	CreateDPT_FromAtaInformation
 	jc		SHORT DetectDrives_DriveNotFound
