Index: trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/Tools/Prepare.asm
===================================================================
--- trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/Tools/PrepareBuffer.asm	(revision 218)
+++ trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h/Tools/Prepare.asm	(revision 221)
@@ -5,6 +5,78 @@
 SECTION .text
 
+;--------------------------------------------------------------------
+; Prepare_ByLoadingDapToESSIandVerifyingForTransfer
+;	Parameters:
+;		SI:		Offset to DAP
+;		DS:DI:	Ptr to DPT
+;		SS:BP:	Ptr to IDEPACK
+;	Parameters on INTPACK:
+;		DS:SI:	Ptr to Disk Address Packet
+;	Returns:
+;		BX:		Index to command lookup table
+;		ES:SI:	Ptr to Disk Address Packet (DAP)
+;		Exits from INT 13h if invalid DAP
+;	Corrupts registers:
+;		AX, DX
+;--------------------------------------------------------------------
+%ifdef MODULE_EBIOS
+ALIGN JUMP_ALIGN
+Prepare_ByLoadingDapToESSIandVerifyingForTransfer:
+	; Load pointer to DAP to ES:SI and make sure it is valid
+	mov		es, [bp+IDEPACK.intpack+INTPACK.ds]	; ES:SI to point Disk Address Packet
+	cmp		BYTE [es:si+DAP.bSize], MINIMUM_DAP_SIZE
+	jb		SHORT InvalidDAP
+
+	; Make sure that sector count is valid
+	mov		ax, [es:si+DAP.wSectorCount]
+	test	ax, ax
+	jz		SHORT ZeroSectorsRequestedSoNoErrors
+	cmp		ax, BYTE 127
+	ja		SHORT InvalidNumberOfSectorsRequested
+	; Fall to GetEbiosCommandIndexToBX
+
+;--------------------------------------------------------------------
+; GetEbiosCommandIndexToBX
+;	Parameters:
+;		DS:DI:	Ptr to DPT
+;		ES:SI:	Ptr to DAP (Disk Address Packet)
+;	Returns:
+;		BX:		Index to command lookup table
+;	Corrupts registers:
+;		AX, DX
+;--------------------------------------------------------------------
+GetEbiosCommandIndexToBX:
+	; LBA28 or LBA48 command
+	xor		dx, dx
+	mov		al, [es:si+DAP.qwLBA+3]	; Load LBA48 byte 3 (bits 24...31)
+	and		ax, 00F0h				; Clear LBA28 bits 24...27
+	or		ax, [es:si+DAP.qwLBA+4]	; Set bits from LBA bytes 4 and 5
+	cmp		dx, ax					; Set CF if any of bits 28...47 set
+	rcl		dx, 1					; DX = 0 for LBA28, DX = 1 for LBA48
+	call	Prepare_GetOldInt13hCommandIndexToBX
+	or		bx, dx					; Set block mode / single sector bit
+	ret
+%endif
+
+
+;--------------------------------------------------------------------
+; Prepare_GetOldInt13hCommandIndexToBX
+;	Parameters:
+;		DS:DI:	Ptr to DPT
+;	Returns:
+;		BX:		Index to command lookup table
+;	Corrupts registers:
+;		Nothing
+;--------------------------------------------------------------------
+ALIGN JUMP_ALIGN
+Prepare_GetOldInt13hCommandIndexToBX:
+	; Block mode or single sector
+	mov		bl, [di+DPT.bFlagsHigh]
+	and		bx, BYTE FLGH_DPT_BLOCK_MODE_SUPPORTED	; Bit 1
+	ret
+
+
 ;---------------------------------------------------------------------
-; PrepareBuffer_ToESSIforOldInt13hTransfer
+; Prepare_BufferToESSIforOldInt13hTransfer
 ;	Parameters:
 ;		AL:		Number of sectors to transfer
@@ -19,5 +91,5 @@
 ;--------------------------------------------------------------------
 ALIGN JUMP_ALIGN
-PrepareBuffer_ToESSIforOldInt13hTransfer:
+Prepare_BufferToESSIforOldInt13hTransfer:
 	; Normalize buffer pointer
 	mov		bx, [bp+IDEPACK.intpack+INTPACK.bx]	; Load offset
@@ -27,9 +99,21 @@
 	mov		es, bx								; Segment normalized
 	and		si, BYTE 0Fh						; Offset normalized
+	; Fall to Prepare_ByValidatingSectorsInALforOldInt13h
 
-	; Check if valid number of sectors
+
+;---------------------------------------------------------------------
+; Prepare_ByValidatingSectorsInALforOldInt13h
+;	Parameters:
+;		AL:		Number of sectors to transfer
+;	Returns:
+;		Exits INT 13h if invalid number of sectors in AL
+;	Corrupts registers:
+;		Nothing
+;--------------------------------------------------------------------
+ALIGN JUMP_ALIGN
+Prepare_ByValidatingSectorsInALforOldInt13h:
 	test	al, al
-	js		SHORT .CheckZeroOffsetFor128Sectors
-	jz		SHORT .InvalidNumberOfSectorsRequested
+	js		SHORT .CheckZeroOffsetFor128Sectors		; 128 or more
+	jz		SHORT InvalidNumberOfSectorsRequested	; Zero not allowed for old INT 13h
 	ret		; Continue with transfer
 
@@ -37,13 +121,36 @@
 .CheckZeroOffsetFor128Sectors:
 	cmp		al, 128
-	ja		SHORT .InvalidNumberOfSectorsRequested
+	ja		SHORT InvalidNumberOfSectorsRequested
 	mov		ah, RET_HD_BOUNDARY
 	test	si, si								; Offset must be zero to xfer 128 sectors
-	jnz		SHORT .CannotAlignPointerProperly
+	jnz		SHORT CannotAlignPointerProperly
 	ret		; Continue with transfer
 
-.InvalidNumberOfSectorsRequested:
+InvalidDAP:
+InvalidNumberOfSectorsRequested:
+Prepare_ReturnFromInt13hWithInvalidFunctionError:
 	mov		ah, RET_HD_INVALID
-.CannotAlignPointerProperly:
+ZeroSectorsRequestedSoNoErrors:
+CannotAlignPointerProperly:
 	jmp		Int13h_ReturnFromHandlerAfterStoringErrorCodeFromAH
 
+
+
+; Command lookup tables
+g_rgbReadCommandLookup:
+	db		COMMAND_READ_SECTORS		; 00b, CHS or LBA28 single sector
+	db		COMMAND_READ_SECTORS_EXT	; 01b, LBA48 single sector
+	db		COMMAND_READ_MULTIPLE		; 10b, CHS or LBA28 block mode
+	db		COMMAND_READ_MULTIPLE_EXT	; 11b, LBA48 block mode
+
+g_rgbWriteCommandLookup:
+	db		COMMAND_WRITE_SECTORS
+	db		COMMAND_WRITE_SECTORS_EXT
+	db		COMMAND_WRITE_MULTIPLE
+	db		COMMAND_WRITE_MULTIPLE_EXT
+
+g_rgbVerifyCommandLookup:
+	db		COMMAND_VERIFY_SECTORS
+	db		COMMAND_VERIFY_SECTORS_EXT
+	db		COMMAND_VERIFY_SECTORS
+	db		COMMAND_VERIFY_SECTORS_EXT
