|
//ide_test.c
//include files #include <stdio.h> #include <sysreg.h> #include <builtins.h> #include <defTS101.h> #include <signal.h> #include "ide_test.h"
/*********************** may be changed by user *******************/ //define BeginGroup of the file to be created(better larger than 3) #define NEWBEGINGRP 4
//define the length of the file to be created #define NEWLENGTH 720*625*2//2 frame
//define the length of the file to be added #define ADDLENGTH 4099
//define source cluster of file needed to copy #define COPYFILE 4
//define source cluster of the file to be deleted #define DELFILE 4
//define the attribute of the file #define ATTRIB 32
//define parameters of the file to be created #define NEWYEAR 2005 #define NEWMONTH 2 #define NEWDAY 7 #define NEWHOUR 15 #define NEWMINUTE 2 #define NEWSECOND 48 //only be even
//define parameters of the file to be copied #define COPYYEAR 2005 #define COPYMONTH 2 #define COPYDAY 8 #define COPYHOUR 16 #define COPYMINUTE 15 #define COPYSECOND 30 //only be even
//define parameters of the file to be added #define ADDYEAR 2005 #define ADDMONTH 2 #define ADDDAY 14 #define ADDHOUR 16 #define ADDMINUTE 03 #define ADDSECOND 18 //only be even
/******************************************************************/
//define global variables unsigned char uRcvBuffer[512],uRcvBuffer1[512];//used for data transfer extern unsigned char NewFileBuf[NEWLENGTH];//used for the content of the file to be created extern unsigned char AddFileBuf[ADDLENGTH];//used for the content of the file to be added unsigned short atadevinfo[256]; static unsigned short errcount = 0; static unsigned char status = 0; static unsigned int groupnum = 0; static struct PartitionTable MBRTable; static struct FAT32LogicPartition CPartition; static struct FileReg OperatorFile, myfile, mycopyfile,mynewfile,myaddfile; unsigned int uError = 0; unsigned char temp[512]; unsigned int NxtFreeGrp;
//define functions void InitDSP(); unsigned char InitIDE(); unsigned char InitMBR(); unsigned char InitCPartition(); unsigned char ListFile(); unsigned char atadev_get_block(unsigned int lba, unsigned char sector_count, unsigned char* buffer); unsigned char atadev_put_block(unsigned int lba, unsigned char sector_count, unsigned char* buffer);
unsigned char atadev_get_block_quick(unsigned int lba, unsigned char sector_count, unsigned char* buffer); unsigned char atadev_put_block_quick(unsigned int lba, unsigned char sector_count, unsigned char* buffer); unsigned char Check_NoRequest(); unsigned char Check_HaveRequest(); unsigned char Check_DateReady(); unsigned char CopyGroup(unsigned int srcgroup, unsigned int dstgroup); unsigned int newgrp(); unsigned char WriteDFT(); unsigned char CopyFile(); unsigned char DelFile(unsigned int srcgroup); unsigned char NewFile(); unsigned char WriteNewDFT(); unsigned char WriteFile(); unsigned char WriteGroup(unsigned int dstgroup,unsigned int ClusNums); unsigned char WriteWholeGroup(unsigned int dstgroup,unsigned int ClusNums); unsigned char AddToFile();//just can add data to the new created file unsigned char AddGroup(unsigned int dstgroup,unsigned int ClusNums); unsigned char AddWholeGroup(unsigned int dstgroup,unsigned int ClusNums); unsigned char WriteAddDFT();
/**************************** main program start *******************************/ // Hard disk test main program void main() { int i; printf("\n\r ****************** Hard disk test begin ******************\n"); InitDSP(); if(!InitIDE()) printf("\n\r InitIDE error!\n"); else { if(InitMBR()) { if((MBRTable.PartitionType == 0x0b) || (MBRTable.PartitionType == 0x0c)) { if(InitCPartition()) { if(ListFile()) printf("\n\r ListFile end!"); else printf("\n\r ListFile error!"); if(NewFile()) printf("\n\r NewFile end!"); else printf("\n\r NewFile error!"); if(ListFile()) printf("\n\r ListFile end!"); else printf("\n\r ListFile error!"); /* //NOTICE:before CopyFile,you should ListFile to look for the file which to be copied if(CopyFile()) printf("\n\r CopyFile end!"); else printf("\n\r CopyFile error!"); if(ListFile()) printf("\n\r ListFile end!"); else printf("\n\r ListFile error!"); if(DelFile(DELFILE)) printf("\n\r DeleteFile end!"); else printf("\n\r DeleteFile error!"); if(ListFile()) printf("\n\r ListFile end!"); else printf("\n\r ListFile error!"); if(AddToFile()) printf("\n\r AddToFile end!"); else printf("\n\r AddToFile error!"); if(ListFile()) printf("\n\r ListFile end!"); else printf("\n\r ListFile error!"); */ } else printf("\n\r InitCPartition error"); } else printf("\n\r Partition Type is not FAT32!\n"); } else printf("\n\n\n\r InitMBR error!\n"); } printf("\n\n\r ****************** Hard disk test end ******************");
} /**************************** main program end *******************************/
/*************************** initialization start ****************************/ // initialize DSP void InitDSP( void ) { //------------------------- Init SYSCON and SDRCON ----------------------
__builtin_sysreg_write(__SYSCON, SYSCON_MSH_SLOW | SYSCON_MSH_WT3 | SYSCON_MSH_IDLE | SYSCON_MS1_SLOW | SYSCON_MS1_WT3 | SYSCON_MS1_IDLE | SYSCON_MS0_SLOW | SYSCON_MS0_WT3 | SYSCON_MS0_IDLE);
__builtin_sysreg_write(__SDRCON, SDRCON_INIT | SDRCON_RAS2PC4 | SDRCON_PC2RAS3 | SDRCON_REF600 | SDRCON_PG256 | SDRCON_CLAT2 | SDRCON_ENBL); }
//initialize ide unsigned char InitIDE() { unsigned int i=0; unsigned char error=0;
if(!Check_NoRequest()) { printf("\n\r Check_NoRequest : 001"); return 0; } DEV_HEAD = DEV_HEAD & 0xEF; for(i=0;i<10;i++);
if(!Check_NoRequest()) { printf("\n\r Check_NoRequest : 002 "); return 0; } COMMAND = IDENTIFY_DEVICE; for (i=0;i<10;i++); if(!Check_HaveRequest()) { printf("\n\r Check_HaveRequest : 003 "); return 0; } for(i=0;i<256;i++) atadevinfo[i] = DATA; for (i=0;i<10;i++); if(!Check_NoRequest()) { printf("\n\r Check_NoRequest : 004 "); return 0; }
if (status & ERRBIT) error = ERRORREG;
if (error) { printf("\n\r atadevinfo read error : 005 "); return 0; }
if(!Check_NoRequest()) { printf("\n\r Check_NoRequest : 006 "); return 0; } DEV_HEAD = DEV_HEAD & 0xEF; for(i=0;i<10;i++); if(!Check_NoRequest()) { printf("\n\r Check_NoRequest : 007 "); return 0; } SECTOR_COUNT = (atadevinfo[47] & 0xFF); for(i=0;i<10;i++); if(!Check_DateReady()) { printf("\n\r Check_DateReady : 008 "); return 0; } COMMAND = SET_MULTIPLE; for(i=0;i<10;i++); if(!Check_NoRequest()) { printf("\n\r Check_NoRequest : 009 "); return 0; } error = 0; if (status & ERRBIT) error = ERRORREG; if (error) { printf("\n\r SECTOR_COUNT set error : 010 "); return 0; } return 1; } /*************************** initialization end ****************************/
/************************* check Hard disk status start ****************************/
//Hard disk free and have no data request,then return 1 (BSYBIT=0, DRQBIT=0) unsigned char Check_NoRequest() { unsigned int iNumLoops = 0; while((iNumLoops++ < LOOPTIME) && (((status = ATASTATUS) & BSYBIT) || (status & DRQBIT))) { } if(iNumLoops >= LOOPTIME) return 0; else return 1; }
//Hard disk free and data ready,then return 1 (BSYBIT=0, DRDYBIT=1) unsigned char Check_DateReady() { unsigned int iNumLoops = 0; while((iNumLoops++ < LOOPTIME)&& (!((status = ATASTATUS) & DRDYBIT)||(status & BSYBIT))) { } if(iNumLoops >= LOOPTIME) return 0; else return 1; }
//Hard disk free and have data request,then return 1 (BSYBIT=0, DRQBIT=1) unsigned char Check_HaveRequest() { unsigned int iNumLoops = 0; while((iNumLoops++ < LOOPTIME)&& (((status = ATASTATUS) & BSYBIT) || !(status & DRQBIT))) { } if(iNumLoops>=LOOPTIME) return 0; else return 1; } /************************* check Hard disk status end ****************************/
/*************** read and write Hard disk program start *****************/
//read Hard disk unsigned char atadev_get_block(unsigned int lba, unsigned char sector_count, unsigned char* buffer) { unsigned char error = 1; unsigned int i = 0; errcount = 0;
while (error) { if(!Check_NoRequest()) return 0; DEV_HEAD = DEV_HEAD & 0xEF; for(i=0;i<10;i++); if(!Check_NoRequest()) return 0; SECTOR_COUNT = sector_count; SECTOR_NUMBER = (unsigned char) (lba & 0xFF); CYLINDER_LOW = (unsigned char) ((lba >> 8) & 0xFF); CYLINDER_HIGH = (unsigned char) ((lba >> 16) & 0xFF); DEV_HEAD = ((unsigned char) ((lba >> 24) & 0xF)) | LBAMODE; for(i=0;i<10;i++);
if(!Check_DateReady()) return 0; COMMAND = READ_MULTIPLE; for(i=0;i<10000000;i++);
if(!Check_HaveRequest()) return 0; for (i = 0; i < 256; i++) { ((unsigned short *)buffer)[i] = DATA; } for(i=0;i<10;i++); //translate buffer[256] to buffer[512] for (i = 0; i < 256; i++) { temp[2*i] = buffer[i] & 0x000000FF; temp[2*i+1] = (buffer[i] & 0x0000FF00) >> 8; } for (i = 0;i < 512; i++) buffer[i]=temp[i]; if(!Check_NoRequest()) return 0;
if (status & ERRBIT) { error = ERRORREG; errcount++; } else error = 0;
if (errcount > 10) break; } if(error) return 0; else return 1; }
//write Hard disk unsigned char atadev_put_block(unsigned int lba, unsigned char sector_count, unsigned char* buffer) { unsigned char error = 1; unsigned int i = 0; errcount = 0; //translate buffer[512] to buffer[256] for (i = 0; i < 512; i++) { temp[i/2] = buffer[i] | buffer[i+1] << 8; i++; } for (i = 0;i < 256; i++) buffer[i]=temp[i]; while (error) { if(!Check_NoRequest()) return 0; DEV_HEAD = DEV_HEAD & 0xEF; for(i=0;i<10;i++); if(!Check_NoRequest()) return 0; SECTOR_COUNT = sector_count; SECTOR_NUMBER = (unsigned char) (lba & 0xFF); CYLINDER_LOW = (unsigned char) ((lba >> 8) & 0xFF); CYLINDER_HIGH = (unsigned char) ((lba >> 16) & 0xFF); DEV_HEAD = ((unsigned char) ((lba >> 24) & 0xF)) | LBAMODE; for(i=0;i<10;i++);
if(!Check_DateReady()) return 0; COMMAND = WRITE_MULTIPLE; for(i=0;i<10000000;i++);
if(!Check_HaveRequest()) return 0; for (i = 0; i < 256; i++) DATA = ((unsigned short *)buffer)[i]; for(i=0;i<10;i++);
if(!Check_NoRequest()) return 0;
if (status & ERRBIT) { error = ERRORREG; errcount++; } else error = 0;
if (errcount > 10) break; }
if(error) return 0; else return 1; }
//read Hard disk quickly unsigned char atadev_get_block_quick(unsigned int lba, unsigned char sector_count, unsigned char* buffer) { unsigned char error = 1; unsigned int i = 0; errcount = 0;
while (error) { if(!Check_NoRequest()) return 0; DEV_HEAD = DEV_HEAD & 0xEF; for(i=0;i<10;i++); if(!Check_NoRequest()) return 0; SECTOR_COUNT = sector_count; SECTOR_NUMBER = (unsigned char) (lba & 0xFF); CYLINDER_LOW = (unsigned char) ((lba >> 8) & 0xFF); CYLINDER_HIGH = (unsigned char) ((lba >> 16) & 0xFF); DEV_HEAD = ((unsigned char) ((lba >> 24) & 0xF)) | LBAMODE; for(i=0;i<10;i++);
if(!Check_DateReady()) return 0; COMMAND = READ_MULTIPLE; for(i=0;i<10;i++);
if(!Check_HaveRequest()) return 0; for (i = 0; i < 256; i++) { ((unsigned short *)buffer)[i] = DATA; } for(i=0;i<10;i++); //translate buffer[256] to buffer[512] for (i = 0; i < 256; i++) { temp[2*i] = buffer[i] & 0x000000FF; temp[2*i+1] = (buffer[i] & 0x0000FF00) >> 8; } for (i = 0;i < 512; i++) buffer[i]=temp[i]; if(!Check_NoRequest()) return 0;
if (status & ERRBIT) { error = ERRORREG; errcount++; } else error = 0;
if (errcount > 10) break; } if(error) return 0; else return 1; }
//write Hard disk quickly unsigned char atadev_put_block_quick(unsigned int lba, unsigned char sector_count, unsigned char* buffer) { unsigned char error = 1; unsigned int i = 0; errcount = 0; //translate buffer[512] to buffer[256] for (i = 0; i < 512; i++) { temp[i/2] = buffer[i] | buffer[i+1] << 8; i++; } for (i = 0;i < 256; i++) buffer[i]=temp[i]; while (error) { if(!Check_NoRequest()) return 0; DEV_HEAD = DEV_HEAD & 0xEF; for(i=0;i<10;i++); if(!Check_NoRequest()) return 0; SECTOR_COUNT = sector_count; SECTOR_NUMBER = (unsigned char) (lba & 0xFF); CYLINDER_LOW = (unsigned char) ((lba >> 8) & 0xFF); CYLINDER_HIGH = (unsigned char) ((lba >> 16) & 0xFF); DEV_HEAD = ((unsigned char) ((lba >> 24) & 0xF)) | LBAMODE; for(i=0;i<10;i++);
if(!Check_DateReady()) return 0; COMMAND = WRITE_MULTIPLE; for(i=0;i<10;i++);
if(!Check_HaveRequest()) return 0; for (i = 0; i < 256; i++) DATA = ((unsigned short *)buffer)[i]; for(i=0;i<10;i++);
if(!Check_NoRequest()) return 0;
if (status & ERRBIT) { error = ERRORREG; errcount++; } else error = 0;
if (errcount > 10) break; }
if(error) return 0; else return 1; }
/************** read and write Hard disk card program end *************/
/**************************** FAT32 start ****************************/
//read MBR table unsigned char InitMBR() { atadev_get_block(0, 1, uRcvBuffer);//read MBR boot sector(0 sector) if(!((uRcvBuffer[510] == 0x55) && (uRcvBuffer[511] == 0xAA))) return 0; MBRTable.BootId = uRcvBuffer[0x1be]; MBRTable.PartitionType = uRcvBuffer[0x1c2]; MBRTable.PartitionFirstSector = uRcvBuffer[0x1c6] | (uRcvBuffer[0x1c7]<<8) | (uRcvBuffer[0x1c8]<<16) | (uRcvBuffer[0x1c9] <<24); MBRTable.PartitionSectorNum = uRcvBuffer[0x1ca] | (uRcvBuffer[0x1cb]<<8) | (uRcvBuffer[0x1cc]<<16) | (uRcvBuffer[0x1cd] <<24); return 1; }
//read FAT32 logic hard disk configuration unsigned char InitCPartition() { unsigned int i = 0,k=0; atadev_get_block(MBRTable.PartitionFirstSector, 1, uRcvBuffer);//read BPB if(!((uRcvBuffer[510] == 0x55) && (uRcvBuffer[511] == 0xAA))) return 0; //count of bytes per sector CPartition.bpb.BytePerSector = uRcvBuffer[0xb] | (uRcvBuffer[0xc]<<8); if(CPartition.bpb.BytePerSector != 512) { printf("\n ERROR: Byte per sector is not 512!"); return 0; }
//number of sectors per cluster CPartition.bpb.SectorPerGroup = uRcvBuffer[0xd]; //number of reserved sectors in the reserved region of the volume starting at the first sector of the volume CPartition.bpb.SectorReserved = uRcvBuffer[0xe] | (uRcvBuffer[0xf]<<8); //count of FAT data structures on the volume CPartition.bpb.FATNum = uRcvBuffer[0x10]; if(CPartition.bpb.FATNum != 2) { printf("\n ERROR: FATNum is not 2!"); return 0; }
//sectors per track CPartition.bpb.SectorPerHead = uRcvBuffer[0x18] | (uRcvBuffer[0x19]<<8); if(CPartition.bpb.SectorPerHead != 63) { printf("\n ERROR: SectorPerHead is not 63!"); return 0; } //number of heads CPartition.bpb.HeadNum = uRcvBuffer[0x1a] | (uRcvBuffer[0x1b]<<8); if(CPartition.bpb.HeadNum != 255) { printf("\n ERROR: HeadNum is not 255!"); return 0; }
//count of hidden sectors prededing the partition that contains this FAT volume CPartition.bpb.SectorHide = uRcvBuffer[0x1c] | (uRcvBuffer[0x1d]<<8) | (uRcvBuffer[0x1e]<<16) | (uRcvBuffer[0x1f] <<24); if(CPartition.bpb.SectorHide != 63) { printf("\n ERROR: SectorHide is not 63!"); return 0; } //total count of sectors on the volume CPartition.bpb.LogicTotalSector = uRcvBuffer[0x20] | (uRcvBuffer[0x21]<<8) | (uRcvBuffer[0x22]<<16) | (uRcvBuffer[0x23] <<24); //count of sectors occupied by one FAT CPartition.bpb.SectorPerFAT = uRcvBuffer[0x24] | (uRcvBuffer[0x25]<<8) | (uRcvBuffer[0x26]<<16) | (uRcvBuffer[0x27] <<24);
//first cluster number of the root directory CPartition.bpb.BeginGroup = uRcvBuffer[0x2c] | (uRcvBuffer[0x2d]<<8) | (uRcvBuffer[0x2e]<<16) | (uRcvBuffer[0x2f] <<24);
//the cluster number at which the drive should start looking for free cluster CPartition.bpb.FSInfo = uRcvBuffer[0x30] | (uRcvBuffer[0x31]<<8); atadev_get_block(MBRTable.PartitionFirstSector + CPartition.bpb.FSInfo, 1, uRcvBuffer1);//read BPB if(!((uRcvBuffer1[0] == 0x52) & (uRcvBuffer1[1] == 0x52) & (uRcvBuffer1[2] == 0x61) & (uRcvBuffer1[3] == 0x41))) { printf("Load FSInfo error!"); return 0; } NxtFreeGrp = uRcvBuffer1[0x1ec] | (uRcvBuffer1[0x1ed]<<8) | (uRcvBuffer1[0x1ee]<<16) |(uRcvBuffer1[0x1ef]<<24); //count of sectors per boot record CPartition.bpb.SectorPerBOOT = uRcvBuffer[0x32]; if(CPartition.bpb.SectorPerBOOT != 6) { printf("\n ERROR: SectorPerBOOT is not 6!"); return 0; } printf("\n\r SectorPerGroup = %d", CPartition.bpb.SectorPerGroup); printf("\n\r SectorReserved = %d", CPartition.bpb.SectorReserved); printf("\n\r SectorPerFAT = %d", CPartition.bpb.SectorPerFAT); printf("\n\r LogicTotalSector = %d", CPartition.bpb.LogicTotalSector); printf("\n\r BeginGroup = %d", CPartition.bpb.BeginGroup); CPartition.FAT1BeginSector = MBRTable.PartitionFirstSector + CPartition.bpb.SectorReserved;
CPartition.DATABeginSector = CPartition.FAT1BeginSector + CPartition.bpb.FATNum * CPartition.bpb.SectorPerFAT;
atadev_get_block(CPartition.FAT1BeginSector, 1, uRcvBuffer); if(!((uRcvBuffer[0] == 0xf8) && (uRcvBuffer[1] == 0xff))) { printf("\n\r Read FAT1 error"); return 0; }
atadev_get_block(CPartition.FAT1BeginSector+CPartition.bpb.SectorPerFAT, 1, uRcvBuffer); if(!((uRcvBuffer[0] == 0xf8) && (uRcvBuffer[1] == 0xff))) { printf("\n\r Read FAT2 error"); return 0; }
return 1; }
/**************************** FAT32 end ****************************/
/******************* user-defined functions start ******************/
//initialize file void InitFile(unsigned char fileindex) { unsigned char j=0; unsigned char i=fileindex; for(j=0; j<8; j++) OperatorFile.FileName[j] = uRcvBuffer[32*i+j]; for(j=0; j<3; j++) OperatorFile.FileExt[j] = uRcvBuffer[32*i+j+8]; OperatorFile.Attrib = uRcvBuffer[32*i+0x0b]; OperatorFile.LastTime = uRcvBuffer[32*i+0x16] | (uRcvBuffer[32*i+0x17]<<8); OperatorFile.LastDate = uRcvBuffer[32*i+0x18] | (uRcvBuffer[32*i+0x19]<<8); OperatorFile.BeginGroup = uRcvBuffer[32*i+0x1a] | (uRcvBuffer[32*i+0x1b]<<8); OperatorFile.FileLength = uRcvBuffer[32*i+0x1c] | (uRcvBuffer[32*i+0x1d]<<8) | (uRcvBuffer[32*i+0x1e]<<16) | (uRcvBuffer[32*i+0x1f]<<24); }
//view DFB of the file,not the data(in the data section) of the file void ViewFile() { unsigned char j=0; unsigned int YEAR,MONTH,DAY,HOUR,MINUTE,SECOND; printf("\n\r Filename: "); for(j=0; j<8; j++) printf("%c", OperatorFile.FileName[j]); printf("\r Extname: "); for(j=0; j<3; j++) printf("%c", OperatorFile.FileExt[j]);
printf("\r Attrib: %d", OperatorFile.Attrib); HOUR = (OperatorFile.LastTime & 0XF800) >> 11; MINUTE = (OperatorFile.LastTime &0X07E0) >> 5; SECOND = (OperatorFile.LastTime & 0X001F) * 2; printf("\r LastTime: %d"" : ""%d"" : ""%d", HOUR,MINUTE,SECOND); YEAR = ((OperatorFile.LastDate & 0XFE00) >> 9) + 1980; MONTH = (OperatorFile.LastDate & 0X01E0) >> 5; DAY = OperatorFile.LastDate & 0X001F; printf("\r LastDate: %d"" / ""%d"" / ""%d", MONTH,DAY,YEAR); printf("\r BeginGroup: %d", OperatorFile.BeginGroup); printf("\r FileLength: %d", OperatorFile.FileLength); }
//list all files which in the Hard disk unsigned char ListFile() { unsigned int i=0, j=0,k=0;
printf("\n\r Listing the files in the Hard disk:"); //read the foremost 2 clusters of the DATA section which used for DFT for(i=0; i<2 * CPartition.bpb.SectorPerGroup; i++) { //read 16 DFBs atadev_get_block_quick(CPartition.DATABeginSector + i, 1, uRcvBuffer); //every block has 512 bytes,every DFB of file occupies 32 bytes,so 512/32=16 for(j=0; j<16; j++) { InitFile(j); if(OperatorFile.Attrib == 32)//file is archive if(OperatorFile.FileLength > 0) { ViewFile(); //printf("\ri=%x,\rj=%x",i,j); } if(OperatorFile.BeginGroup == COPYFILE) myfile = OperatorFile; } } printf("\n\n"); return 1; }
//copy file (include the DFB and the data of the file) unsigned char CopyFile() { unsigned int i=0, j=0; unsigned int srcgroup = 0, dstgroup = 0; groupnum = 0; OperatorFile = myfile; ViewFile(); //copy the DFB of the file mycopyfile = myfile; mycopyfile.FileName[0] = 'C'; mycopyfile.FileName[1] = 'O'; mycopyfile.FileName[2] = 'P'; mycopyfile.FileName[3] = 'Y'; mycopyfile.FileName[4] = 'F'; mycopyfile.FileName[5] = 'I'; mycopyfile.FileName[6] = 'L'; mycopyfile.FileName[7] = 'E'; mycopyfile.LastTime = (COPYHOUR << 11) | (COPYMINUTE << 5) | (COPYSECOND / 2); mycopyfile.LastDate = ((COPYYEAR-1980) << 9) | (COPYMONTH << 5) | COPYDAY; OperatorFile = mycopyfile; ViewFile(); //copy the data of the file srcgroup = myfile.BeginGroup; dstgroup = newgrp()/*NxtFreeGrp*/;//find the first new unused cluster mycopyfile.BeginGroup = dstgroup; if(dstgroup == 0) { printf("\n\r Can not find free space!"); return 0; } while(1) { printf("\n\r srcgroup=%d, dstgroup=%d",srcgroup,dstgroup); //copy cluster CopyGroup(srcgroup, dstgroup); //wait for the next source cluster i = srcgroup * 4 / 512; j = srcgroup - i * 512 / 4; atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); srcgroup = uRcvBuffer[4*j+0] | (uRcvBuffer[4*j+1]<<8) | (uRcvBuffer[4*j+2]<<16) | (uRcvBuffer[4*j+3]<<24); //source cluster to the end if(srcgroup == 0x0fffffff) { //modify destination cluster i = dstgroup * 4 / 512; j = dstgroup - i * 512 / 4; atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); uRcvBuffer[4*j+0] = 0xFF; uRcvBuffer[4*j+1] = 0xFF; uRcvBuffer[4*j+2] = 0xFF; uRcvBuffer[4*j+3] = 0x0F; atadev_put_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); //write file register item if(!WriteDFT()) { printf("\n\r ERROR: Write DFT failed!"); return 0; } return 1; } //modify destination cluster to the previous destination cluster i = dstgroup * 4 / 512; j = dstgroup - i * 512 / 4; //first write something,prevent find new cluster is this cluster again atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); uRcvBuffer[4*j+0] = 0x55; atadev_put_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); //next new cluster,content is the content of the next object cluster dstgroup = newgrp(); if(dstgroup == 0) { printf("\n\r Can not find free space!"); return 0; } atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); uRcvBuffer[4*j+0] = dstgroup & 0X000000FF; uRcvBuffer[4*j+1] = (dstgroup & 0X0000FF00) >> 8; uRcvBuffer[4*j+2] = (dstgroup & 0X00FF0000) >> 16; uRcvBuffer[4*j+3] = (dstgroup & 0XFF000000) >> 24; atadev_put_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); } }
//copy cluster unsigned char CopyGroup(unsigned int srcgroup, unsigned int dstgroup) { unsigned int i=0,j=0, srcsector=0, dstsector=0; unsigned char flag=0;
for(i=0; i<CPartition.bpb.SectorPerGroup; i++) { srcsector = i + (srcgroup-2)*CPartition.bpb.SectorPerGroup + CPartition.DATABeginSector; dstsector = i + (dstgroup-2)*CPartition.bpb.SectorPerGroup + CPartition.DATABeginSector; flag=1; while(flag) { atadev_get_block_quick(srcsector, 1, uRcvBuffer); atadev_put_block_quick(dstsector, 1, uRcvBuffer); //translate uRcvBuffer[256] to uRcvBuffer[512] for (j = 0; j < 256; j++) { temp[2*j] = uRcvBuffer[j] & 0x000000FF; temp[2*j+1] = (uRcvBuffer[j] & 0x0000FF00) >> 8; } for (j = 0; j < 512; j++) uRcvBuffer[j] = temp[j]; flag = 0; atadev_get_block_quick(dstsector, 1, uRcvBuffer1); for(j = 0; j < 512; j++) { if(uRcvBuffer[j]!=uRcvBuffer1[j]) { flag = 1; printf("\n\r Copy sector error, now repeating!"); } } } } return 1; }
//find new unused cluster unsigned int newgrp() { unsigned int i=0, j=0; unsigned int temp; static unsigned int ClusNumb = 0;
for(i=0; i<CPartition.bpb.SectorPerFAT ; i++) { atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); for(j=0; j<512/4; j++)//4 bytes(32 bits) describe 1 cluster number(occupies 1 cote) { temp = uRcvBuffer[4*j+0] | (uRcvBuffer[4*j+1]<<8) | (uRcvBuffer[4*j+2]<<16) | (uRcvBuffer[4*j+3]<<24); if( (temp == 0x00000000) && ((i!=0) || (j >= 4)) )//the foremost 2 cotes(8 bytes) of the FAT1 are reserved for the identification { //the following 2 cotes are reserved for the DFT ClusNumb = j + i * 512 / 4; return ClusNumb; } } } return 0; }
//write file register item unsigned char WriteDFT() { unsigned int i=0, j=0, k=0; for(i=0; i< 2 * CPartition.bpb.SectorPerGroup ; i++) { atadev_get_block_quick(CPartition.DATABeginSector + i, 1, uRcvBuffer); for(j=0; j<16; j++) { InitFile(j); if(OperatorFile.Attrib != 32) { for(k=0; k<8; k++) uRcvBuffer[32*j+k] = mycopyfile.FileName[k]; for(k=0; k<3; k++) uRcvBuffer[32*j+k+8] = mycopyfile.FileExt[k];
uRcvBuffer[32*j+0x0b] = mycopyfile.Attrib; uRcvBuffer[32*j+0x16] = mycopyfile.LastTime ; uRcvBuffer[32*j+0x17] = mycopyfile.LastTime>>8; uRcvBuffer[32*j+0x18] = mycopyfile.LastDate; uRcvBuffer[32*j+0x19] = mycopyfile.LastDate>>8; uRcvBuffer[32*j+0x1a] = mycopyfile.BeginGroup; uRcvBuffer[32*j+0x1b] = mycopyfile.BeginGroup>>8; uRcvBuffer[32*j+0x1c] = mycopyfile.FileLength; uRcvBuffer[32*j+0x1d] = mycopyfile.FileLength>>8; uRcvBuffer[32*j+0x1e] = mycopyfile.FileLength>>16; uRcvBuffer[32*j+0x1f] = mycopyfile.FileLength>>24; atadev_put_block_quick(CPartition.DATABeginSector + i, 1, uRcvBuffer); return 1; } } } return 0; }
//delete DFB of file(the data of the file are still in the DATA section) unsigned char DelFile(unsigned int srcgroup) { unsigned int i=0, j=0, k=0;
for(i=0; i< 2 * CPartition.bpb.SectorPerGroup ; i++) { atadev_get_block_quick(CPartition.DATABeginSector + i, 1, uRcvBuffer); for(j=0; j<16; j++) { InitFile(j); if(OperatorFile.BeginGroup == srcgroup) { printf("\n\r Found the file, cleaning it now!"); for(k=0; k<32; k++) uRcvBuffer[32*j+k] = 0; atadev_put_block_quick(CPartition.DATABeginSector + i, 1, uRcvBuffer); } } } return 1; } unsigned char NewFile() { unsigned int i=0; //set the DFB parameters mynewfile.FileName[0] = 'T'; mynewfile.FileName[1] = 'E'; mynewfile.FileName[2] = 'S'; mynewfile.FileName[3] = 'T'; mynewfile.FileName[4] = ' '; mynewfile.FileName[5] = ' '; mynewfile.FileName[6] = ' '; mynewfile.FileName[7] = ' '; mynewfile.FileExt[0] = 'T'; mynewfile.FileExt[1] = 'X'; mynewfile.FileExt[2] = 'T'; mynewfile.Attrib = ATTRIB; mynewfile.LastTime = (NEWHOUR << 11) | (NEWMINUTE << 5) | (NEWSECOND / 2); mynewfile.LastDate = ((NEWYEAR-1980) << 9) | (NEWMONTH << 5) | NEWDAY; mynewfile.BeginGroup = NEWBEGINGRP; mynewfile.FileLength = NEWLENGTH; //write the content of the file to be created NewFileBuf[0] = 'T'; NewFileBuf[1] = 'E'; NewFileBuf[2] = 'S'; NewFileBuf[3] = 'T'; NewFileBuf[4] = 'i'; NewFileBuf[5] = 'n'; NewFileBuf[6] = 'g'; NewFileBuf[7] = ':';
for(i = 8; i < 512; i++) NewFileBuf[i] = '.'; NewFileBuf[512] = 't'; NewFileBuf[513] = 's'; NewFileBuf[514] = 't'; for(i = 515; i < 2*512; i++) NewFileBuf[i] = '.'; NewFileBuf[2*512] ='t'; NewFileBuf[2*512+1] ='s'; NewFileBuf[2*512+2] ='t'; for(i = 2*512+3; i < 3*512; i++) NewFileBuf[i] = '.'; NewFileBuf[3*512] = 't'; NewFileBuf[3*512+1] = 's'; NewFileBuf[3*512+2] = 't'; for(i = 3*512+3; i < 4*512; i++) NewFileBuf[i] = '.'; NewFileBuf[4*512] = 't'; NewFileBuf[4*512+1] = 's'; NewFileBuf[4*512+2] = 't'; for(i = 4*512+3; i < 5*512; i++) NewFileBuf[i] = '.'; NewFileBuf[5*512] = 't'; NewFileBuf[5*512+1] = 's'; NewFileBuf[5*512+2] = 't'; for(i = 5*512+3; i < 6*512; i++) NewFileBuf[i] = '.'; NewFileBuf[6*512] = 't'; NewFileBuf[6*512+1] = 's'; NewFileBuf[6*512+2] = 't'; for(i = 6*512+3; i < 7*512; i++) NewFileBuf[i] = '.'; NewFileBuf[7*512] = 't'; NewFileBuf[7*512+1] = 's'; NewFileBuf[7*512+2] = 't'; for(i = 7*512+3; i < 720*625*2-3; i++) NewFileBuf[i] = ':'; NewFileBuf[720*625*2-3] = 'E'; NewFileBuf[720*625*2-2] = 'N'; NewFileBuf[720*625*2-1] = 'D'; WriteFile();
return 1; }
unsigned char WriteFile() { unsigned int i=0, j=0, k=0; unsigned int dstgroup=0; unsigned int ClusNums = 0; //(groupnum+1):count of the cluster the new file occupies //(FileLength-1):the reason -1 is to prevent the FileLength is just the integral multiple of 1 cluster but occupies one more cluster groupnum = (mynewfile.FileLength - 1)/ CPartition.bpb.BytePerSector / CPartition.bpb.SectorPerGroup; ClusNums = groupnum; OperatorFile = mynewfile; ViewFile(); //write the data of the file dstgroup = mynewfile.BeginGroup; while(1) { for(k=0;k<ClusNums;k++) { //the new created data is smaller than 1 cluster if(groupnum == 0) { WriteGroup(dstgroup,ClusNums); //modify destination cluster i = dstgroup * 4 / 512; j = dstgroup - i * 512 / 4; atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); uRcvBuffer[4*j+0] = 0xFF; uRcvBuffer[4*j+1] = 0xFF; uRcvBuffer[4*j+2] = 0xFF; uRcvBuffer[4*j+3] = 0x0F; atadev_put_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer);
//write file register item if(!WriteNewDFT()) { printf("\n\r ERROR: Write DFT failed!"); return 0; } return 1; }
//the new created data is larger than 1 cluster groupnum = groupnum - 1; WriteWholeGroup(dstgroup,k); //write the next destination cluster number to the previous destination cluster in FAT1 i = dstgroup * 4 / 512; j = dstgroup - i * 512 / 4; //first write something,prevent find new cluster is this cluster again atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); uRcvBuffer[4*j+0] = 0x55; atadev_put_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); //next new cluster,content is the content of the next object cluster dstgroup = newgrp(); if(dstgroup == 0) { printf("\n\r Can not find free space!"); return 0; } //modify destination cluster in FAT1 atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); uRcvBuffer[4*j+0] = dstgroup & 0X000000FF; uRcvBuffer[4*j+1] = (dstgroup & 0X0000FF00) >> 8; uRcvBuffer[4*j+2] = (dstgroup & 0X00FF0000) >> 16; uRcvBuffer[4*j+3] = (dstgroup & 0XFF000000) >> 24; atadev_put_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); } } }
//write the register item of the new created file unsigned char WriteNewDFT() { unsigned int i=0, j=0, k=0; for(i=0; i< 2 * CPartition.bpb.SectorPerGroup ; i++) { atadev_get_block_quick(CPartition.DATABeginSector + i, 1, uRcvBuffer); for(j=0; j<16; j++) { InitFile(j); if(OperatorFile.Attrib != 32) { for(k=0; k<8; k++) uRcvBuffer[32*j+k] = mynewfile.FileName[k]; for(k=0; k<3; k++) uRcvBuffer[32*j+k+8] = mynewfile.FileExt[k];
uRcvBuffer[32*j+0x0b] = mynewfile.Attrib; uRcvBuffer[32*j+0x16] = mynewfile.LastTime ; uRcvBuffer[32*j+0x17] = mynewfile.LastTime>>8; uRcvBuffer[32*j+0x18] = mynewfile.LastDate; uRcvBuffer[32*j+0x19] = mynewfile.LastDate>>8; uRcvBuffer[32*j+0x1a] = mynewfile.BeginGroup; uRcvBuffer[32*j+0x1b] = mynewfile.BeginGroup>>8; uRcvBuffer[32*j+0x1c] = mynewfile.FileLength; uRcvBuffer[32*j+0x1d] = mynewfile.FileLength>>8; uRcvBuffer[32*j+0x1e] = mynewfile.FileLength>>16; uRcvBuffer[32*j+0x1f] = mynewfile.FileLength>>24; atadev_put_block_quick(CPartition.DATABeginSector + i, 1, uRcvBuffer); return 1; } } } return 0; }
//write the cluster content of the new created file (the content does not occupy the whole cluster) unsigned char WriteGroup(unsigned int dstgroup,unsigned int ClusNums) { unsigned int i=0,j=0,t=0,m=0,l=0,k=0,dstsector=0; unsigned char flag=0; t = (NEWLENGTH - ClusNums * CPartition.bpb.SectorPerGroup * CPartition.bpb.BytePerSector) / CPartition.bpb.BytePerSector; m = (NEWLENGTH - ClusNums * CPartition.bpb.SectorPerGroup * CPartition.bpb.BytePerSector) - CPartition.bpb.BytePerSector * t; for(i=0; i<CPartition.bpb.SectorPerGroup; i++) { dstsector = i + (dstgroup-2)*CPartition.bpb.SectorPerGroup + CPartition.DATABeginSector; flag=1; while(flag) { for(l = 0; l < t; l++) { for(k = 0; k < 512; k++) uRcvBuffer[k] = NewFileBuf[k + 512 * l + ClusNums * CPartition.bpb.SectorPerGroup * CPartition.bpb.BytePerSector]; atadev_put_block_quick(dstsector + l, 1, uRcvBuffer); //translate uRcvBuffer[256] to uRcvBuffer[512] for (j = 0; j < 256; j++) { temp[2*j] = uRcvBuffer[j] & 0x000000FF; temp[2*j+1] = (uRcvBuffer[j] & 0x0000FF00) >> 8; } for (j = 0;j < 512; j++) uRcvBuffer[j]=temp[j]; flag = 0; atadev_get_block_quick(dstsector + l, 1, uRcvBuffer1); for(j=0; j<512; j++) { if(uRcvBuffer[j]!=uRcvBuffer1[j]) { flag = 1; printf("\n\r Write sector error, now repeating!"); } } } for(k = 0; k < m; k++) uRcvBuffer[k] = NewFileBuf[k + 512*t + ClusNums * CPartition.bpb.SectorPerGroup * CPartition.bpb.BytePerSector]; for(k = m; k < 512; k++) uRcvBuffer[k]=' '; atadev_put_block_quick(dstsector + t, 1, uRcvBuffer); //translate uRcvBuffer[256] to uRcvBuffer[512] for (j = 0; j < 256; j++) { temp[2*j] = uRcvBuffer[j] & 0x000000FF; temp[2*j+1] = (uRcvBuffer[j] & 0x0000FF00) >> 8; } for (j = 0;j < 512; j++) uRcvBuffer[j]=temp[j]; flag = 0; atadev_get_block_quick(dstsector + t, 1, uRcvBuffer1); for(j=0; j<512; j++) { if(uRcvBuffer[j]!=uRcvBuffer1[j]) { flag = 1; printf("\n\r Write sector error, now repeating!"); } } } } return 1; }
//write the cluster content of the new created file (the content occupies the whole cluster) unsigned char WriteWholeGroup(unsigned int dstgroup,unsigned int ClusNums) { unsigned int i=0,j=0,dstsector=0; unsigned char flag=0;
for(i=0; i<CPartition.bpb.SectorPerGroup; i++) { dstsector = i + (dstgroup-2)*CPartition.bpb.SectorPerGroup + CPartition.DATABeginSector; flag = 1; while(flag) { for(j = 0; j < 512; j++) uRcvBuffer[j] = NewFileBuf[j+512*i+ ClusNums * CPartition.bpb.SectorPerGroup * CPartition.bpb.BytePerSector]; atadev_put_block_quick(dstsector, 1, uRcvBuffer); //translate uRcvBuffer[256] to uRcvBuffer[512] for (j = 0; j < 256; j++) { temp[2*j] = uRcvBuffer[j] & 0x000000FF; temp[2*j+1] = (uRcvBuffer[j] & 0x0000FF00) >> 8; } for (j = 0;j < 512; j++) uRcvBuffer[j]=temp[j]; flag = 0; atadev_get_block_quick(dstsector, 1, uRcvBuffer1); for(j = 0; j < 512; j++) { if(uRcvBuffer[j] != uRcvBuffer1[j]) { flag = 1; printf("\n\r Write sector error, now repeating!"); } } } } return 1; }
//just can add data to the new created file unsigned char AddToFile() { unsigned int i=0,j=0,k=0,m=0; unsigned int dstgroup=0; unsigned int ClusNums = 0; unsigned int DstClusNumb = 0; unsigned int ModifyIndex = 0; //set the DFB parameters(name and BeginGroup should be same as the new created file's) myaddfile.FileName[0] = 'T'; myaddfile.FileName[1] = 'E'; myaddfile.FileName[2] = 'S'; myaddfile.FileName[3] = 'T'; myaddfile.FileName[4] = ' '; myaddfile.FileName[5] = ' '; myaddfile.FileName[6] = ' '; myaddfile.FileName[7] = ' '; myaddfile.FileExt[0] = 'T'; myaddfile.FileExt[1] = 'X'; myaddfile.FileExt[2] = 'T'; myaddfile.Attrib = ATTRIB; myaddfile.LastTime = (ADDHOUR << 11) | (ADDMINUTE << 5) | (ADDSECOND / 2); myaddfile.LastDate = ((ADDYEAR-1980) << 9) | (ADDMONTH << 5) | ADDDAY; myaddfile.BeginGroup = NEWBEGINGRP; myaddfile.FileLength = ADDLENGTH; //write the content of the file to be added AddFileBuf[0] = '\n'; AddFileBuf[1] = 'A'; AddFileBuf[2] = 'D'; AddFileBuf[3] = 'D'; AddFileBuf[4] = 'i'; AddFileBuf[5] = 'n'; AddFileBuf[6] = 'g'; AddFileBuf[7] = ':'; for(i = 8; i < 512; i++) AddFileBuf[i] = '.'; AddFileBuf[512] = 'a'; AddFileBuf[513] = 'd'; AddFileBuf[514] = 'd'; for(i = 515; i < 2*512; i++) AddFileBuf[i] = '.'; AddFileBuf[2*512] ='a'; AddFileBuf[2*512+1] ='d'; AddFileBuf[2*512+2] ='d'; for(i = 2*512+3; i < 1437; i++) AddFileBuf[i] = '.'; AddFileBuf[1437] = 'a'; AddFileBuf[1438] = 'd'; AddFileBuf[1439] = 'd'; for(i = 1440; i<4096; i++) AddFileBuf[i] = '.'; AddFileBuf[4096] = 'E'; AddFileBuf[4097] = 'N'; AddFileBuf[4098] = 'D'; //(groupnum+1):count of the cluster the new file occupies //(FileLength-1):the reason -1 is to prevent the FileLength is just the integral multiple of 1 cluster but occupies one more cluster groupnum = (myaddfile.FileLength - 1) / CPartition.bpb.BytePerSector / CPartition.bpb.SectorPerGroup; ClusNums = groupnum; //add the data to the file dstgroup = newgrp(); DstClusNumb = dstgroup; while(1) { for(m = 0; m < ClusNums; m++) { //the new added data is smaller than 1 cluster if(groupnum == 0) { AddGroup(dstgroup,ClusNums); //modify destination cluster i = dstgroup * 4 / 512; j = dstgroup - i * 512 / 4; atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); uRcvBuffer[4*j+0] = 0xFF; uRcvBuffer[4*j+1] = 0xFF; uRcvBuffer[4*j+2] = 0xFF; uRcvBuffer[4*j+3] = 0x0F; atadev_put_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer);
//modify the cluster number to point to the first cluster number of the added file ModifyIndex = NEWBEGINGRP + (NEWLENGTH-1)/CPartition.bpb.BytePerSector/CPartition.bpb.SectorPerGroup; i = ModifyIndex * 4 / 512; j = ModifyIndex - i * 512 / 4; atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); uRcvBuffer[4*j+0] = DstClusNumb & 0x000000FF; uRcvBuffer[4*j+1] = (DstClusNumb & 0x0000FF00) >> 8; uRcvBuffer[4*j+2] = (DstClusNumb & 0x00FF0000) >> 16; uRcvBuffer[4*j+3] = (DstClusNumb & 0xFF000000) >> 24; atadev_put_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); //write file register item if(!WriteAddDFT()) { printf("\n\r ERROR: Write DFT failed!"); return 0; } return 1; }
//the new created data is larger than 1 cluster groupnum = groupnum - 1; AddWholeGroup(dstgroup,m); //write the next destination cluster number to the previous destination cluster in FAT1 i = dstgroup * 4 / 512; j = dstgroup - i * 512 / 4; //first write something,prevent find new cluster is this cluster again atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); uRcvBuffer[4*j+0] = 0x55; atadev_put_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); //next new cluster,content is the content of the next object cluster dstgroup = newgrp(); if(dstgroup == 0) { printf("\n\r Can not find free space!"); return 0; } //modify destination cluster in FAT1 atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); uRcvBuffer[4*j+0] = DstClusNumb & 0x000000FF; uRcvBuffer[4*j+1] = (DstClusNumb & 0x0000FF00) >> 8; uRcvBuffer[4*j+2] = (DstClusNumb & 0x00FF0000) >> 16; uRcvBuffer[4*j+3] = (DstClusNumb & 0xFF000000) >> 24; atadev_put_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer); } } }
//add the cluster content of the new created file (the content does not occupy the whole cluster) unsigned char AddGroup(unsigned int dstgroup,unsigned int ClusNums) { unsigned int i=0,j=0,t=0,m=0,l=0,k=0,dstsector=0; unsigned char flag=0; t = (ADDLENGTH - ClusNums * CPartition.bpb.SectorPerGroup * CPartition.bpb.BytePerSector) / CPartition.bpb.BytePerSector; m = (ADDLENGTH - ClusNums * CPartition.bpb.SectorPerGroup * CPartition.bpb.BytePerSector) - CPartition.bpb.BytePerSector * t; for(i=0; i<CPartition.bpb.SectorPerGroup; i++) { dstsector = i + (dstgroup-2)*CPartition.bpb.SectorPerGroup + CPartition.DATABeginSector; flag=1; while(flag) { for(l = 0; l < t; l++) { for(k = 0; k < 512; k++) uRcvBuffer[k] = AddFileBuf[k + 512 * l + ClusNums * CPartition.bpb.SectorPerGroup * CPartition.bpb.BytePerSector]; atadev_put_block(dstsector + l, 1, uRcvBuffer); //translate uRcvBuffer[256] to uRcvBuffer[512] for (j = 0; j < 256; j++) { temp[2*j] = uRcvBuffer[j] & 0x000000FF; temp[2*j+1] = (uRcvBuffer[j] & 0x0000FF00) >> 8; } for (j = 0;j < 512; j++) uRcvBuffer[j]=temp[j]; flag = 0; atadev_get_block(dstsector + l, 1, uRcvBuffer1); for(j=0; j<512; j++) { if(uRcvBuffer[j]!=uRcvBuffer1[j]) { flag = 1; printf("\n\r Write sector error, now repeating!"); } } } for(k = 0; k < m; k++) uRcvBuffer[k] = AddFileBuf[k + 512*t + ClusNums * CPartition.bpb.SectorPerGroup * CPartition.bpb.BytePerSector]; for(k = m; k < 512; k++) uRcvBuffer[k]=' '; atadev_put_block(dstsector + t, 1, uRcvBuffer); //translate uRcvBuffer[256] to uRcvBuffer[512] for (j = 0; j < 256; j++) { temp[2*j] = uRcvBuffer[j] & 0x000000FF; temp[2*j+1] = (uRcvBuffer[j] & 0x0000FF00) >> 8; } for (j = 0;j < 512; j++) uRcvBuffer[j]=temp[j]; flag = 0; atadev_get_block(dstsector + t, 1, uRcvBuffer1); for(j=0; j<512; j++) { if(uRcvBuffer[j]!=uRcvBuffer1[j]) { flag = 1; printf("\n\r Write sector error, now repeating!"); } } } } return 1; }
//add the cluster content of the new created file (the content occupies the whole cluster) unsigned char AddWholeGroup(unsigned int dstgroup,unsigned int ClusNums) { unsigned int i=0,j=0,dstsector=0; unsigned char flag=0;
for(i=0; i<CPartition.bpb.SectorPerGroup; i++) { dstsector = i + (dstgroup-2)*CPartition.bpb.SectorPerGroup + CPartition.DATABeginSector; flag = 1; while(flag) { for(j = 0; j < 512; j++) uRcvBuffer[j] = AddFileBuf[j+512*i + ClusNums * CPartition.bpb.SectorPerGroup * CPartition.bpb.BytePerSector]; atadev_put_block(dstsector, 1, uRcvBuffer); //translate uRcvBuffer[256] to uRcvBuffer[512] for (j = 0; j < 256; j++) { temp[2*j] = uRcvBuffer[j] & 0x000000FF; temp[2*j+1] = (uRcvBuffer[j] & 0x0000FF00) >> 8; } for (j = 0;j < 512; j++) uRcvBuffer[j]=temp[j]; flag = 0; atadev_get_block(dstsector, 1, uRcvBuffer1); for(j = 0; j < 512; j++) { if(uRcvBuffer[j] != uRcvBuffer1[j]) { flag = 1; printf("\n\r Write sector error, now repeating!"); } } } } return 1; }
unsigned char WriteAddDFT() { unsigned int i=0, j=0, k=0, AddLength; //(NEWLENGTH-1):the reason -1 is to prevent the NEWLENGTH is just the integral multiple of 1 cluster but occupies one more cluster AddLength = ((NEWLENGTH - 1) / CPartition.bpb.BytePerSector / CPartition.bpb.SectorPerGroup + 1) * CPartition.bpb.SectorPerGroup * CPartition.bpb.BytePerSector + ADDLENGTH; for(i = 0; i< 2 * CPartition.bpb.SectorPerGroup ; i++) { atadev_get_block(CPartition.DATABeginSector + i, 1, uRcvBuffer); for(j=0; j<16; j++) { InitFile(j); if(OperatorFile.BeginGroup == NEWBEGINGRP) { for(k=0; k<8; k++) uRcvBuffer[32*j+k] = myaddfile.FileName[k]; for(k=0; k<3; k++) uRcvBuffer[32*j+k+8] = myaddfile.FileExt[k];
uRcvBuffer[32*j+0x0b] = ATTRIB; uRcvBuffer[32*j+0x16] = myaddfile.LastTime & 0X000000FF; uRcvBuffer[32*j+0x17] = (myaddfile.LastTime & 0X0000FF00) >> 8; uRcvBuffer[32*j+0x18] = myaddfile.LastDate & 0X000000FF; uRcvBuffer[32*j+0x19] = (myaddfile.LastDate & 0X0000FF00) >>8; uRcvBuffer[32*j+0x1a] = NEWBEGINGRP & 0X000000FF; uRcvBuffer[32*j+0x1b] = (NEWBEGINGRP & 0X0000FF00) >>8; uRcvBuffer[32*j+0x1c] = AddLength & 0X000000FF; uRcvBuffer[32*j+0x1d] = (AddLength & 0X0000FF00) >>8; uRcvBuffer[32*j+0x1e] = (AddLength & 0X00FF0000) >>16; uRcvBuffer[32*j+0x1f] = (AddLength & 0XFF000000) >>24; atadev_put_block(CPartition.DATABeginSector + i, 1, uRcvBuffer); return 1; } } } return 0; }
|