lib/sw_services/xilloader/src/xloader_sd.c
int XLoader_SdInit(u32 DeviceFlags)
{
	int Status = XST_FAILURE;
	FRESULT Rc;
	u32 MultiBootOffset;
	u8 PdiSrc = (u8)(DeviceFlags & XLOADER_PDISRC_FLAGS_MASK);
	u8 DrvNum = XLoader_GetDrvNumSD(PdiSrc);
	static FATFS FatFileSystem;
	u32 CapSecureAccess = (u32)PM_CAP_ACCESS | (u32)PM_CAP_SECURE;
	u32 ErrorCode;

	Status = XPlmi_MemSetBytes(BootFile, sizeof(BootFile), 0U, sizeof(BootFile));
	if (Status != XST_SUCCESS) {
		Status = XPlmi_UpdateStatus(XLOADER_ERR_MEMSET,
			(int)XLOADER_ERR_MEMSET_SD_BOOT_FILE);
		goto END;
	}

	if ((PdiSrc == XLOADER_PDI_SRC_SD0) ||
		(PdiSrc == XLOADER_PDI_SRC_EMMC0)) {
		SdDeviceNode = PM_DEV_SDIO_0;
		SdCdnReg = PMC_IOU_SLCR_SD0_CDN_CTRL;
		ErrorCode = XLOADER_ERR_PM_DEV_SDIO_0;
	}
	else {
		SdDeviceNode = PM_DEV_SDIO_1;
		SdCdnReg = PMC_IOU_SLCR_SD1_CDN_CTRL;
		ErrorCode = XLOADER_ERR_PM_DEV_SDIO_1;
	}

	Status = XPm_RequestDevice(PM_SUBSYS_PMC, SdDeviceNode,
		CapSecureAccess, XPM_DEF_QOS, 0U, XPLMI_CMD_SECURE);
	if (Status != XST_SUCCESS) {
		Status = XPlmi_UpdateStatus(ErrorCode, Status);
		goto END;
	}
	SdCdnVal = XPlmi_In32(SdCdnReg);
	XPlmi_Out32(SdCdnReg, PMC_IOU_SLCR_SD0_CDN_CTRL_SD0_CDN_CTRL_MASK);

	/*
	 * Filesystem boot modes require the filename extension as well as
	 * the logical drive in which the secondary pdi file is present.
	 * To meet these requirements and to reuse the same code for primary
	 * and secondary boot modes, bits 0 to 3 in DeviceFlags denote the
	 * PdiSrc. Bits 4 to 19 denote the file name extension.
	 * Example if the offset is 4, file name should be BOOT0004.BIN, these
	 * bits are analogous to multiboot offset in case of primary boot mode.
	 * Bits 20, 21, 22 and 23 denote the logical drive number of the
	 * secondary device. Please note that bits 20, 21, 22 and 23 in device
	 * flags actually map to bits 16, 17, 18 and 19 in secondary device
	 * address specified in bif file.
	 */
	DeviceFlags >>= XLOADER_SD_ADDR_SHIFT;
	MultiBootOffset = (DeviceFlags & XLOADER_SD_ADDR_MASK);
	DeviceFlags >>= XLOADER_LOGICAL_DRV_SHIFT;
	DrvNum += (u8)(DeviceFlags & XLOADER_LOGICAL_DRV_MASK);

	/* Set logical drive number */
	/* Register volume work area, initialize device */
	BootFile[0U] = (char)DrvNum + 48;
	BootFile[1U] = ':';
	BootFile[2U] = '/';
	Rc = f_mount(&FatFileSystem, BootFile, 0U);

	XLoader_Printf(DEBUG_INFO,"SD: rc= %.8x\n\r", Rc);

	if (Rc != FR_OK) {
		Status = XPlmi_UpdateStatus(XLOADER_ERR_SD_INIT, (int)Rc);
		XLoader_Printf(DEBUG_GENERAL, "XLOADER_ERR_SD_INIT\n\r");
		XPlmi_Out32(SdCdnReg, SdCdnVal);
		goto END;
	}

	/*
	 * Create boot image name
	 */
	Status = XLoader_MakeSdFileName(MultiBootOffset);
	if (Status != XST_SUCCESS) {
		goto END;
	}

	Rc = f_open(&FFil, BootFile, (BYTE)FA_READ);
	if (Rc != FR_OK) {
		XLoader_Printf(DEBUG_INFO, "SD: Unable to open file %s: %d\n",
				BootFile, Rc);
		Status = XPlmi_UpdateStatus(XLOADER_ERR_SD_F_OPEN, (int)Rc);
		XLoader_Printf(DEBUG_GENERAL, "XLOADER_ERR_SD_F_OPEN\n\r");
		XPlmi_Out32(SdCdnReg, SdCdnVal);
		(void)f_unmount(BootFile);
		goto END;
	}
	Status = XST_SUCCESS;

END:
	return Status;
}
/*****************************************************************************/
/**
 * @brief	This function is used to initialize the sd controller and driver.
 *
 * @param	DeviceFlags have the bootmode information.
 *
 * @return	XST_SUCCESS on success and error code on failure
 *
 *****************************************************************************/
@brief       This function is used to initialize the sd controller and driver.
@param       DeviceFlags have the bootmode information.

@return      XST_SUCCESS on success and error code on failure
@brief This function is used to initialize the sd controller and driver . @param DeviceFlags have the bootmode information . @return XST_SUCCESS on success and error code on failure
int XLoader_SdInit(u32 DeviceFlags)
{
	int Status = XST_FAILURE;
	FRESULT Rc;
	u32 MultiBootOffset;
	u8 PdiSrc = (u8)(DeviceFlags & XLOADER_PDISRC_FLAGS_MASK);
	u8 DrvNum = XLoader_GetDrvNumSD(PdiSrc);
	static FATFS FatFileSystem;
	u32 CapSecureAccess = (u32)PM_CAP_ACCESS | (u32)PM_CAP_SECURE;
	u32 ErrorCode;
	Status = XPlmi_MemSetBytes(BootFile, sizeof(BootFile), 0U, sizeof(BootFile));
	if (Status != XST_SUCCESS) {
		Status = XPlmi_UpdateStatus(XLOADER_ERR_MEMSET,
			(int)XLOADER_ERR_MEMSET_SD_BOOT_FILE);
		goto END;
	}
	if ((PdiSrc == XLOADER_PDI_SRC_SD0) ||
		(PdiSrc == XLOADER_PDI_SRC_EMMC0)) {
		SdDeviceNode = PM_DEV_SDIO_0;
		SdCdnReg = PMC_IOU_SLCR_SD0_CDN_CTRL;
		ErrorCode = XLOADER_ERR_PM_DEV_SDIO_0;
	}
	else {
		SdDeviceNode = PM_DEV_SDIO_1;
		SdCdnReg = PMC_IOU_SLCR_SD1_CDN_CTRL;
		ErrorCode = XLOADER_ERR_PM_DEV_SDIO_1;
	}
	Status = XPm_RequestDevice(PM_SUBSYS_PMC, SdDeviceNode,
		CapSecureAccess, XPM_DEF_QOS, 0U, XPLMI_CMD_SECURE);
	if (Status != XST_SUCCESS) {
		Status = XPlmi_UpdateStatus(ErrorCode, Status);
		goto END;
	}
	SdCdnVal = XPlmi_In32(SdCdnReg);
	XPlmi_Out32(SdCdnReg, PMC_IOU_SLCR_SD0_CDN_CTRL_SD0_CDN_CTRL_MASK);
	DeviceFlags >>= XLOADER_SD_ADDR_SHIFT;
	MultiBootOffset = (DeviceFlags & XLOADER_SD_ADDR_MASK);
	DeviceFlags >>= XLOADER_LOGICAL_DRV_SHIFT;
	DrvNum += (u8)(DeviceFlags & XLOADER_LOGICAL_DRV_MASK);
	BootFile[0U] = (char)DrvNum + 48;
	BootFile[1U] = ':';
	BootFile[2U] = '/';
	Rc = f_mount(&FatFileSystem, BootFile, 0U);
	XLoader_Printf(DEBUG_INFO,"SD: rc= %.8x\n\r", Rc);
	if (Rc != FR_OK) {
		Status = XPlmi_UpdateStatus(XLOADER_ERR_SD_INIT, (int)Rc);
		XLoader_Printf(DEBUG_GENERAL, "XLOADER_ERR_SD_INIT\n\r");
		XPlmi_Out32(SdCdnReg, SdCdnVal);
		goto END;
	}
	Status = XLoader_MakeSdFileName(MultiBootOffset);
	if (Status != XST_SUCCESS) {
		goto END;
	}
	Rc = f_open(&FFil, BootFile, (BYTE)FA_READ);
	if (Rc != FR_OK) {
		XLoader_Printf(DEBUG_INFO, "SD: Unable to open file %s: %d\n",
				BootFile, Rc);
		Status = XPlmi_UpdateStatus(XLOADER_ERR_SD_F_OPEN, (int)Rc);
		XLoader_Printf(DEBUG_GENERAL, "XLOADER_ERR_SD_F_OPEN\n\r");
		XPlmi_Out32(SdCdnReg, SdCdnVal);
		(void)f_unmount(BootFile);
		goto END;
	}
	Status = XST_SUCCESS;
END:
	return Status;
}
int XLoader_SdInit ( u32 DeviceFlags ) { int Status = XST_FAILURE ; FRESULT Rc ; u32 MultiBootOffset ; u8 PdiSrc = ( u8 ) ( DeviceFlags & XLOADER_PDISRC_FLAGS_MASK ) ; u8 DrvNum = XLoader_GetDrvNumSD ( PdiSrc ) ; static FATFS FatFileSystem ; u32 CapSecureAccess = ( u32 ) PM_CAP_ACCESS | ( u32 ) PM_CAP_SECURE ; u32 ErrorCode ; Status = XPlmi_MemSetBytes ( BootFile , sizeof ( BootFile ) , 0U , sizeof ( BootFile ) ) ; if ( Status != XST_SUCCESS ) { Status = XPlmi_UpdateStatus ( XLOADER_ERR_MEMSET , ( int ) XLOADER_ERR_MEMSET_SD_BOOT_FILE ) ; goto END ; } if ( ( PdiSrc == XLOADER_PDI_SRC_SD0 ) || ( PdiSrc == XLOADER_PDI_SRC_EMMC0 ) ) { SdDeviceNode = PM_DEV_SDIO_0 ; SdCdnReg = PMC_IOU_SLCR_SD0_CDN_CTRL ; ErrorCode = XLOADER_ERR_PM_DEV_SDIO_0 ; } else { SdDeviceNode = PM_DEV_SDIO_1 ; SdCdnReg = PMC_IOU_SLCR_SD1_CDN_CTRL ; ErrorCode = XLOADER_ERR_PM_DEV_SDIO_1 ; } Status = XPm_RequestDevice ( PM_SUBSYS_PMC , SdDeviceNode , CapSecureAccess , XPM_DEF_QOS , 0U , XPLMI_CMD_SECURE ) ; if ( Status != XST_SUCCESS ) { Status = XPlmi_UpdateStatus ( ErrorCode , Status ) ; goto END ; } SdCdnVal = XPlmi_In32 ( SdCdnReg ) ; XPlmi_Out32 ( SdCdnReg , PMC_IOU_SLCR_SD0_CDN_CTRL_SD0_CDN_CTRL_MASK ) ; DeviceFlags >>= XLOADER_SD_ADDR_SHIFT ; MultiBootOffset = ( DeviceFlags & XLOADER_SD_ADDR_MASK ) ; DeviceFlags >>= XLOADER_LOGICAL_DRV_SHIFT ; DrvNum += ( u8 ) ( DeviceFlags & XLOADER_LOGICAL_DRV_MASK ) ; BootFile [ 0U ] = ( char ) DrvNum + 48 ; BootFile [ 1U ] = ' ' ; BootFile [ 2U ] = ' ' ; Rc = f_mount ( & FatFileSystem , BootFile , 0U ) ; XLoader_Printf ( DEBUG_INFO , " \n \r " , Rc ) ; if ( Rc != FR_OK ) { Status = XPlmi_UpdateStatus ( XLOADER_ERR_SD_INIT , ( int ) Rc ) ; XLoader_Printf ( DEBUG_GENERAL , " \n \r " ) ; XPlmi_Out32 ( SdCdnReg , SdCdnVal ) ; goto END ; } Status = XLoader_MakeSdFileName ( MultiBootOffset ) ; if ( Status != XST_SUCCESS ) { goto END ; } Rc = f_open ( & FFil , BootFile , ( BYTE ) FA_READ ) ; if ( Rc != FR_OK ) { XLoader_Printf ( DEBUG_INFO , " \n " , BootFile , Rc ) ; Status = XPlmi_UpdateStatus ( XLOADER_ERR_SD_F_OPEN , ( int ) Rc ) ; XLoader_Printf ( DEBUG_GENERAL , " \n \r " ) ; XPlmi_Out32 ( SdCdnReg , SdCdnVal ) ; ( void ) f_unmount ( BootFile ) ; goto END ; } Status = XST_SUCCESS ; END : return Status ; }
@brief       This function is used to initialize the sd controller and driver.
@brief This function is used to initialize the sd controller and driver .
/*
	 * Filesystem boot modes require the filename extension as well as
	 * the logical drive in which the secondary pdi file is present.
	 * To meet these requirements and to reuse the same code for primary
	 * and secondary boot modes, bits 0 to 3 in DeviceFlags denote the
	 * PdiSrc. Bits 4 to 19 denote the file name extension.
	 * Example if the offset is 4, file name should be BOOT0004.BIN, these
	 * bits are analogous to multiboot offset in case of primary boot mode.
	 * Bits 20, 21, 22 and 23 denote the logical drive number of the
	 * secondary device. Please note that bits 20, 21, 22 and 23 in device
	 * flags actually map to bits 16, 17, 18 and 19 in secondary device
	 * address specified in bif file.
	 */ /* Set logical drive number */ /* Register volume work area, initialize device */ /*
	 * Create boot image name
	 */