// UpdateFile.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "sense4.h"
#include "Psense4.h"
#include <stdio.h>
#include "AccKey.h"


int main(int argc, char* argv[])
{
	SENSE4_CONTEXT * pCtx = NULL;
	DWORD			dwRet;
	BYTE			pPin[24]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
									0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
									0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38};
	BYTE			pBuf[250];
	DWORD			dwBytesWritten = 0;
	DWORD FileSize = 0;


	//Access key
	pCtx = AccessKey();
	if(pCtx == NULL)
	{
		printf("Access device failed.");
		return 0;
	}
	
	//Change current directory to MF
	dwRet = S4ChangeDir(pCtx,"\\");
	
	//Verify DEV PIN
	dwRet=S4VerifyPin(pCtx,pPin,24,S4_DEV_PIN);

	//Update VM executable file 0101, the FileSize MUST be 0.
	dwRet=S4WriteFile(pCtx,"0101",0,pBuf,250,0,&dwBytesWritten,S4_UPDATE_FILE,S4_EXE_FILE);


	//Update RSA public file 0301, the Filesize MUST be 0.
	dwRet=S4WriteFile(pCtx,"0301",0,pBuf,sizeof(S4_RSA_PUBLIC_KEY),0, &dwBytesWritten,S4_UPDATE_FILE,S4_RSA_PUBLIC_FILE);

	//Update RSA private file 0302
	dwRet=S4WriteFile(pCtx,"0302",0,pBuf,sizeof(S4_RSA_PRIVATE_KEY),0,&dwBytesWritten,S4_UPDATE_FILE,S4_RSA_PRIVATE_FILE);

	//Update RSA public file by invoking PS4WriteFile, on this occasion the content of the public file(*.pub)
	//MUST be the format defined as S4_RSA_PRIVATE_KEY.
	FileSize = 0;
	dwRet = PS4WriteFile(pCtx, "0301", "c:\\PublicFile.pub", &FileSize, S4_UPDATE_FILE, S4_RSA_PUBLIC_FILE, &dwBytesWritten);

	//Update RSA private file by invoking PS4WriteFile, on this occasion the content of the private file
	//MUST be the format defined as S4_RSA_PRIVATE_KEY.
	FileSize = 0;
	dwRet = PS4WriteFile(pCtx, "0302", "c:\\PrivateFile.pub", &FileSize, S4_UPDATE_FILE, S4_RSA_PRIVATE_FILE, &dwBytesWritten);

	//Update XA executable file by invoking PS4WriteFile.
	FileSize = 0;
	dwRet = PS4WriteFile(pCtx, "0303", "c:\\XAFile.bin", &FileSize, S4_UPDATE_FILE, S4_XA_EXE_FILE, &dwBytesWritten);
	
	//Update VM executable file by invoking PS4WriteFile
	FileSize = 0;
	dwRet = PS4WriteFile(pCtx, "0303", "c:\\VMFile.bin", &FileSize, S4_UPDATE_FILE, S4_EXE_FILE, &dwBytesWritten);


	//.......Add your code here......//

	//Close device
	S4Close(pCtx);

	//Free memory
	free(pCtx);
	pCtx = NULL;	
	
	return 0;
}
