Example #1
0
	FileSystem(const char* volume, uint32_t size_):
		volume_name(volume){

		int32 initialize_result = 0;
		int8_t volume_name_found = 0;

		size = size_;

		//Find offset from GlobalFilesystemHandler:
		//(uint32_t)&eeprom_buffer +
		name_offset = GlobalFileSystemHandler::get_instance().add_new_filesystem(this, size);
		header_offset = name_offset + EEFS_MAX_DEVICENAME_SIZE;

		//Check if the given volume name is found at that offset:
		if(!volume_exist(volume, name_offset)){
			//No volume by that name exist at the given offset.
			//Are we allowed to create a new filesystem?
			bool generate_new_filesystem = GlobalFileSystemHandler::get_instance().get_generate_new_filesystem_flag();
			if(!generate_new_filesystem){
				//No we are not. Then we'll have to call the corrupted filesystem hook:
				CORRUPTED_FILESYSTEM_HOOK;

				//Now it's OK to create the new filesystem!
				GlobalFileSystemHandler::get_instance().set_generate_new_filesystem_flag(1);
			}

			Debug.put("\n\rCreating new volume: ");
			Debug.put(volume);
			create_filesystem(volume, name_offset, size);
		}

		//Initialize filesystem:
		initialize_result = EEFS_InitFS(const_cast<char*>(volume_name), header_offset);

		//If the filesystem doesn't exist for some reason.
		if(initialize_result == EEFS_NO_SUCH_DEVICE){
				//Something is very wrong!!
				while(1);
		}

		eefs_device = EEFS_GetDevicePtr(initialize_result);
		initialize_result = EEFS_Mount(const_cast<char*>(volume_name), const_cast<char*>(volume_name));

		Debug.transmit();
	}
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;
}