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