generateOutput.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //-------------------------------------------------------------------------------
  2. #include "generateOutput.h"
  3. //-------------------------------------------------------------------------------
  4. #include "generateSave.h"
  5. #include "generateBin.h"
  6. #include "parseConfig.h"
  7. #include "loadBasicPacket.h"
  8. //-------------------------------------------------------------------------------
  9. #include <dirent.h>
  10. #include<sys/types.h>
  11. #include<sys/stat.h>
  12. //-------------------------------------------------------------------------------
  13. extern bool generateSaveInfo(void);
  14. extern bool genTempBinFiles();
  15. extern bool MergeBins2Mem(void);
  16. //-------------------------------------------------------------------------------
  17. void removeTempFile( const char *fileName )
  18. {
  19. if( (fileName == NULL) || (!strlen(fileName)) ) return;
  20. if( miDeleteTempFileFlag ) remove( fileName );
  21. }
  22. //---------------------------------------------------------------------------
  23. static inline bool checkOutputIsLocalFolder()
  24. {
  25. if( strlen(miOutputPathName) == 0 )
  26. {
  27. strcpy( miOutputPathName, PATHNAME_OUTPUT );
  28. errMsg( "miOutputPathName = NULL!\n");
  29. return true;
  30. }
  31. if( strlen(miOutputPathName) == 1 )
  32. {
  33. if( miOutputPathName[0] == '.' ) return true;
  34. }
  35. return false;
  36. }
  37. static inline void deleteContextOfOutputFolder()
  38. {
  39. char tempName[FILENAME_SIZE];
  40. DIR *dir = opendir( miOutputPathName );
  41. if( dir == NULL ) return;
  42. struct dirent * ptr;
  43. while( (ptr = readdir(dir)) != NULL )
  44. {
  45. if( ptr->d_name[0] == '.' ) continue;
  46. snprintf( tempName, sizeof(tempName), "%s/%s", miOutputPathName, ptr->d_name );
  47. remove( tempName );
  48. }
  49. closedir(dir);
  50. }
  51. static inline bool chmodOutputFolder()
  52. {
  53. DIR *dir;
  54. char tempName[FILENAME_SIZE];
  55. if( checkOutputIsLocalFolder() ) return true;
  56. dir = opendir( miOutputPathName );
  57. if( dir == NULL ) return false;
  58. struct dirent * ptr;
  59. while( (ptr = readdir(dir)) != NULL )
  60. {
  61. if( ptr->d_name[0] == '.' ) continue;
  62. snprintf( tempName, sizeof(tempName), "%s/%s", miOutputPathName, ptr->d_name );
  63. chmod( tempName, 0777 );
  64. }
  65. closedir(dir);
  66. return true;
  67. }
  68. static inline void createOutputFolder()
  69. {
  70. if( checkOutputIsLocalFolder() ) return;
  71. deleteContextOfOutputFolder();
  72. mkdir( miOutputPathName, 0777 );
  73. chmod( miOutputPathName, 0777 );
  74. }
  75. bool copyToOutputFolder( const char *targetName, const char *srcName )
  76. {
  77. char saveFullyName[FILENAME_SIZE];
  78. snprintf( saveFullyName, sizeof(saveFullyName), "%s/%s", miOutputPathName, targetName );
  79. dbgMsg( "[copyToOutputFolder] %s\n", saveFullyName );
  80. return copyFile( saveFullyName, srcName, 0 );
  81. }
  82. //-------------------------------------------------------------------------------
  83. void stuff_File_with_zero(FILE *fpIn, u32 size)
  84. {
  85. int i =0, buffer = 0;
  86. for(i=0; i < size; i++)
  87. fwrite( &buffer, 1, 1, fpIn );
  88. }
  89. bool copyFileContent( FILE *fpOut, const char *sourceFileName, u32 cpySize )
  90. {
  91. FILE *fpIn;
  92. int i, readSize, fileSize;
  93. u8 readBuf[TMP_BUFFER_SIZE] = {0};
  94. fileSize = getFileSize( sourceFileName );
  95. if( fileSize == 0 )
  96. {
  97. errMsg( "FileSize (%s) is zero!\n", sourceFileName );
  98. return false;
  99. }
  100. if( (cpySize != 0) && ( cpySize <= fileSize ) ) fileSize = cpySize;
  101. fpIn = fopen( sourceFileName, "rb" );
  102. if( fpIn == NULL )
  103. {
  104. errMsg( "File (%s) is not exist\n", sourceFileName );
  105. return false;
  106. }
  107. for( i = 0; i < fileSize; i+= TMP_BUFFER_SIZE )
  108. {
  109. readSize = fread( readBuf, 1, TMP_BUFFER_SIZE, fpIn );
  110. if( readSize < 0 )
  111. {
  112. errMsg( "fread (%s)\n", sourceFileName );
  113. return false;
  114. }
  115. fwrite( readBuf, 1, readSize, fpOut );
  116. }
  117. fclose( fpIn );
  118. return true;
  119. }
  120. bool copyFile( const char *targetName, const char *sourceName, u32 cpySize )
  121. {
  122. bool ret;
  123. FILE *fpOut = fopen( targetName, "wb" );
  124. if( fpOut == NULL )
  125. {
  126. errMsg( "Create writable file failed (%s)\n", targetName );
  127. return false;
  128. }
  129. ret = copyFileContent( fpOut, sourceName, cpySize );
  130. fclose( fpOut );
  131. //dbgMsg( "copyFile %d\n", ret );
  132. return ret;
  133. }
  134. //-------------------------------------------------------------------------------
  135. #if 0
  136. static inline bool stripFlashBin()
  137. {
  138. bool ret;
  139. u32 codeValidSize, dataValidSize;
  140. dbgMsg( "\nstripFlashBin ==>\n" );
  141. ret = calculateImageValidSize( &codeValidSize, &dataValidSize );
  142. dbgMsg( "FlashValidSize: Code=%dKB, Data=%dKB\n", codeValidSize, dataValidSize );
  143. codeValidSize *= 1024;
  144. dataValidSize *= 1024;
  145. if( ret ) ret = copyFile( FILENAME_CODEVALID, FILENAME_CODEBIN, codeValidSize );
  146. if( ret && dataValidSize ) ret = copyFile( FILENAME_DATAVALID, FILENAME_DATABIN, dataValidSize );
  147. #if 0
  148. removeTempFile( FILENAME_CODEBIN );
  149. removeTempFile( FILENAME_DATABIN );
  150. #endif
  151. return ret;
  152. }
  153. #endif
  154. //-------------------------------------------------------------------------------
  155. static inline void clearTemporaryFlies()
  156. {
  157. //removeTempFile( FILENAME_CODEVALID );
  158. //removeTempFile( FILENAME_DATAVALID );
  159. removeTempFile( FILENAME_PACKETSAVE );
  160. //removeTempFile( FILENAME_PACKETIMAGE );
  161. removeTempFile( FILENAME_CODEBIN );
  162. //removeTempFile( FILENAME_BINWITHINFO );
  163. //if( isLoadBasicPacket() ) freeBasicPacket();
  164. #if MI_COPY_MERGE_SOURCE
  165. //removeTempFile(flashModuleInfo[MODULE_BOOTROM].tmpBinName);
  166. //removeTempFile(flashModuleInfo[MODULE_AUDIOROM].tmpBinName);
  167. #endif
  168. }
  169. #if 0
  170. static inline bool generatePacketBin( const char *targetName )
  171. {
  172. bool ret = true;
  173. dbgMsg( "\ngeneratePacketBin ==>\n" );
  174. FILE *fpOut = fopen( FILENAME_PACKETIMAGE, "wb" );
  175. if( fpOut == NULL ) return false;
  176. if( ret ) ret = copyFileContent( fpOut, FILENAME_PACKETSAVE, 0 );
  177. fseek( fpOut, SAVEINFO_SIZE, SEEK_SET );
  178. if( ret ) ret = copyFileContent( fpOut, FILENAME_CODEVALID, 0 );
  179. #if( CONFIG_DATA_FLASH_SIZE != 0 )
  180. if( ret ) ret = copyFileContent( fpOut, FILENAME_DATAVALID, 0 );
  181. #endif
  182. fclose( fpOut );
  183. copyToOutputFolder( targetName, FILENAME_PACKETIMAGE );
  184. return ret;
  185. }
  186. #endif
  187. static inline bool generateBinwithInfo( const char *targetName )
  188. {
  189. bool ret = true;
  190. int _SaveFile_size;
  191. dbgMsg( "\ngenerateBinwithInfo ==>\n" );
  192. FILE *fpOut = fopen( targetName, "wb" );
  193. if( fpOut == NULL ) return false;
  194. if( ret ) ret = copyFileContent( fpOut, FILENAME_CODEBIN, 0 );
  195. //size will be KB alignment,
  196. if( ret ) ret = copyFileContent( fpOut, FILENAME_PACKETSAVE, 0 );
  197. //get _SaveFile_size (4 byte alignment)
  198. _SaveFile_size = ROUND_UP(getFileSize(FILENAME_PACKETSAVE),4)*4;
  199. stuff_File_with_zero(fpOut, _SaveFile_size- getFileSize(FILENAME_PACKETSAVE));
  200. //write .save size to last word
  201. _SaveFile_size = _SaveFile_size+sizeof(_SaveFile_size); //add _SaveFile_size itself
  202. fwrite( &_SaveFile_size, sizeof(_SaveFile_size), 1, fpOut );
  203. fclose( fpOut );
  204. //copyToOutputFolder( targetName, FILENAME_BINWITHINFO );
  205. return ret;
  206. }
  207. bool mergeImageMain()
  208. {
  209. bool ret = true;
  210. createOutputFolder();
  211. //transfer ini to bins
  212. if( ret ) ret = genTempBinFiles();
  213. #if 0
  214. if( ret ) ret = generateFlashBin();
  215. #else
  216. if( ret ) ret = MergeBins2Mem(); //just a test
  217. #endif
  218. #if 0
  219. if( miGenerateOriginalBinFlag && ret ) ret = generateBinSaveInfo();
  220. #endif
  221. if( miGeneratePacketFlag )
  222. {
  223. // if( ret ) ret = stripFlashBin();
  224. if( ret ) ret = generateSaveInfo();
  225. //if( ret ) ret = generatePacketBin( miPacketName );
  226. //append .save to bin file end
  227. if( ret ) ret = generateBinwithInfo( miBinWithInfo );
  228. }
  229. clearTemporaryFlies();
  230. //chmodOutputFolder();
  231. return ret;
  232. }