b7412422e5cf13071470f7596c2d24a827121489
[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 }
82 function fileCopy(source,destination)
83 {
84         inputFile = new File(source);
85     outputFile = new File(destination);
86     infile = new  java.io.FileReader(inputFile);
87     out = new java.io.FileWriter(outputFile);
88     var c;
90     while ((c = infile.read()) != -1)
91       out.write(c);
93     infile.close();
94     out.close();
95 }
97 function pausecomp(millis)
98  {
99   var date = new Date();
100   var curDate = null;
101   do { curDate = new Date(); }
102   while(curDate-date < millis)
103   { 
104   //print("Waiting "+millis+"ms...\r\n") 
105   }
108 function getFilesizeinBytes(file)
110   inputFile = new File(file);
111   return inputFile.length();
114 function fileCompare(file1,file2,bytes)
116     a = new File(file1);
117     b = new File(file2);
118     infile1 = new  java.io.FileReader(a);
119     infile2 = new java.io.FileReader(b);
120     var c;
121     while(bytes) {
122         c = infile1.read();
123         d = infile2.read();
124         if (c != d)
125             return 1;
126         bytes--;
127     }
128     infile1.close();
129     infile2.close();
130     return 0
133 // Check the content loaded into DDR against the original file
134 function checkDdrContent(nPage, nAddress, nSize, nTypeSize, sFilename, saveByteSwap)
136     script.traceWrite("Comparing DDR content with " + sFilename + " ...");
137     file = new File(sFilename);
138     cfile = file.getParent() + "\\ddr-out";
139     script.traceWrite("cfile is " + cfile);
140     debugSession.memory.saveRaw(nPage, nAddress, cfile, nSize/4, nTypeSize, saveByteSwap); // Get content from DDR and load it into file at ../ddr-out
141     script.traceWrite("File size is eeprom " + getFilesizeinBytes(sFilename));
142     script.traceWrite("File size is " + cfile + " " + getFilesizeinBytes(cfile));
143     var cmp = fileCompare(sFilename, cfile, nSize)
144     script.traceWrite("Compare returns " + cmp);
145     delete(cfile);
146     return cmp;
149 function cleanup()
151         if (testEnv.cioFile != null)
152         {
153           // Stop CIO logging.
154           debugSession.endCIOLogging();
155         }
157         debugSession.terminate();
158         debugServer.stop()
160         // Stop logging and exit.
161         script.traceEnd();
162         java.lang.System.exit(0);
165 //*******************************************
166 // Declarations and Inititalizations
167 var nandwriter_dir    = java.lang.System.getProperty("user.dir");
168 var dss_script_dir    = java.lang.System.getenv("DSS_SCRIPT_DIR");
169 var host_os           = "";
170 var script_logs    = nandwriter_dir+"/logs/";
171 var script_configs = nandwriter_dir+"/configs/";
172 var script_binaries = nandwriter_dir+"/binaries/";
173 var targetConfig = "";
174 var writeAll = false;
175 var writerImages = "";
176 var big_endian = false;
177 var targetFlag = "unknown";
178 var targetConfig = "unknown";
179 var emul560 = false;
180 var xds200 = false;
181 var emulation_spec = "onboard XDS100";
182 var verifyDdr = false;
184 testEnv.cioFile = null;
186 if (java.lang.System.getProperty("os.name").match(/Linux/i))
188         host_os = "-linuxhost";
191 // Parse the arguments
192 if (arguments.length > 0 && arguments.length < 4)
194     // parse the board spec
195     var board_spec = arguments[0].toLowerCase();
196     board_spec = board_spec.replace(/^tmd(x|s)/, "");
197     board_spec = board_spec.replace(/^evmc/, "evm");
198     
199     // find endian, user wants
200     if (board_spec.match(/-be$/))
201     {
202         big_endian = true;
203         board_spec = board_spec.replace(/-be$/, "");
204     }
205     else
206         board_spec = board_spec.replace(/-le$/, "");
207         
208     // find onboard emulation option for this board 
209     if (board_spec.match(/lx?e$/))
210     {
211         emul560 = true;
212         emulation_spec = "XDS560 mezzanine";
213         board_spec = board_spec.replace(/e$/, "");
214     }
215       
216     if (board_spec.match(/ls$/))
217     {
218         xds200 = true;
219         emulation_spec = "XDS200 emulator";
220         board_spec = board_spec.replace(/ls$/, "l");
221     }    
222         
223     // for now, use the same software for lx and l variants
224     board_spec = board_spec.replace(/lx$/, "l");
225     
226     // for now, treat evm6618l as an alias for evm6670l
227     board_spec = board_spec.replace(/evm6618/, "evm6670");
228         
229     targetFlag = board_spec;
230     
231     endian_spec = (big_endian ? "-be" : "");
232     
233     board_binaries = script_binaries + targetFlag + endian_spec + "/";
234     targetConfig = java.lang.System.getenv("PROGRAM_EVM_TARGET_CONFIG_FILE");
235     if (!targetConfig)    
236         targetConfig = script_configs + targetFlag + "/" + targetFlag + (emul560 ? "e" : "") + (xds200 ? "s" : "") + host_os + ".ccxml";
238     print("board: " + targetFlag);
239     print("endian: " + (big_endian ? "Big" : "Little"));
240     print("emulation: " + emulation_spec);
241     print("binaries: " + board_binaries);
242     print("ccxml: " + targetConfig);
243     
244     var dir = new File(board_binaries);
245     if (!dir.exists())
246     {
247         print("board binaries directory not found");
248         java.lang.System.exit(2);
249     }
250     
251     if(arguments[1])
252         verifyDdr = (arguments[1] == 1) ? true : false;
253     
254     if(arguments[2])
255         writerImages = arguments[2];
256     else
257         writeAll = true;   
259 else
261   print("Syntax error in command line");
262         print("Syntax: program_evm.js [tmdx|tmds]evm[c](<device>)l[x][e][-le|-be] [1|0] [images_to_write]")
263   
264         print("    tmdx: TMDX type EVM")
265         print("    tmds: TMDS type EVM")
266         print("    c: Not used, for backward compatibility")
267         print("    <device> is the board name e.g 6472,6678 etc")
268         print("    l: Low cost EVM")
269         print("    x: EVM supports encryption")
270         print("    e: EVM uses 560 Mezzanine Emulator daughter card")
271         print("    le: Little Endian")
272         print("    be: Big Endian")
274         print("    example: TMDXEVM6678L-le")
275         print("    [1|0] OPTIONAL. If 1, ddr contents are verified against file to be written else ddr contents are not verified. Recommended to set to 1 for use with 560 Mezzanine Emulator daughter card only ")     
276         print("    [images_to_write] OPTIONAL is a list of the images to be written")
277         print("    example: eeprom50,nor")
278         print("    If not specified all (eeprom50,eeprom51,nand,nor) will be written")
279         java.lang.System.exit(0);
282 var i2cwriterbinary = board_binaries + "eepromwriter_" + targetFlag + ".out";
283 var nandwriterbinary = board_binaries + "nandwriter_" + targetFlag + ".out";
284 var norwriterbinary = board_binaries + "norwriter_" + targetFlag + ".out";
285 var eepromwriter_input51 = board_binaries + "eepromwriter_input51.txt";
286 var eepromwriter_input50 = board_binaries + "eepromwriter_input50.txt";
287 var eepromwriter_input = board_binaries + "eepromwriter_input.txt";
288 var eeprom50 = board_binaries + "eeprom50.bin";
289 var eeprom51 = board_binaries + "eeprom51.bin";
290 var nand = board_binaries + "nand.bin";
291 var nor = board_binaries + "nor.bin";
293 // Note: nAddress is the load address for using eepromwriter.
294 //       nandNorAddress is the address used for nandwriter and norwriter.
295 switch (targetFlag)
297         case "evm6457l":
298                 cpu_id = "C64XP_1";
299                 var nAddress = 0x800000;
300                 var nandNorAddress = nAddress;
301                 var iblByteSwap = false;
302                 break;
303         case "evm6474l":
304                 cpu_id = "C64XP_0";
305                 var nAddress = 0x800000;
306                 var nandNorAddress = nAddress;
307                 var iblByteSwap = false;
308                 break;
309         case "evm6455":
310                 cpu_id = "C64XP_0";
311                 var nAddress = 0x800000;
312                 var nandNorAddress = nAddress;
313                 var iblByteSwap = false;
314                 break;
315         case "evm6474":
316                 cpu_id = "C64XP_1A";
317                 var nAddress = 0x800000;
318                 var nandNorAddress = nAddress;
319                 var iblByteSwap = false;
320                 break;
321         case "evm6472l":
322                 cpu_id = "C64XP_A";
323                 var nAddress = 0x800000;
324                 var nandNorAddress = nAddress;
325                 var iblByteSwap = false;
326                 break;
327         case "evm6670l":
328                 cpu_id = "C66xx_0";
329                 var nAddress = 0x0C000000;
330                 var nandNorAddress = 0x80000000;
331                 var iblByteSwap = false;
332                 break;
333         case "evm6678l":
334                 cpu_id = "C66xx_0";
335                 var nAddress = 0x0C000000;
336                 var nandNorAddress = 0x80000000;
337                 var iblByteSwap = false;
338                 break;
339         case "evm6657l":
340                 cpu_id = "C66xx_0";
341                 var nAddress = 0x0C000000;
342                 var nandNorAddress = 0x80000000;
343                 var iblByteSwap = false;
344                 break;
345         default:
346                 script.traceWrite("Could not file cpu id for target " + targetFlag + "\n");
351 start = localTime();
352 testEnv.cioFile = script_logs+targetFlag+"_"+start+"-cio"+".txt";
353 // Create a log file in the current directory to log script execution
354 script.traceBegin(script_logs+targetFlag+"_"+start+"-trace"+".txt")
356 // Configure target
357 debugServer.setConfig(targetConfig);
358 pausecomp(1000);
359 debugSession = debugServer.openSession("*",cpu_id);
361 if (testEnv.cioFile != null)
362         debugSession.beginCIOLogging(testEnv.cioFile);
363 pausecomp(1000);
364 debugSession.target.connect();
365 pausecomp(1000);
366 debugSession.target.reset();
367 pausecomp(1000);
369 //POST
370 if(writeAll || writerImages.match(/eeprom50/))
372         //Write EEPROM
373         start = localTime();
374         script.traceWrite("Start writing eeprom50");
375         script.traceWrite("Writer:" + i2cwriterbinary + "\r\n");
376         script.traceWrite("Image:" + eeprom50 + "\r\n");
377         if (isFile(i2cwriterbinary) && isFile(eeprom50)) 
378         {
379                 fileCopy(eepromwriter_input50,eepromwriter_input);
380                 debugSession.memory.loadProgram(i2cwriterbinary);
381                 var nPage = 0x0;
382         //      var nAddress = 0x80000000;
383                 var sFilename = eeprom50 ;
384                 var nTypeSize = 32;
385                 var bByteSwap = false;
386                 var nSize = getFilesizeinBytes(sFilename);
387                 try
388                 {
389                         debugSession.memory.loadRaw(nPage, nAddress, sFilename, nTypeSize, bByteSwap);
390                 }
391                 catch (ex)
392                 {
393                    errCode = getErrorCode(ex);
394                    script.traceWrite("Error code #" + errCode + ", could not load file " + sFilename +
395                                         " to target memory!");
396                 }
397                 debugSession.target.run()
398                 if (verifyDdr){
399                         if(checkDdrContent(nPage, nAddress, nSize, nTypeSize, sFilename, bByteSwap) == 1) {
400                                 script.traceWrite("Programming eeprom50 has failed. Please retry programming \n");
401                                 cleanup();
402                         }
403                 }
404                 end = localTime();
405         }
406         else
407         {
408                 script.traceWrite("Required EEPROM50 files do not exist in " + board_binaries + "\n");
409          
410         }
413 //IBL 
414 if(writeAll || writerImages.match(/eeprom51/))
416         start = localTime();
417         script.traceWrite("Start writing eeprom51");
418         script.traceWrite("Writer:" + i2cwriterbinary + "\r\n");
419         script.traceWrite("Image:" + eeprom51 + "\r\n");
420         
421         if (isFile(i2cwriterbinary) && isFile(eeprom51)) 
422         {
423                 fileCopy(eepromwriter_input51,eepromwriter_input);
424                 debugSession.memory.loadProgram(i2cwriterbinary);
425                 var nPage = 0x0;
426         //      var nAddress = 0x80000000;
427                 var sFilename = eeprom51;
428                 var nTypeSize = 32;
429                 var bByteSwap = iblByteSwap;
430                 var nSize = getFilesizeinBytes(sFilename);
431                 try
432                 {
433                         debugSession.memory.loadRaw(nPage, nAddress, sFilename, nTypeSize, bByteSwap);
434                 }
435                 catch (ex)
436                 {
437                    errCode = getErrorCode(ex);
438                    script.traceWrite("Error code #" + errCode + ", could not load file " + sFilename +
439                                         " to target memory!");
440                 }
442                 debugSession.target.run()
443                 if (verifyDdr){
444                         if(checkDdrContent(nPage, nAddress, nSize, nTypeSize, sFilename, bByteSwap) == 1) {
445                                 script.traceWrite("Programming eeprom51 has failed. Please retry programming \n");
446                                 cleanup();
447                         }
448                 }
449                 end = localTime();
450         }
451         else
452         {
453                 script.traceWrite("Required EEPROM51 files do not exist in " + board_binaries + "\n");
454          
455         }
459 //NAND
460 if(writeAll || writerImages.match(/nand/))
462     if (writerImages.match(/format/))
463     {
464        /* No action is taken for NAND since it is format*/
465     }
466         else
467         {
468         start_nand = localTime();
469         script.traceWrite("Writer:" + nandwriterbinary + "\r\n");
470         script.traceWrite("NAND:" + nand + "\r\n");
471         if (isFile(nand) && isFile(nandwriterbinary)) 
472         {
473                 debugSession.memory.loadProgram(nandwriterbinary);
474                 var nPage = 0x0;
475         //      var nAddress = 0x80000000;
476                 var sFilename = nand;
477                 var nTypeSize = 32;
478                 var bByteSwap = false;
479                 var nSize = getFilesizeinBytes(sFilename);
480     
481                 try
482                 {   
483                         script.traceWrite("Start loading nand.bin");
484                         debugSession.memory.loadRaw(nPage, nandNorAddress, sFilename, nTypeSize, bByteSwap);
485     
486                 }
487                 catch (ex)
488                 {
489                    errCode = getErrorCode(ex);
490                    script.traceWrite("Error code #" + errCode + ", could not load file " + sFilename +
491                                         " to target memory!");
492                 }
493                 script.traceWrite("Start programming NAND");
494                 debugSession.target.run()
495                 if (verifyDdr){
496                         if(checkDdrContent(nPage, nandNorAddress, nSize, nTypeSize, sFilename, bByteSwap) == 1) {
497                                 script.traceWrite("Programming nand has failed. Please retry programming \n");
498                                 cleanup();
499                         }
500                 }
501                 script.traceWrite("End programming NAND");
502                 end_nand = localTime();
503         }
504         else
505         {
506                 script.traceWrite("Required NAND files does not exist in " + board_binaries + "\n");
507          
508         }
509      }
512 //FORMAT the flash
513 if (writerImages.match(/format/))
515 //NAND Erase all
516     if (writerImages.match(/nand/))
517     {
518         start_nand = localTime();
519         script.traceWrite("Writer:" + nandwriterbinary + "\r\n");
520         if (isFile(nandwriterbinary)) 
521         {
522                 debugSession.memory.loadProgram(nandwriterbinary);
523                 var nPage = 0x0;
524                 var nValue = 0x12345678;
525     
526                 try
527                 {   
528                         script.traceWrite("Formatting NAND device ... Initiated");
529                         // Get the address of that symbol
530                         var flag_address = debugSession.symbol.getAddress("nand_erase_flag")                    
531                         debugSession.memory.writeWord(nPage, flag_address, nValue);
532                 }
533                 catch (ex)
534                 {
535                    errCode = getErrorCode(ex);
536                    script.traceWrite("Error code #" + errCode + ", could not set the nandwriter for erasing all nand blocks! ");
537                 }
538                 debugSession.target.run()
539                 script.traceWrite("Formatting NAND device ...Completed");
540                         end_nand = localTime();
541         }
542         else
543         {
544                 script.traceWrite("Required NAND binary does not exist in " + board_binaries + "\n");
545          
546         }
547     }
548         else
549         { 
550           /* No action is taken */
551           script.traceWrite("FLASH FORMAT - No Action is taken, please provide the supported format command string, e.g., format-nand");
552         }
554     if (testEnv.cioFile != null)
555     {
556         // Stop CIO logging.
557         debugSession.endCIOLogging();
558     }
559     
560     debugSession.terminate();
561     debugServer.stop()
562     
563     // Stop logging and exit.
564     script.traceEnd();
565     java.lang.System.exit(0);
566         
569 //NOR
570 if(writeAll || writerImages.match(/nor/))
572         start_nor = localTime();
573         script.traceWrite("Writer:" + norwriterbinary + "\r\n");
574         script.traceWrite("NOR:" + nor + "\r\n");
576         
577         if (isFile(nor) && isFile(norwriterbinary)) 
578         {
579         
580                 debugSession.memory.loadProgram(norwriterbinary);
581                 var nPage = 0x0;
582         //      var nAddress = 0x80000000;
583                 var sFilename = nor ;
584                 var nTypeSize = 32;
585                 var bByteSwap = false;
586                 var nSize = getFilesizeinBytes(sFilename);
587                 try
588                 {   
589                         script.traceWrite("Start loading nor.bin");
590                         debugSession.memory.loadRaw(nPage, nandNorAddress, sFilename, nTypeSize, bByteSwap);
592                 }
593                 catch (ex)
594                 {
595                    errCode = getErrorCode(ex);
596                    script.traceWrite("Error code #" + errCode + ", could not load file " + sFilename +
597                                         " to target memory!");
598                 }
599                 script.traceWrite("Start programming NOR");
600                 script.traceWrite(localTime());
601                 debugSession.target.run()
602                 if (verifyDdr){
603                         if(checkDdrContent(nPage, nandNorAddress, nSize, nTypeSize, sFilename, bByteSwap) == 1) {
604                                 script.traceWrite("Programming nor has failed. Please retry programming \n");
605                                 cleanup();
606                         }
607                 }
608                 script.traceWrite("End programming NOR");
609                 end_nor = localTime();
610     }
611         else
612         {
613                 script.traceWrite("Required NOR files does not exist in " + board_binaries + "\n");
614          
615         }
619 if (testEnv.cioFile != null)
621         // Stop CIO logging.
622         debugSession.endCIOLogging();
625 debugSession.terminate();
626 debugServer.stop()
628 // Stop logging and exit.
629 script.traceEnd();
630 java.lang.System.exit(0);