コード例 #1
0
ファイル: SDcard.c プロジェクト: vnamiran/UCIsd_NMV
AtaError mmcConfigFs(char *fileName) {
    Uint16 index;
    AtaError ata_error;
    unsigned int diskType;

    ata_error = ATA_ERROR_NONE;

    for (index = 0; index < CSL_MMCSD_ATA_BUF_SIZE; index++) {
        gMmcWriteBuf[index] = 0x4142;
        AtaWrBuf[index] = 0x4344;
        gMmcReadBuf[index] = 0x0;
    }

    /* Call init function initialize ATA state structure */
    gpstrAtaDrive->AtaInitAtaMediaState = (AtaError (*)(void *)) MMC_initState;
    gpstrAtaMMCState->hMmcSd = mmcsdHandle;
    gpstrAtaDrive->pAtaMediaState = gpstrAtaMMCState;
    gpstrAtaDrive->AtaInitAtaMediaState(gpstrAtaDrive);

    /* Set the temp write buffer */
    gpstrAtaDrive->_AtaWriteBuffer = AtaWrBuf;

    diskType = CSL_MMCSD_ATAFS_DISKTYPE;
    /* Call ATA_systemInit() to intialize some values whcih are
     used by chk_mmc() function */
    ata_error = ATA_systemInit(gpstrAtaDrive, diskType);

    chk_mmc(gpstrAtaDrive, &diskType);
    if (diskType != CSL_MMCSD_ATAFS_DISKTYPE) {
        ata_error = ATA_systemInit(gpstrAtaDrive, diskType);
        if (ata_error != ATA_ERROR_NONE) {
            printf("ATA_systemInit Failed\n");
            printf("Format the SD card\n");
            return (ata_error);
        }
    }

    printf("\nATA File System Initialization successful\n");

    /* Find the first file available */
    ata_error = ATA_fileInit(gpstrAtaDrive, pAtaFile);
    if (ata_error) {
        printf("ATA_fileInit error (0x%x)\n", ata_error);
        return (ata_error);
    }

    ata_error = ATA_setLongDirectoryName(pAtaFile, fileName);
    if (ata_error != ATA_ERROR_NONE) {
        printf("ATA_setLongDirectoryName Failed\n");
        return (ata_error);
    }

    ata_error = ATA_createDirectoryLong(pAtaFile, fileName);
    if (ata_error != ATA_ERROR_NONE) {
        printf("ATA_createDirectoryLong Failed\n");
        return (ata_error);
    }

    ata_error = ATA_cdRoot(pAtaFile);
    if (ata_error != ATA_ERROR_NONE) {
        printf("ATA_cdRoot Failed\n");
        return (ata_error);
    }

    do {
        ata_error = ATA_findNext(pAtaFile);
        if (ata_error != ATA_ERROR_NONE) {
            printf("Cannot find created directory\n");
            return (ata_error);
        }

        ata_error = ATA_getLongName(pAtaFile, ataFileName, 0, 10);
        if (ata_error != ATA_ERROR_NONE) {
            printf("ATA_getLongName failed\n");
            return (ata_error);
        }
    } while (strcmp(ataFileName, fileName) != 0);

    ata_error = ATA_cd(pAtaFile);
    if (ata_error != ATA_ERROR_NONE) {
        printf("ATA_cdRoot Failed\n");
        return (ata_error);
    }

    /* Set the file name */
    ATA_setFileName(pAtaFile, fileName, "txt");

    ata_error = ATA_create(pAtaFile);
    if (ata_error != ATA_ERROR_NONE) {
        printf("ATA_create Failed\n");
        return (ata_error);
    } else {
        printf("\nFile Creation on SD card is Successful\n");
    }

    /* Write data to the file */
    ata_error = ATA_write(pAtaFile, gMmcWriteBuf, CSL_MMCSD_ATA_BUF_SIZE);
    if (ata_error != ATA_ERROR_NONE) {
        printf("ATA_write Failed\n");
        return (ata_error);
    } else {
        printf("\nWriting Data to the file on SD card successful\n");
    }

    /* Reset the file pointer to the beginning */
    ATA_seek(pAtaFile, 0);

    /* Read the data from the file in little endian mode */
    ata_error = ATA_readLittleEndian(pAtaFile, gMmcReadBuf,
                                     CSL_MMCSD_ATA_BUF_SIZE);
    if (ata_error != ATA_ERROR_NONE) {
        printf("ATA_readLittleEndian Failed\n");
        return (ata_error);
    } else {
        printf("\nReading Data from the file on SD card successful\n");
    }

    /* Close the file */
    ata_error = ATA_close(pAtaFile);
    if (ata_error != ATA_ERROR_NONE) {
        printf("ATA_close Failed\n");
        return (ata_error);
    }

    /* Compare the data read and data written */
    for (index = 0; index < CSL_MMCSD_ATA_BUF_SIZE; index++) {
        if (gMmcWriteBuf[index] != gMmcReadBuf[index]) {
            ata_error = 1;
            printf("\nMMCSD Read and Write Buffers do not Match\n");
            break;
        }
    }

    if (ata_error == 0) {
        printf("\nMMCSD Read and Write Buffers Match\n");
    }

    return (ata_error);
}
コード例 #2
0
ファイル: ataformat.c プロジェクト: psucurran/NoiseNinjas
AtaError ATA_format(AtaState *pDrive, int flag)
{
	AtaSector testsector, testsector1;
	int x; AtaError ret_stat = ATA_ERROR_NONE;
    AtaUint16 *_AtaWriteBuffer = pDrive->_AtaWriteBuffer;
    AtaUint16 _AtaWriteBuffer1[256];
/*SUSMIT - FAT32 format fix*/
  AtaSector PhySector;
  AtaUint16 fOffset, fAlign;
  AtaCluster write_cluster, RootCluster;
  int LastCluster = 0;
  unsigned short FatNo;
/*SUSMIT - FAT32 format fix*/

    if (pDrive==NULL) return(ATA_ERROR_INVALID_PARAM); //KR042810

	for(x=0; x< 256; x++) _AtaWriteBuffer1[x] = 0;
/*Removed this on order to reduce memory, we won't remember boot records
so, our format won't be able to fix disks with Bad MBR/BR but will
be able to format*/
#if 0
	switch(pDrive->FatType) {
      case ATA_FAT_TYPE_12:
        /*write MBR*/
        ret_stat = _AtaReadSector((AtaSector)0, pDrive, &_AtaWriteBuffer[0], 0);
        if(ret_stat) return(ret_stat);
        ret_stat = pDrive->AtaWriteSector((AtaSector)0, pDrive->pAtaMediaState, &MBR_8MB_FAT12_MMC[0], 0);
        if(ret_stat) return(ret_stat);
        /*Write BR*/
        ret_stat = _AtaReadSector((AtaSector)pDrive->BootPhySector, pDrive, &_AtaWriteBuffer[0], 0);
        if(ret_stat) return(ret_stat);
        ret_stat = pDrive->AtaWriteSector((AtaSector)pDrive->BootPhySector, pDrive->pAtaMediaState, &BR_8MB_FAT12_MMC[0], 0);
        if(ret_stat) return(ret_stat);
        break;
      case ATA_FAT_TYPE_16:
        /*Write MBR*/
        ret_stat = _AtaReadSector((AtaSector)0, pDrive, &_AtaWriteBuffer[0], 0);
        if(ret_stat) return(ret_stat);
        ret_stat = pDrive->AtaWriteSector((AtaSector)0, pDrive->pAtaMediaState, &MBR_32MB_FAT16_MMC[0], 0);
        if(ret_stat) return(ret_stat);
        /*Write BR*/
        ret_stat = _AtaReadSector((AtaSector)pDrive->BootPhySector, pDrive, &_AtaWriteBuffer[0], 0);
        if(ret_stat) return(ret_stat);
        ret_stat = pDrive->AtaWriteSector((AtaSector)pDrive->BootPhySector, pDrive->pAtaMediaState, &BR_32MB_FAT16_MMC[0], 0);
        if(ret_stat) return(ret_stat);
        break;
      case ATA_FAT_TYPE_32:
        /*Write MBR*/
        ret_stat = _AtaReadSector((AtaSector)0, pDrive, &_AtaWriteBuffer[0], 0);
        if(ret_stat) return(ret_stat);
        ret_stat = pDrive->AtaWriteSector((AtaSector)0, pDrive->pAtaMediaState, &MBR_32MB_FAT16_MMC[0], 0);
        if(ret_stat) return(ret_stat);
        /*Write BR*/
        ret_stat = _AtaReadSector((AtaSector)pDrive->BootPhySector, pDrive, &_AtaWriteBuffer[0], 0);
        if(ret_stat) return(ret_stat);
        ret_stat = pDrive->AtaWriteSector((AtaSector)pDrive->BootPhySector, pDrive->pAtaMediaState, &BR_32MB_FAT16_MMC[0], 0);
        if(ret_stat) return(ret_stat);
        break;
      default: return(ATA_ERROR_BAD_MEDIA);
    }
#endif
	/*Clear FAT*/
	if(pDrive->FatType == ATA_FAT_TYPE_32) {
/*SUSMIT - FAT32 format fix*/
    RootCluster = _AtaReadDoubleWord(pDrive->BootPhySector, pDrive, 44);			/* Fat Size/Sector Count  byte 36 - 39 */
	ret_stat = _AtaReadSector((AtaSector)pDrive->RootDirSector, pDrive, &_AtaWriteBuffer[0], 0);
	if(ret_stat) return(ret_stat);
	_AtaWriteBuffer1[0] = 0xFFF8;
	_AtaWriteBuffer1[1] = 0xFFFF;
	_AtaWriteBuffer1[2] = 0xFFF8;
	_AtaWriteBuffer1[3] = 0xFFFF;
	_AtaWriteBuffer1[4] = 0xFFF8;
	_AtaWriteBuffer1[5] = 0xFFFF;
	ret_stat = pDrive->AtaWriteSector((AtaSector)pDrive->RootDirSector, pDrive->pAtaMediaState, &_AtaWriteBuffer1[0], 0);
	if(ret_stat) return(ret_stat);
    ret_stat = pDrive->AtaWriteSectorFlush(pDrive->pAtaMediaState);
    if(ret_stat) return(ret_stat);
	for(FatNo=0; FatNo<pDrive->NumberOfFats; FatNo++)
	{
      PhySector = _AtaCalculatePhySectorAndOffsetOfFatEntry(RootCluster, pDrive, &fOffset, &fAlign);
		PhySector +=
		(
		  (unsigned long)FatNo *
		  pDrive->LogSectorsPerFat *
		  (unsigned long)(pDrive->WordsPerLogSector/ATA_WORDS_PER_PHY_SECTOR)
		);
	  ret_stat = _AtaReadFatEntryAtPhySector(PhySector, pDrive, fOffset, fAlign, &LastCluster, &write_cluster);
	  if(ret_stat) return(ret_stat);
      ret_stat = _AtaWriteFatEntryAtPhySector(PhySector, pDrive, 0x0FFFFFFF, fOffset, fAlign, 1,1);
	  if(ret_stat) return(ret_stat);
	}
/*SUSMIT - FAT32 format fix*/
	}
	else {
	testsector=pDrive->FirstFatSector;
	ret_stat = _AtaReadSector((AtaSector)testsector, pDrive, &_AtaWriteBuffer[0], 0);
	if(ret_stat) return(ret_stat);
	_AtaWriteBuffer1[0] = 0xFFF8;
	_AtaWriteBuffer1[1] = 0xFFFF;
	ret_stat = pDrive->AtaWriteSector((AtaSector)testsector++, pDrive->pAtaMediaState, &_AtaWriteBuffer1[0], 0);
	if(ret_stat) return(ret_stat);
    ret_stat = pDrive->AtaWriteSectorFlush(pDrive->pAtaMediaState);
    if(ret_stat) return(ret_stat);
	_AtaWriteBuffer1[0] = 0;
	_AtaWriteBuffer1[1] = 0;
	testsector1 = pDrive->FirstFatSector+pDrive->LogSectorsPerFat;
	while(testsector<testsector1) {
      ret_stat = _AtaReadSector((AtaSector)testsector, pDrive, &_AtaWriteBuffer[0], 0);
      if(ret_stat) return(ret_stat);
      ret_stat = pDrive->AtaWriteSector((AtaSector)testsector++, pDrive->pAtaMediaState, &_AtaWriteBuffer1[0], 0);
      if(ret_stat) return(ret_stat);
      ret_stat = pDrive->AtaWriteSectorFlush(pDrive->pAtaMediaState);
      if(ret_stat) return(ret_stat);
    }
    ret_stat = _AtaReadSector((AtaSector)testsector, pDrive, &_AtaWriteBuffer[0], 0);
	if(ret_stat) return(ret_stat);
	_AtaWriteBuffer1[0] = 0xFFF8;
	_AtaWriteBuffer1[1] = 0xFFFF;
	ret_stat = pDrive->AtaWriteSector((AtaSector)testsector++, pDrive->pAtaMediaState, &_AtaWriteBuffer1[0], 0);
	if(ret_stat) return(ret_stat);
    ret_stat = pDrive->AtaWriteSectorFlush(pDrive->pAtaMediaState);
    if(ret_stat) return(ret_stat);
	_AtaWriteBuffer1[0] = 0;
	_AtaWriteBuffer1[1] = 0;
	testsector1 = pDrive->FirstDataSector;
	while(testsector<testsector1) {
      ret_stat = _AtaReadSector((AtaSector)testsector, pDrive, &_AtaWriteBuffer[0], 0);
      if(ret_stat) return(ret_stat);
      ret_stat = pDrive->AtaWriteSector((AtaSector)testsector++, pDrive->pAtaMediaState, &_AtaWriteBuffer1[0], 0);
      if(ret_stat) return(ret_stat);
      ret_stat = pDrive->AtaWriteSectorFlush(pDrive->pAtaMediaState);
      if(ret_stat) return(ret_stat);
    } }
	/*only for full format*/
	if (flag) {
	testsector = pDrive->FirstDataSector;
	while(testsector<=pDrive->TotalSectors) {
      ret_stat = _AtaReadSector((AtaSector)testsector, pDrive, &_AtaWriteBuffer[0], 0);
      if(ret_stat) return(ret_stat);
      ret_stat = pDrive->AtaWriteSector((AtaSector)testsector++, pDrive->pAtaMediaState, &_AtaWriteBuffer1[0], 0);
      if(ret_stat) return(ret_stat);
      ret_stat = pDrive->AtaWriteSectorFlush(pDrive->pAtaMediaState);
      if(ret_stat) return(ret_stat);
    } }
/* SUSMIT - 25 FEB 2003 - NMP*/
/*	ret_stat = ATA_systemInit(pDrive);*/
	ret_stat = ATA_systemInit(pDrive,0);
/* SUSMIT - 25 FEB 2003 - NMP*/
	return(ret_stat);
}
コード例 #3
0
/**
 *  \brief  Function to configure file system and perform
 *          read/write operations
 *
 *  \param  fileName - Name of the file to be created
 *
 *  \return Test result
 */
AtaError mmcConfigFs(char    *fileName)
{
    Uint16        index;
    AtaError      ata_error;
	unsigned int diskType;

    ata_error = ATA_ERROR_NONE;

    for(index = 0; index < CSL_MMCSD_ATA_BUF_SIZE; index++)
    {
        gMmcWriteBuf[index] = 0x4142;
		AtaWrBuf[index] = 0x4344;
        gMmcReadBuf[index]  = 0x0;
    }

	/* Call init function initialize ATA state structure */
    gpstrAtaDrive->AtaInitAtaMediaState = (AtaError (*)(void *))MMC_initState;
	gpstrAtaMMCState->hMmcSd = mmcsdHandle;
    gpstrAtaDrive->pAtaMediaState = gpstrAtaMMCState;
    gpstrAtaDrive->AtaInitAtaMediaState(gpstrAtaDrive);

	/* For partitioned disk, 'diskType' should be 0
	   and for unpartiotioned disk, it should be 1
	 */
	 /* chk_mmc() function is used to check the partition type of
	    SD card.
	    ATA_systemInit() function needs to be called
	    with 'diskType' set to 0 before calling chk_mmc().
	    chk_mmc() function will check whether the disk is partitioned
	    or unpartitioned. If disk is not partitioned it will change the
	    'diskType' value to 1 otherwise it will not change the diskType value.
		After calling chk_mmc() if 'diskType' is not '0' , It means that
		the SD card is not partitioned and ATA_systemInit() needs to be
		called with 'diskType' value modified by chk_mmc() function */

	diskType = CSL_MMCSD_ATAFS_DISKTYPE;
	/* Call ATA_systemInit() to intialize some values whcih are
	  used by chk_mmc() function */
	ata_error = ATA_systemInit(gpstrAtaDrive, diskType);

	chk_mmc(gpstrAtaDrive, &diskType);
	if(diskType != CSL_MMCSD_ATAFS_DISKTYPE)
	{
		ata_error = ATA_systemInit(gpstrAtaDrive, diskType);
	    if(ata_error != ATA_ERROR_NONE)
	    {
			printf("ATA_systemInit Failed\n");
			printf("Format the SD card\n");
	        return(ata_error);
	    }
	}

	printf("\nATA File System Initialization successful\n");

	/* Find the first file available */
    ata_error =  ATA_fileInit(gpstrAtaDrive, pAtaFile);
    if(ata_error) {
        printf("ATA_fileInit error (0x%x)\n", ata_error);
        return(ata_error);
    }

	/* Set the temp write buffer */
    pAtaFile->pDrive->_AtaWriteBuffer = AtaWrBuf;

	/* Set the file name */
	ATA_setFileName(pAtaFile, fileName, "txt");

    ata_error = ATA_create(pAtaFile);
    if(ata_error != ATA_ERROR_NONE)
    {
    	printf("ATA_create Failed\n");
        return(ata_error);
    }
    else
    {
		printf("\nFile Creation on SD card is Successful\n");
	}

	/* Write data to the file */
    ata_error = ATA_write(pAtaFile, gMmcWriteBuf, CSL_MMCSD_ATA_BUF_SIZE);
    if(ata_error != ATA_ERROR_NONE)
    {
		printf("ATA_write Failed\n");
        return(ata_error);
    }
    else
    {
		printf("\nWriting Data to the file on SD card successful\n");
	}

	/* Reset the file pointer to the beginning */
	ATA_seek (pAtaFile, 0);

	/* Read the data from the file in little endian mode */
    ata_error = ATA_readLittleEndian(pAtaFile,gMmcReadBuf, CSL_MMCSD_ATA_BUF_SIZE);
    if(ata_error != ATA_ERROR_NONE)
    {
		printf("ATA_readLittleEndian Failed\n");
        return(ata_error);
    }
    else
    {
		printf("\nReading Data from the file on SD card successful\n");
	}

	/* Close the file */
    ata_error = ATA_close(pAtaFile);
    if(ata_error != ATA_ERROR_NONE)
    {
		printf("ATA_close Failed\n");
        return(ata_error);
    }

	/* Compare the data read and data written */
    for(index = 0; index < CSL_MMCSD_ATA_BUF_SIZE; index++)
    {
        if(gMmcWriteBuf[index] != gMmcReadBuf[index])
        {
            ata_error = 1;
            printf("\nMMCSD Read and Write Buffers do not Match\n");
            break;
        }
    }

    if(ata_error == 0)
    {
        printf("\nMMCSD Read and Write Buffers Match\n");
    }

    return(ata_error);
}