Rev Author Line No. Line
2061 mija 1 /******************** (C) COPYRIGHT 2010 STMicroelectronics ********************
2 * File Name : STMFlashLoader.cpp
3 * Author : MCD Application Team
4 * Version : v2.2.0
5 * Date : 05/03/2010
6 * Description : STM Flash Loader command line version
7 ********************************************************************************
8 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
9 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
10 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
11 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
12 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
13 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
14 *******************************************************************************/
15  
16 #include "stdafx.h"
17 #include "string.h"
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <dos.h>
23  
24 #include "../STBLLIB/STBLLIB.h"
25 #include "../Files/Files.h"
26 #include "ini.h"
27  
28  
29 #define NONE = 0;
30 #define ODD = 1;
31 #define EVEN = 2;
32  
33 typedef enum STATE {OK,KO};
34  
35 char MapFile[256];
36 PMAPPING pmMapping;
37  
38 int TimeBO = 100;
39  
40 BOOL SHOW_OK = TRUE; // Set to TRUE/FALSE to show/hide OK status messages
41 BOOL SHOW_KO = TRUE; // Set to TRUE/FALSE to show/hide KO status messages
42  
43 /*******************************************************************************************/
44 /* Function : FileExist */
45 /* IN : file name */
46 /* OUT : boolean */
47 /* Description : verify if the given file exists */
48 /*******************************************************************************************/
49 BOOL FileExist(LPCTSTR filename)
50 {
51 // Data structure for FindFirstFile
52 WIN32_FIND_DATA findData;
53  
54 // Clear find structure
55 ZeroMemory(&findData, sizeof(findData));
56  
57 // Search the file
58 HANDLE hFind = FindFirstFile( filename, &findData );
59 if ( hFind == INVALID_HANDLE_VALUE )
60 {
61 // File not found
62 return false;
63 }
64  
65 // File found
66  
67 // Release find handle
68 FindClose( hFind );
69 hFind = NULL;
70  
71 // The file exists
72 return true;
73 }
74  
75 /*******************************************************************************************/
76 /* Function : void man() */
77 /* IN : */
78 /* OUT : */
79 /* Description : print the manual on the standard output */
80 /*******************************************************************************************/
81 void man()
82 {
83 printf("STMicroelectronics UART Flash Loader command line v2.2.0 \n\n");
84 printf(" Usage : \n\n");
85 printf(" STMFlashLoader.exe [options] [Agrument][[options] [Agrument]...] \n\n");
86  
87 printf(" -? (Show this help) \n");
88 printf(" -c (Establish connection to the COM port) \n");
89 printf(" --pn port_nb : e.g: 1, 2 ..., default 1 \n");
90 printf(" --br baud_rate : e.g: 115200, 57600 ..., default 57600 \n");
91 printf(" --db data_bits : value in {5,6,7,8} ..., default 8 \n");
92 printf(" --pr parity : value in {NONE,ODD,EVEN} ..., default EVEN \n");
93 printf(" --sb stop_bits : value in {1,1.5,2} ..., default 1 \n");
94 printf(" --ec echo : value OFF or ECHO or LISTEN ..., default is OFF \n");
95 printf(" --co control : Enable or Disable RTS and DTR outputs control \n");
96 printf(" : value OFF or ON ..., default is OFF \n");
97 printf(" --to time_out : (ms) e.g 1000, 2000, 3000 ..., default 5000 \n");
98  
99 printf(" -Rts (set Rts line to Hi, Lo)\n");
100 printf(" --State : State in {Hi, Lo} \n");
101 printf(" -Dtr (Set Rts line to Hi, Lo)\n");
102 printf(" --State : State in {Hi, Lo}\n");
103  
104 printf(" -i device_name (e.g STM32_Low-density_16K, [See the Map directory]) \n");
105  
106 printf(" -e (erase flash pages\n");
107 printf(" --all all pages : erase all pages\n");
108 printf(" --sec number_of_pages_group pages_group_codes : erase specified group pages \n");
109  
110 printf(" -u (Upload flash contents to a .bin, .hex or .s19 file )\n");
111 printf(" --fn file_name : full path name of the file \n");
112  
113 printf(" -d (Download the content of a file into MCU flash) \n");
114 printf(" --a address(hex): start @ in hex ; ignored if it is not a binary file \n");
115 printf(" --fn file_name : full path name (.bin, .hex or .s19 file) \n");
116 printf(" --v : verify after download \n");
117 printf(" --o : optimize; removes FFs data \n");
118  
119  
120 printf(" -r (Run the flash code at the specified address \n");
121 printf(" --a address(hex) : address in hexadecimal) \n");
122  
123  
124 printf(" -p (Enable or Disable protections) \n");
125 printf(" --ewp : enable write protection for sector codes (e.g 1,2,etc.) \n");
126 printf(" --dwp : disable write protection \n");
127 printf(" --drp : disable read protection \n");
128 printf(" --erp : enable read protection, all arguments following this one will fail \n");
129  
130  
131 printf(" -o (Get or Set STM32 option bytes: use -d command for STM8!) \n");
132 printf(" --get --fn file_name : get option bytes from the device \n");
133 printf(" and write it in the specified file \n");
134 printf(" --set --fn file_name : load option bytes from the specified file \n");
135 printf(" and write it to the device \n");
136 printf(" --set --vals --OPB hex_val : set the specified option byte; OPB in: User, \n");
137 printf(" RDP, Data0, Data1, WRP0, WRP1, WRP2, WRP3 \n");
138  
139  
140 }
141  
142  
143  
144  
145  
146 /*******************************************************************************************/
147 /* Function : ParityToInt */
148 /* IN : parity as string (NONE, ODD, EVEN) */
149 /* OUT : integer */
150 /* Description : Get the integer representation of the given parity */
151 /*******************************************************************************************/
152 int ParityToInt(char* parity)
153 {
154 if (strcmp(parity,"NONE")==0) return 0;
155 else if(strcmp(parity,"ODD")==0) return 1;
156 else if(strcmp(parity,"EVEN")==0) return 2;
157 else return 2;
158 }
159  
160  
161 /*******************************************************************************************/
162 /* Function : ModeToInt */
163 /* IN : Mode as string (OFF, ECHO, LISTEN) */
164 /* OUT : int */
165 /* Description : Get the int representation of the given string Mode */
166 /*******************************************************************************************/
167 int ModeToInt(char* status)
168 {
169 if (strcmp(status,"OFF")==0) return 0;
170 else if(strcmp(status,"ECHO")==0) return 1;
171 else if(strcmp(status,"LISTEN")==0) return 2;
172 else return 0;
173 }
174  
175 /*******************************************************************************************/
176 /* Function : StatusToBool */
177 /* IN : Status as string (ON, OFF) */
178 /* OUT : Bool */
179 /* Description : Get the boolean representation of the given string ON/OFF */
180 /*******************************************************************************************/
181 bool StatusToBool(char* status)
182 {
183 if (strcmp(status,"OFF")==0) return false;
184 else if(strcmp(status,"ON")==0) return true;
185 else return false;
186 }
187  
188  
189 /*******************************************************************************************/
190 /* Function : Is_Option */
191 /* IN : option as string */
192 /* OUT : boolean */
193 /* Description : Verify if the given string present an option */
194 /*******************************************************************************************/
195 bool Is_Option(char* option)
196 {
197 if (strcmp(option,"-?")==0) return true;
198 else if (strcmp(option,"-c")==0) return true;
199 else if (strcmp(option,"-i")==0) return true;
200 else if (strcmp(option,"-e")==0) return true;
201 else if (strcmp(option,"-u")==0) return true;
202 else if (strcmp(option,"-d")==0) return true;
203 else if (strcmp(option,"-v")==0) return true;
204 else if (strcmp(option,"-p")==0) return true;
205 else if (strcmp(option,"-r")==0) return true;
206 else if (strcmp(option,"-o")==0) return true;
207 else if (strcmp(option,"-Rts")==0) return true;
208 else if (strcmp(option,"-Dtr")==0) return true;
209 else if (strcmp(option,"-Auto")==0) return true;
210 else return false;
211 }
212  
213 /*******************************************************************************************/
214 /* Function : Is_SubOption */
215 /* IN : sub-option as string */
216 /* OUT : boolean */
217 /* Description : Verify if the given string present a sub-option */
218 /*******************************************************************************************/
219 bool Is_SubOption(char* suboption)
220 {
221 if (strcmp(suboption,"--pn")==0) return true;
222 else if (strcmp(suboption,"--br")==0) return true;
223 else if (strcmp(suboption,"--db")==0) return true;
224 else if (strcmp(suboption,"--pr")==0) return true;
225 else if (strcmp(suboption,"--sb")==0) return true;
226 else if (strcmp(suboption,"--ec")==0) return true;
227 else if (strcmp(suboption,"--co")==0) return true;
228 else if (strcmp(suboption,"--to")==0) return true;
229 else if (strcmp(suboption,"--lcs")==0) return true;
230 else if (strcmp(suboption,"--all")==0) return true;
231 else if (strcmp(suboption,"--sec")==0) return true;
232 else if (strcmp(suboption,"--a")==0) return true;
233 else if (strcmp(suboption,"--s")==0) return true;
234 else if (strcmp(suboption,"--fn")==0) return true;
235 else if (strcmp(suboption,"--v")==0) return true;
236 else if (strcmp(suboption,"--o")==0) return true;
237 else if (strcmp(suboption,"--erp")==0) return true;
238 else if (strcmp(suboption,"--drp")==0) return true;
239 else if (strcmp(suboption,"--ewp")==0) return true;
240 else if (strcmp(suboption,"--dwp")==0) return true;
241 else if (strcmp(suboption,"--get")==0) return true;
242 else if (strcmp(suboption,"--set")==0) return true;
243 else if (strcmp(suboption,"--vals")==0) return true;
244 else if (strcmp(suboption,"--RDP")==0) return true;
245 else if (strcmp(suboption,"--User")==0) return true;
246 else if (strcmp(suboption,"--Data0")==0) return true;
247 else if (strcmp(suboption,"--Data1")==0) return true;
248 else if (strcmp(suboption,"--WRP0")==0) return true;
249 else if (strcmp(suboption,"--WRP1")==0) return true;
250 else if (strcmp(suboption,"--WRP2")==0) return true;
251 else if (strcmp(suboption,"--WRP3")==0) return true;
252 else if (strcmp(suboption,"--Hi")==0) return true;
253 else if (strcmp(suboption,"--Lo")==0) return true;
254 else return false;
255 }
256  
257 /*******************************************************************************************/
258 /* Function : write_debug_info */
259 /* IN : */
260 /* OUT : */
261 /* Description : print the output messages on the standart output */
262 /*******************************************************************************************/
263 void write_debug_info(char *msg, int page, DWORD addr, float size, STATE status)
264 {
265 char d_info[256];
266  
267 if((page==0) && (addr==0) && (size==0))
268 {
269 if(status == OK)
270 sprintf(d_info, "%s \t\t\t\t [OK] \n", msg);
271 else
272 sprintf(d_info, "%s \t\t\t\t [KO] \n", msg);
273 }
274 else if(status == OK)
275 sprintf(d_info, "%s \t page %i \t @0x %8X \t size %.2f(KB) \t [OK] \n", msg, page, addr, (float)size);
276 else
277 sprintf(d_info, "%s \t page %i \t @0x %8X \t size %.2f(KB) \t [KO] \n", msg, page, addr, (float)size);
278  
279 if((SHOW_OK && (status == OK)) || (SHOW_KO && (status == KO))) printf(d_info);
280 }
281  
282 /*******************************************************************************************/
283 /* Function : main */
284 /* IN : */
285 /* OUT : */
286 /* Description : */
287 /*******************************************************************************************/
288 int main(int argc, char* argv[])
289 {
290 START:
291  
292 BYTE Res = SUCCESS;
293 BYTE User, RDP, Data0, Data1, WRP0, WRP1, WRP2, WRP3;
294 bool WaitForMoreSubOpt = TRUE;
295  
296 //Initializing default serial connection parameters
297 int portname = 1;
298 long BaudRate = 57600 ;
299 int DataBits = 8;
300 int parity = ParityToInt("EVEN");
301 double nbStopBit = 1;
302 int timeout = 5000;
303 bool control = false;
304  
305 int nsec = 0;
306 DWORD address = 0x00000000;
307 DWORD size = 0x00000000;
308 char* filename;
309 char devname[256] = "STM32_Low-density_32K.STmap";
310 bool Verify = FALSE;
311 bool optimize = FALSE;
312 int becho = 0;
313  
314 char Drive[3], Dir[256], Fname[256], Ext[256];
315 char *ptr;
316  
317 bool bAuto = false;
318  
319 if (argc == 1) // wrong parameters
320 man();
321 else
322 {
323 int arg_index = 1;
324  
325 while(arg_index < argc)
326 {
327 if(!Is_Option(argv[arg_index]))
328 {
329 if (arg_index < argc - 1)
330 printf("bad parameter [%s] \n", argv[arg_index]);
331  
332 if(COM_is_Open())
333 COM_Close();
334  
335  
336 if (bAuto)
337 goto Done_Success;
338 printf("\n Press any key to continue ...");
339 getchar();
340 return 1;
341 }
342  
343 //============================ Show the man =========================================
344 if (strcmp(argv[arg_index],"-?")==0)
345 {
346 man();
347 return 0;
348 }
349 //=============================== connect ============================================
350 else if (strcmp(argv[arg_index],"-c")==0)
351 {
352 while(arg_index < argc)
353 {
354 if (arg_index< argc-1)
355 arg_index++;
356 else
357 break;
358  
359 if(Is_Option(argv[arg_index])) // Set default connection settings and continue with the next option
360 break;
361  
362 else if(Is_SubOption(argv[arg_index])) // Get connection settings
363 {
364 if (arg_index< argc-1)
365 arg_index++;
366 else
367 break;
368  
369 if (strcmp(argv[arg_index-1],"--pn")==0) portname = atoi(argv[arg_index]);//port name (e.g COM1, COM2 ..., default COM1) \n");
370 else if (strcmp(argv[arg_index-1],"--br")==0) BaudRate = atoi(argv[arg_index]);//baud reate (e.g 115200, 128000 ..., default 57600) \n");
371 else if (strcmp(argv[arg_index-1],"--db")==0) DataBits = atoi(argv[arg_index]);//data bits (in {5,6,7,8} ..., default 8) \n");
372 else if (strcmp(argv[arg_index-1],"--pr")==0) parity = ParityToInt(argv[arg_index]); //parity (in {NONE,ODD,EVEN} ..., default EVEN) \n");
373 else if (strcmp(argv[arg_index-1],"--sb")==0) nbStopBit= atof(argv[arg_index]);//stop bits (in {1,1.5,2} ..., default 1) \n");
374 else if (strcmp(argv[arg_index-1],"--to")==0) timeout = atoi(argv[arg_index]);//time out (e.g 1000, 2000, 3000 ..., default 5) \n");
375 else if (strcmp(argv[arg_index-1],"--ec")==0) becho = ModeToInt(argv[arg_index]); // Echo back mode, default is OFF \n");
376 else if (strcmp(argv[arg_index-1],"--co")==0) control = StatusToBool(argv[arg_index]); // Outputs Control ON/OFF, default is OFF \n");
377  
378 }
379 else
380 {
381 if (arg_index < argc - 1)
382 printf("bad parameter [%s] \n", argv[arg_index]);
383  
384 if(COM_is_Open())
385 COM_Close();
386  
387 printf("\n Press any key to continue ... 2");
388 getchar();
389 return 1;
390 }
391 }
392  
393  
394  
395 // Apply serial connection settings
396 TARGET_SetComIntType(0);
397 SetCOMSettings(portname, BaudRate, DataBits, parity, nbStopBit);
398 STBL_SetFlowControl(control);
399  
400  
401 // Opening serial connection
402 Res = COM_Open();
403 SetTimeOut(1000);
404  
405 if ((Res != SUCCESS) && (Res != COM_ALREADY_OPENED))
406 {
407 write_debug_info("Opening Port", 0 ,0, 0, KO);
408 printf("Cannot open the com port, the port may \n be used by another application \n");
409  
410 if(COM_is_Open())
411 COM_Close();
412  
413 printf("\n Press any key to continue ... 3");
414 getchar();
415 return 1;
416 }
417 else
418 write_debug_info("Opening Port", 0 ,0, 0, OK);
419  
420  
421  
422  
423 STBL_SetEcho(becho); // Setting Echo back mode
424  
425  
426  
427  
428 }
429  
430  
431 //============================ Auto option =======================================
432 else if (strcmp(argv[arg_index],"-Auto")==0)
433 {
434 if (arg_index< argc)
435 arg_index++;
436 else
437 break;
438  
439 bAuto = true;
440  
441 // BOOT0 = High
442 STBL_SetDtr(TRUE);
443 Sleep(50);
444 // Reset = Low
445 STBL_SetRts(TRUE);
446  
447 Sleep(50);
448  
449 // Reset = High
450 STBL_SetRts(FALSE);
451  
452 STBL_SetDtr(FALSE);
453 Sleep(100);
454 COM_Close();
455 COM_Open();
456  
457 STBL_SetDtr(TRUE);
458 Sleep(50);
459 // Reset = Low
460 STBL_SetRts(TRUE);
461  
462 Sleep(50);
463  
464 // Reset = High
465 STBL_SetRts(FALSE);
466  
467 STBL_SetDtr(FALSE);
468 Sleep(500);
469 //Sleep(1000);
470 write_debug_info("Setting device to BOOT0", 0 ,0, 0, OK);
471  
472 //printf("\n RTS set low. Press any key to continue ... 4");
473 //getchar();
474 }
475  
476 //============================ command RTS pin =======================================
477 else if (strcmp(argv[arg_index],"-Rts")==0)
478 {
479 //_sleep(1000);
480 while(arg_index < argc)
481 {
482 if (arg_index< argc-1) arg_index++;
483 else break;
484  
485 if(Is_Option(argv[arg_index])) break;
486 else if(Is_SubOption(argv[arg_index]))
487 {
488  
489  
490  
491 if (strcmp(argv[arg_index],"--Hi")==0)
492 {
493 write_debug_info("Set Rts line", 0 ,0, 0,OK);
494 STBL_SetRts(TRUE);
495  
496  
497  
498 }
499 else if (strcmp(argv[arg_index],"--Lo")==0)
500 {
501 write_debug_info("Reset Rts line", 0 ,0, 0,OK);
502 STBL_SetRts(FALSE);
503 }
504 else
505 {
506 write_debug_info("bad parameter [Set Rts line] should be Hi or Lo ", 0 ,0, 0,KO);
507  
508 if (arg_index < argc - 1)
509 printf("bad parameter [%s] \n", argv[arg_index]);
510  
511 if(COM_is_Open())
512 COM_Close();
513  
514 printf("\n Press any key to continue ... 5");
515 getchar();
516 return 1;
517 }
518 }
519 else
520 {
521 if (arg_index < argc - 1)
522 printf("bad parameter [%s] \n", argv[arg_index]);
523  
524 if(COM_is_Open())
525 COM_Close();
526  
527 printf("\n Press any key to continue ... 6");
528 getchar();
529 return 1;
530 }
531 }
532 }
533 //============================ command DTR pin =======================================
534 else if (strcmp(argv[arg_index],"-Dtr")==0)
535 {
536 while(arg_index < argc)
537 {
538 if (arg_index< argc-1)
539 arg_index++;
540 else
541 break;
542  
543 if(Is_Option(argv[arg_index]))
544 break;
545  
546 else if(Is_SubOption(argv[arg_index]))
547 {
548 if (strcmp(argv[arg_index],"--Hi")==0)
549 {
550 write_debug_info("Set Dtr line", 0 ,0, 0,OK);
551 STBL_SetDtr(TRUE);
552  
553 }
554 else if (strcmp(argv[arg_index],"--Lo")==0)
555 {
556 write_debug_info("Reset Dtr line", 0 ,0, 0,OK);
557 STBL_SetDtr(FALSE);
558 }
559 else
560 {
561 write_debug_info("bad parameter [Set Dtr line] should be Hi or Lo ", 0 ,0, 0,KO);
562  
563 if (arg_index < argc - 1)
564 printf("bad parameter [%s] \n", argv[arg_index]);
565  
566 if(COM_is_Open())
567 COM_Close();
568  
569 printf("\n Press any key to continue ... 7");
570 getchar();
571 return 1;
572 }
573 }
574 else
575 {
576 if (arg_index < argc - 1) printf("bad parameter [%s] \n", argv[arg_index]);
577 if (arg_index < argc - 1)
578 printf("bad parameter [%s] \n", argv[arg_index]);
579  
580 if(COM_is_Open())
581 COM_Close();
582  
583 printf("\n Press any key to continue ... 8");
584 getchar();
585 return 1;
586 }
587 }
588 }
589 //============================ ERASE =================================================
590 else if (strcmp(argv[arg_index],"-e")==0)
591 {
592  
593 while(arg_index < argc)
594 {
595 if (!WaitForMoreSubOpt)
596 break;
597  
598 if (arg_index< argc-1)
599 arg_index++;
600 else
601 break;
602  
603 if(Is_Option(argv[arg_index]))
604 break;
605  
606 else if(Is_SubOption(argv[arg_index]))
607 {
608 if (arg_index< argc)
609 arg_index++;
610 else
611 break;
612  
613 //******************** This section is only for STM8 boot loader *******************
614 BYTE Version;
615 Commands pCmds;
616 CString m_Version;
617 if (STBL_GET(&Version, &pCmds) == SUCCESS)
618 {
619 m_Version.Format("%x.%x",Version/16, Version & 0x0F) ;
620 }
621 CIni Ini((LPCSTR)MapFile);
622  
623 if(Ini.IsKeyExist((LPCTSTR)"Product",(LPCTSTR)m_Version))
624 {
625 CString E_W_ROUTINEs = Ini.GetString((LPCTSTR)"Product",(LPCTSTR)m_Version, "");
626 CString Path(*__argv);
627  
628 char fullPath [MAX_PATH];
629  
630 GetModuleFileName(0, fullPath, (MAX_PATH));
631  
632 Path=fullPath;
633  
634 int j=Path.ReverseFind('\\')+1;
635 if(j) Path=Path.Left(j);
636  
637 CString ToFind;
638  
639 ToFind.Format("%s%s%s", Path, "STM8_Routines\\", E_W_ROUTINEs);
640  
641 if(!E_W_ROUTINEs.IsEmpty())
642 {
643 if(!FileExist((LPCTSTR)ToFind))
644 {
645 printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing [%s]\n", ToFind);
646 }
647 else
648 {
649 HANDLE Image;
650 if (FILES_ImageFromFile((LPSTR)(LPCSTR)ToFind,&Image, 1)== FILES_NOERROR)
651 {
652 FILES_SetImageName(Image,(LPSTR)(LPCSTR)ToFind);
653  
654 DWORD NbElements;
655 if (FILES_GetImageNbElement(Image, &NbElements) == FILES_NOERROR)
656 {
657 for (int el=0; el< (int)NbElements;el++)
658 {
659 IMAGEELEMENT Element={0};
660 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
661 {
662 Element.Data=new BYTE[Element.dwDataLength];
663 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
664 {
665 if (STBL_DNLOAD(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS)
666 {
667  
668 }
669 }
670 }
671 }
672  
673 // Verify writen data
674 BOOL VerifySuccess = TRUE;
675 _sleep(100);;
676  
677 #ifndef _VS6_USED
678 int el;
679 #endif
680 for (el=0; el< (int)NbElements;el++)
681 {
682 IMAGEELEMENT Element={0};
683 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
684 {
685 Element.Data=new BYTE[Element.dwDataLength];
686 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
687 {
688 if (STBL_VERIFY(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS)
689 {
690 VerifySuccess = FALSE;
691 char str[255];
692 sprintf(str, "%s at address :0x%X. \n%s \nPlease disable the write protection then try agin.", "Data not matching ", Element.dwAddress, "The page may be write protected.");
693 AfxMessageBox(str, MB_OK|MB_ICONEXCLAMATION);
694 return 1;
695 }
696 }
697 }
698 }
699 }
700 }
701 else
702 {
703 AfxMessageBox("Unable to load data from this file " + ToFind + " ...");
704 return -1;
705 }
706 }
707 }
708 }
709 else
710 {
711 int family = Ini.GetInt((LPCTSTR)"Product",(LPCTSTR)"family", 0);
712 if(family == 3)
713 {
714 printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing\n");
715 }
716 }
717 //End****************** This section is only for STM8 boot loader *******************
718  
719 //End****************** This section is only for STM8 boot loader *******************
720  
721 printf("\n ERASING ... \n");
722 if (strcmp(argv[arg_index-1],"--all")==0)
723 {
724  
725 WaitForMoreSubOpt = false;
726 Res = STBL_ERASE(0xFFFF, NULL);
727  
728  
729  
730 if (Res != SUCCESS)
731 write_debug_info("erasing all pages", 0 ,0, 0, KO);
732 else
733 write_debug_info("erasing all pages", 0 ,0, 0, OK);
734 }
735 else if (strcmp(argv[arg_index-1],"--sec")==0)
736 {
737 WaitForMoreSubOpt = true;
738  
739 nsec = atoi(argv[arg_index]);
740 LPWORD sectors = (LPWORD)malloc(nsec *2 + 2);
741  
742  
743 sectors[0] = 0;
744 for(int i = 1; i<= nsec; i++)
745 {
746 sectors[0]++;
747 arg_index++;
748 sectors[sectors[0]] = atoi(argv[arg_index]);
749 }
750  
751 WaitForMoreSubOpt = false;
752  
753 printf("\nerasing %i sectors : ", sectors[0]);
754  
755 #ifndef _VS6_USED
756 int i;
757 #endif
758  
759 for(i = 1; i<= nsec; i++)
760 {
761 printf("<%i>", sectors[i]);
762 }
763 printf("\n");
764  
765 Res = STBL_ERASE(nsec, (LPBYTE)sectors+2);
766 if (Res != SUCCESS)
767 {
768 write_debug_info("erasing", 0 ,0, 0, KO);
769  
770 if(COM_is_Open())
771 COM_Close();
772  
773 printf("\n Press any key to continue ... 9");
774 getchar();
775 return 1;
776 }
777 else
778 write_debug_info("erasing", 0 ,0, 0, OK);
779  
780 arg_index++;
781 }
782 }
783 else
784 {
785 if (arg_index < argc - 1)
786 printf("bad parameter [%s] \n", argv[arg_index]);
787  
788 if(COM_is_Open())
789 COM_Close();
790  
791 printf("\n Press any key to continue ... 10");
792 getchar();
793 return 1;
794 }
795 }
796 }
797 //============================ UPLOAD ===============================================
798 else if (strcmp(argv[arg_index],"-u")==0)
799 {
800 while(arg_index < argc)
801 {
802 if (arg_index< argc-1)
803 arg_index++;
804 else
805 break;
806  
807 if(Is_Option(argv[arg_index]))
808 break;
809  
810 else if(Is_SubOption(argv[arg_index]))
811 {
812 if (arg_index< argc)
813 arg_index++;
814 else
815 break;
816  
817 /*if (strcmp(argv[arg_index-1],"--a")==0)
818 {
819 address = _tcstoul(argv[arg_index], 0, 16) ;
820 }
821 else if (strcmp(argv[arg_index-1],"--s")==0)
822 {
823 size = _tcstoul(argv[arg_index], 0, 16) ;
824 }
825 else */if (strcmp(argv[arg_index-1],"--fn")==0)
826 {
827 filename = argv[arg_index];
828 }
829 }
830 else
831 {
832 if (arg_index < argc - 1)
833 printf("bad parameter [%s] \n", argv[arg_index]);
834  
835 if(COM_is_Open())
836 COM_Close();
837  
838 printf("\n Press any key to continue ... 11");
839 getchar();
840 return 1;
841 }
842 }
843  
844 printf("\n UPLOADING ... \n\n");
845  
846 HANDLE Handle;
847 FILES_CreateImage(&Handle, 0);
848  
849 FILES_CreateImageFromMapping(&Handle,pmMapping);
850  
851 DWORD NbElements = 0;
852 if (FILES_GetImageNbElement(Handle, &NbElements) == FILES_NOERROR)
853 {
854 if (NbElements > 0)
855 {
856 for(int i = 0; i< (int)NbElements; i++)
857 {
858 IMAGEELEMENT Element={0};
859 // Get element data size
860 if (FILES_GetImageElement(Handle, i, &Element) == FILES_NOERROR)
861 {
862 //Upload element data
863 Element.Data = (LPBYTE)malloc(Element.dwDataLength);
864 if (STBL_UPLOAD(Element.dwAddress, Element.Data, Element.dwDataLength) == SUCCESS)
865 {
866 //Insert elment in the Image
867 write_debug_info("Uploading", i ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, OK);
868 FILES_SetImageElement(Handle,i,FALSE,Element);
869 }
870 else
871 {
872 write_debug_info("Uploading", i ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, KO);
873  
874 if(COM_is_Open())
875 COM_Close();
876  
877 printf("\n Press any key to continue ... 12");
878 getchar();
879 return 1;
880 }
881 }
882 }
883 }
884 }
885  
886 if(!FileExist((LPCTSTR)filename))
887 {
888 printf( "file %s does not exist .. Creating file\n", filename);
889 FILE* fp = fopen((LPCTSTR)filename, "a+");
890 fclose(fp);
891 }
892  
893 printf( "Writing data ...\n");
894  
895 if (FILES_ImageToFile((LPSTR)(LPCSTR)filename,Handle) != FILES_NOERROR)
896 {
897 printf( "cannot write to file %s \n", filename);
898  
899 if(COM_is_Open())
900 COM_Close();
901  
902 printf("\n Press any key to continue ... 13");
903 getchar();
904 return 1;
905 }
906 else
907 printf("\n Uploaded data is dumped on %s", filename);
908 }
909  
910  
911  
912 //============================ Get Device map file name ==============================
913 else if (strcmp(argv[arg_index],"-i")==0)
914 {
915 if (arg_index< argc)
916 arg_index++;
917 else
918 break;
919  
920  
921 sprintf(devname,"%s.STmap", argv[arg_index]);
922  
923 char Drive[3], Dir[256], Fname[256], Ext[256];
924 _splitpath(argv[0],Drive,Dir,Fname,Ext);
925  
926 sprintf(MapFile, "%s%s%s%s", Drive, Dir , "Map\\", devname);
927  
928 pmMapping = NULL;
929 WORD size = 0;
930  
931 WORD PacketSize = 0;
932 pmMapping = NULL;
933 WORD Size = 0;
934 char MapName[256];
935 // Get the number of sectors in the flash target: pmMapping should be NULL
936 // number of sectors is returned in the Size value
937 BYTE PagePerSector = 0;
938  
939 if (!FileExist((LPCTSTR)MapFile))
940 {
941 printf("This version is not intended to support the <%s> target\n", argv[arg_index]);
942  
943 if(COM_is_Open())
944 COM_Close();
945  
946 printf("\n Press any key to continue ... 14");
947 getchar();
948 return 1;
949 }
950  
951 FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)MapName, &PacketSize, pmMapping, &PagePerSector);
952 // Allocate the mapping structure memory
953 pmMapping = (PMAPPING)malloc(sizeof(MAPPING));
954 pmMapping->NbSectors = 0;
955 pmMapping->pSectors = (PMAPPINGSECTOR) malloc((Size) * sizeof(MAPPINGSECTOR));
956  
957 // Get the mapping info
958 FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)(LPCTSTR)MapName, &PacketSize, pmMapping, &PagePerSector);
959  
960 SetPaketSize(PacketSize);
961  
962  
963  
964 //sending BL config byte (0x7F) & identifing target
965  
966  
967  
968 Res = STBL_Init_BL();
969  
970 if (Res == UNREOGNIZED_DEVICE)
971 {
972 write_debug_info("Activating device", 0 ,0, 0, KO);
973  
974 if(COM_is_Open())
975 COM_Close();
976  
977 printf("Unrecognized device... Please, reset your device then try again \n");
978  
979 if(COM_is_Open())
980 COM_Close();
981  
982 printf("Please, reset your device then press any key to continue \n");
983 printf("\n Press any key to continue ... 15");
984 getchar();
985 goto START;
986 }
987 else if (Res != SUCCESS)
988 {
989 write_debug_info("Activating device", 0 ,0, 0, KO);
990 printf("No response from the target, the Boot loader can not be started. \nPlease, verify the boot mode configuration, reset your device then try again. \n");
991  
992 if(COM_is_Open())
993 COM_Close();
994  
995 printf("Please, reset your device then then press any key to continue \n");
996 printf("\n Press any key to continue ... 16");
997 getchar();
998 goto START;
999 }
1000  
1001 _sleep(TimeBO);
1002  
1003 write_debug_info("Activating device", 0 ,0, 0, OK);
1004 //Getting Target informations (version, available commands)
1005 BYTE Version ;
1006 Commands pCmds;
1007  
1008 Res = STBL_GET(&Version, &pCmds);
1009 if (Res != SUCCESS)
1010 {
1011 if(COM_is_Open())
1012 COM_Close();
1013  
1014 printf("\n Press any key to continue ... 17");
1015 getchar();
1016 return 1;
1017 }
1018  
1019 SetTimeOut(timeout);
1020  
1021 if (arg_index< argc)
1022 arg_index++;
1023 else
1024 break;
1025 }
1026 //============================ DOWNLOAD ==============================================
1027 else if (strcmp(argv[arg_index],"-d")==0)
1028 {
1029 while(arg_index < argc)
1030 {
1031 if (arg_index< argc-1)
1032 arg_index++;
1033 else
1034 break;
1035  
1036 if(Is_Option(argv[arg_index]))
1037 break;
1038  
1039 else if(Is_SubOption(argv[arg_index]))
1040 {
1041 if (arg_index< argc)
1042 arg_index++;
1043 else
1044 break;
1045  
1046 if (strcmp(argv[arg_index-1],"--a")==0)
1047 {
1048 address = _tcstoul(argv[arg_index], 0, 16) ;
1049 }
1050 else if (strcmp(argv[arg_index-1],"--v")==0)
1051 {
1052 Verify = true;
1053 arg_index--;
1054 }
1055 else if (strcmp(argv[arg_index-1],"--o")==0)
1056 {
1057 optimize = TRUE;
1058 arg_index--;
1059 }
1060 else if (strcmp(argv[arg_index-1],"--fn")==0)
1061 {
1062 filename = argv[arg_index];
1063 _splitpath(filename,Drive,Dir,Fname,Ext);
1064 ptr=strupr(Ext);
1065 strcpy(Ext, ptr);
1066 }
1067 }
1068 else
1069 {
1070 if (arg_index < argc - 1)
1071 printf("bad parameter [%s] \n", argv[arg_index]);
1072  
1073 if(COM_is_Open())
1074 COM_Close();
1075  
1076 printf("\n Press any key to continue ... 18");
1077 getchar();
1078 return 1;
1079 }
1080 }
1081  
1082 PMAPPINGSECTOR pSector = pmMapping->pSectors;
1083 for(int i = 1; i<= (int)pmMapping->NbSectors; i++)
1084 {
1085 if ((strcmp(Ext, ".BIN")!=0) && (i==0))
1086 address = pSector->dwStartAddress;
1087  
1088 pSector->UseForOperation = TRUE;
1089 pSector++;
1090 }
1091  
1092 if(!FileExist((LPCTSTR)filename))
1093 {
1094 printf( "file does not exist %s \n", filename);
1095  
1096 if(COM_is_Open())
1097 COM_Close();
1098  
1099 printf("\n Press any key to continue ... 19");
1100 getchar();
1101 return 1;
1102 }
1103  
1104 //****************** This section is only for STM8 boot loader *******************
1105 BYTE Version;
1106 Commands pCmds;
1107 CString m_Version;
1108 if (STBL_GET(&Version, &pCmds) == SUCCESS)
1109 {
1110 m_Version.Format("%x.%x",Version/16, Version & 0x0F) ;
1111 }
1112 CIni Ini((LPCSTR)MapFile);
1113  
1114 if(Ini.IsKeyExist((LPCTSTR)"Product",(LPCTSTR)m_Version))
1115 {
1116 CString E_W_ROUTINEs = Ini.GetString((LPCTSTR)"Product",(LPCTSTR)m_Version, "");
1117 CString Path(*__argv);
1118 int j=Path.ReverseFind('\\')+1;
1119 if(j) Path=Path.Left(j);
1120  
1121 CString ToFind;
1122  
1123 ToFind.Format("%s%s%s", Path, "STM8_Routines\\", E_W_ROUTINEs);
1124  
1125 if(!E_W_ROUTINEs.IsEmpty())
1126 {
1127 if(!FileExist((LPCTSTR)ToFind))
1128 {
1129 printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing [%s]\n", ToFind);
1130 }
1131 else
1132 {
1133 HANDLE Image;
1134 if (FILES_ImageFromFile((LPSTR)(LPCSTR)ToFind,&Image, 1)== FILES_NOERROR)
1135 {
1136 FILES_SetImageName(Image,(LPSTR)(LPCSTR)ToFind);
1137  
1138 DWORD NbElements;
1139 if (FILES_GetImageNbElement(Image, &NbElements) == FILES_NOERROR)
1140 {
1141 for (int el=0; el< (int)NbElements;el++)
1142 {
1143 IMAGEELEMENT Element={0};
1144 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
1145 {
1146 Element.Data=new BYTE[Element.dwDataLength];
1147 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
1148 {
1149 if (STBL_DNLOAD(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS)
1150 {
1151  
1152 }
1153 }
1154 }
1155 }
1156  
1157 // Verify writen data
1158 BOOL VerifySuccess = TRUE;
1159 _sleep(100);
1160  
1161 #ifndef _VS6_USED
1162 int el;
1163 #endif
1164  
1165 for (el=0; el< (int)NbElements;el++)
1166 {
1167 IMAGEELEMENT Element={0};
1168 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
1169 {
1170 Element.Data=new BYTE[Element.dwDataLength];
1171 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
1172 {
1173 if (STBL_VERIFY(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS)
1174 {
1175 VerifySuccess = FALSE;
1176 char str[255];
1177 sprintf(str, "%s at address :0x%X. \n%s \nPlease disable the write protection then try agin.", "Data not matching ", Element.dwAddress, "The page may be write protected.");
1178 AfxMessageBox(str, MB_OK|MB_ICONEXCLAMATION);
1179 return 1;
1180 }
1181 }
1182 }
1183 }
1184 }
1185 }
1186 else
1187 {
1188 AfxMessageBox("Unable to load data from this file " + ToFind + " ...");
1189 return -1;
1190 }
1191 }
1192 }
1193 }
1194 else
1195 {
1196 int family = Ini.GetInt((LPCTSTR)"Product",(LPCTSTR)"family", 0);
1197 if(family == 3)
1198 {
1199 printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing\n");
1200 }
1201 }
1202 //End****************** This section is only for STM8 boot loader *******************
1203  
1204 printf("\n DOWNLOADING ... \n\n");
1205  
1206 HANDLE Handle;
1207 if (FILES_ImageFromFile((LPSTR)(LPCSTR)filename,&Handle, 1) == FILES_NOERROR)
1208 {
1209 FILES_SetImageName(Handle,(LPSTR)(LPCSTR)filename);
1210  
1211 DWORD NbElements = 0;
1212 if (FILES_GetImageNbElement(Handle, &NbElements) == FILES_NOERROR)
1213 {
1214 if ( NbElements > 0 )
1215 { // if binary file -> change the elemnts address
1216 if (strcmp(Ext, ".BIN")==0)
1217 {
1218 for (int i=0;i< (int)NbElements;i++)
1219 {
1220 IMAGEELEMENT Element={0};
1221 if (FILES_GetImageElement(Handle, i, &Element) == FILES_NOERROR)
1222 {
1223 Element.Data= (LPBYTE)malloc(Element.dwDataLength);
1224 if (FILES_GetImageElement(Handle, i, &Element) == FILES_NOERROR)
1225 {
1226 Element.dwAddress = Element.dwAddress + address;
1227 FILES_SetImageElement(Handle, i, FALSE, Element);
1228 }
1229 }
1230 }
1231 }
1232 }
1233 }
1234  
1235 FILES_FilterImageForOperation(Handle, pmMapping, OPERATION_UPLOAD, optimize);
1236 }
1237 else
1238 {
1239 printf("cannot open file %s \n", filename);
1240  
1241 if(COM_is_Open())
1242 COM_Close();
1243  
1244 printf("\n Press any key to continue ... 20");
1245 getchar();
1246 return 1;
1247 }
1248  
1249 DWORD NbElements = 0;
1250 if (FILES_GetImageNbElement(Handle, &NbElements) == FILES_NOERROR)
1251 {
1252 for (int el=0; el< (int)NbElements;el++)
1253 {
1254 IMAGEELEMENT Element={0};
1255 if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR)
1256 {
1257 Element.Data= (LPBYTE)malloc(Element.dwDataLength);
1258 if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR)
1259 {
1260 if ((strcmp(Ext, ".BIN")==0) && (el==0))
1261 Element.dwAddress = address;
1262  
1263 if (STBL_DNLOAD(Element.dwAddress, Element.Data, Element.dwDataLength, optimize) != SUCCESS)
1264 {
1265 write_debug_info( "downloading", el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, KO);
1266 write_debug_info("The flash may be read protected; use -p --drp to disable write protection." , 0, 0, 0, KO);
1267  
1268 if(COM_is_Open())
1269 COM_Close();
1270  
1271 printf("\n Press any key to continue ... 21");
1272 getchar();
1273 return 1;
1274 }
1275  
1276 write_debug_info( "downloading", el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, OK);
1277 }
1278 }
1279 }
1280 }
1281  
1282 bool VerifySuccess = true;
1283 if (Verify)
1284 {
1285 printf("\n VERIFYING ... \n\n");
1286  
1287  
1288 for (int el=0; el< (int)NbElements;el++)
1289 {
1290 IMAGEELEMENT Element={0};
1291 if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR)
1292 {
1293 Element.Data=(LPBYTE)malloc(Element.dwDataLength);
1294 if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR)
1295 {
1296 if ((strcmp(Ext, ".BIN")==0) && (el==0))
1297 Element.dwAddress = address;
1298  
1299 if (STBL_VERIFY(Element.dwAddress, Element.Data, Element.dwDataLength, optimize) != SUCCESS)
1300 {
1301 VerifySuccess = false;
1302 write_debug_info("verifying" ,el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, KO);
1303 write_debug_info("some pages may be write protected; use -p --dwp to disable write protection." , 0, 0, 0, KO);
1304  
1305 if(COM_is_Open())
1306 COM_Close();
1307  
1308 printf("\n Press any key to continue ... 22");
1309 getchar();
1310 return 1;
1311 }
1312  
1313 write_debug_info("verifying" ,el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, OK);
1314 }
1315 }
1316 }
1317 }
1318  
1319 }
1320 //============================ VERIFY ================================================
1321 else if (strcmp(argv[arg_index],"-v")==0)
1322 {
1323 if (arg_index< argc)
1324 arg_index++;
1325 else
1326 break;
1327 }
1328 //============================ Program option bytes ==================================
1329 else if (strcmp(argv[arg_index],"-o")==0)
1330 {
1331 while(arg_index < argc)
1332 {
1333 if (arg_index< argc-1)
1334 arg_index++;
1335 else
1336 break;
1337  
1338 if(Is_Option(argv[arg_index]))
1339 break;
1340  
1341 else if(Is_SubOption(argv[arg_index]))
1342 {
1343 if (arg_index< argc)
1344 arg_index++;
1345 else
1346 break;
1347  
1348 if (strcmp(argv[arg_index-1],"--get")==0)
1349 {
1350 if (arg_index< argc)
1351 arg_index++;
1352 else
1353 break;
1354  
1355 if (strcmp(argv[arg_index-1],"--fn")==0)
1356 filename = argv[arg_index];
1357  
1358 if(TARGET_GetSIFData(&User, &RDP, &Data0, &Data1, &WRP0, &WRP1, &WRP2, &WRP3) == SUCCESS)
1359 {
1360 write_debug_info("Getting Option bytes data" ,0 ,0, 0, OK);
1361  
1362 HANDLE Image;
1363 if (FILES_CreateImage(&Image, 1) == FILES_NOERROR)
1364 {
1365 IMAGEELEMENT Element={0};
1366 Element.dwAddress = 0x1FFFF800;
1367 Element.dwDataLength = 16;
1368 Element.Data = (LPBYTE)malloc(Element.dwDataLength);
1369  
1370 {
1371 Element.Data[0] = RDP;
1372 Element.Data[1] = ~RDP;
1373 Element.Data[2] = User;
1374 Element.Data[3] = ~User;
1375 Element.Data[4] = Data0;
1376 Element.Data[5] = ~Data0;
1377 Element.Data[6] = Data1;
1378 Element.Data[7] = ~Data1;
1379 Element.Data[8] = WRP0;
1380 Element.Data[9] = ~WRP0;
1381 Element.Data[10] = WRP1;
1382 Element.Data[11] = ~WRP1;
1383 Element.Data[12] = WRP2;
1384 Element.Data[13] = ~WRP2;
1385 Element.Data[14] = WRP3;
1386 Element.Data[15] = ~WRP3;
1387 }
1388  
1389 FILES_SetImageElement(Image,0,TRUE,Element);
1390 if (FILES_ImageToFile((LPSTR)(LPCSTR)filename,Image) != FILES_NOERROR)
1391 {
1392 write_debug_info("Saving Option bytes data",0 ,0, 0, KO);
1393 }
1394 else write_debug_info("Saving Option bytes data",0 ,0, 0, OK);
1395 }
1396 }
1397 else write_debug_info("Getting Option bytes data" ,0 ,0, 0, KO);
1398 }
1399 else if (strcmp(argv[arg_index-1],"--set")==0)
1400 {
1401 if (arg_index< argc) arg_index++;
1402 else break;
1403  
1404 if (strcmp(argv[arg_index-1],"--fn")==0)
1405 {
1406 filename = argv[arg_index];
1407  
1408 HANDLE OPBImage;
1409  
1410 if(!FileExist((LPCTSTR)filename))
1411 {
1412 printf( "file does not exist %s \n", filename);
1413  
1414 if(COM_is_Open())
1415 COM_Close();
1416  
1417 printf("\n Press any key to continue ... 23");
1418 getchar();
1419 return 1;
1420 }
1421  
1422 if (FILES_ImageFromFile((LPSTR)(LPCSTR)filename, &OPBImage, 0) == FILES_NOERROR)
1423 {
1424 DWORD NbElements = 0;
1425 if (FILES_GetImageNbElement(OPBImage, &NbElements) == FILES_NOERROR)
1426 {
1427 if ( NbElements == 1 )
1428 {
1429 IMAGEELEMENT Element={0};
1430 if (FILES_GetImageElement(OPBImage, 0, &Element) == FILES_NOERROR)
1431 {
1432 Element.Data= (LPBYTE)malloc(Element.dwDataLength);
1433 if (FILES_GetImageElement(OPBImage, 0, &Element) == FILES_NOERROR)
1434 {
1435 RDP = Element.Data[0] ;
1436 User = Element.Data[2] ;
1437 Data0 = Element.Data[4] ;
1438 Data1 = Element.Data[6] ;
1439 WRP0 = Element.Data[8] ;
1440 WRP1 = Element.Data[10];
1441 WRP2 = Element.Data[12];
1442 WRP3 = Element.Data[14];
1443  
1444  
1445 if (TARGET_SetSIFData(User, RDP, Data0, Data1, WRP0, WRP1, WRP2, WRP3) == SUCCESS)
1446 {
1447 write_debug_info("Setting Option bytes data" ,0 ,0, 0, OK);
1448  
1449 if(COM_is_Open())
1450 COM_Close();
1451  
1452 COM_Open();
1453  
1454 if(STBL_Init_BL() != SUCCESS)
1455 write_debug_info("Resetting device" ,0 ,0, 0, KO);
1456 else
1457 write_debug_info("Resetting device" ,0 ,0, 0, OK);
1458 }
1459 else
1460 write_debug_info("Setting Option bytes data" ,0 ,0, 0, KO);
1461 }
1462 }
1463 }
1464 }
1465 }
1466 }
1467 else if (strcmp(argv[arg_index-1],"--vals")==0)
1468 {
1469 TARGET_GetSIFData(&User, &RDP, &Data0, &Data1, &WRP0, &WRP1, &WRP2, &WRP3);
1470  
1471 while(arg_index< argc)
1472 {
1473 if(Is_Option(argv[arg_index]))
1474 break;
1475 else if(Is_SubOption(argv[arg_index]))
1476 {
1477 arg_index++;
1478 if(strcmp(argv[arg_index-1],"--RDP")==0) { RDP = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1479 else if(strcmp(argv[arg_index-1],"--User")==0) { User = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1480 else if(strcmp(argv[arg_index-1],"--data0")==0){ Data0 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1481 else if(strcmp(argv[arg_index-1],"--data1")==0){ Data1 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1482 else if(strcmp(argv[arg_index-1],"--WRP0")==0) { WRP0 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1483 else if(strcmp(argv[arg_index-1],"--WRP1")==0) { WRP1 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1484 else if(strcmp(argv[arg_index-1],"--WRP2")==0) { WRP2 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1485 else if(strcmp(argv[arg_index-1],"--WRP3")==0) { WRP3 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1486 }
1487 }
1488 if (TARGET_SetSIFData(User, RDP, Data0, Data1, WRP0, WRP1, WRP2, WRP3) != SUCCESS)
1489 write_debug_info("Setting Option bytes data" ,0 ,0, 0, KO);
1490 else
1491 {
1492 write_debug_info("Setting Option bytes data" ,0 ,0, 0, OK);
1493  
1494 if(COM_is_Open())
1495 COM_Close();
1496  
1497 COM_Open();
1498  
1499 if(STBL_Init_BL() != SUCCESS)
1500 write_debug_info("Resetting device" ,0 ,0, 0, KO);
1501 else
1502 write_debug_info("Resetting device" ,0 ,0, 0, OK);
1503 }
1504 arg_index--;
1505 }
1506 }
1507 }
1508 else
1509 {
1510 if (arg_index < argc - 1)
1511 printf("bad parameter [%s] \n", argv[arg_index]);
1512  
1513 if(COM_is_Open())
1514 COM_Close();
1515  
1516 printf("\n Press any key to continue ... 24");
1517 getchar();
1518 return 1;
1519 }
1520 }
1521 }
1522 //============================ Set/Unset R/W protection ==========================
1523 else if (strcmp(argv[arg_index],"-p")==0)
1524 {
1525 while(arg_index < argc)
1526 {
1527 if (arg_index< argc-1)
1528 arg_index++;
1529 else
1530 break;
1531  
1532 if(Is_Option(argv[arg_index]))
1533 break;
1534  
1535 else if(Is_SubOption(argv[arg_index]))
1536 {
1537 if (arg_index< argc)
1538 arg_index++;
1539 else
1540 break;
1541  
1542 if (strcmp(argv[arg_index-1],"--erp")==0)
1543 {
1544 if(STBL_READOUT_PROTECT() != SUCCESS)
1545 write_debug_info( "enabling read protection", 0 , 0, 0, KO);
1546 else
1547 write_debug_info( "enabling read protection", 0 , 0, 0, OK);
1548  
1549 _sleep(TimeBO);
1550  
1551 if(STBL_Init_BL() != SUCCESS)
1552 write_debug_info( "reseting device", 0 , 0, 0, KO);
1553 else
1554 write_debug_info( "reseting device", 0 , 0, 0, OK);
1555  
1556 arg_index--;
1557 }
1558 else if (strcmp(argv[arg_index-1],"--drp")==0)
1559 {
1560 if(STBL_READOUT_PERM_UNPROTECT() == SUCCESS)
1561 {
1562 write_debug_info( "disabling read protection", 0 , 0, 0, OK);
1563  
1564 _sleep(TimeBO);
1565  
1566 if(STBL_Init_BL() != SUCCESS)
1567 write_debug_info( "reseting device", 0 , 0, 0, KO);
1568 else
1569 write_debug_info( "reseting device", 0 , 0, 0, OK);
1570 }
1571 else
1572 write_debug_info( "disabling read protection", 0 , 0, 0, KO);
1573  
1574 arg_index--;
1575 }
1576 else if (strcmp(argv[arg_index-1],"--ewp")==0)
1577 {
1578 LPBYTE sectors;
1579 if(Is_Option(argv[arg_index])) break;
1580  
1581 nsec = atoi(argv[arg_index]);
1582 sectors = (LPBYTE)malloc(nsec + 1);
1583  
1584  
1585 sectors[0] = 0;
1586 for(int i = 1; i<= nsec; i++)
1587 {
1588 sectors[0]++;
1589 arg_index++;
1590 sectors[sectors[0]] = atoi(argv[arg_index]);
1591 }
1592  
1593  
1594 printf("\nenabling write protection %i sectors : ", sectors[0]);
1595  
1596 #ifndef _VS6_USED
1597 int i;
1598 #endif
1599  
1600 for(i = 1; i<= nsec; i++)
1601 {
1602 printf("<%i>", sectors[i]);
1603 }
1604 printf("\n");
1605  
1606 if(STBL_WRITE_PROTECT(((LPBYTE)sectors)[0],&((LPBYTE)sectors)[1]) != SUCCESS)
1607 write_debug_info( "enabling write protection", 0 , 0, 0, KO);
1608 else
1609 write_debug_info( "enabling write protection", 0 , 0, 0, OK);
1610  
1611 _sleep(TimeBO);
1612  
1613 if(STBL_Init_BL() != SUCCESS)
1614 write_debug_info( "reseting device", 0 , 0, 0, KO);
1615 else
1616 write_debug_info( "reseting device", 0 , 0, 0, OK);
1617 }
1618 else if (strcmp(argv[arg_index-1],"--dwp")==0)
1619 {
1620 if(STBL_WRITE_PERM_UNPROTECT() != SUCCESS)
1621 write_debug_info( "disabling write protection", 0 , 0, 0, KO);
1622 else
1623 write_debug_info( "disabling write protection", 0 , 0, 0, OK);
1624  
1625 _sleep(TimeBO);
1626  
1627 if(STBL_Init_BL() != SUCCESS)
1628 write_debug_info( "reseting device", 0 , 0, 0, KO);
1629 else
1630 write_debug_info( "reseting device", 0 , 0, 0, OK);
1631 arg_index--;
1632 }
1633 }
1634 else
1635 {
1636 if (arg_index < argc - 1)
1637 printf("bad parameter [%s] \n", argv[arg_index]);
1638  
1639 if(COM_is_Open())
1640 COM_Close();
1641  
1642 printf("\n Press any key to continue ... 25");
1643 getchar();
1644 return 1;
1645 }
1646 }
1647 }
1648 //============================ Run at address ========================================
1649 else if (strcmp(argv[arg_index],"-r")==0)
1650 {
1651 while(arg_index < argc)
1652 {
1653 if (arg_index< argc-1)
1654 arg_index++;
1655 else
1656 break;
1657  
1658 if(Is_Option(argv[arg_index]))
1659 break;
1660  
1661 else if(Is_SubOption(argv[arg_index]))
1662 {
1663 if (arg_index< argc)
1664 arg_index++;
1665 else
1666 break;
1667  
1668 PMAPPINGSECTOR pSector = pmMapping->pSectors;
1669 address = pSector->dwStartAddress;
1670  
1671 if (strcmp(argv[arg_index-1],"--a")==0)
1672 {
1673 address = _tcstoul(argv[arg_index], 0, 16) ;
1674 }
1675 }
1676 else
1677 {
1678 if (arg_index < argc - 1)
1679 printf("bad parameter [%s] \n", argv[arg_index]);
1680  
1681 if(COM_is_Open())
1682 COM_Close();
1683  
1684 printf("\n Press any key to continue ... 26");
1685 getchar();
1686 return 1;
1687 }
1688  
1689 if (STBL_GO(address) == SUCCESS)
1690 {
1691 printf("Your code is running...\n");
1692 }
1693 else
1694 {
1695 printf( "run fails \n");
1696 }
1697 }
1698 }
1699 else
1700 {
1701 if (arg_index < argc - 1)
1702 printf("bad parameter [%s] \n", argv[arg_index]);
1703  
1704 if(COM_is_Open())
1705 COM_Close();
1706  
1707 printf("\n Press any key to continue ... 27");
1708 getchar();
1709 return 1;
1710 }
1711 }
1712 }
1713  
1714 Done_Success:
1715 if (bAuto)
1716 {
1717 // commented
1718 STBL_SetDtr(FALSE);
1719 Sleep(50);
1720  
1721 if(COM_is_Open())
1722 COM_Close();
1723 COM_Open();
1724  
1725 // Reset = Low
1726 STBL_SetRts(TRUE);
1727 Sleep(50);
1728 write_debug_info("Unset BOOT0 & RESET ", 0 ,0, 0, OK);
1729 // Reset = High
1730 STBL_SetRts(FALSE);
1731  
1732 // printf("command executed succesfully, press any key to close the COM port and exit");
1733 // printf("\n RTS set high. Press any key to continue ... 28");
1734 // getchar();
1735 }
1736  
1737 printf("\nFlashing done. Enjoy ... \n");
1738  
1739 if(COM_is_Open())
1740 COM_Close();
1741  
1742 return 0;
1743 }
1744  
1745  
1746 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE******/