예제 #1
0
esint8 if_readBuf(hwInterface* file,euint32 address,euint8* buf)
{
	if (SD_ReadSector (address, buf, 1) == SD_TRUE)
		return 0;
	else
		return (-1);
}
예제 #2
0
int GetNextFileSector(FileInfo *finfo, uint8_t *buffer, uint32_t *pCount)
{
    // check to see if we're at the end of the file
    if (finfo->bytesRemaining == 0)
        return -1;
    
    // check to see if we're at the end of the cluster
    if (finfo->sectorsRemainingInCluster == 0) {
        VolumeInfo *vinfo = finfo->vinfo;
        uint32_t next;
        
        // check to see if this is the last cluster
        if (finfo->cluster == 0)
            return -1;
            
        // get the next cluster number
        if (GetFATEntry(vinfo, buffer, finfo->cluster, &next) != 0) {
            DPRINTF("GetFATEntry %d failed\n", finfo->cluster);
            return -1;
        }
            
        // check for the end of the cluster chain
        if (next >= vinfo->endOfClusterChain) {
            finfo->cluster = 0;
            return -1;
        }
            
        // setup the next cluster
        finfo->cluster = next;
        finfo->sector = vinfo->firstDataSector + (next - 2) * vinfo->sectorsPerCluster;
        finfo->sectorsRemainingInCluster = vinfo->sectorsPerCluster;
    }
        
    // get the next sector of the file
    if (SD_ReadSector(finfo->sector, buffer) != 0) {
        DPRINTF("SD_ReadSector %d failed\n", finfo->sector);
        return 1;
    }
    
    // move to the next sector in the cluster
    ++finfo->sector;
    --finfo->sectorsRemainingInCluster;
    
    // return the current count and update the number of bytes remaining
    if ((*pCount = finfo->bytesRemaining) > SECTOR_SIZE)
        *pCount = SECTOR_SIZE;
    finfo->bytesRemaining -= *pCount;
    
    return 0;
}
예제 #3
0
int GetFATEntry(VolumeInfo *vinfo, uint8_t *buffer, uint32_t cluster, uint32_t *pEntry)
{
    uint32_t sector, offset;
    
    // determine that offset based on the fat type
    switch (vinfo->type) {
    case TYPE_FAT12:
        return -1; // BUG: fix this
        break;
    case TYPE_FAT16:
        offset = cluster * sizeof(uint16_t);
        break;
    case TYPE_FAT32:
        offset = cluster * sizeof(uint32_t);
        break;
    default:
        return -1;
    }
    
    // determine the sector number and offset
    sector = vinfo->firstFATSector + (offset / vinfo->bytesPerSector);
    offset %= vinfo->bytesPerSector;
    
    // get the sector containing the fat entry
    if (SD_ReadSector(sector, buffer) != 0) {
        DPRINTF("SD_ReadSector %d failed\n", sector);
        return -1;
    }
    
    // get the fat entry
    switch (vinfo->type) {
    case TYPE_FAT12:
        // BUG: fix this!
        break;
    case TYPE_FAT16:
        *pEntry = *(uint16_t *)(buffer + offset);
        break;
    case TYPE_FAT32:
        *pEntry = *(uint32_t *)(buffer + offset) & 0x0fffffff;
        break;
    }
    
    return 0;
}
예제 #4
0
error SD_Leer(byte *direccion, dato lectura[][tam_dato]){
    UINT32 u32SD_Block; // Convertimos la direccion en una variable de 32 bits
	
	SPI_Init();
	u32SD_Block = direccion[0];
	u32SD_Block <<= 8;
	u32SD_Block |= direccion[1];
	u32SD_Block <<= 8;
	u32SD_Block |= direccion[2];
	u32SD_Block <<= 8;
	u32SD_Block |= direccion[3];
    
    (void) SD_CalculaDireccion(dir_lectura, lectura);
    (void) SD_ReadSector(u32SD_Block,(UINT8 *) lectura);
    (void)Cpu_Delay100US(100); 
    //ban_bufferTx=0;
        
    return _ERR_OK;

}
예제 #5
0
error SD_LeerDireccion(){
	UINT32 u32SD_Arch;
    int h=0, i=0;
    (void)SD_Assert();
    
    //u32SD_Block=DIRECCION_BIN; //Cargar la direccion del sector fisico donde se encuentran las direcciones de lectura y escritura 
    //(UINT32) u16FAT_Data_BASE
    (void)SD_ReadSector((UINT32) u16FAT_Data_BASE, (UINT8 *) Buffer_Envio); //Lee el sector que contiene las direcciones de lectura y escritura
    
    for(i=0;i<4;i++)
    	dir_lectura[i] = Buffer_Envio[0][i]; 
    												//Se guardan las direcciones en las variables
    for( ;i<8;i++)
    	dir_escritura[i-4] = Buffer_Envio[0][i];
    
    for( ;i<12;i++)
    	dir_base_lat[i-8] = Buffer_Envio[0][i];
    
    for( ;i<16;i++)
        dir_base_lon[i-12] = Buffer_Envio[0][i];
    
    id = Buffer_Envio[0][i];
    
    if(dir_lectura[0]==0&&dir_lectura[1]==0&&dir_lectura[2]==0&&dir_lectura[3]==0&&
    		dir_escritura[0]==0&&dir_escritura[1]==0&&dir_escritura[2]==0&&dir_escritura[3]==0){
    	u32SD_Arch=(UINT32) u16FAT_Data_BASE + 1; //Establece el segundo sector del archivo como el inicio de lectura y escritura
    	dir_lectura[0]=(byte)(u32SD_Arch>>24);
		dir_lectura[1]=(byte)(u32SD_Arch>>16);
		dir_lectura[2]=(byte)(u32SD_Arch>>8);
		dir_lectura[3]=(byte)(u32SD_Arch);
		dir_escritura[0]=(byte)(u32SD_Arch>>24);
		dir_escritura[1]=(byte)(u32SD_Arch>>16);
		dir_escritura[2]=(byte)(u32SD_Arch>>8);
		dir_escritura[3]=(byte)(u32SD_Arch);
		SD_Assert();
		for(i=0;i<4;i++)
			Buffer_Envio[0][i] = dir_lectura[i];
		for(;i<8;i++)
			Buffer_Envio[0][i] = dir_escritura[i-4];
		(void) SD_WriteSector((UINT32) u16FAT_Data_BASE, (UINT8 *) Buffer_Envio); //2600 sector del archivo binario, hacer vble global
    }
예제 #6
0
int MountFS(uint8_t *buffer, VolumeInfo *vinfo)
{
    uint8_t  sectorsPerCluster, numberOfFATs;
    uint16_t bytesPerSector, reservedSectorCount, rootEntryCount;
    uint32_t start, firstFATSector, firstRootDirectorySector, rootDirectorySectorCount;
    uint32_t firstDataSector, dataSectorCount, clusterCount, totalSectorCount, FATSize;
    uint32_t endOfClusterChain;
#ifdef FSDEBUG
    char *volumeLabel;
#endif
    int type;
       
    if (SD_ReadSector(0, buffer) != 0) {
        DPRINTF("SD_ReadSector 0 failed\n");
        return -1;
    }
    
    // check to see if there is a master boot record
    if (strncmp((char *)&buffer[BOOT_FILE_SYSTEM_TYPE], "FAT12", 5) != 0
    &&  strncmp((char *)&buffer[BOOT_FILE_SYSTEM_TYPE], "FAT16", 5) != 0
    &&  strncmp((char *)&buffer[BOOT_FILE_SYSTEM_TYPE_32], "FAT32", 5) != 0) {

        // get the first partition information
#ifdef FSDEBUG
        uint8_t status = buffer[MBR_PARTITIONS + PART_STATUS];
        uint32_t size = GetU32(buffer, MBR_PARTITIONS + PART_SECTOR_COUNT);
#endif
        start = GetU32(buffer, MBR_PARTITIONS + PART_FIRST_SECTOR);
#ifdef FSDEBUG
        DPRINTF("status: %02x, start: %08x, size %08x\n", status, start, size);
#endif            
        // get the boot sector of the first partition
        if (SD_ReadSector(start, buffer) != 0) {
            DPRINTF("SD_ReadSector %d failed\n", start);
            return -1;
        }
    }
    
    // no master boot record
    else
        start = 0;
    
    bytesPerSector = GetU16(buffer, BOOT_BYTES_PER_SECTOR);
    sectorsPerCluster = buffer[BOOT_SECTORS_PER_CLUSTER];
    reservedSectorCount = GetU16(buffer, BOOT_RESERVED_SECTOR_COUNT);
    numberOfFATs = buffer[BOOT_NUMBER_OF_FATS];
    rootEntryCount = GetU16(buffer, BOOT_ROOT_ENTRY_COUNT);
    if ((totalSectorCount = GetU16(buffer, BOOT_TOTAL_SECTOR_COUNT)) == 0)
        totalSectorCount = GetU32(buffer, BOOT_TOTAL_SECTOR_COUNT_32);
    if ((FATSize = GetU16(buffer, BOOT_FAT_SIZE)) == 0)
        FATSize = GetU32(buffer, BOOT_FAT_SIZE_32);
    firstFATSector = start + reservedSectorCount;
    firstRootDirectorySector = firstFATSector + numberOfFATs * FATSize; // recomputed for FAT32 later
    rootDirectorySectorCount = (rootEntryCount * ENTRY_SIZE + bytesPerSector - 1) / bytesPerSector;
    firstDataSector = firstRootDirectorySector + rootDirectorySectorCount;
    dataSectorCount = totalSectorCount - reservedSectorCount - numberOfFATs * FATSize - rootDirectorySectorCount;
    clusterCount = dataSectorCount / sectorsPerCluster;
    
    // these magic numbers come from a Microsoft paper on the FAT32 File System
    if (clusterCount < 4085) {
        type = TYPE_FAT12;
        endOfClusterChain = 0x00000ff8;
    }
    else if (clusterCount < 65525) {
        type = TYPE_FAT16;
        endOfClusterChain = 0x0000fff8;
    }
    else {
        type = TYPE_FAT32;
        endOfClusterChain = 0xfffffff8;
    }
        
    switch (type) {
    case TYPE_FAT12:
    case TYPE_FAT16:
#ifdef FSDEBUG
        volumeLabel = (char *)&buffer[BOOT_VOLUME_LABEL];
#endif
        vinfo->rootDirectoryCluster = 0;
        break;
    case TYPE_FAT32:
#ifdef FSDEBUG
        volumeLabel = (char *)&buffer[BOOT_VOLUME_LABEL_32];
#endif
        vinfo->rootDirectoryCluster = GetU32(buffer, BOOT_ROOT_CLUSTER_32);
        firstRootDirectorySector = firstDataSector + (vinfo->rootDirectoryCluster - 2) * sectorsPerCluster;
        rootDirectorySectorCount = sectorsPerCluster;
        break;
    }
    
    vinfo->type = type;
    vinfo->bytesPerSector = bytesPerSector;
    vinfo->sectorsPerCluster = sectorsPerCluster;
    vinfo->firstRootDirectorySector = firstRootDirectorySector;
    vinfo->rootDirectorySectorCount = rootDirectorySectorCount;
    vinfo->rootEntryCount = rootEntryCount;
    vinfo->firstFATSector = firstFATSector;
    vinfo->firstDataSector = firstDataSector;
    vinfo->clusterCount = clusterCount;
    vinfo->endOfClusterChain = endOfClusterChain;
    
#ifdef FSDEBUG
    DPRINTF("label:                    %-11.11s\n", volumeLabel);
    DPRINTF("type:                     ");
    switch (type) {
    case TYPE_FAT12:
        DPRINTF("FAT12\n");
        break;
    case TYPE_FAT16:
        DPRINTF("FAT16\n");
        break;
    case TYPE_FAT32:
        DPRINTF("FAT32\n");
        break;
    default:
        DPRINTF("UNKNOWN\n");
        break;
    }
    DPRINTF("bytesPerSector:           %d\n", bytesPerSector);
    DPRINTF("sectorsPerCluster:        %d\n", sectorsPerCluster);
    DPRINTF("reservedSectorCount:      %d\n", reservedSectorCount);
    DPRINTF("numberOfFATs:             %d\n", numberOfFATs);
    DPRINTF("rootEntryCount:           %d\n", rootEntryCount);
    DPRINTF("totalSectorCount:         %d\n", totalSectorCount);
    DPRINTF("FATSize:                  %d\n", FATSize);
    DPRINTF("firstRootDirectorySector: %d\n", firstRootDirectorySector);
    DPRINTF("rootDirectorySectorCount: %d\n", rootDirectorySectorCount);
    DPRINTF("firstFATSector:           %d\n", firstFATSector);
    DPRINTF("firstDataSector:          %d\n", firstDataSector);
    DPRINTF("dataSectorCount:          %d\n", dataSectorCount);
    DPRINTF("clusterCount:             %d\n", clusterCount);
#endif
    
    return 0;
}
예제 #7
0
int FindFile(uint8_t *buffer, VolumeInfo *vinfo, const char *name, FileInfo *finfo)
{
    uint32_t cluster, sector, count;
    int i, j;
    
    // start with the first root directory sector
    cluster = vinfo->rootDirectoryCluster;
    sector = vinfo->firstRootDirectorySector;
    count = vinfo->rootDirectorySectorCount;
    
    // loop through all directory clusters
    for (;;) {
    
        // loop through each sector of the current cluster
        for (j = 0; j < count; ++j) {
        
            // get the next sector in this cluster
            if (SD_ReadSector(sector + j, buffer) != 0) {
                DPRINTF("SD_ReadSector %d failed\n", sector + j);
                return -1;
            }

            // find a file in this directory sector
            for (i = 0; i < SECTOR_SIZE; i += ENTRY_SIZE) {
                uint8_t flag = buffer[i + ENTRY_NAME];
                uint32_t firstCluster;
                uint8_t attr;
                switch (flag) {
                case 0xe5:
                case 0x00:
                    break;
                default:
                    attr = buffer[i + ENTRY_ATTRIBUTES];
                    firstCluster = (GetU16(buffer, i + ENTRY_CLUSTER_HIGH) << 16) | GetU16(buffer, i + ENTRY_CLUSTER_LOW);
                    if ((attr & ATTR_LONG_NAME_MASK) != ATTR_LONG_NAME
                    && !(attr & ATTR_HIDDEN)
                    && !(attr & ATTR_VOLUME_ID)) {
                        if (strncmp(name, (char *)&buffer[i + ENTRY_NAME], NAME_SIZE) == 0) {
                            finfo->vinfo = vinfo;
                            finfo->cluster = firstCluster;
                            finfo->sector = vinfo->firstDataSector + (firstCluster - 2) * vinfo->sectorsPerCluster;
                            finfo->sectorsRemainingInCluster = vinfo->sectorsPerCluster;
                            finfo->bytesRemaining = GetU32(buffer, i + ENTRY_FILESIZE);
                            return 0;
                        }
                    }
                    break;
                }
                
                // check for the end of the directory
                if (flag == 0x00)
                    return -1;
            }
        }
        
        // move ahead to the next cluster
        if (!cluster)
            break;
            
        // get the next cluster number
        if (GetFATEntry(vinfo, buffer, cluster, &cluster) != 0)
            return -1;
            
        // check for the end of the cluster chain
        if (cluster < 2)
            break;
            
        // setup to process the next cluster
        sector = vinfo->firstDataSector + (cluster - 2) * vinfo->sectorsPerCluster;
        count = vinfo->sectorsPerCluster;
    }
    
    return -1;
}
예제 #8
0
int ListExecutables(uint8_t *buffer, VolumeInfo *vinfo) {
	uint32_t cluster, sector, count;
	char ext[3];
	fileCount=0;
	int i, j;
	cluster = vinfo->rootDirectoryCluster;
	sector = vinfo->firstRootDirectorySector;
	count = vinfo->rootDirectorySectorCount;

	for (;;) {

		for (j = 0; j < count; ++j) {
			if(SD_ReadSector(sector + j, buffer) != 0) {
				DPRINTF("SD_ReadSector %d failed\n", sector + j);
				return -1;
			}

			for (i = 0; i < SECTOR_SIZE; i += ENTRY_SIZE) {
				uint8_t flag = buffer[i + ENTRY_NAME];
				uint8_t attr;
				switch(flag) {
					case 0xe5:
					case 0x00:
						break;
					default:
						attr = buffer[i + ENTRY_ATTRIBUTES];
						if ((attr & ATTR_LONG_NAME_MASK) != ATTR_LONG_NAME
						&& !(attr & ATTR_HIDDEN)
						&& !(attr & ATTR_VOLUME_ID)) {
							if (fileCount >= MAX_ENTRIES) return 0;
							
							strncpy(ext, (char *)&buffer[i+ENTRY_NAME+8],3);
							DPRINTF("%s\n",ext);
							
							if(strcmp(ext,EXTENSION)!=0)
								continue;
							strncpy(menuList[fileCount], (char *)&buffer[i+ENTRY_NAME], NAME_SIZE);
							menuList[fileCount][11] = 0;
							fileCount++;		
						}
						break;
				}
				if(flag == 0x00) {
					if(fileCount>0) {
						return 0;
					} else {
						return -1;
					}
				}
			}
		}

		if (!cluster)
			break;

		if (GetFATEntry(vinfo, buffer, cluster, &cluster) !=0)
			return -1;

		if (cluster < 2)
			break;

		sector = vinfo->firstDataSector + (cluster -2) * vinfo->sectorsPerCluster;
		count = vinfo->sectorsPerCluster;
	}

	return -1;
}