/*********************************************************************//** + * @brief Read DIR contents. + * @return Return code. + * NOT_FOUND + * MATCH_FOUND + **********************************************************************/ int32_t DIR_Open (uint8_t *ent_name_given) { int32_t rc; uint32_t sec_num, first_sec_num,end_sec_num ; FILE_ENTRY* entry = &FAT_CurDirEntry.dir_info; uint8_t ent_type; uint8_t ent_name_read[80]; uint8_t len = 0; uint8_t sum = 0; volatile uint8_t *buf, *afile; DIR_ENTRY* ptr = FAT_CurDirEntry.entries; // Find the entry for the given folder if(entry->FileStatus == 0 ) { if(FAT_StrCaseCmp(ent_name_given,".") == 0) { if(FAT_BootSec.FATType == FAT_32) { entry->CurrClus= FAT_BootSec.RootClus; } else { entry->CurrClus = FAT_BootSec.RootDirStartSec/FAT_BootSec.SecPerClus; } entry->CurrClusOffset = 0; entry->FileStatus = 1; } else { rc = FAT_FindEntry(ent_name_given, entry); if (rc == MATCH_FOUND) { entry->FileStatus = 1; } else { return NOT_FOUND; } } } else { uint16_t i = 0; // Find in the current directory for(i = 0; i < FAT_CurDirEntry.entry_num; i++) { if(FAT_StrCaseCmp(ent_name_given,FAT_CurDirEntry.entries.name) == 0) { *entry = FAT_CurDirEntry.entries.info; entry->FileStatus = 1; break; } } } FAT_CurDirEntry.entry_num = 0; first_sec_num = FAT_GetFirstSectorOfCluster(entry->CurrClus); end_sec_num = first_sec_num + FAT_BootSec.SecPerClus; for(sec_num = first_sec_num; sec_num < end_sec_num; sec_num++) { // Read all sub-entries MS_BulkRecv(sec_num, 1, FATBuffer); /* Read one sector */ buf = FATBuffer; while (buf < (FATBuffer + FAT_BootSec.BytsPerSec)) { rc = NOT_FOUND; ent_type = FAT_ChkEntType(buf); /* Check for the entry type */ if(ent_type == SFN_ENTRY ||ent_type == LFN_ENTRY) { afile = buf; if (ent_type == SFN_ENTRY) { /* If it is short entry get short file name */ FAT_GetSFN(buf, ent_name_read); // add the entry list FAT_StrCopy(ptr->name,ent_name_read); rc = MATCH_FOUND; } else if (ent_type == LFN_ENTRY) { sum = buf[LDIR_CHK_SUM_IDX]; len = FAT_GetLfn(&buf,&sec_num,FATBuffer,FATBuffer+FAT_BootSec.BytsPerSec,ent_name_read); if(len > 0) { FAT_StrCopy(ptr->name,ent_name_read); afile = buf; FAT_GetSFN(buf, ent_name_read); ent_type = FAT_ChkEntType(buf); if(ent_type == SFN_ENTRY && sum == FAT_LFN_ChkSum((uint8_t*)buf)) { // add the entry list rc = MATCH_FOUND; } } } if(rc == MATCH_FOUND) { ptr->info.FileAttr = afile[DIR_ATT_IDX]; ptr->info.CurrClus = ReadLE16U(&afile[DIR_FST_CLUS_HI_IDX])<<16 & 0xFFFF0000; /* High word of the first cluster number*/ ptr->info.CurrClus |= ReadLE16U(&afile[DIR_FST_CLUS_LO_IDX]); /* Low word of the first cluster number */ ptr->info.CurrClusOffset = 0; ptr->info.FileSize = ReadLE32U(&afile[DIR_SIZE_IDX]); /* Get file size */ ptr->info.EntrySec = sec_num; /* Get sector number where the filename is located */ ptr->info.EntrySecOffset = afile - FATBuffer; /* Get offset in this sector where the filename is located */ ptr++; FAT_CurDirEntry.entry_num++; } } if (ent_type == LAST_ENTRY) { /* If it is the last entry, no more entries will exist. */ break; } buf = buf + FAT_ENTRY_SIZE; /* Move to the next entry */ } } return MATCH_FOUND; } |