| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- //-------------------------------------------------------------------------------
- #include "generateOutput.h"
- //-------------------------------------------------------------------------------
- #include "generateSave.h"
- #include "generateBin.h"
- #include "parseConfig.h"
- #include "loadBasicPacket.h"
- //-------------------------------------------------------------------------------
- #include <dirent.h>
- #include<sys/types.h>
- #include<sys/stat.h>
- //-------------------------------------------------------------------------------
- extern bool generateSaveInfo(void);
- extern bool genTempBinFiles();
- extern bool MergeBins2Mem(void);
- //-------------------------------------------------------------------------------
- void removeTempFile( const char *fileName )
- {
- if( (fileName == NULL) || (!strlen(fileName)) ) return;
- if( miDeleteTempFileFlag ) remove( fileName );
- }
- //---------------------------------------------------------------------------
- static inline bool checkOutputIsLocalFolder()
- {
- if( strlen(miOutputPathName) == 0 )
- {
- strcpy( miOutputPathName, PATHNAME_OUTPUT );
- errMsg( "miOutputPathName = NULL!\n");
- return true;
- }
- if( strlen(miOutputPathName) == 1 )
- {
- if( miOutputPathName[0] == '.' ) return true;
- }
- return false;
- }
- static inline void deleteContextOfOutputFolder()
- {
- char tempName[FILENAME_SIZE];
- DIR *dir = opendir( miOutputPathName );
- if( dir == NULL ) return;
-
- struct dirent * ptr;
- while( (ptr = readdir(dir)) != NULL )
- {
- if( ptr->d_name[0] == '.' ) continue;
- snprintf( tempName, sizeof(tempName), "%s/%s", miOutputPathName, ptr->d_name );
- remove( tempName );
- }
- closedir(dir);
- }
- static inline bool chmodOutputFolder()
- {
- DIR *dir;
- char tempName[FILENAME_SIZE];
- if( checkOutputIsLocalFolder() ) return true;
-
- dir = opendir( miOutputPathName );
- if( dir == NULL ) return false;
- struct dirent * ptr;
- while( (ptr = readdir(dir)) != NULL )
- {
- if( ptr->d_name[0] == '.' ) continue;
- snprintf( tempName, sizeof(tempName), "%s/%s", miOutputPathName, ptr->d_name );
- chmod( tempName, 0777 );
- }
- closedir(dir);
- return true;
- }
- static inline void createOutputFolder()
- {
- if( checkOutputIsLocalFolder() ) return;
- deleteContextOfOutputFolder();
- mkdir( miOutputPathName, 0777 );
- chmod( miOutputPathName, 0777 );
- }
- bool copyToOutputFolder( const char *targetName, const char *srcName )
- {
- char saveFullyName[FILENAME_SIZE];
- snprintf( saveFullyName, sizeof(saveFullyName), "%s/%s", miOutputPathName, targetName );
- dbgMsg( "[copyToOutputFolder] %s\n", saveFullyName );
- return copyFile( saveFullyName, srcName, 0 );
- }
- //-------------------------------------------------------------------------------
- void stuff_File_with_zero(FILE *fpIn, u32 size)
- {
- int i =0, buffer = 0;
- for(i=0; i < size; i++)
- fwrite( &buffer, 1, 1, fpIn );
-
- }
- bool copyFileContent( FILE *fpOut, const char *sourceFileName, u32 cpySize )
- {
- FILE *fpIn;
- int i, readSize, fileSize;
- u8 readBuf[TMP_BUFFER_SIZE] = {0};
- fileSize = getFileSize( sourceFileName );
- if( fileSize == 0 )
- {
- errMsg( "FileSize (%s) is zero!\n", sourceFileName );
- return false;
- }
-
- if( (cpySize != 0) && ( cpySize <= fileSize ) ) fileSize = cpySize;
- fpIn = fopen( sourceFileName, "rb" );
- if( fpIn == NULL )
- {
- errMsg( "File (%s) is not exist\n", sourceFileName );
- return false;
- }
- for( i = 0; i < fileSize; i+= TMP_BUFFER_SIZE )
- {
- readSize = fread( readBuf, 1, TMP_BUFFER_SIZE, fpIn );
- if( readSize < 0 )
- {
- errMsg( "fread (%s)\n", sourceFileName );
- return false;
- }
- fwrite( readBuf, 1, readSize, fpOut );
- }
- fclose( fpIn );
- return true;
- }
- bool copyFile( const char *targetName, const char *sourceName, u32 cpySize )
- {
- bool ret;
- FILE *fpOut = fopen( targetName, "wb" );
- if( fpOut == NULL )
- {
- errMsg( "Create writable file failed (%s)\n", targetName );
- return false;
- }
-
- ret = copyFileContent( fpOut, sourceName, cpySize );
- fclose( fpOut );
- //dbgMsg( "copyFile %d\n", ret );
- return ret;
- }
- //-------------------------------------------------------------------------------
- #if 0
- static inline bool stripFlashBin()
- {
- bool ret;
- u32 codeValidSize, dataValidSize;
- dbgMsg( "\nstripFlashBin ==>\n" );
-
- ret = calculateImageValidSize( &codeValidSize, &dataValidSize );
- dbgMsg( "FlashValidSize: Code=%dKB, Data=%dKB\n", codeValidSize, dataValidSize );
- codeValidSize *= 1024;
- dataValidSize *= 1024;
-
- if( ret ) ret = copyFile( FILENAME_CODEVALID, FILENAME_CODEBIN, codeValidSize );
- if( ret && dataValidSize ) ret = copyFile( FILENAME_DATAVALID, FILENAME_DATABIN, dataValidSize );
- #if 0
- removeTempFile( FILENAME_CODEBIN );
- removeTempFile( FILENAME_DATABIN );
- #endif
- return ret;
- }
- #endif
- //-------------------------------------------------------------------------------
- static inline void clearTemporaryFlies()
- {
- //removeTempFile( FILENAME_CODEVALID );
- //removeTempFile( FILENAME_DATAVALID );
- removeTempFile( FILENAME_PACKETSAVE );
- //removeTempFile( FILENAME_PACKETIMAGE );
- removeTempFile( FILENAME_CODEBIN );
- //removeTempFile( FILENAME_BINWITHINFO );
- //if( isLoadBasicPacket() ) freeBasicPacket();
- #if MI_COPY_MERGE_SOURCE
- //removeTempFile(flashModuleInfo[MODULE_BOOTROM].tmpBinName);
- //removeTempFile(flashModuleInfo[MODULE_AUDIOROM].tmpBinName);
- #endif
- }
- #if 0
- static inline bool generatePacketBin( const char *targetName )
- {
- bool ret = true;
- dbgMsg( "\ngeneratePacketBin ==>\n" );
- FILE *fpOut = fopen( FILENAME_PACKETIMAGE, "wb" );
- if( fpOut == NULL ) return false;
- if( ret ) ret = copyFileContent( fpOut, FILENAME_PACKETSAVE, 0 );
- fseek( fpOut, SAVEINFO_SIZE, SEEK_SET );
-
- if( ret ) ret = copyFileContent( fpOut, FILENAME_CODEVALID, 0 );
- #if( CONFIG_DATA_FLASH_SIZE != 0 )
- if( ret ) ret = copyFileContent( fpOut, FILENAME_DATAVALID, 0 );
- #endif
-
- fclose( fpOut );
- copyToOutputFolder( targetName, FILENAME_PACKETIMAGE );
-
- return ret;
- }
- #endif
- static inline bool generateBinwithInfo( const char *targetName )
- {
- bool ret = true;
- int _SaveFile_size;
- dbgMsg( "\ngenerateBinwithInfo ==>\n" );
- FILE *fpOut = fopen( targetName, "wb" );
- if( fpOut == NULL ) return false;
- if( ret ) ret = copyFileContent( fpOut, FILENAME_CODEBIN, 0 );
- //size will be KB alignment,
- if( ret ) ret = copyFileContent( fpOut, FILENAME_PACKETSAVE, 0 );
- //get _SaveFile_size (4 byte alignment)
- _SaveFile_size = ROUND_UP(getFileSize(FILENAME_PACKETSAVE),4)*4;
- stuff_File_with_zero(fpOut, _SaveFile_size- getFileSize(FILENAME_PACKETSAVE));
- //write .save size to last word
- _SaveFile_size = _SaveFile_size+sizeof(_SaveFile_size); //add _SaveFile_size itself
- fwrite( &_SaveFile_size, sizeof(_SaveFile_size), 1, fpOut );
- fclose( fpOut );
- //copyToOutputFolder( targetName, FILENAME_BINWITHINFO );
-
- return ret;
- }
- bool mergeImageMain()
- {
- bool ret = true;
- createOutputFolder();
- //transfer ini to bins
- if( ret ) ret = genTempBinFiles();
- #if 0
- if( ret ) ret = generateFlashBin();
- #else
- if( ret ) ret = MergeBins2Mem(); //just a test
- #endif
- #if 0
- if( miGenerateOriginalBinFlag && ret ) ret = generateBinSaveInfo();
- #endif
- if( miGeneratePacketFlag )
- {
- // if( ret ) ret = stripFlashBin();
- if( ret ) ret = generateSaveInfo();
- //if( ret ) ret = generatePacketBin( miPacketName );
- //append .save to bin file end
- if( ret ) ret = generateBinwithInfo( miBinWithInfo );
- }
- clearTemporaryFlies();
- //chmodOutputFolder();
- return ret;
- }
|