PUBLIC INT32 S2wCert_FlashFileLoad(char *fileName) { INT32 fileLen,index; F_FILE *fd; char name[64]={0}; if(fileName == NULL) return -1; if(!s2wappMainTaskCtxt->fsInit) { App_FsInit(); s2wappMainTaskCtxt->fsInit = TRUE; f_enterFS(); } index=sprintf(name,"A:/certs/"); index=sprintf(name+index,fileName); fileLen = f_filelength(name); if(fileLen == -1) { name[0]='B'; fileLen = f_filelength(name); if(fileLen == -1) { index = -1; goto FlashFileLoadExit; } } index = S2wCert_MemAlloc (fileLen); if(index != -1) { s2w_ssl_cert_table[index].name = gsn_malloc(strlen(fileName) + 1); if(s2w_ssl_cert_table[index].name == NULL) { S2wCert_MemFree(s2w_ssl_cert_table[index].certAddress); index = -1; goto FlashFileLoadExit; } strcpy((char*)s2w_ssl_cert_table[index].name, fileName); s2w_ssl_cert_table[index].location = CERT_IN_FLASH; fd = f_open(name, "r"); if(fd == NULL) { S2wCert_MemFree(s2w_ssl_cert_table[index].certAddress); gsn_free(s2w_ssl_cert_table[index].name); index = -1; goto FlashFileLoadExit; } f_read(s2w_ssl_cert_table[index].certAddress+2, 1, fileLen, fd); s2w_ssl_cert_table[index].certAddress[0] = (fileLen & 0xff); s2w_ssl_cert_table[index].certAddress[1] = ((fileLen >> 8) & 0xff); f_close(fd); }
UINT8 S2w_LoadS2wProfileParams() { F_FILE *fp; fp = f_open("A:/s2w_profile_params.conf","r"); if (!fp) { //S2w_Printf("\r\nFile cannot be opened!\r\n"); return FILE_OPEN_ER; } long file_size = f_filelength("A:/s2w_profile_params.conf"); if(f_read(&profile_params,1,sizeof(S2W_PROFILE_PARAMS_T),fp) != sizeof(S2W_PROFILE_PARAMS_T)) { f_close(fp); return FILE_READ_ER; } f_close(fp); return S2W_SUCCESS; }
UINT8 S2w_SaveS2wProfileParams(S2W_PROFILE_PARAMS_T *defaultProfile) { F_FILE *fp = NULL; INT32 fileLen; memcpy(&profile_params,defaultProfile,sizeof(S2W_PROFILE_PARAMS_T)); fileLen = f_filelength("A:/s2w_profile_params.conf"); if(fileLen != 0 && fileLen != -1) fp = f_open("A:/s2w_profile_params.conf","r+"); else fp = f_open("A:/s2w_profile_params.conf","w"); if (!fp) { //S2w_Printf("\r\nFile cannot be opened!\r\n"); return FILE_OPEN_ER; } if(f_write(&profile_params,1,sizeof(profile_params),fp) != sizeof(profile_params)) { f_close(fp); return FILE_WRITE_ER; } f_close(fp); return S2W_SUCCESS; }
UINT8 S2w_LoadApMacAddr(UINT8 *macAddr) { F_FILE *fp; fp = f_open("A:/s2w_ap_mac.conf","r"); if (!fp) { return FILE_OPEN_ER; } long file_size = f_filelength("A:/s2w_mac.conf"); if(f_read(macAddr,1,file_size,fp) != file_size) { f_close(fp); return FILE_READ_ER; } f_close(fp); return S2W_SUCCESS; }
static portBASE_TYPE prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString ) { const char *pcDestinationFile; char *pcSourceFile; portBASE_TYPE xParameterStringLength; long lSourceLength, lDestinationLength = 0; /* Obtain the name of the destination file. */ pcDestinationFile = FreeRTOS_CLIGetParameter ( pcCommandString, /* The command string itself. */ 2, /* Return the second parameter. */ &xParameterStringLength /* Store the parameter string length. */ ); /* Sanity check something was returned. */ configASSERT( pcDestinationFile ); /* Obtain the name of the source file. */ pcSourceFile = ( char * ) FreeRTOS_CLIGetParameter ( pcCommandString, /* The command string itself. */ 1, /* Return the first parameter. */ &xParameterStringLength /* Store the parameter string length. */ ); /* Sanity check something was returned. */ configASSERT( pcSourceFile ); /* Terminate the string. */ pcSourceFile[ xParameterStringLength ] = 0x00; /* See if the source file exists, obtain its length if it does. */ lSourceLength = f_filelength( pcSourceFile ); if( lSourceLength == 0 ) { sprintf( pcWriteBuffer, "Source file does not exist" ); } else { /* See if the destination file exists. */ lDestinationLength = f_filelength( pcDestinationFile ); if( lDestinationLength != 0 ) { sprintf( pcWriteBuffer, "Error: Destination file already exists" ); } } /* Continue only if the source file exists and the destination file does not exist. */ if( ( lSourceLength != 0 ) && ( lDestinationLength == 0 ) ) { if( prvPerformCopy( pcSourceFile, lSourceLength, pcDestinationFile, pcWriteBuffer, xWriteBufferLen ) == pdPASS ) { sprintf( pcWriteBuffer, "Copy made" ); } else { sprintf( pcWriteBuffer, "Error during copy" ); } } strcat( pcWriteBuffer, cliNEW_LINE ); return pdFALSE; }
UINT8 S2w_LoadS2wProfile(S2W_PROFILE_T *s2wProfile, UINT8 index) { F_FILE *fp = NULL, *fpExt; UINT32 extSize = 0; UINT32 copySize = 0, profExtSize = 0, file_size = 0; if(index == 0) { fp = f_open("A:/s2w_profile0.conf","r"); file_size = f_filelength("A:/s2w_profile0.conf"); extSize = f_filelength("A:/s2w_profile0_ext.conf"); if((extSize != -1) && extSize != 0) { fpExt = f_open("A:/s2w_profile0_ext.conf", "r+"); } else { fpExt = f_open("A:/s2w_profile0_ext.conf", "w+"); extSize = 0; } } else if(index == 1) { fp = f_open("A:/s2w_profile1.conf","r"); file_size = f_filelength("A:/s2w_profile1.conf"); extSize = f_filelength("A:/s2w_profile1_ext.conf"); if((extSize != -1) && extSize != 0) { fpExt = f_open("A:/s2w_profile1_ext.conf", "r+"); } else { fpExt = f_open("A:/s2w_profile1_ext.conf", "w+"); extSize = 0; } } if (!fp || !fpExt) { //S2w_Printf("\r\nFile cannot be opened!\r\n"); if(fpExt) f_close(fpExt); if(fp) f_close(fp); return FILE_OPEN_ER; } profExtSize = sizeof(S2W_PROFILE_T) - offsetof(S2W_PROFILE_T, socRdCnt); if(profExtSize > extSize) { /* OTAFU from 5.1.X where X is less than or equal to 4 - Upgrade */ copySize = extSize; } else { /* If extSize == profileExtSizeThis then it is same FW boot Else if the extSize > profileExtSize it is a downgrade In both cases only copy the size equivalent of the profExtSize */ copySize = profExtSize; } /* Copy the file_size from the profileN.conf to the profile */ if(f_read(&FlashParams.profile[index],1,file_size,fp) != file_size) { f_close(fp); f_close(fpExt); return FILE_READ_ER; } /* Copy the copySize from the profileN_ext.conf to the profile */ if (copySize) { f_read(((UINT8 *)&FlashParams.profile[index]) + offsetof(S2W_PROFILE_T, socRdCnt), 1, copySize, fpExt); } /* Save Back */ /* If this is OTAFU and the profile changed - save the extension */ if (profExtSize > extSize) { f_seek(fpExt,0, F_SEEK_SET); f_write(((UINT8 *)&FlashParams.profile[index]) + offsetof(S2W_PROFILE_T, socRdCnt), 1, profExtSize, fpExt); } if(s2wProfile != &FlashParams.profile[index]) { memcpy(s2wProfile, &FlashParams.profile[index], sizeof(S2W_PROFILE_T)); } f_close(fpExt); f_close(fp); return S2W_SUCCESS; }