Option Explicit

'******************************************************************************
' Keycard generator
'
' (VBScript example file for the commandline-parameter available in 
' Infinity USB 1.40)
'
' Generates a random keyfile in c:\goldkey.hex, and writes the file to a
' GoldCard. This script only demonstrates the possibilities of the commandline-
' paramters, and the written card cannot be used in existing keycard-systems.
' The script however demonstrates how a real-world keycard application can
' be implemented, using the Infinity USB (phoenix) as the keygenerator.
'
' Copyright WB Electronics 2003 - support@wbe.dk
'******************************************************************************

'Create hex-file
dim objFSO, objFSOFile
set objFSO = createobject("Scripting.FileSystemObject")

'Create ascii-file for writing, overwrite if already exists
set objFSOFile = objFSO.createtextfile("c:\goldkey.hex", true, false)


'Write data to the hexfile. Here we're using a fixed key for demonstration purposes.
'In a real-world application this would typically be a random key, which would 
'be stored in a database after generation. The key would then be inserted into the
'application code of the key-card, or appended to a hex-file on disk.

'Key-code and key:
objFSOFile.write ":100000008312DF3086008316DF30860007288113D5" & vbCrLf
'Fuses (codeprotection set, to make sure copying the card is impossible)
objFSOFile.write ":02400E000900A7" & vbCrLf
'End of hex-file marker:
objFSOFile.write ":00000001FF" & vbCrLf
objFSOFile.close

'Cleanup objects
set objFSOFile = nothing
set objFSO = nothing

'Write hex-file to GoldCard using Infinity USB (Phoenix)
dim wshShell, intError
set wshShell = CreateObject("WScript.Shell")

'Run Infinity USB in hidden mode
intError = wshShell.run("""c:\program files\Infinity USB\Infusb.exe"" -hidden -flash:c:\goldkey.hex -card:GoldCard -write", , true)

'Cleanup objects
set wshShell = nothing

'Display errormessage (if any)
select case intError
case 0
	wscript.echo("Keycard written successfully")
case 1
	wscript.echo("Keycard written successfully")
case 2
	wscript.echo("Errorcode: " & intError & " - Verification failed when writing card!")
case 3
	wscript.echo("Errorcode: " & intError & " - No card inserted or card was removed during programming!")
case 4
	wscript.echo("Errorcode: " & intError & " - Syntax error!")
case 5
	wscript.echo("Errorcode: " & intError & " - No hardware connected!")
case 10
	wscript.echo("Errorcode: " & intError & " - Error selecting card using the -card switch!")
case 11
	wscript.echo("Errorcode: " & intError & " - Error loading flash file using the -flash switch!")
case 12
	wscript.echo("Errorcode: " & intError & " - Error loading int. eeprom file using the -int switch!")
case 13
	wscript.echo("Errorcode: " & intError & " - Error loading ext. eeprom file using the -ext switch!")
case else
	wscript.echo("Errorcode: " & intError & " - Unknown error!")
end select


'Parameters for Infinity USB 1.40:
' -hidden           : Executes commands while Infinity USB is not displayed, must be used with -write switch
' -card:cardname    : Selects card. For instance goldcard or funcard
' -flash:filename   : Selects file to load for 1st file (flash on AVR-based - PIC on Microchip based cards)
' -int:filename     : Selects file to load for 2nd file (int. eeprom on AVR-based - ext. eeprom on Microchip based cards)
' -ext:filename     : Selects file to load for 3rd file (ext. eeprom on AVR-based - doesn't exist on Microchip based cards)
' -write            : Writes the selected files to the selected card
