Beispiel #1
0
void init_SD_card(void)
{
	//
	// Enable MMCHS
	//
	PRCMPeripheralClkEnable(PRCM_SDHOST, PRCM_RUN_MODE_CLK);

	//
	// Reset MMCHS
	//
	PRCMPeripheralReset(PRCM_SDHOST);

	//
	// Configure MMCHS
	//
	SDHostInit(SDHOST_BASE);

	//
	// Configure card clock
	//
	SDHostSetExpClk(SDHOST_BASE, PRCMPeripheralClockGet(PRCM_SDHOST), 15000000);
}
int ComputeSHA(const char *sourceFile, uint8_t *resultHash) {
	long sfileHandle = -1;
	unsigned long sToken = 0;
	long retVal = 0;
	unsigned long bytesRead = 0;
	unsigned long readsize = 0;
	uint8_t buffer[BLOCKSIZE];
	SlFsFileInfo_t sFileInfo;

	UART_PRINT("\r\nStarted comuting hash\r\n");

	//Enable MD5SHA module
	PRCMPeripheralClkEnable(PRCM_DTHE, PRCM_RUN_MODE_CLK);
	MAP_SHAMD5IntRegister(SHAMD5_BASE, SHAMD5IntHandler);

	//reset modul
	PRCMPeripheralReset(PRCM_DTHE);

	//clear flags
	g_bContextReadyFlag = false;
	g_bInputReadyFlag = false;

	//Enable Interrupts
	SHAMD5IntEnable(SHAMD5_BASE, SHAMD5_INT_CONTEXT_READY | SHAMD5_INT_PARTHASH_READY | SHAMD5_INT_INPUT_READY | SHAMD5_INT_OUTPUT_READY);

	//wait for context ready flag.
	while (!g_bContextReadyFlag)
		;

	//Configure SHA/MD5 module
	SHAMD5ConfigSet(SHAMD5_BASE, SHAMD5_ALGO_SHA1);

	// get file size
	retVal = sl_FsGetInfo((unsigned char *) sourceFile, sToken, &sFileInfo);
	if (retVal < 0) {
		// File Doesn't exit create a new of 45 KB file
		UART_PRINT("Error during opening the source file\r\n");
		return -1;
	}

	// open the source file for reading
	retVal = sl_FsOpen((unsigned char *) sourceFile, FS_MODE_OPEN_READ, &sToken, &sfileHandle);
	if (retVal < 0) {
		UART_PRINT("Error during opening the source file\r\n");
		return -1;
	}

	SHAMD5DataLengthSet(SHAMD5_BASE, (uint32_t) sFileInfo.FileLen);

	// Copy the files from temporary file to original file
	// If user file has checksum which can be used to verify the temporary
	// file then file should be verified before copying
	while (bytesRead < sFileInfo.FileLen) {
		if ((sFileInfo.FileLen - bytesRead) > BLOCKSIZE) readsize = BLOCKSIZE;
		else readsize = (sFileInfo.FileLen - bytesRead);

		memset(buffer, 0, sizeof(buffer));
		retVal = sl_FsRead(sfileHandle, bytesRead, (unsigned char *) buffer, readsize);
		if (retVal < 0) {
			// Error close the file and delete the temporary file
			retVal = sl_FsClose(sfileHandle, 0, 0, 0);
			UART_PRINT("Error during reading the file\r\n");
			return -1;
		}
		SHAMD5DataWrite(SHAMD5_BASE, buffer);
		bytesRead += readsize;
	}

	// Close the opened files
	retVal = sl_FsClose(sfileHandle, 0, 0, 0);
	if (retVal < 0) {
		// Error close the file and delete the temporary file
		UART_PRINT("Error during close the file\r\n");
		return -1;
	}
	UART_PRINT("Hash successfully\r\n");
	SHAMD5ResultRead(SHAMD5_BASE, resultHash);
	return 0;
}