int8_t create_file(char* filename, uint8_t* data, uint8_t size){ char full_filename[40]; strcpy(full_filename, volume_name); strcat(full_filename,"/"); strcat(full_filename, filename); int32_t descriptor = EEFS_Creat(full_filename, EEFS_ATTRIBUTE_NONE); EEFS_Write(descriptor, data, size); return EEFS_Close(descriptor); }
int8_t save(char* filename, uint8_t* buffer, uint8_t size){ char full_filename[40]; strcpy(full_filename, volume_name); strcat(full_filename,"/"); strcat(full_filename, filename); uint32_t descriptor = EEFS_Open(full_filename, O_RDWR); //Check if file even exists: if(descriptor < 0){ return descriptor; } EEFS_Write(descriptor, buffer, size); EEFS_Close(descriptor); }
int main (int argc, char **argv) { int32 Status; EEFS_DirectoryDescriptor_t *DirDescriptor = NULL; EEFS_DirectoryEntry_t *DirEntry = NULL; EEFS_Stat_t StatBuffer; int32 eefs_fd; int host_fd; int total_used_space; int total_free_space; /* ** Process the command line options */ SetCommandLineOptionsDefaults(&CommandLineOptions); ProcessCommandLineOptions(argc, argv, &CommandLineOptions); /* ** Open the device ( BDM or image file ) */ eefstool_open_device(); /* ** Init the EEFS */ /* ** Mount the EEPROM disk volumes */ Status = EEFS_InitFS("/EEDEV1", CommandLineOptions.EEFSMemoryAddress); if ( Status == 0 ) { Status = EEFS_Mount("/EEDEV1",eefs_mountpoint); if ( Status != 0 ) { printf("Error: Failed to mount EEPROM File System\n"); } } else { printf("Error: Failed to initialize EEPROM FS\n"); } /* ** Process the command */ if ( CommandLineOptions.DirectoryListingCommand == TRUE ) { /* ** Get a directory of the EEFS */ DirDescriptor = EEFS_OpenDir(eefs_mountpoint); if ( DirDescriptor != NULL ) { DirEntry = NULL; printf("--> EEFS Directory:\n"); printf("%32s %10s\n","Filename","Size"); printf("------------------------------------------------------\n"); while ( (DirEntry = EEFS_ReadDir(DirDescriptor)) != NULL ) { if ( DirEntry->InUse != 0 ) { printf("%32s %10d\n",DirEntry->Filename,(int)DirEntry->MaxFileSize); } } printf("------------------------------------------------------\n"); Status = EEFS_CloseDir(DirDescriptor); } } if ( CommandLineOptions.EepromUsageCommand == TRUE ) { /* ** Dump the EEPROM usage stats for the EEFS */ total_used_space = 0; total_free_space = 0; DirDescriptor = EEFS_OpenDir(eefs_mountpoint); if ( DirDescriptor != NULL ) { DirEntry = NULL; printf("--> EEFS Usage Stats:\n"); printf("%32s %10s %10s\n","Filename","Size", "Max Size"); printf("------------------------------------------------------\n"); while ( (DirEntry = EEFS_ReadDir(DirDescriptor)) != NULL ) { if ( DirEntry->InUse != 0 ) { strcpy(eefs_filename, eefs_mountpoint); strcat(eefs_filename,"/"); strcat(eefs_filename,DirEntry->Filename); Status = EEFS_Stat(eefs_filename, &StatBuffer); if ( Status == 0 ) { printf("%32s %10d %10d\n",DirEntry->Filename, (int)StatBuffer.FileSize, (int)DirEntry->MaxFileSize); total_used_space = total_used_space + StatBuffer.FileSize; total_free_space = total_free_space + ( DirEntry->MaxFileSize - StatBuffer.FileSize); } else { printf("Error: Cannot get Stat buffer for file: %s\n",eefs_filename); } } } printf("------------------------------------------------------\n"); printf("Total Used space = %d bytes.\n",total_used_space); printf("Total Free space = %d bytes.\n",total_free_space); printf("Total Space = %d bytes.\n",total_used_space + total_free_space); Status = EEFS_CloseDir(DirDescriptor); } } else if ( CommandLineOptions.CopyFromCommand == TRUE ) //读取EEPROM复制到文件系统中 { //打开一个文件只写 host_fd = open(CommandLineOptions.Filename2, O_CREAT | O_WRONLY, S_IRWXU ); if ( host_fd < 0 ) { printf("Error opening host file: %s\n",CommandLineOptions.Filename2); } else { strcpy(eefs_filename, eefs_mountpoint); strcat(eefs_filename,"/"); // strcat(eefs_filename,CommandLineOptions.Filename1); eefs_fd = EEFS_Open(eefs_filename, 0);//打开EEFS 文件系统 if ( eefs_fd < 0 ) { printf("Error opening EEFS file: %s\n",CommandLineOptions.Filename1); close(host_fd); } else { int32 DataRead; int DataWritten; boolean DataToRead; char buffer[512]; /* ** Copy the file */ printf("Copying: EEPROM File System: %s, to the host: %s\n", CommandLineOptions.Filename1, CommandLineOptions.Filename2); DataToRead = TRUE; while ( DataToRead == TRUE ) { DataRead = EEFS_Read(eefs_fd, &buffer[0], 512); //读取512字节到BUF中 if ( DataRead == 0 ) { DataToRead = FALSE; } else { DataWritten = write(host_fd, &buffer[0],DataRead); //将EEPROM内的数据写入文件中 if ( DataWritten != DataRead ) { printf("Warning: Amount of data written != Data Read\n"); } } } /* End while */ EEFS_Close(eefs_fd);//关闭EE文件系统 close(host_fd); printf("Copy completed\n"); } } } else if ( CommandLineOptions.CopyToCommand == TRUE ) //从文件系统复制到EEPROM中 { host_fd = open(CommandLineOptions.Filename1, O_RDONLY, S_IRWXU ); if ( host_fd < 0 ) { printf("Error opening host file: %s\n",CommandLineOptions.Filename1); } else { strcpy(eefs_filename, eefs_mountpoint); strcat(eefs_filename,"/"); strcat(eefs_filename,CommandLineOptions.Filename2); //源文件/命令行文件--格式 eefs_fd = EEFS_Creat(eefs_filename, 0); //创建一个名为文件eefs_filename变量包含的 if ( eefs_fd < 0 ) { printf("Error calling EEFS_Creat on EEFS file: %s\n",CommandLineOptions.Filename2); close(host_fd); } else { int32 DataRead; int DataWritten; boolean DataToRead; char buffer[512]; /* ** Copy the file */ printf("Copying: From the host file %s, to the EEPROM File System file: %s.\n", CommandLineOptions.Filename1, CommandLineOptions.Filename2); DataToRead = TRUE; while ( DataToRead == TRUE ) { DataRead = read(host_fd, &buffer[0], 512); if ( DataRead == 0 ) { DataToRead = FALSE; } else { DataWritten = EEFS_Write(eefs_fd, &buffer[0],DataRead); if ( DataWritten != DataRead ) { printf("Warning: Amount of data written != Data Read\n"); } } } /* End while */ EEFS_Close(eefs_fd); close(host_fd); printf("\nCopy completed\n"); } } } else if ( CommandLineOptions.DeleteCommand == TRUE ) { printf("Deleting %s from the EEPROM File system\n", CommandLineOptions.Filename1); printf("Done\n"); } else if ( CommandLineOptions.RenameCommand == TRUE ) { printf("Rename a file on the EEPROM file system from: %s, to %s\n", CommandLineOptions.Filename1, CommandLineOptions.Filename2); } /* ** Close the device ( BDM or image file ) */ eefstool_close_device(); return 0; }