//*************************************************************************** //Function: to get DIR/FILE list or a single file address (cluster number) or to delete a specified file //Arguments: #1 - flag: GET_LIST, GET_FILE or DELETE #2 - pointer to file name (0 if arg#1 is GET_LIST) //return: first cluster of the file, if flag = GET_FILE // print file/dir list of the root directory, if flag = GET_LIST // Delete the file mentioned in arg#2, if flag = DELETE //**************************************************************************** struct dir_Structure* findFiles (unsigned char flag, unsigned char *fileName) { unsigned long cluster, sector, firstSector; struct dir_Structure *dir; unsigned int i; unsigned char j; cluster = rootCluster; //root cluster while(1) { firstSector = getFirstSector (cluster); for(sector = 0; sector < sectorPerCluster; sector++) { SD_readSingleBlock (firstSector + sector); for(i=0; i<bytesPerSector; i+=32) { dir = (struct dir_Structure *) &buffer[i]; if(dir->name[0] == EMPTY) //indicates end of the file list of the directory { if((flag == GET_FILE) || (flag == DELETE)) //transmitString("File does not exist!"); return 0; } else if((dir->name[0] != DELETED) && (dir->attrib != ATTR_LONG_NAME)) { if((flag == GET_FILE) || (flag == DELETE)) { for(j=0; j<11; j++) if(dir->name[j] != fileName[j]) break; if(j == 11) { if(flag == GET_FILE) return (dir); } } } } } cluster = (getSetNextCluster (cluster, GET, 0)); if(cluster > 0x0ffffff6) return 0; if(cluster == 0) { //transmitString("Error in getting cluster"); return 0; } } return 0; }
unsigned long findFirmware() { unsigned long cluster, firstSector; unsigned char attrib; unsigned int firstClusterHI; // high word of first cluster unsigned int firstClusterLO; // low word of first cluster unsigned long firstCluster; unsigned long theCluster; unsigned long sector; int i,j; cluster = rootCluster; firstSector = getFirstSector(cluster); SD_readSingleBlock(firstSector); for (i = 0; i < bytesPerSector; i += 32) { if (buffer[i] == EMPTY) { return 0; } attrib = buffer[ATTRIB+i]; if ((buffer[i] != DELETED) && attrib != ATTR_LONG_NAME) { // check for firmware file if ((buffer[i] == 'F') && (buffer[i+1] == 'I') && (buffer[i+2] == 'R') && (buffer[i+3] == 'M') && (buffer[i+8] == 'B') && (buffer[i+9] == 'I') && (buffer[i+10] == 'N')) { firstClusterLO = *(unsigned int*)&buffer[i+FIRSTCLUSTERLO]; // assume firstClusterHI is 0 firstCluster = firstClusterLO; return firstCluster; } } } return 0; }
fs_node* fat32_init(unsigned int drive, unsigned int addr, unsigned int offset){ int driveNum = 0; fatOffset = offset; read_sector_lba28(drive,addr,sizeof(hdd[driveNum]),offset,(unsigned char*)&hdd[driveNum]); fat32* f = (fat32*)(&hdd[driveNum].extended); bpb* b = (bpb*)&hdd[driveNum]; unsigned int firstSector = getFirstSector(0,f->rootCluster); //unsigned char* sector = (unsigned char*)malloc(hdd[driveNum].bytesPerSector); fs_node* fat32_root = (fs_node*)malloc(sizeof(fs_node)); skprintf(fat32_root->name, "fat32"); fat32_root->mask = fat32_root->uid = fat32_root->gid = fat32_root->inode = fat32_root->length = 0; fat32_root->flags = FS_DIRECTORY; fat32_root->read = 0; fat32_root->write = 0; fat32_root->open = 0; fat32_root->close = 0; fat32_root->readdir = &readdir_fat32; fat32_root->finddir = &finddir_fat32; fat32_root->ptr = 0; fat32_root->impl = 0; skprintf(fat32_root->name,"FS"); fat32_root->fs_inode= malloc(sizeof(fat32_inode)); ((fat32_inode*)fat32_root->fs_inode)->drive = drive; ((fat32_inode*)fat32_root->fs_inode)->addr = addr; ((fat32_inode*)fat32_root->fs_inode)->offset = offset; ((fat32_inode*)fat32_root->fs_inode)->sector = firstSector; /*fs_node* fat32_dev = (fs_node*)malloc(sizeof(fs_node)); skprintf(fat32_dev->name, "dev"); fat32_dev->mask = fat32_dev->uid = fat32_dev->gid = fat32_dev->inode = fat32_dev->length = 0; fat32_dev->flags = FS_DIRECTORY; fat32_dev->read = 0; fat32_dev->write = 0; fat32_dev->open = 0; fat32_dev->close = 0; fat32_dev->readdir = &readdir_fat32; fat32_dev->finddir = &finddir_fat32; fat32_dev->ptr = 0; fat32_dev->impl = 0; */ listDir(fat32_root,0); return fat32_root; }
unsigned int read_fat32(struct fs_node* node, unsigned int offset, unsigned int size, unsigned char* buffer){ fat32_inode* fsNode = ((fat32_inode*)node->fs_inode); fat32* f = (fat32*)(&hdd[0].extended); unsigned int sectorOffset = offset/512; //nth sector in cluster (0-7) unsigned int insideSectorOffset = offset%512; //position inside sector unsigned int clusterOffset = offset/(hdd[0].sectorPerCluster*512); //nth cluster in files cluster chain if ( node->length < size ) size = node->length; /* avoid that underflow. */ if ( node->length < offset+size){ if (offset > node->length) return 0; else size = node->length-offset; } if (size<=0) return 0; unsigned int nextC = fsNode->cluster; //kprintf("Reading from off=%d... cluster=%d sector=%d insideSector=%d size=%d\n",offset,clusterOffset,sectorOffset,insideSectorOffset,size); while (clusterOffset>0){ nextC = nextCluster(node,nextC); if (nextC == -1){ buffer = NULL; return 0; } clusterOffset--; } unsigned int sectorNr = getFirstSector(0,nextC); char sector[512]; read_sector_lba28(fsNode->drive,fsNode->addr,1,sectorNr+sectorOffset,(unsigned char*)§or); unsigned int i = insideSectorOffset; unsigned int x = 0; while ((i<512) && (i-insideSectorOffset)<size){ buffer[x++] = sector[i++]; } //kprintf(""); return size; }
struct fs_node* finddir_fat32(struct fs_node* node,char *name){ char sector[513]; char fileName[128]; fat32_inode* fsNode = ((fat32_inode*)node->fs_inode); read_sector_lba28(fsNode->drive,fsNode->addr,1,fsNode->sector,(unsigned char*)§or); //vread_sector_lba28(fsNode->drive,fsNode->addr,1,fsNode->sector,(unsigned char*)§or); unsigned int seq = -1; bool isLFN = false; for(int i = 0;i<BytePerSector;i+=32){ fat32_dir *fd = (fat32_dir*)§or[i]; if (fd->fileName[0]==0xE5 || fd->fileName[0]==0x05) { continue; } else if (fd->fileName[0]==0x00) { i = BytePerSector; //end break; } else { if (fd->attr == 0x0F){ if (seq == -1){ seq = fd->fileName[0]-0x40; fileName[seq*13]='\0'; isLFN = true; } seq--; int nr = 0; for(int j = 1;j<=9;j+=2) fileName[seq*13+(nr++)] = (§or[i])[j]; for(int j = 0;j<=10;j+=2) fileName[seq*13+(nr++)] = (§or[i])[j+0x0E]; fileName[seq*13+(nr++)] = (§or[i])[0x1C]; fileName[seq*13+(nr++)] = (§or[i])[2+0x1C]; } else{ if (!isLFN){ //kprintf("not lfn!\n"); int x; for(x = 7;x>=0;x--){ if (fd->fileName[x]!=' ') break; } x++; memcpy(fileName,fd->fileName,x); if (fd->attr!=0x10){ fileName[x] = '.'; fileName[x+1]=fd->fileName[8]; fileName[x+2]=fd->fileName[9]; fileName[x+3]=fd->fileName[10]; fileName[x+4]='\0'; } else fileName[x] = '\0'; isLFN = false; } if (fd->attr == 0x10){ if (fileName[0] != '\0'){ if (strcmp(fileName,name)){ unsigned int newSector = getFirstSector(0,fd->firstClusterNumber*10+fd->lowFirstCluster); fs_node* new_node = (fs_node*)malloc(sizeof(fs_node)); new_node->mask = new_node->uid = new_node->gid = new_node->inode = new_node->length = 0; new_node->flags = FS_DIRECTORY; new_node->read = 0; new_node->write = 0; new_node->open = 0; new_node->close = 0; new_node->readdir = &readdir_fat32; new_node->finddir = &finddir_fat32; new_node->ptr = 0; new_node->impl = 0; memcpy(new_node->name,fileName,128); new_node->fs_inode = (fat32_inode*)malloc(sizeof(fat32_inode)); unsigned int offset = ((fat32_inode*)node->fs_inode)->offset; unsigned int addr = ((fat32_inode*)node->fs_inode)->addr; unsigned int drive = ((fat32_inode*)node->fs_inode)->drive; ((fat32_inode*)new_node->fs_inode)->offset = offset; ((fat32_inode*)new_node->fs_inode)->addr = addr; ((fat32_inode*)new_node->fs_inode)->sector = newSector; ((fat32_inode*)new_node->fs_inode)->cluster = fd->firstClusterNumber*10+fd->lowFirstCluster; ((fat32_inode*)new_node->fs_inode)->drive = drive; /*char sector1[513]; char fileName1[255]; isLFN = false; seq = -1; read_sector_lba28(drive,addr,1,newSector,(unsigned char*)§or1); for(int i1 = 0;i1<BytePerSector;i1+=32){ fat32_dir *fd1 = (fat32_dir*)§or1[i1]; if (fd1->fileName[0]==0xE5 || fd1->fileName[0]==0x05) { //skip continue; } else if (fd1->fileName[0]==0x00) { i = BytePerSector; //end break; } else { //--- if (fd1->attr == 0x0F){ kprintf("part of lfn\n"); if (seq == -1){ seq = fd1->fileName[0]-0x40; fileName1[seq*13]='\0'; isLFN = true; } kprintf("seq => %d %d %c\n",seq, fd1->fileName[0],fd1->fileName[0]); seq--; kprintf("seq => %d\n",seq); int nr = 0; for(int j = 1;j<=9;j+=2) fileName1[seq*13+(nr++)] = (§or1[i1])[j]; for(int j = 0;j<=10;j+=2) fileName1[seq*13+(nr++)] = (§or1[i1])[j+0x0E]; fileName1[seq*13+(nr++)] = (§or1[i1])[0x1C]; fileName1[seq*13+(nr++)] = (§or1[i1 ])[2+0x1C]; } else{ if (!isLFN){ kprintf("just a short name!\n"); int x; for(x = 7;x>=0;x--){ if (fd1->fileName[x]!=' ') break; } x++; memcpy(fileName1,fd1->fileName,x); fileName1[x] = '.'; fileName1[x+1]=fd1->fileName[8]; fileName1[x+2]=fd1->fileName[9]; fileName1[x+3]=fd1->fileName[10]; fileName1[x+4]='\0'; isLFN = false; } else kprintf("end of lfn\n"); kprintf("%x %s\n\n",fd1->attr,fileName1); seq = -1; } //--- } } while(1==1){}*/ return new_node; } // else kprintf("%s %s\n",fileName,name); } }else { if (strcmp(fileName,name)){ unsigned int newSector = getFirstSector(0,fd->firstClusterNumber*10+fd->lowFirstCluster); //kprintf("sector %d\n",newSector); fs_node* new_node = (fs_node*)malloc(sizeof(fs_node)); new_node->mask = new_node->uid = new_node->gid = new_node->inode = 0; new_node->flags = FS_FILE; new_node->length = fd->fileSize; new_node->read = &read_fat32; new_node->write = 0; new_node->open = 0; new_node->close = 0; new_node->readdir = 0; new_node->finddir = 0; new_node->ptr = 0; new_node->impl = 0; memcpy(new_node->name,fileName,128); new_node->fs_inode = malloc(sizeof(fat32_inode)); unsigned int offset = ((fat32_inode*)node->fs_inode)->offset; unsigned int addr = ((fat32_inode*)node->fs_inode)->addr; unsigned int drive = ((fat32_inode*)node->fs_inode)->drive; ((fat32_inode*)new_node->fs_inode)->offset = offset; ((fat32_inode*)new_node->fs_inode)->addr = addr; ((fat32_inode*)new_node->fs_inode)->sector = newSector; ((fat32_inode*)new_node->fs_inode)->cluster = fd->firstClusterNumber*10+fd->lowFirstCluster; ((fat32_inode*)new_node->fs_inode)->drive = drive; fat32_inode* fsInode = (fat32_inode*)(new_node->fs_inode); return new_node; } // else kprintf("%s %s\n",fileName,name); } seq = -1; } } } return NULL; }
//uint8_t readCfgFile(DATE *adate,TIME *atime, uint8_t *filename_dat, uint8_t atomic) uint8_t readCfgFile(char *filename_dat) //, uint8_t atomic) { DIR *dir; uint32_t cluster, fileSize, firstSector; uint16_t k; uint8_t i,j,ld,lt; uint8_t filename[]= "CONFIG CFG"; uint8_t filename2[]="_CONFIG CFG"; dir = findFiles (GET_FILE, filename, filename); //, atomic); //get the file location if(dir == 0) return(0); cluster = (((uint32_t) dir->firstClusterHI) << 16) | dir->firstClusterLO; fileSize = dir->fileSize; ld = 0; lt = 0; while(1) { ClusterNumber = cluster; firstSector = getFirstSector (); for(j=0; j<SectorPerCluster; j++) { SD_readSingleBlock(firstSector + j); //, atomic); for(k=0; k<512; k++) { switch(buffer[k]) { case 'S': k++; if(buffer[k]=='T') { k++; Time.h = getDec((uint8_t*)buffer+k); //k +=2; Time.m = getDec((uint8_t*)buffer+k+2); //k +=2; Time.s = getDec((uint8_t*)buffer+k+4); } else if(buffer[k]=='D') { k++; Date.d = getDec(&buffer[k]); //k +=2; Date.m = getDec(&buffer[k+2]); //k +=2; Date.y = 20 + getDec(&buffer[k+4]); } else if(buffer[k]=='F') { k = k+2; for(i=0;i<8;i++) { if(buffer[k+i] < 0x21) //if any control code and space { filename_dat[i] = 0x00; break; } filename_dat[i] = buffer[k+i]; } filename_dat[i+1] = 0x00; // i = strlcpy(filename_dat,&(buffer[k]),9); k = k+i; } else if(buffer[k]=='I') { k +=2; for(i=0;i<13;i++) { if(buffer[k+i] < 0x20) //if any control code break; inset[i] = buffer[k+i]; } } break; case 'A': k++; if(buffer[k]=='D') { k++; ADate[ld].d = getDec((uint8_t*)buffer+k); //k +=2; ADate[ld].m = getDec((uint8_t*)buffer+k+2); //k +=2; ADate[ld].y = 20 + getDec((uint8_t*)buffer+k+4); ld++; } else if(buffer[k]=='T') { k++; ATime[lt].h = getDec((uint8_t*)buffer+k); //k +=2; ATime[lt].m = getDec((uint8_t*)buffer+k+2); //k +=2; ATime[lt].s = getDec((uint8_t*)buffer+k+4); lt++; } break; } if (k >= fileSize ) { findFiles(RENAME, filename, filename2); //, atomic); return(lt); } } } ClusterNumber = cluster; cluster = getSetNextCluster (GET); //, 0, atomic); if(cluster == 0) { //#if BIGAVR == 1 // printf_P(PSTR("ERR GETTING CLUSTERb\n")); //#endif //BIGAVR return(0); } } return(0); }
//uint8_t readCfgFile(DATE *adate,TIME *atime, uint8_t *filename_dat, uint8_t atomic) uint8_t readCfgFile(uint8_t *filename_dat, uint8_t atomic) { DIR *dir; uint32_t cluster, fileSize, firstSector; uint16_t k; uint8_t i,j,ld,lt; uint8_t filename[]="CONFIG CFG"; uint8_t filename2[]="_CONFIG CFG"; TIME_t time; DATE_t date; dir = findFiles (GET_FILE, filename, filename, atomic); //get the file location if(dir == 0) return(0); cluster = (((uint32_t) dir->firstClusterHI) << 16) | dir->firstClusterLO; fileSize = dir->fileSize; ld = 0; lt = 0; while(1) { firstSector = getFirstSector (cluster); for(j=0; j<SectorPerCluster; j++) { SD_readSingleBlock(firstSector + j, atomic); for(k=0; k<512; k++) { switch(buffer[k]) { case 'S': k++; switch(buffer[k]) { case 'T': k++; time.h = getDec((uint8_t*)buffer+k); time.m = getDec((uint8_t*)buffer+k+2); time.s = getDec((uint8_t*)buffer+k+4); break; case 'D': k++; date.d = getDec(&buffer[k]); date.m = getDec(&buffer[k+2]); date.y = 20 + getDec(&buffer[k+4]); break; case 'F': k = k+2; for(i=0;i<13;i++) { if(buffer[k+i] < 0x20) //if any control code break; filename_dat[i] = buffer[k+i]; } k = k+i; break; case 'M': //mask definitions: M - mic only, S - SHT sensor only, T - DS1820 1-wire sensor, X - Mic+SHT, '-' - nothing k++; for(i=0;i<12;i++) { Mask.SHT = Mask.SHT<<1; Mask.MIC = Mask.MIC<<1; switch(buffer[k+i]) { // case 'X': //SHT mask generated from mic mask! // Mask.SHT |= 0x0001; case 'M': Mask.MIC |= 0x0001; break; // case 'S': // Mask.SHT |= 0x0001; // break; case 'T': Mask.DS |= 0x01; // any T will cause DS mask to be one... break; default: break; } } k = k+i; } break; case 'A': k++; if(buffer[k]=='D') { k++; ADate[ld].d = getDec((uint8_t*)buffer+k); //k +=2; ADate[ld].m = getDec((uint8_t*)buffer+k+2); //k +=2; ADate[ld].y = 20 + getDec((uint8_t*)buffer+k+4); ld++; } else if(buffer[k]=='T') { k++; ATime[lt].h = getDec((uint8_t*)buffer+k); //k +=2; ATime[lt].m = getDec((uint8_t*)buffer+k+2); //k +=2; ATime[lt].s = getDec((uint8_t*)buffer+k+4); lt++; } break; } if (k >= fileSize ) { findFiles(RENAME,filename, filename2, atomic); RTC_SetDateTime(&date, &time); //?? if(Mask.SHT) // { generate_mask_bm(); // } return(lt); } } } cluster = getSetNextCluster (cluster, GET, 0, atomic); if(cluster == 0) { #if RSCOM == 1 printf_P(PSTR("ERR GETTING CLUSTERb\n")); #endif //BIGAVR return(0); } } return(0); }