importPackage(Packages.com.ti.debug.engine.scripting); importPackage(Packages.com.ti.ccstudio.scripting.environment); importPackage(Packages.java.lang); function printExit(exit_reason, exit_code) { for(i = 0; i < cpuSessions.length; terminateSession(cpuSessions[i++])); debugServer.stop(); print( "\n" + ( (exit_code > 0) ? "ERROR: " : "" ) + exit_reason + "\n"); java.lang.System.exit(exit_code); // return process.exit(exit_code); } function deMangleString(argStr) { tempStr = []; for(j = 0; j < argStr.length; j++) { var ch = argStr[j]; if(ch == "'") tempStr[j] = ""; else if(ch == "|") tempStr[j] = " "; else tempStr[j] = ch; } return tempStr.join(""); } function initSession(sessionName) { var session; print("Opening session \"" + sessionName + "\""); var attempt = 0; while(attempt < 3) { try { session = debugServer.openSession(sessionName); print("Session [" + sessionName + "] opened: " + session); session.target.connect(); print( "Connected to [" + sessionName + "]: " + session.target.isConnected() + "\n" ); return session; } catch( ex ) { attempt++; print( "Exception " + attempt + " caught: " + ex); } } printExit("Failed to connect to CPU [" + sessionName +"]", 6); } function terminateSession(session) { print( "Closing session [" + session.getCPUName() + "]"); try { session.target.disconnect(); print( "Tgt [" + session + "] disconnected: " + !session.target.isConnected() ); session.terminate(); } catch( ex ) { print( "Exception caught: " + ex ); } print("\n"); } // // main program // var FlashKeyNames = [ "FlashKey0", "FlashKey1", "FlashKey2", "FlashKey3", "FlashKey4", "FlashKey5", "FlashKey6", "FlashKey7" ]; var FlashSectors = [ "FlashSectorA", "FlashSectorB", "FlashSectorC", "FlashSectorD", "FlashSectorE", "FlashSectorF", "FlashSectorG", "FlashSectorH" ]; var script = ScriptingEnvironment.instance(); var debugServer = script.getServer( "DebugServer.1" ); var cpuSessions = []; var deviceCCXMLFile = null; var applicationFile = null; var eraseSector = [false, false, false, false, false, false, false, false]; var FlashKeyValues = [ "FFFF", "FFFF", "FFFF", "FFFF", "FFFF", "FFFF", "FFFF", "FFFF" ]; var selectedCPU = -1; var softwareChecksum = 0; var useKey = false; var useMask = false; var useDownload = false; var useChecksum = false; try { for(i = 0; i < arguments.length; i++) { inputArg = arguments[i]; switch (inputArg) { case '--ccxml': i++; var deviceCCXMLFile = deMangleString(arguments[i]); print("CCXML set to " + deviceCCXMLFile); break; case '--mask': i++; if(arguments[i] != "-") { hexMask = parseInt(arguments[i], 16); if(hexMask == NaN) { printExit("Invalid erase mask (" + arguments[i] + ")", 1); } for(j = 0; j < 8; j++) { eraseSector[j] = (hexMask & (1 << (7 - j))) > 0 ? false : true; print("\t" + FlashSectors[j] + " = " + eraseSector[j]); } useMask = true; } break; case "--cpu": i++; selectedCPU = parseInt(arguments[i]); break; case "--key": i++; if(arguments[i] != "-") { if(arguments[i].length != 16) { printExit("Security key should be 16 characters", 2); } print("Setting security key:"); for(j = 0; j < 8; j++) { FlashKeyValues[j] = arguments[i].substring(j*4, 4); print("\t" + FlashKeyNames[j] + " = " + FlashKeyValues[j]); } useKey = true; } break; case '--app': i++; var appFName = deMangleString(arguments[i]); print("App filename = " + appFName); if(appFName.substring(appFName.lastIndexOf('\\')+1) != "-") { applicationFile = deMangleString(arguments[i]); print("Download file set to " + applicationFile); useDownload = true; } break; case '--chk': i++; if(arguments[i] != "-") { softwareChecksum = parseInt(arguments[i], 16); if(softwareChecksum == NaN) { printExit("Invalid erase mask (" + arguments[i] + ")", 3); } useChecksum = true; } break; case '--log': i++; var logLevel = arguments[i]; switch(logLevel) { case 'OFF': case 'SEVERE': case 'WARNING': case 'INFO': case 'CONFIG': case 'FINE': case 'FINER': case 'FINEST': case 'ALL': print("LogLevel set to " + logLevel); script.traceSetConsoleLevel(logLevel); break; default: print("LogLevel [" + logLevel + "] unsupported"); break; } default: break; } } if(deviceCCXMLFile == null || deviceCCXMLFile == "") { printExit("No CCXML specified", 4); } if(selectedCPU < 0) { printExit("No CPU target specified", 5); } debugServer.setConfig( deviceCCXMLFile ); cpuArr = debugServer.getListOfCPUs(); } catch(ex) { printExit("Unspecified error:" + ex, 20); } for(i = 0; i < cpuArr.length; cpuSessions[i] = initSession(cpuArr[i++])); try { var session = cpuSessions[selectedCPU]; var cpu = cpuArr[selectedCPU]; if(useKey == true) { print("Unlocking"); for(i = 0; i < 8; session.flash.options.setString(FlashKeyNames[i], FlashKeyValues[i++])); session.flash.unlock(); print("CPU " + session.getCPUName() + " unlocked"); } if(useMask == true) { print("Erasing flash on " + cpu); for(i = 0; i < 8; session.flash.options.setBoolean(FlashSectors[i], eraseSector[i++])); session.flash.erase(); print("CPU " + session.getCPUName() + " flash eraseed"); } if(useDownload == true) { print("Flashing " + cpu); session.memory.loadProgram(applicationFile); print(applicationFile + " flashed to " + session.getCPUName()); } if(useKey == true) { for(i = 0; i < 8; session.flash.options.setString(FlashKeyNames[i], FlashKeyValues[i++])); session.flash.lock(); print("CPU " + session.getCPUName() + " locked"); } if(useChecksum == true) { print("Calculating checksum"); var calc = session.flash.getFlashChecksum(); if(calc != softwareChecksum) { printExit("Checksum failure: calculated checksum = " + calc + ", NOT " + softwareChecksum.toString(16), 7); } print("Checksum " + softwareChecksum.toString(16) + " verified"); print("\n"); } } catch(ex) { printExit("Operation error " + ex, 5); } printExit("DSS Script finished", 0); /* unlockCPU(s); print("\n"); eraseFlash(s, eraseMask); print("\n"); lockCPU(s); print("\n"); flashApplication(s, applicationFile); print("\n"); */