Merge pull request #2 in PROCESSOR-SDK/mcsdk-tools from PRSDK-2194 to master
[keystone-rtos/mcsdk-tools.git] / program_evm / program_evm.js
1 // factory_defaults dss script
2 // Import the DSS packages into our namespace to save on typing
3 importPackage(Packages.com.ti.debug.engine.scripting);
4 importPackage(Packages.com.ti.ccstudio.scripting.environment);
5 importPackage(Packages.java.lang);
6 importPackage(Packages.java.io);
7 importPackage(Packages.java.util);
9 // Create our scripting environment object - which is the main entry point into
10 // any script and the factory for creating other Scriptable ervers and Sessions
11 var script = ScriptingEnvironment.instance()
13 var debugScriptEnv = ScriptingEnvironment.instance();
14 // program_evm environment.
15 testEnv = {};
17 // Get the Debug Server and start a Debug Session
18 var debugServer = script.getServer("DebugServer.1");
20 //***************Functions define***************************
23 function isFile(path)
24 {
25         try
26         {   
27                 file = new java.io.FileReader(path);
28         }
29         catch (ex)
30         {
31                 return false;
32         }
34         return true;
36 }
39 //****************Get New Time Stamp***********************
40 function localTime()
41 {
42         // get time stamp
43         var currentTime = new Date();
44         var year = currentTime.getFullYear();
45         var month = currentTime.getMonth() + 1;
46         month = month + "";
47         if (month.length == 1)
48         {
49                 month = "0" + month;
50         }
51         var day = currentTime.getDate();
52         var hour = currentTime.getHours();
53         var minute = currentTime.getMinutes();
54         minute = minute + "";
55         if (minute.length == 1)
56         {
57                 minute = "0" + minute;
58         }
59         var second = currentTime.getSeconds();
60         second = second + "";
61         if (second.length == 1)
62         {
63                 second = "0" + second;
64         }
65     
66         return (year+"_"+month+"_"+day+"_"+hour+minute+second);
67 }
69 /**
70  * Get error code from the given exception.
71  * @param {exception} The exception from which to get the error code.
72  */
73 function getErrorCode(exception)
74 {
75         var ex2 = exception.javaException;
76         if (ex2 instanceof Packages.com.ti.ccstudio.scripting.environment.ScriptingException) {
77                 return ex2.getErrorID();
78         }
79         return 0;
80 }
81 function fileCopy(source,destination)
82 {
83         inputFile = new File(source);
84     outputFile = new File(destination);
85     infile = new  java.io.FileReader(inputFile);
86     out = new java.io.FileWriter(outputFile);
87     var c;
89     while ((c = infile.read()) != -1)
90       out.write(c);
92     infile.close();
93     out.close();
94 }
96 function pausecomp(millis)
97  {
98   var date = new Date();
99   var curDate = null;
100   do { curDate = new Date(); }
101   while(curDate-date < millis)
102   { 
103   //print("Waiting "+millis+"ms...\r\n") 
104   }
107 //*******************************************
108 // Declarations and Inititalizations
109 var nandwriter_dir    = java.lang.System.getProperty("user.dir");
110 var dss_script_dir    = java.lang.System.getenv("DSS_SCRIPT_DIR");
111 var host_os           = "";
112 var script_logs    = nandwriter_dir+"/logs/";
113 var script_configs = nandwriter_dir+"/configs/";
114 var script_binaries = "../prebuilt-images/";
115 var targetConfig = "";
116 var writeAll = false;
117 var writerImages = "";
118 var big_endian = false;
119 var targetFlag = "unknown";
120 var targetConfig = "unknown";
121 var emul560 = false;
122 var xds200 = false;
123 var emulation_spec = "onboard XDS100";
124 testEnv.cioFile = null;
126 if (java.lang.System.getProperty("os.name").match(/Linux/i))
128         host_os = "-linuxhost";
131 // Parse the arguments
132 if (arguments.length > 0 && arguments.length < 3)
134     // parse the board spec
135     var board_spec = arguments[0].toLowerCase();
136     board_spec = board_spec.replace(/^tmd(x|s)/, "");
137     board_spec = board_spec.replace(/^evmc/, "evm");
138     
139     // find endian, user wants
140     if (board_spec.match(/-be$/))
141     {
142         big_endian = true;
143         board_spec = board_spec.replace(/-be$/, "");
144     }
145     else
146         board_spec = board_spec.replace(/-le$/, "");
147         
148     // find onboard emulation option for this board 
149     if (board_spec.match(/lx?e$/))
150     {
151         emul560 = true;
152         emulation_spec = "XDS560 mezzanine";
153         board_spec = board_spec.replace(/e$/, "");
154     }
155       
156     if (board_spec.match(/ls$/))
157     {
158         xds200 = true;
159         emulation_spec = "XDS200 emulator";
160         board_spec = board_spec.replace(/ls$/, "l");
161     }
163     if (board_spec.match(/k2h$/) || board_spec.match(/k2e$/) || board_spec.match(/k2l$/) || board_spec.match(/k2g$/))
164     {
165         emulation_spec = "XDS2xx emulator";
166     }
168         // for now, use the same software for lx and l variants
169     board_spec = board_spec.replace(/lx$/, "l");
171     // for now, treat evm6618l as an alias for evm6670l
172     board_spec = board_spec.replace(/evm6618/, "evm6670");
174     targetFlag = board_spec;
176     endian_spec = (big_endian ? "-be" : "");
178     board_binaries = script_binaries;
179     targetConfig = java.lang.System.getenv("PROGRAM_EVM_TARGET_CONFIG_FILE");
180     if (!targetConfig)
181         targetConfig = script_configs + targetFlag + "/" + targetFlag + (emul560 ? "e" : "") + (xds200 ? "s" : "") + host_os + ".ccxml";
183     print("board: " + targetFlag);
184     print("endian: " + (big_endian ? "Big" : "Little"));
185     print("emulation: " + emulation_spec);
186     print("binaries: " + board_binaries);
187     print("ccxml: " + targetConfig);
188     
189     var dir = new File(board_binaries);
190     if (!dir.exists())
191     {
192         print("board binaries directory not found");
193         java.lang.System.exit(2);
194     }
195     
196     if(arguments[1])
197         writerImages = arguments[1];
198     else
199         writeAll = true;   
201 else
203   print("Syntax error in command line");
204         print("Syntax: program_evm.js [tmdx|tmds]evm[c](<device>)l[x][e][-le|-be] [images_to_write]")
205   
206         print("    tmdx: TMDX type EVM")
207         print("    tmds: TMDS type EVM")
208         print("    c: Not used, for backward compatibility")
209         print("    <device> is the board name e.g 6472,6678 etc")
210         print("    l: Low cost EVM")
211         print("    x: EVM supports encryption")
212         print("    e: EVM uses 560 Mezzanine Emulator daughter card")
213         print("    le: Little Endian")
214         print("    be: Big Endian")
216         print("    example: TMDXEVM6678L-le")   
217         print("    [images_to_write] OPTIONAL is a list of the images to be written")
218         print("    example: eeprom50,nor")
219         print("    If not specified all (eeprom50,eeprom51,nand,nor) will be written")
220         java.lang.System.exit(0);
223 var i2cwriterbinary = board_binaries + "eepromwriter_" + targetFlag + ".out";
224 var nandwriterbinary = board_binaries + "nandwriter_" + targetFlag + ".out";
225 var norwriterbinary = board_binaries + "norwriter_" + targetFlag + ".out";
226 var eepromwriter_input51 = board_binaries + "eepromwriter_input51.txt";
227 var eepromwriter_input50 = board_binaries + "eepromwriter_input50.txt";
228 var eepromwriter_input = board_binaries + "eepromwriter_input.txt";
229 var eeprom50 = board_binaries + "eeprom50.bin";
230 var eeprom51 = board_binaries + "eeprom51.bin";
231 var nand = board_binaries + "nand.bin";
232 var nor = board_binaries + "nor.bin";
234 var custom_config = script_configs+targetFlag+"/program_evm_config";
235 if (isFile(custom_config))
237         file = new java.io.BufferedReader(new java.io.FileReader(custom_config))
238         while((line = file.readLine()) != null) {
239                 params = line.split("\\s+");
240                 if (params[1] == undefined) continue;
241                 if ("board_binaries" == params[0]) board_binaries = params[1];
242                 else if ("norwriterbinary" == params[0]) norwriterbinary = board_binaries + params[1];
243                 else if ("nandwriterbinary" == params[0]) nandwriterbinary = board_binaries + params[1];
244                 else if ("i2cwriterbinary" == params[0]) i2cwriterbinary = board_binaries + params[1];
245                 else if ("nand" == params[0]) nand = board_binaries + params[1];
246                 else if ("nor" == params[0]) nor = board_binaries + params[1];
247         }
248         file.close();
251 // Note: nAddress is the load address for using eepromwriter.
252 //       nandNorAddress is the address used for nandwriter and norwriter.
253 switch (targetFlag)
255         case "evm6457l":
256                 cpu_id = "C64XP_1";
257                 var nAddress = 0x800000;
258                 var nandNorAddress = nAddress;
259                 var iblByteSwap = false;
260                 break;
261         case "evm6474l":
262                 cpu_id = "C64XP_0";
263                 var nAddress = 0x800000;
264                 var nandNorAddress = nAddress;
265                 var iblByteSwap = false;
266                 break;
267         case "evm6455":
268                 cpu_id = "C64XP_0";
269                 var nAddress = 0x800000;
270                 var nandNorAddress = nAddress;
271                 var iblByteSwap = false;
272                 break;
273         case "evm6474":
274                 cpu_id = "C64XP_1A";
275                 var nAddress = 0x800000;
276                 var nandNorAddress = nAddress;
277                 var iblByteSwap = false;
278                 break;
279         case "evm6472l":
280                 cpu_id = "C64XP_A";
281                 var nAddress = 0x800000;
282                 var nandNorAddress = nAddress;
283                 var iblByteSwap = false;
284                 break;
285         case "evm6670l":
286                 cpu_id = "C66xx_0";
287                 var nAddress = 0x0C000000;
288                 var nandNorAddress = 0x80000000;
289                 var iblByteSwap = false;
290                 break;
291         case "evm6678l":
292                 cpu_id = "C66xx_0";
293                 var nAddress = 0x0C000000;
294                 var nandNorAddress = 0x80000000;
295                 var iblByteSwap = false;
296                 break;
297         case "evm6657l":
298                 cpu_id = "C66xx_0";
299                 var nAddress = 0x0C000000;
300                 var nandNorAddress = 0x80000000;
301                 var iblByteSwap = false;
302                 break;
303         case "evmk2h":
304                 cpu_id = "C66xx_0";
305                 var nAddress = 0x0C000000;
306                 var nandNorAddress = 0x80000000;
307                 var iblByteSwap = false;
308         break;
309         case "evmk2e":
310                 cpu_id = "C66xx_0";
311                 var nAddress = 0x0C000000;
312                 var nandNorAddress = 0x80000000;
313                 var iblByteSwap = false;
314         break;
315         case "evmk2l":
316                 cpu_id = "C66xx_0";
317                 var nAddress = 0x0C000000;
318                 var nandNorAddress = 0x80000000;
319                 var iblByteSwap = false;
320         break;
321         case "evmk2g":
322                 cpu_id = "C66xx_0";
323                 var nAddress = 0x0C000000;
324                 var nandNorAddress = 0x80000000;
325                 var iblByteSwap = false;
326         break;
327         default:
328                 script.traceWrite("Could not file cpu id for target " + targetFlag + "\n");
333 start = localTime();
334 testEnv.cioFile = script_logs+targetFlag+"_"+start+"-cio"+".txt";
335 // Create a log file in the current directory to log script execution
336 script.traceBegin(script_logs+targetFlag+"_"+start+"-trace"+".txt")
338 // Configure target
339 debugServer.setConfig(targetConfig);
340 pausecomp(1000);
341 debugSession = debugServer.openSession("*",cpu_id);
343 if (testEnv.cioFile != null)
344         debugSession.beginCIOLogging(testEnv.cioFile);
345 pausecomp(1000);
346 debugSession.target.connect();
347 pausecomp(1000);
348 debugSession.target.reset();
349 pausecomp(1000);
351 //POST
352 if(writeAll || writerImages.match(/eeprom50/))
354         //Write EEPROM
355         start = localTime();
356         script.traceWrite("Start writing eeprom50");
357         script.traceWrite("Writer:" + i2cwriterbinary + "\r\n");
358         script.traceWrite("Image:" + eeprom50 + "\r\n");
359         if (isFile(i2cwriterbinary) && isFile(eeprom50)) 
360         {
361                 fileCopy(eepromwriter_input50,eepromwriter_input);
362                 debugSession.memory.loadProgram(i2cwriterbinary);
363                 var nPage = 0x0;
364         //      var nAddress = 0x80000000;
365                 var sFilename = eeprom50 ;
366                 var nTypeSize = 32;
367                 var bByteSwap = false;
368                 try
369                 {
370                         debugSession.memory.loadRaw(nPage, nAddress, sFilename, nTypeSize, bByteSwap);
371                 }
372                 catch (ex)
373                 {
374                    errCode = getErrorCode(ex);
375                    script.traceWrite("Error code #" + errCode + ", could not load file " + sFilename +
376                                         " to target memory!");
377                 }
378                 debugSession.target.run()
379                 end = localTime();
380         }
381         else
382         {
383                 script.traceWrite("Required EEPROM50 files do not exist in " + board_binaries + "\n");
384          
385         }
388 //IBL 
389 if(writeAll || writerImages.match(/eeprom51/))
391         start = localTime();
392         script.traceWrite("Start writing eeprom51");
393         script.traceWrite("Writer:" + i2cwriterbinary + "\r\n");
394         script.traceWrite("Image:" + eeprom51 + "\r\n");
395         
396         if (isFile(i2cwriterbinary) && isFile(eeprom51)) 
397         {
398                 fileCopy(eepromwriter_input51,eepromwriter_input);
399                 debugSession.memory.loadProgram(i2cwriterbinary);
400                 var nPage = 0x0;
401         //      var nAddress = 0x80000000;
402                 var sFilename = eeprom51;
403                 var nTypeSize = 32;
404                 var bByteSwap = iblByteSwap;
405                 try
406                 {
407                         debugSession.memory.loadRaw(nPage, nAddress, sFilename, nTypeSize, bByteSwap);
408                 }
409                 catch (ex)
410                 {
411                    errCode = getErrorCode(ex);
412                    script.traceWrite("Error code #" + errCode + ", could not load file " + sFilename +
413                                         " to target memory!");
414                 }
416                 debugSession.target.run()
417                 end = localTime();
418         }
419         else
420         {
421                 script.traceWrite("Required EEPROM51 files do not exist in " + board_binaries + "\n");
422          
423         }
427 //NAND
428 if(writeAll || writerImages.match(/nand/))
430     if (writerImages.match(/format/))
431     {
432        /* No action is taken for NAND since it is format*/
433     }
434         else
435         {
436         start_nand = localTime();
437         script.traceWrite("Writer:" + nandwriterbinary + "\r\n");
438         script.traceWrite("NAND:" + nand + "\r\n");
439         if (isFile(nand) && isFile(nandwriterbinary)) 
440         {
441                 debugSession.memory.loadProgram(nandwriterbinary);
442                 var nPage = 0x0;
443         //      var nAddress = 0x80000000;
444                 var sFilename = nand;
445                 var nTypeSize = 32;
446                 var bByteSwap = false;
447     
448                 try
449                 {   
450                         script.traceWrite("Start loading nand.bin");
451                         debugSession.memory.loadRaw(nPage, nandNorAddress, sFilename, nTypeSize, bByteSwap);
452     
453                 }
454                 catch (ex)
455                 {
456                    errCode = getErrorCode(ex);
457                    script.traceWrite("Error code #" + errCode + ", could not load file " + sFilename +
458                                         " to target memory!");
459                 }
460                 script.traceWrite("Start programming NAND");
461                 debugSession.target.run()
462                 script.traceWrite("End programming NAND");
463                 end_nand = localTime();
464         }
465         else
466         {
467                 script.traceWrite("Required NAND files does not exist in " + board_binaries + "\n");
468          
469         }
470      }
473 //FORMAT the flash
474 if (writerImages.match(/format/))
476 //NAND Erase all
477     if (writerImages.match(/nand/))
478     {
479         start_nand = localTime();
480         script.traceWrite("Writer:" + nandwriterbinary + "\r\n");
481         if (isFile(nandwriterbinary)) 
482         {
483                 debugSession.memory.loadProgram(nandwriterbinary);
484                 var nPage = 0x0;
485                 var nValue = 0x12345678;
486     
487                 try
488                 {   
489                         script.traceWrite("Formatting NAND device ... Initiated");
490                         // Get the address of that symbol
491                         var flag_address = debugSession.symbol.getAddress("nand_erase_flag")                    
492                         debugSession.memory.writeWord(nPage, flag_address, nValue);
493                 }
494                 catch (ex)
495                 {
496                    errCode = getErrorCode(ex);
497                    script.traceWrite("Error code #" + errCode + ", could not set the nandwriter for erasing all nand blocks! ");
498                 }
499                 debugSession.target.run()
500                 script.traceWrite("Formatting NAND device ...Completed");
501                         end_nand = localTime();
502         }
503         else
504         {
505                 script.traceWrite("Required NAND binary does not exist in " + board_binaries + "\n");
506          
507         }
508     }
509         else
510         { 
511           /* No action is taken */
512           script.traceWrite("FLASH FORMAT - No Action is taken, please provide the supported format command string, e.g., format-nand");
513         }
515     if (testEnv.cioFile != null)
516     {
517         // Stop CIO logging.
518         debugSession.endCIOLogging();
519     }
520     
521     debugSession.terminate();
522     debugServer.stop()
523     
524     // Stop logging and exit.
525     script.traceEnd();
526     java.lang.System.exit(0);
527         
530 //NOR
531 if(writeAll || writerImages.match(/nor/))
533         start_nor = localTime();
534         script.traceWrite("Writer:" + norwriterbinary + "\r\n");
535         script.traceWrite("NOR:" + nor + "\r\n");
537         
538         if (isFile(nor) && isFile(norwriterbinary)) 
539         {
540         
541                 debugSession.memory.loadProgram(norwriterbinary);
542                 var nPage = 0x0;
543         //      var nAddress = 0x80000000;
544                 var sFilename = nor ;
545                 var nTypeSize = 32;
546                 var bByteSwap = false;
547                 try
548                 {   
549                         script.traceWrite("Start loading nor.bin");
550                         debugSession.memory.loadRaw(nPage, nandNorAddress, sFilename, nTypeSize, bByteSwap);
552                 }
553                 catch (ex)
554                 {
555                    errCode = getErrorCode(ex);
556                    script.traceWrite("Error code #" + errCode + ", could not load file " + sFilename +
557                                         " to target memory!");
558                 }
559                 script.traceWrite("Start programming NOR");
560                 script.traceWrite(localTime());
561                 debugSession.target.run()
562                 script.traceWrite("End programming NOR");
563                 end_nor = localTime();
564     }
565         else
566         {
567                 script.traceWrite("Required NOR files does not exist in " + board_binaries + "\n");
568          
569         }
573 if (testEnv.cioFile != null)
575         // Stop CIO logging.
576         debugSession.endCIOLogging();
579 debugSession.terminate();
580 debugServer.stop()
582 // Stop logging and exit.
583 script.traceEnd();
584 java.lang.System.exit(0);