lib/sw_services/xilloader/src/xloader_sd.c
int XLoader_RawCopy(u64 SrcAddr, u64 DestAddr, u32 Length, u32 Flags)
{
	int Status = XST_FAILURE;
	u8 ReadBuffer[XLOADER_SD_RAW_BLK_SIZE];
	u8* ReadBuffPtr;
	u16 DestOffset = (u16)(SrcAddr % XLOADER_SD_RAW_BLK_SIZE);
	u64 StartBlock = SrcAddr / XLOADER_SD_RAW_BLK_SIZE;
	u32 TrfLen;
	u32 NumBlocks;
	(void) Flags;

	XLoader_Printf(DEBUG_INFO, "SD Raw Reading Src 0x%0x%08x,"
		"Dest 0x%0x%08x, Length 0x%0x, Flags 0x%0x\r\n",
		(u32)(SrcAddr >> 32U), (u32)(SrcAddr), (u32)(DestAddr >> 32U),
		(u32)DestAddr, Length, Flags);
	SdInstance.Dma64BitAddr = 0U;
	Status = XSdPs_ReadPolled(&SdInstance, (u32)StartBlock, 1U, ReadBuffer);
	if (Status != XST_SUCCESS) {
		goto END;
	}

	TrfLen = (u32)(XLOADER_SD_RAW_BLK_SIZE - DestOffset);
	if (Length < TrfLen) {
		TrfLen = Length;
	}
	Status = XPlmi_DmaXfr((u64)(UINTPTR)&ReadBuffer[DestOffset], DestAddr,
		(TrfLen >> XPLMI_WORD_LEN_SHIFT), XPLMI_PMCDMA_0);
	if (Status != XST_SUCCESS) {
		goto END;
	}
	DestAddr += TrfLen;
	Length -= TrfLen;
	++StartBlock;

	while (Length > XLOADER_SD_CHUNK_SIZE) {
		if((DestAddr >> 32U) == 0U) {
			ReadBuffPtr = (u8 *)(UINTPTR)(DestAddr);
		}
		else {
			SdInstance.Dma64BitAddr = DestAddr;
			ReadBuffPtr = NULL;
		}
		Status  = XSdPs_ReadPolled(&SdInstance, (u32)StartBlock,
			XLOADER_NUM_SECTORS, ReadBuffPtr);
		if (Status != XST_SUCCESS) {
			goto END;
		}
		SrcAddr += XLOADER_SD_CHUNK_SIZE;
		DestAddr += XLOADER_SD_CHUNK_SIZE;
		Length -= XLOADER_SD_CHUNK_SIZE;
		StartBlock += XLOADER_NUM_SECTORS;
	}
	if (Length == 0U) {
		goto END;
	}

	if((DestAddr >> 32U) == 0U) {
		ReadBuffPtr = (u8 *)(UINTPTR)(DestAddr);
	}
	else {
		SdInstance.Dma64BitAddr = DestAddr;
		ReadBuffPtr = NULL;
	}
	NumBlocks = Length / XLOADER_SD_RAW_BLK_SIZE;
	if (NumBlocks != 0U) {
		Status = XSdPs_ReadPolled(&SdInstance, (u32)StartBlock,
			NumBlocks, ReadBuffPtr);
		if (Status != XST_SUCCESS) {
			goto END;
		}
		TrfLen = NumBlocks * XLOADER_SD_RAW_BLK_SIZE;
		DestAddr += TrfLen;
		Length -= TrfLen;
		StartBlock += NumBlocks;
	}
	if (Length == 0U) {
		goto END;
	}
	SdInstance.Dma64BitAddr = 0U;
	Status  = XSdPs_ReadPolled(&SdInstance, (u32)StartBlock, 1U, ReadBuffer);
	if (Status != XST_SUCCESS) {
		goto END;
	}
	Status = XPlmi_DmaXfr((u64)(UINTPTR)&ReadBuffer[0U], DestAddr,
		(Length >> XPLMI_WORD_LEN_SHIFT), XPLMI_PMCDMA_0);

END:
	return Status;
}
/*****************************************************************************/
/**
 * @brief	This function is used to copy the data from SD/eMMC to
 * destination address in raw boot mode only.
 *
 * @param	SrcAddr is the address of the SD flash where copy should
 * 		start from
 * @param	DestAddr is the address of the destination where it
 * 		should copy to
 * @param	Length of the bytes to be copied
 * @param	Flags param is unused and is only included for compliance with
 * 		other device boot modes
 *
 * @return	XST_SUCCESS on success and error code on failure
 *
 *****************************************************************************/
@brief       This function is used to copy the data from SD/eMMC to
destination address in raw boot mode only.

@param       SrcAddr is the address of the SD flash where copy should
start from
@param       DestAddr is the address of the destination where it
should copy to
@param       Length of the bytes to be copied
@param       Flags param is unused and is only included for compliance with
other device boot modes

@return      XST_SUCCESS on success and error code on failure
@brief This function is used to copy the data from SD / eMMC to destination address in raw boot mode only . @param SrcAddr is the address of the SD flash where copy should start from @param DestAddr is the address of the destination where it should copy to @param Length of the bytes to be copied @param Flags param is unused and is only included for compliance with other device boot modes @return XST_SUCCESS on success and error code on failure
int XLoader_RawCopy(u64 SrcAddr, u64 DestAddr, u32 Length, u32 Flags)
{
	int Status = XST_FAILURE;
	u8 ReadBuffer[XLOADER_SD_RAW_BLK_SIZE];
	u8* ReadBuffPtr;
	u16 DestOffset = (u16)(SrcAddr % XLOADER_SD_RAW_BLK_SIZE);
	u64 StartBlock = SrcAddr / XLOADER_SD_RAW_BLK_SIZE;
	u32 TrfLen;
	u32 NumBlocks;
	(void) Flags;
	XLoader_Printf(DEBUG_INFO, "SD Raw Reading Src 0x%0x%08x,"
		"Dest 0x%0x%08x, Length 0x%0x, Flags 0x%0x\r\n",
		(u32)(SrcAddr >> 32U), (u32)(SrcAddr), (u32)(DestAddr >> 32U),
		(u32)DestAddr, Length, Flags);
	SdInstance.Dma64BitAddr = 0U;
	Status = XSdPs_ReadPolled(&SdInstance, (u32)StartBlock, 1U, ReadBuffer);
	if (Status != XST_SUCCESS) {
		goto END;
	}
	TrfLen = (u32)(XLOADER_SD_RAW_BLK_SIZE - DestOffset);
	if (Length < TrfLen) {
		TrfLen = Length;
	}
	Status = XPlmi_DmaXfr((u64)(UINTPTR)&ReadBuffer[DestOffset], DestAddr,
		(TrfLen >> XPLMI_WORD_LEN_SHIFT), XPLMI_PMCDMA_0);
	if (Status != XST_SUCCESS) {
		goto END;
	}
	DestAddr += TrfLen;
	Length -= TrfLen;
	++StartBlock;
	while (Length > XLOADER_SD_CHUNK_SIZE) {
		if((DestAddr >> 32U) == 0U) {
			ReadBuffPtr = (u8 *)(UINTPTR)(DestAddr);
		}
		else {
			SdInstance.Dma64BitAddr = DestAddr;
			ReadBuffPtr = NULL;
		}
		Status  = XSdPs_ReadPolled(&SdInstance, (u32)StartBlock,
			XLOADER_NUM_SECTORS, ReadBuffPtr);
		if (Status != XST_SUCCESS) {
			goto END;
		}
		SrcAddr += XLOADER_SD_CHUNK_SIZE;
		DestAddr += XLOADER_SD_CHUNK_SIZE;
		Length -= XLOADER_SD_CHUNK_SIZE;
		StartBlock += XLOADER_NUM_SECTORS;
	}
	if (Length == 0U) {
		goto END;
	}
	if((DestAddr >> 32U) == 0U) {
		ReadBuffPtr = (u8 *)(UINTPTR)(DestAddr);
	}
	else {
		SdInstance.Dma64BitAddr = DestAddr;
		ReadBuffPtr = NULL;
	}
	NumBlocks = Length / XLOADER_SD_RAW_BLK_SIZE;
	if (NumBlocks != 0U) {
		Status = XSdPs_ReadPolled(&SdInstance, (u32)StartBlock,
			NumBlocks, ReadBuffPtr);
		if (Status != XST_SUCCESS) {
			goto END;
		}
		TrfLen = NumBlocks * XLOADER_SD_RAW_BLK_SIZE;
		DestAddr += TrfLen;
		Length -= TrfLen;
		StartBlock += NumBlocks;
	}
	if (Length == 0U) {
		goto END;
	}
	SdInstance.Dma64BitAddr = 0U;
	Status  = XSdPs_ReadPolled(&SdInstance, (u32)StartBlock, 1U, ReadBuffer);
	if (Status != XST_SUCCESS) {
		goto END;
	}
	Status = XPlmi_DmaXfr((u64)(UINTPTR)&ReadBuffer[0U], DestAddr,
		(Length >> XPLMI_WORD_LEN_SHIFT), XPLMI_PMCDMA_0);
END:
	return Status;
}
int XLoader_RawCopy ( u64 SrcAddr , u64 DestAddr , u32 Length , u32 Flags ) { int Status = XST_FAILURE ; u8 ReadBuffer [ XLOADER_SD_RAW_BLK_SIZE ] ; u8 * ReadBuffPtr ; u16 DestOffset = ( u16 ) ( SrcAddr % XLOADER_SD_RAW_BLK_SIZE ) ; u64 StartBlock = SrcAddr / XLOADER_SD_RAW_BLK_SIZE ; u32 TrfLen ; u32 NumBlocks ; ( void ) Flags ; XLoader_Printf ( DEBUG_INFO , " " " \r \n " , ( u32 ) ( SrcAddr >> 32U ) , ( u32 ) ( SrcAddr ) , ( u32 ) ( DestAddr >> 32U ) , ( u32 ) DestAddr , Length , Flags ) ; SdInstance . Dma64BitAddr = 0U ; Status = XSdPs_ReadPolled ( & SdInstance , ( u32 ) StartBlock , 1U , ReadBuffer ) ; if ( Status != XST_SUCCESS ) { goto END ; } TrfLen = ( u32 ) ( XLOADER_SD_RAW_BLK_SIZE - DestOffset ) ; if ( Length < TrfLen ) { TrfLen = Length ; } Status = XPlmi_DmaXfr ( ( u64 ) ( UINTPTR ) & ReadBuffer [ DestOffset ] , DestAddr , ( TrfLen >> XPLMI_WORD_LEN_SHIFT ) , XPLMI_PMCDMA_0 ) ; if ( Status != XST_SUCCESS ) { goto END ; } DestAddr += TrfLen ; Length -= TrfLen ; ++ StartBlock ; while ( Length > XLOADER_SD_CHUNK_SIZE ) { if ( ( DestAddr >> 32U ) == 0U ) { ReadBuffPtr = ( u8 * ) ( UINTPTR ) ( DestAddr ) ; } else { SdInstance . Dma64BitAddr = DestAddr ; ReadBuffPtr = NULL ; } Status = XSdPs_ReadPolled ( & SdInstance , ( u32 ) StartBlock , XLOADER_NUM_SECTORS , ReadBuffPtr ) ; if ( Status != XST_SUCCESS ) { goto END ; } SrcAddr += XLOADER_SD_CHUNK_SIZE ; DestAddr += XLOADER_SD_CHUNK_SIZE ; Length -= XLOADER_SD_CHUNK_SIZE ; StartBlock += XLOADER_NUM_SECTORS ; } if ( Length == 0U ) { goto END ; } if ( ( DestAddr >> 32U ) == 0U ) { ReadBuffPtr = ( u8 * ) ( UINTPTR ) ( DestAddr ) ; } else { SdInstance . Dma64BitAddr = DestAddr ; ReadBuffPtr = NULL ; } NumBlocks = Length / XLOADER_SD_RAW_BLK_SIZE ; if ( NumBlocks != 0U ) { Status = XSdPs_ReadPolled ( & SdInstance , ( u32 ) StartBlock , NumBlocks , ReadBuffPtr ) ; if ( Status != XST_SUCCESS ) { goto END ; } TrfLen = NumBlocks * XLOADER_SD_RAW_BLK_SIZE ; DestAddr += TrfLen ; Length -= TrfLen ; StartBlock += NumBlocks ; } if ( Length == 0U ) { goto END ; } SdInstance . Dma64BitAddr = 0U ; Status = XSdPs_ReadPolled ( & SdInstance , ( u32 ) StartBlock , 1U , ReadBuffer ) ; if ( Status != XST_SUCCESS ) { goto END ; } Status = XPlmi_DmaXfr ( ( u64 ) ( UINTPTR ) & ReadBuffer [ 0U ] , DestAddr , ( Length >> XPLMI_WORD_LEN_SHIFT ) , XPLMI_PMCDMA_0 ) ; END : return Status ; }
@brief       This function is used to copy the data from SD/eMMC to
 destination address in raw boot mode only.
@brief This function is used to copy the data from SD / eMMC to destination address in raw boot mode only .