examples/google_voice_assistant/main/wmfwparse.cpp
/*
 *  Function: ProcessBinHeader
 * 
 *  @brief  Processes the header of the WMFW file and skips past it.
 * 
 *  @param  binFile    Open handle to file to process
 * 
 *  @retval WMFW_SUCCESS            Succeeded
 *  @retval WMFW_BAD_FILE_FORMAT    Couldn't parse header
 * 
 */


@brief  Processes the header of the WMFW file and skips past it.

@param  binFile    Open handle to file to process

@retval WMFW_SUCCESS            Succeeded
@retval WMFW_BAD_FILE_FORMAT    Couldn't parse header
@brief Processes the header of the WMFW file and skips past it . @param binFile Open handle to file to process @retval WMFW_SUCCESS Succeeded @retval WMFW_BAD_FILE_FORMAT Couldn ' t parse header
int ProcessBinHeader(FILE *binFile)
{
	WMFW_STATUS     status = WMFW_SUCCESS;
	char            magic[4];       
	unsigned int    length;         
	size_t          amountRead = 0;
	unsigned char * headerData = NULL;
	printf("Parsing .bin file header:\n");
	amountRead = fread(magic, sizeof(char), 4, binFile);
	if (amountRead < 4)
	{
		printf("Error: WMDR identifier not found at the start of the file.\n");
		status = WMFW_BAD_FILE_FORMAT;
		goto binHeaderDone;
	}
	for (int c = 0; c < bin_id_length; c++)
	{
		if (bin_id_string[c] != magic[c])
		{
			printf("Error: WMDR identifier not found at the start of the file: %c%c%c%c\n",
				magic[0], magic[1], magic[2], magic[3]
			);
			status = WMFW_BAD_FILE_FORMAT;
			goto binHeaderDone;
		}
	}
	printf("\tWMDR identifier found\n");
	amountRead = fread(&length, sizeof(length), 1, binFile);
	if (amountRead < 1)
	{
		printf("Couldn't read full header length: %d\n", amountRead);
		status = WMFW_BAD_FILE_FORMAT;
		goto binHeaderDone;
	}
	printf("\tHeader Length: %d bytes\n", length);
    {   
    	unsigned long headerDataLength = (long)length - 8;
    	headerData = (unsigned char*)malloc(headerDataLength);
    	amountRead = fread(headerData, 1, headerDataLength, binFile);
    	if (amountRead < headerDataLength)
    	{
    		printf("Couldn't read full header data: %d\n", amountRead);
    		status = WMFW_BAD_FILE_FORMAT;
    		goto binHeaderDone;
    	}
    	if (headerDataLength == sizeof(BinFileHeaderFormat01))
    	{
    		BinFileHeaderFormat01 * fileData;
    		fileData = (BinFileHeaderFormat01*)headerData;
    		printf("\tFile Format Version: %02X\n", fileData->fileFormatVersion);
    		if (fileData->fileFormatVersion != 0x01)
    		{
    			printf("\tError: File Format %02X not compatible. Exiting.\n", fileData->fileFormatVersion);
    			status = WMFW_BAD_FILE_FORMAT;
    			goto binHeaderDone;
    		}
    		printf("\tFirmware Revision: %08X\n", fileData->firmwareRevision);
    		if (fileData->firmwareRevision != fwIdBlock.firmwareRevision)
    		{
    			printf("\nWarning: Firmware Revision Mismatch\n\tBin  File Firmware Revision: %08X\n\tWMFW File Firmware Revision: %08X\n", fileData->firmwareRevision, fwIdBlock.firmwareRevision);
    		}
    		switch (fileData->coreType)
    		{
    		case CORETYPE_ADSP2:
    			printf("\tCore Type: ADSP2\n");
    			printf("\tCore Revision: %08X\n", fileData->coreRevision);
    			if (fileData->coreRevision != 0x000501)
    			{
    				printf("\tError: Core Revision field does not match expected value for ADSP2. Exiting.\n");
    				status = WMFW_BAD_PARAM;
    				goto binHeaderDone;
    			}
    			break;
    		case CORETYPE_HALOCORE:
    			printf("\tCore Type: Halo Core\n");
    			printf("\tCore Revision: 0x%08X\n", fileData->coreRevision);
    			break;
    		default:
    			printf("Error: Core type 0x%02X not compatible. Exiting.\n", fileData->coreType);
    			status = WMFW_BAD_PARAM;
    			goto binHeaderDone;
    		}
    	}
    }
	binHeaderDone:
		free(headerData);
		return status;
}
int ProcessBinHeader ( FILE * binFile ) { WMFW_STATUS status = WMFW_SUCCESS ; char magic [ 4 ] ; unsigned int length ; size_t amountRead = 0 ; unsigned char * headerData = NULL ; printf ( " \n " ) ; amountRead = fread ( magic , sizeof ( char ) , 4 , binFile ) ; if ( amountRead < 4 ) { printf ( " \n " ) ; status = WMFW_BAD_FILE_FORMAT ; goto binHeaderDone ; } for ( int c = 0 ; c < bin_id_length ; c ++ ) { if ( bin_id_string [ c ] != magic [ c ] ) { printf ( " \n " , magic [ 0 ] , magic [ 1 ] , magic [ 2 ] , magic [ 3 ] ) ; status = WMFW_BAD_FILE_FORMAT ; goto binHeaderDone ; } } printf ( " \t \n " ) ; amountRead = fread ( & length , sizeof ( length ) , 1 , binFile ) ; if ( amountRead < 1 ) { printf ( " \n " , amountRead ) ; status = WMFW_BAD_FILE_FORMAT ; goto binHeaderDone ; } printf ( " \t \n " , length ) ; { unsigned long headerDataLength = ( long ) length - 8 ; headerData = ( unsigned char * ) malloc ( headerDataLength ) ; amountRead = fread ( headerData , 1 , headerDataLength , binFile ) ; if ( amountRead < headerDataLength ) { printf ( " \n " , amountRead ) ; status = WMFW_BAD_FILE_FORMAT ; goto binHeaderDone ; } if ( headerDataLength == sizeof ( BinFileHeaderFormat01 ) ) { BinFileHeaderFormat01 * fileData ; fileData = ( BinFileHeaderFormat01 * ) headerData ; printf ( " \t \n " , fileData -> fileFormatVersion ) ; if ( fileData -> fileFormatVersion != 0x01 ) { printf ( " \t \n " , fileData -> fileFormatVersion ) ; status = WMFW_BAD_FILE_FORMAT ; goto binHeaderDone ; } printf ( " \t \n " , fileData -> firmwareRevision ) ; if ( fileData -> firmwareRevision != fwIdBlock . firmwareRevision ) { printf ( " \n \n \t \n \t \n " , fileData -> firmwareRevision , fwIdBlock . firmwareRevision ) ; } switch ( fileData -> coreType ) { case CORETYPE_ADSP2 : printf ( " \t \n " ) ; printf ( " \t \n " , fileData -> coreRevision ) ; if ( fileData -> coreRevision != 0x000501 ) { printf ( " \t \n " ) ; status = WMFW_BAD_PARAM ; goto binHeaderDone ; } break ; case CORETYPE_HALOCORE : printf ( " \t \n " ) ; printf ( " \t \n " , fileData -> coreRevision ) ; break ; default : printf ( " \n " , fileData -> coreType ) ; status = WMFW_BAD_PARAM ; goto binHeaderDone ; } } } binHeaderDone : free ( headerData ) ; return status ; }
Function: ProcessBinHeader
Function : ProcessBinHeader
// File identifier, should be 'WMDR' // Header length // Read our file identifier. This is the first thing in the file and should be WMDR.  // Check it. // Read the length of the header. // Length of remaining header data is the length field minus 8 bytes (for WMDR and length) // Read rest of the header data. This section is optional. // It is useful as an additional error check, but can be safely skipped over without affecting // the coefficient download process if you are certain the file format version is compatible // If the data length matches the recognised file format for version 0x01, try mapping // that format onto the data block // Check that the file format field is compatible with this parser // Check that the firmware version matches the one downloaded by the WMFW // Check which core type the bin file works with // Check that the core revision field reads 0.5.1 (this is the only valid value for ADSP2) // Free the header data memory