//------------------------------------------------------------------------------- #include "parseConfig.h" #include "loadBasicPacket.h" //------------------------------------------------------------------------------- const char* ACTION_NAME[ACTION_MAX] = { "FileName", "Protected", "MaxSize", "NVMData", "PathName", "Output", "BasicPacket", "END" }; enum { ACTION_FILENAME = 0, ACTION_PROTECTED, ACTION_MAXSIZE, ACTION_NVMDATA, ACTION_PATHNAME, ACTION_OUTPUT, ACTION_BASICPACKET, ACTION_ENDFILE }; int parseAction; //u32 flashModuleInfoNum; NorFlashModuleInfo flashModuleInfo[MODULE_MAX]; NorFlashModulePathInfo flashModulePathInfo[PATHINFO_MAX]; char DEFAULT_FILE_NAME[] = "S2.code.bin"; char BIN_FILENAME[128]; NVMData gNVMData[MODULE_MAX]; u32 gNVMData_count = 0; #ifndef CONFIG_BIN_FILENAME #define CONFIG_BIN_FILENAME DEFAULT_FILE_NAME #endif //--------------------------------------------------------------------------- static inline int check_keyword( const char *keyword, const char* tempString ) { //return (!strncmp(keyword, tempString, strlen(keyword))) ;gaia fixed char tmp[FILENAME_SIZE]; char *ptr = NULL; memset(tmp ,'\0', FILENAME_SIZE); strcpy(tmp ,tempString); ptr = strstr(tmp, "=" ); if (ptr != NULL) { *ptr = '\0'; } if(!strncmp(keyword, "CONFIG_", (int)7)) //parse string CONFIG_ return false; return (strlen(keyword)==0)?false:(!strncmp(keyword, tmp, strlen(tmp))) ; } //--------------------------------------------------------------------------- static bool copy_settingString( char* dstString, const char* tempString ) { char* tmp = strstr( tempString, "=" ); if (tmp == NULL) return false; tempString = tmp + 1; strcpy( dstString, tempString ); return true; } //--------------------------------------------------------------------------- static bool copy_settingYesNo( bool* dstValue, const char* tempString ) { char* tmp = strstr( tempString, "=" ); if (tmp == NULL) return false; tempString = tmp + 1; switch( tempString[0] ) { case 'y': case 'Y': *dstValue = true; break; case 'n': case 'N': *dstValue = false; break; default: return false; } return true; } //--------------------------------------------------------------------------- char* fgetLine( char *s, int size, FILE *fpIn ) { int i = 0; while( i == 0 ) { if( fgets( s, size, fpIn ) == NULL ) return NULL; i = strlen( s ); if( s[i-1] == '\n' ) { i--; if( (i != 0) && s[i-1] == '\r' ) i--; s[i] = '\0'; } } return s; } //--------------------------------------------------------------------------- // Search ModuleInfo & ActionIndex //--------------------------------------------------------------------------- NorFlashModuleInfo* searchModuleInfo( const char *moduleName ) { int i = 0; for( i = 0; i < MODULE_MAX; i++ ) { if( check_keyword( flashModuleInfo[i].moduleName, moduleName ) ) { //printFlashModuleInfo( &flashModuleInfo[i] ); return &flashModuleInfo[i]; } } //showMsg( "warning! searchModuleInfo failed (%s)\n", moduleName ); return NULL; } NorFlashModulePathInfo* searchModulePathInfo( const char *moduleName ) { int i = 0; for( i = 0; i < PATHINFO_MAX; i++ ) { if( check_keyword( flashModulePathInfo[i].moduleName, moduleName ) ) return &flashModulePathInfo[i]; } errMsg( "searchModulePathInfo failed (%s)\n", moduleName ); return NULL; } static inline int searchActionIndex( const char *actionName ) { int i = 0; for( i = 0; i moduleName , bootrom_str, strlen(bootrom_str))){ //hard coding.....carefully int tmp_str_len = strlen(moduleInfo->fileName); showMsg( "bootrom_append_dbg_string: %s ->", moduleInfo->fileName); memcpy(&moduleInfo->fileName[tmp_str_len-4], dbg_str, strlen(dbg_str)+1); //moduleInfo->fileName[tmp_str_len+4]='\0'; showMsg( " %s \n", moduleInfo->fileName); } } #endif void initFlashModuleInfo(int moduleIndex, const char *moduleName, u32 flashAddr1, u32 flashSize1) { //initSingleNorFlashModuleInfoCrossFlash( moduleName, flashAddr1, flashSize1, 0, 0 ); NorFlashModuleInfo *info; info = &flashModuleInfo[moduleIndex]; //flashModuleInfoNum++ info->isValid = false; info->isProtected = false; info->isInitModuleNameByUser = false; info->isInitProtectedByUser = false; info->flashAddr1 = flashAddr1; info->flashSize1 = flashSize1; info->max_module_size = 0; // info->flashAddr2 = 0; // info->flashSize2 = 0; info->fileName[0] = '\0'; strcpy( info->moduleName, moduleName ); printf("info->moduleName %s\n", info->moduleName); } //--------------------------------------------------------------------------- // Parse mergeImage.cfg //--------------------------------------------------------------------------- static inline bool parseMIC_FileName( const char* tempString ) { bool ret = false; #ifdef CONFIG_APPEND_NAME_TO_CIKEY_HDCPKEY char *token; char tokenfileName[FILENAME_SIZE]; char tname[FILENAME_SIZE]; #endif NorFlashModuleInfo *moduleInfo = searchModuleInfo( tempString ); if( moduleInfo == NULL ) return false; if( moduleInfo->isInitModuleNameByUser ) { errMsg( "[%s] re-assign fileName\n", moduleInfo->moduleName ); return false; } if( copy_settingString( moduleInfo->fileName, tempString ) ) { moduleInfo->isInitModuleNameByUser = true; if( moduleInfo->fileName[0] != '\0' ) moduleInfo->isValid = true; ret = true; } #ifdef CONFIG_APPEND_NAME_TO_CIKEY_HDCPKEY memset(tname, 0, FILENAME_SIZE); strcpy(tokenfileName, moduleInfo->fileName ); token = strtok(tokenfileName,"/"); /*There are two delimiters here*/ while (token != NULL){ strcpy(tname, token); token = strtok(NULL," /"); } strcpy(moduleInfo->eachfileName, tname); #endif #if 0//ndef RELEASE_VER /*special take care bootrom debug/release version */ bootrom_append_dbg_string(moduleInfo); #endif return ret; } static inline bool parseMIC_Protected( const char* tempString ) { NorFlashModuleInfo *moduleInfo = searchModuleInfo( tempString ); if( moduleInfo == NULL ) return false; if( moduleInfo->isInitProtectedByUser ) { errMsg( "[%s] re-assign protect\n", moduleInfo->moduleName ); return false; } if( copy_settingYesNo( &moduleInfo->isProtected, tempString ) ) { moduleInfo->isInitProtectedByUser = true; return true; } return false; } static inline bool parseMIC_MaxSize( const char* tempString ) { char max_size[12]; NorFlashModuleInfo *moduleInfo = searchModuleInfo( tempString ); memset(max_size, '\0', 12); if( moduleInfo == NULL ) return false; if(copy_settingString(max_size, tempString ) ) { moduleInfo->max_module_size = atoi(max_size); return true; } return false; } static inline bool parseMIC_NVMdata( const char* tempString ) { char tmp[FILENAME_SIZE]; char *ptr; memset(tmp, 0, FILENAME_SIZE); strcpy(tmp, tempString); ptr = strstr(tmp, "=" ); if (ptr == NULL) return false; *ptr = '\0'; gNVMData[gNVMData_count].id = atoi(tmp); strcpy(gNVMData[gNVMData_count].pathName, ptr+1); gNVMData_count++; return true; } #if 0 static inline bool parseMIC_PathName( const char* tempString ) { NorFlashModulePathInfo *modulePathInfo = searchModulePathInfo( tempString ); if( modulePathInfo == NULL ) return false; return copy_settingString( modulePathInfo->pathName, tempString ); } #endif static inline bool parseMIC_Output( const char* tempString ) { if( check_keyword( "Folder", tempString ) ) return copy_settingString( miOutputPathName, tempString ); else if( check_keyword( "judgeDebugVersion", tempString ) ) return copy_settingYesNo( &miJudgeDebugVersionFlag, tempString ); return false; } static inline bool parseMIC_BasicPacket( const char* tempString ) { if( check_keyword( "BasicPacket", tempString ) ) { if( !copy_settingString( miBaseImageName, tempString ) ) return false; return loadBasicPacket(); } return false; } //--------------------------------------------------------------------------- static inline bool parseMIC_Action( const char* tempString ) { int index = searchActionIndex( tempString ); if( index < 0 ) return false; parseAction = index; return true; } static inline bool parseMIC_Setting( const char* tempString ) { switch( parseAction ) { case ACTION_FILENAME: return parseMIC_FileName( tempString ); case ACTION_PROTECTED: return parseMIC_Protected( tempString ); case ACTION_MAXSIZE: return parseMIC_MaxSize( tempString ); case ACTION_NVMDATA: return parseMIC_NVMdata( tempString ); // case ACTION_PATHNAME: return parseMIC_PathName( tempString ); case ACTION_OUTPUT: return parseMIC_Output( tempString ); case ACTION_BASICPACKET: return parseMIC_BasicPacket( tempString ); case ACTION_ENDFILE: return true; default: errMsg( "parseAction (%d) not define\n", parseAction ); return false; } } static inline bool parseMergeImageConfigString( const char* tempString ) { switch( tempString[0] ) { case '@': return parseMIC_Action( tempString+1 ); case '#': return true; default: return parseMIC_Setting( tempString ); } } // ivan char *parseQuota(char *str) { int i, n=strlen(str); char *p=str; for(i=0;iisInitModuleNameByUser ) return true; if( info->isInitProtectedByUser ) return true; info->isProtected = true; dbgMsg( "BasicSaveInfo: set %s protected\n", info->moduleName ); return true; } static inline bool parseBasicSaveInfo( const char *filename) { int parseLine; bool ret = true; char tempString[TMP_BUFFER_SIZE]; FILE *fpIn = fopen( filename, "rt" ); if( fpIn == NULL ) { errMsg( "BasicSaveInfo (%s) not found!\n", filename ); return false; } dbgMsg( "\nparseBasicSaveInfo =>\n" ); for( parseLine = 1; !feof(fpIn); parseLine++ ) { if( fgetLine( tempString, TMP_BUFFER_SIZE, fpIn ) != NULL ) { #if DBG_SHOW_CONFIG_LINE dbgMsg( "line%d: %s\n",parseLine, tempString ); #endif if( check_keyword( "#END", tempString ) ) break; ret = parseBasicSaveInfoString( tempString ); } if( !ret ) { errMsg( "Parse config failed\n, line:%d , string: %s\n", parseLine, tempString ); break; } } fclose( fpIn ); return ret; } //--------------------------------------------------------------------------- // init & load moduleInfo //--------------------------------------------------------------------------- #if 0 void initSingleNorFlashModuleInfoCrossFlash( const char *moduleName, u32 flashAddr1, u32 flashSize1, u32 flashAddr2, u32 flashSize2 ) { NorFlashModuleInfo *info; info = &flashModuleInfo[flashModuleInfoNum++]; info->isValid = false; info->isProtected = false; info->isInitModuleNameByUser = false; info->isInitProtectedByUser = false; info->flashAddr1 = flashAddr1; info->flashSize1 = flashSize1; info->flashAddr2 = flashAddr2; info->flashSize2 = flashSize2; info->fileName[0] = '\0'; strcpy( info->moduleName, moduleName ); } #endif inline void printFlashModuleInfo( NorFlashModuleInfo *info ) { #if DBG_SHOW_PARSE_RESULT showMsg( "[Module] %s\n", info->moduleName ); showMsg( "isValid=%s\n", info->isValid ? "true" : "false" ); showMsg( "isProtected=%s\n", info->isProtected ? "true" : "false" ); showMsg( "isInitModuleNameByUser=%s\n", info->isInitModuleNameByUser ? "true" : "false" ); showMsg( "isInitProtectedByUser=%s\n", info->isInitProtectedByUser ? "true" : "false" ); showMsg( "flashAddr1=0x%08x\n", info->flashAddr1 ); showMsg( "flashSize1=0x%08x\n", info->flashSize1 ); showMsg( "flashAddr2=0x%08x\n", info->flashAddr2 ); showMsg( "flashSize2=0x%08x\n", info->flashSize2 ); showMsg( "fileName=%s\n", info->fileName ); #endif } static inline void reviewFlashModuleInfoFlag(int index) { //NorFlashModuleInfo *info if( isLoadBasicPacket() ) return; #if MI_PROTECT_INVALID_MODULE if( (flashModuleInfo[index].isInitModuleNameByUser) && (!flashModuleInfo[index].isValid) ) { #if defined(OP_IRCommandType) if( MODULE_OPTION == index) return; #endif flashModuleInfo[index].isProtected= true; showMsg( "[%s] Auto force set protect=y\n", flashModuleInfo[index].moduleName ); } #endif } /* remove space after filename */ static inline void reviewFlashModuleFileName( NorFlashModuleInfo *info ) { int max = strlen(info->fileName); for( ; max > 0 ; max-- ) { if( info->fileName[max-1] != ' ' ) break; } info->fileName[max] = '\0'; } static inline void reviewFolderString( const char *folderName, char *folderString, const char *defaultFolder ) { u32 strLen = strlen(folderString); while( strLen && (folderString[strLen-1] == '/' ) ) folderString[--strLen] = '\0'; if( strLen == 0 ) strcpy( folderString, defaultFolder ); dbgMsg( "%s : %s\n", folderName, folderString ); } #include "init_flash_module.h" //--------------------------------------------------------------------------- static inline void initSingleNorFlashModulePathInfo( NorFlashModulePathInfo *info, const char *moduleName, const char *pathName ) { strcpy( info->moduleName, moduleName ); strcpy( info->pathName, pathName ); } #if 0 static inline void initNorFlashModulePathInfo() { initSingleNorFlashModulePathInfo( &flashModulePathInfo[0], "BootROM", PATHNAME_BOOTROM ); initSingleNorFlashModulePathInfo( &flashModulePathInfo[1], "AudioROM", PATHNAME_AUDIOROM ); } #endif //--------------------------------------------------------------------------- bool loadNorFlashModuleInfo() { int index; //initNorFlashModulePathInfo(); initNorFlashModuleInfo(); parseAction = -1; flashModuleInfo[MODULE_OPTION].isValid = true; //force Option = valid flashModuleInfo[MODULE_END].isValid = true; //force END = valid #ifdef CONFIG_SUPPORT_NVM_PRELOAD flashModuleInfo[MODULE_NVM].isValid = true; //force END = valid #endif if( !parseMergeImageConfigFile(miConfigFile) ) return false; for( index = 0; index < MODULE_MAX; index++ ) { reviewFlashModuleInfoFlag(index); reviewFlashModuleFileName( &flashModuleInfo[index] ); printFlashModuleInfo( &flashModuleInfo[index] ); } reviewFolderString( "OutputFolder", miOutputPathName, PATHNAME_OUTPUT ); //reviewFolderString( "BootROMFolder", searchModulePathInfo( MODULENAME_BOOTROM )->pathName, PATHNAME_BOOTROM ); //reviewFolderString( "AudioROMFolder", searchModulePathInfo( MODULENAME_AUDIOROM )->pathName, PATHNAME_AUDIOROM ); //if( isLoadBasicPacket() ) parseBasicSaveInfo( BASEIMAGE_SAVE ); return true; } //--------------------------------------------------------------------------- // parse Program Argument ... //--------------------------------------------------------------------------- static inline void showProgramUsageHint() { showMsg( "\nUsage: mergeImage targetDir targetName \n\n" ); showMsg( "=> targetDir: The directory of source files.\n"); showMsg( "=> targetName: Final production file-name. Don't need to include the file extension. (*.packet *.bin)\n\n"); showMsg( "The options are:\n" ); showMsg( "=> -Config=: Re-specified the mergeImage configuration file.\n" ); showMsg( "=> -deleteTempFile=: Re-specified delete intermediate files or not.\n" ); showMsg( "=> -generatePacket=: Re-specified generate packet or not.\n" ); showMsg( "=> -generateOriginalBin=: Re-specified generate original-bin or not.\n" ); showMsg( "\n" ); } static inline void showFlashMapItemInfo( const char* s, u32 addr, u32 size ) { showMsg( "%8s: 0x%08x, %d KB\n", s, addr, ROUND_KB( size ) ); } #if 0 static inline void showDynamicItemOfFlashMap() /* debug for sisspi_flashalloc.h*/ { showMsg( "\nDynamicItemOfFlashMap=>\n" ); showFlashMapItemInfo( "RRTTable", SPI_RRTTABLE_FLASHADDR, SPI_RRTTABLE_FLASHSIZE ); // showFlashMapItemInfo( "RRTTable2", SPI_RRTTABLE2_FLASHADDR, SPI_RRTTABLE2_FLASHSIZE ); showFlashMapItemInfo( "Logo", SPI_LOGO_FLASHADDR, SPI_LOGO_FLASHSIZE ); // showFlashMapItemInfo( "Logo2", SPI_LOGO2_FLASHADDR, SPI_LOGO2_FLASHSIZE ); showFlashMapItemInfo( "Backup64K", SPI_MEMALLOCBACKUP_FLASHADDR, 64*1024 ); showFlashMapItemInfo( "MultiTable", SPI_MULTITABLE_FLASHADDR, SPI_MULTITABLE_FLASHSIZE ); showFlashMapItemInfo( "AudioROM", SPI_AUDIOROM_FLASHADDR, SPI_AUDIOROM_FLASHSIZE ); showFlashMapItemInfo( "Kernel", SPI_KERNEL_FLASHADDR, SPI_KERNEL_FLASHSIZE ); showMsg( "\n" ); } #endif static inline bool parseProgArg_Option( const char *optionArgument ) { if( check_keyword( "-Config=", optionArgument ) ) { if( copy_settingString( miConfigFile, optionArgument ) ) return true; } else if( check_keyword( "-deleteTempFile=", optionArgument ) ) { if( copy_settingYesNo( &miDeleteTempFileFlag, optionArgument ) ) return true; } else if( check_keyword( "-generatePacket=", optionArgument ) ) { if( copy_settingYesNo( &miGeneratePacketFlag, optionArgument ) ) return true; } else if( check_keyword( "-generateOriginalBib=", optionArgument ) ) { if( copy_settingYesNo( &miGenerateOriginalBinFlag, optionArgument ) ) return true; } return false; } static inline bool parseProgArg_Necessary( const char *targetDir, const char *targetName ) { if( chdir( targetDir ) != 0 ) { errMsg( "Cannot find the specified folder: %s\n", targetDir ); return false; } strcpy( miPacketName, targetName ); strcat( miPacketName, ".packet" ); #if 0 strcpy( miCodeBinName, targetName ); strcat( miCodeBinName, ".code.bin" ); strcpy( miDataBinName, targetName ); strcat( miDataBinName, ".data.bin" ); #endif strcpy( miBinWithInfo, targetName ); //strcat( miBinWithInfo, ".code.bin" ); strcpy( miSaveInfoName, targetName ); strcat( miSaveInfoName, ".save" ); #ifdef MERGEFEATURE strcpy( miConfigFile , MERGEFEATURE ); #else showMsg( "[Waring] No define MERGEFEATURE in Project.h\n" ); strcpy( miConfigFile , FILENAME_MICONFIG ); #endif miBaseImageName[0] = '\0'; strcpy( miOutputPathName, PATHNAME_OUTPUT ); showMsg( "TargetDirectory: %s\n", targetDir ); showMsg( "TargetName: %s\n", targetName ); return true; } bool parseProgArg( int argc, char *argv[] ) { int i; if( argc < 2 ) { showProgramUsageHint(); return false; } #if 0 if( argc == 2 ) { if( check_keyword( "-printmap", argv[1] ) ) showDynamicItemOfFlashMap( ); return false; } #endif strcpy( BIN_FILENAME , CONFIG_BIN_FILENAME ); if( !parseProgArg_Necessary( argv[1], BIN_FILENAME ) ) return false; for( i = 3; i < argc; i++ ) { if( !parseProgArg_Option( argv[i] ) ) { errMsg( "parse Argument Option failed : %s\n", argv[i] ); return false; } } showMsg( "miConfigFile: %s\n", miConfigFile ); showMsg( "miDeleteTempFileFlag: %d\n", miDeleteTempFileFlag ); showMsg( "miGeneratePacketFlag: %d\n", miGeneratePacketFlag ); showMsg( "miGenerateOriginalBinFlag: %d\n", miGenerateOriginalBinFlag ); return true; }