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);
2062 kakl 443 Sleep(100);
2061 mija 444 // Reset = Low
445 STBL_SetRts(TRUE);
446  
2062 kakl 447 Sleep(100);
2061 mija 448  
449 // Reset = High
450 STBL_SetRts(FALSE);
2062 kakl 451 Sleep(100);
452  
2061 mija 453  
454 STBL_SetDtr(FALSE);
455 Sleep(100);
456 COM_Close();
457 COM_Open();
458  
459 STBL_SetDtr(TRUE);
2062 kakl 460 Sleep(100);
2061 mija 461 // Reset = Low
462 STBL_SetRts(TRUE);
463  
2062 kakl 464 Sleep(100);
2061 mija 465  
466 // Reset = High
467 STBL_SetRts(FALSE);
2062 kakl 468 Sleep(100);
2061 mija 469  
470 STBL_SetDtr(FALSE);
471 Sleep(500);
2062 kakl 472  
2061 mija 473 write_debug_info("Setting device to BOOT0", 0 ,0, 0, OK);
474  
475 //printf("\n RTS set low. Press any key to continue ... 4");
476 //getchar();
477 }
478  
479 //============================ command RTS pin =======================================
480 else if (strcmp(argv[arg_index],"-Rts")==0)
481 {
482 //_sleep(1000);
483 while(arg_index < argc)
484 {
485 if (arg_index< argc-1) arg_index++;
486 else break;
487  
488 if(Is_Option(argv[arg_index])) break;
489 else if(Is_SubOption(argv[arg_index]))
490 {
491  
492  
493  
494 if (strcmp(argv[arg_index],"--Hi")==0)
495 {
496 write_debug_info("Set Rts line", 0 ,0, 0,OK);
497 STBL_SetRts(TRUE);
498  
499  
500  
501 }
502 else if (strcmp(argv[arg_index],"--Lo")==0)
503 {
504 write_debug_info("Reset Rts line", 0 ,0, 0,OK);
505 STBL_SetRts(FALSE);
506 }
507 else
508 {
509 write_debug_info("bad parameter [Set Rts line] should be Hi or Lo ", 0 ,0, 0,KO);
510  
511 if (arg_index < argc - 1)
512 printf("bad parameter [%s] \n", argv[arg_index]);
513  
514 if(COM_is_Open())
515 COM_Close();
516  
517 printf("\n Press any key to continue ... 5");
518 getchar();
519 return 1;
520 }
521 }
522 else
523 {
524 if (arg_index < argc - 1)
525 printf("bad parameter [%s] \n", argv[arg_index]);
526  
527 if(COM_is_Open())
528 COM_Close();
529  
530 printf("\n Press any key to continue ... 6");
531 getchar();
532 return 1;
533 }
534 }
535 }
536 //============================ command DTR pin =======================================
537 else if (strcmp(argv[arg_index],"-Dtr")==0)
538 {
539 while(arg_index < argc)
540 {
541 if (arg_index< argc-1)
542 arg_index++;
543 else
544 break;
545  
546 if(Is_Option(argv[arg_index]))
547 break;
548  
549 else if(Is_SubOption(argv[arg_index]))
550 {
551 if (strcmp(argv[arg_index],"--Hi")==0)
552 {
553 write_debug_info("Set Dtr line", 0 ,0, 0,OK);
554 STBL_SetDtr(TRUE);
555  
556 }
557 else if (strcmp(argv[arg_index],"--Lo")==0)
558 {
559 write_debug_info("Reset Dtr line", 0 ,0, 0,OK);
560 STBL_SetDtr(FALSE);
561 }
562 else
563 {
564 write_debug_info("bad parameter [Set Dtr line] should be Hi or Lo ", 0 ,0, 0,KO);
565  
566 if (arg_index < argc - 1)
567 printf("bad parameter [%s] \n", argv[arg_index]);
568  
569 if(COM_is_Open())
570 COM_Close();
571  
572 printf("\n Press any key to continue ... 7");
573 getchar();
574 return 1;
575 }
576 }
577 else
578 {
579 if (arg_index < argc - 1) printf("bad parameter [%s] \n", argv[arg_index]);
580 if (arg_index < argc - 1)
581 printf("bad parameter [%s] \n", argv[arg_index]);
582  
583 if(COM_is_Open())
584 COM_Close();
585  
586 printf("\n Press any key to continue ... 8");
587 getchar();
588 return 1;
589 }
590 }
591 }
592 //============================ ERASE =================================================
593 else if (strcmp(argv[arg_index],"-e")==0)
594 {
595  
596 while(arg_index < argc)
597 {
598 if (!WaitForMoreSubOpt)
599 break;
600  
601 if (arg_index< argc-1)
602 arg_index++;
603 else
604 break;
605  
606 if(Is_Option(argv[arg_index]))
607 break;
608  
609 else if(Is_SubOption(argv[arg_index]))
610 {
611 if (arg_index< argc)
612 arg_index++;
613 else
614 break;
615  
616 //******************** This section is only for STM8 boot loader *******************
617 BYTE Version;
618 Commands pCmds;
619 CString m_Version;
620 if (STBL_GET(&Version, &pCmds) == SUCCESS)
621 {
622 m_Version.Format("%x.%x",Version/16, Version & 0x0F) ;
623 }
624 CIni Ini((LPCSTR)MapFile);
625  
626 if(Ini.IsKeyExist((LPCTSTR)"Product",(LPCTSTR)m_Version))
627 {
628 CString E_W_ROUTINEs = Ini.GetString((LPCTSTR)"Product",(LPCTSTR)m_Version, "");
629 CString Path(*__argv);
630  
631 char fullPath [MAX_PATH];
632  
633 GetModuleFileName(0, fullPath, (MAX_PATH));
634  
635 Path=fullPath;
636  
637 int j=Path.ReverseFind('\\')+1;
638 if(j) Path=Path.Left(j);
639  
640 CString ToFind;
641  
642 ToFind.Format("%s%s%s", Path, "STM8_Routines\\", E_W_ROUTINEs);
643  
644 if(!E_W_ROUTINEs.IsEmpty())
645 {
646 if(!FileExist((LPCTSTR)ToFind))
647 {
648 printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing [%s]\n", ToFind);
649 }
650 else
651 {
652 HANDLE Image;
653 if (FILES_ImageFromFile((LPSTR)(LPCSTR)ToFind,&Image, 1)== FILES_NOERROR)
654 {
655 FILES_SetImageName(Image,(LPSTR)(LPCSTR)ToFind);
656  
657 DWORD NbElements;
658 if (FILES_GetImageNbElement(Image, &NbElements) == FILES_NOERROR)
659 {
660 for (int el=0; el< (int)NbElements;el++)
661 {
662 IMAGEELEMENT Element={0};
663 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
664 {
665 Element.Data=new BYTE[Element.dwDataLength];
666 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
667 {
668 if (STBL_DNLOAD(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS)
669 {
670  
671 }
672 }
673 }
674 }
675  
676 // Verify writen data
677 BOOL VerifySuccess = TRUE;
678 _sleep(100);;
679  
2062 kakl 680 //#ifndef _VS6_USED
2061 mija 681 int el;
2062 kakl 682 //#endif
2061 mija 683 for (el=0; el< (int)NbElements;el++)
684 {
685 IMAGEELEMENT Element={0};
686 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
687 {
688 Element.Data=new BYTE[Element.dwDataLength];
689 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
690 {
691 if (STBL_VERIFY(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS)
692 {
693 VerifySuccess = FALSE;
694 char str[255];
695 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.");
696 AfxMessageBox(str, MB_OK|MB_ICONEXCLAMATION);
697 return 1;
698 }
699 }
700 }
701 }
702 }
703 }
704 else
705 {
706 AfxMessageBox("Unable to load data from this file " + ToFind + " ...");
707 return -1;
708 }
709 }
710 }
711 }
712 else
713 {
714 int family = Ini.GetInt((LPCTSTR)"Product",(LPCTSTR)"family", 0);
715 if(family == 3)
716 {
717 printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing\n");
718 }
719 }
720 //End****************** This section is only for STM8 boot loader *******************
721  
722 //End****************** This section is only for STM8 boot loader *******************
723  
724 printf("\n ERASING ... \n");
725 if (strcmp(argv[arg_index-1],"--all")==0)
726 {
727  
728 WaitForMoreSubOpt = false;
729 Res = STBL_ERASE(0xFFFF, NULL);
730  
731  
732  
733 if (Res != SUCCESS)
734 write_debug_info("erasing all pages", 0 ,0, 0, KO);
735 else
736 write_debug_info("erasing all pages", 0 ,0, 0, OK);
737 }
738 else if (strcmp(argv[arg_index-1],"--sec")==0)
739 {
740 WaitForMoreSubOpt = true;
741  
742 nsec = atoi(argv[arg_index]);
743 LPWORD sectors = (LPWORD)malloc(nsec *2 + 2);
744  
745  
746 sectors[0] = 0;
747 for(int i = 1; i<= nsec; i++)
748 {
749 sectors[0]++;
750 arg_index++;
751 sectors[sectors[0]] = atoi(argv[arg_index]);
752 }
753  
754 WaitForMoreSubOpt = false;
755  
756 printf("\nerasing %i sectors : ", sectors[0]);
757  
2062 kakl 758 //#ifndef _VS6_USED
2061 mija 759 int i;
2062 kakl 760 //#endif
2061 mija 761  
762 for(i = 1; i<= nsec; i++)
763 {
764 printf("<%i>", sectors[i]);
765 }
766 printf("\n");
767  
768 Res = STBL_ERASE(nsec, (LPBYTE)sectors+2);
769 if (Res != SUCCESS)
770 {
771 write_debug_info("erasing", 0 ,0, 0, KO);
772  
773 if(COM_is_Open())
774 COM_Close();
775  
776 printf("\n Press any key to continue ... 9");
777 getchar();
778 return 1;
779 }
780 else
781 write_debug_info("erasing", 0 ,0, 0, OK);
782  
783 arg_index++;
784 }
785 }
786 else
787 {
788 if (arg_index < argc - 1)
789 printf("bad parameter [%s] \n", argv[arg_index]);
790  
791 if(COM_is_Open())
792 COM_Close();
793  
794 printf("\n Press any key to continue ... 10");
795 getchar();
796 return 1;
797 }
798 }
799 }
800 //============================ UPLOAD ===============================================
801 else if (strcmp(argv[arg_index],"-u")==0)
802 {
803 while(arg_index < argc)
804 {
805 if (arg_index< argc-1)
806 arg_index++;
807 else
808 break;
809  
810 if(Is_Option(argv[arg_index]))
811 break;
812  
813 else if(Is_SubOption(argv[arg_index]))
814 {
815 if (arg_index< argc)
816 arg_index++;
817 else
818 break;
819  
820 /*if (strcmp(argv[arg_index-1],"--a")==0)
821 {
822 address = _tcstoul(argv[arg_index], 0, 16) ;
823 }
824 else if (strcmp(argv[arg_index-1],"--s")==0)
825 {
826 size = _tcstoul(argv[arg_index], 0, 16) ;
827 }
828 else */if (strcmp(argv[arg_index-1],"--fn")==0)
829 {
830 filename = argv[arg_index];
831 }
832 }
833 else
834 {
835 if (arg_index < argc - 1)
836 printf("bad parameter [%s] \n", argv[arg_index]);
837  
838 if(COM_is_Open())
839 COM_Close();
840  
841 printf("\n Press any key to continue ... 11");
842 getchar();
843 return 1;
844 }
845 }
846  
847 printf("\n UPLOADING ... \n\n");
848  
849 HANDLE Handle;
850 FILES_CreateImage(&Handle, 0);
851  
852 FILES_CreateImageFromMapping(&Handle,pmMapping);
853  
854 DWORD NbElements = 0;
855 if (FILES_GetImageNbElement(Handle, &NbElements) == FILES_NOERROR)
856 {
857 if (NbElements > 0)
858 {
859 for(int i = 0; i< (int)NbElements; i++)
860 {
861 IMAGEELEMENT Element={0};
862 // Get element data size
863 if (FILES_GetImageElement(Handle, i, &Element) == FILES_NOERROR)
864 {
865 //Upload element data
866 Element.Data = (LPBYTE)malloc(Element.dwDataLength);
867 if (STBL_UPLOAD(Element.dwAddress, Element.Data, Element.dwDataLength) == SUCCESS)
868 {
869 //Insert elment in the Image
870 write_debug_info("Uploading", i ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, OK);
871 FILES_SetImageElement(Handle,i,FALSE,Element);
872 }
873 else
874 {
875 write_debug_info("Uploading", i ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, KO);
876  
877 if(COM_is_Open())
878 COM_Close();
879  
880 printf("\n Press any key to continue ... 12");
881 getchar();
882 return 1;
883 }
884 }
885 }
886 }
887 }
888  
889 if(!FileExist((LPCTSTR)filename))
890 {
891 printf( "file %s does not exist .. Creating file\n", filename);
892 FILE* fp = fopen((LPCTSTR)filename, "a+");
893 fclose(fp);
894 }
895  
896 printf( "Writing data ...\n");
897  
898 if (FILES_ImageToFile((LPSTR)(LPCSTR)filename,Handle) != FILES_NOERROR)
899 {
900 printf( "cannot write to file %s \n", filename);
901  
902 if(COM_is_Open())
903 COM_Close();
904  
905 printf("\n Press any key to continue ... 13");
906 getchar();
907 return 1;
908 }
909 else
910 printf("\n Uploaded data is dumped on %s", filename);
911 }
912  
913  
914  
915 //============================ Get Device map file name ==============================
916 else if (strcmp(argv[arg_index],"-i")==0)
917 {
918 if (arg_index< argc)
919 arg_index++;
920 else
921 break;
922  
923  
924 sprintf(devname,"%s.STmap", argv[arg_index]);
925  
926 char Drive[3], Dir[256], Fname[256], Ext[256];
927 _splitpath(argv[0],Drive,Dir,Fname,Ext);
928  
929 sprintf(MapFile, "%s%s%s%s", Drive, Dir , "Map\\", devname);
930  
931 pmMapping = NULL;
932 WORD size = 0;
933  
934 WORD PacketSize = 0;
935 pmMapping = NULL;
936 WORD Size = 0;
937 char MapName[256];
938 // Get the number of sectors in the flash target: pmMapping should be NULL
939 // number of sectors is returned in the Size value
940 BYTE PagePerSector = 0;
941  
942 if (!FileExist((LPCTSTR)MapFile))
943 {
944 printf("This version is not intended to support the <%s> target\n", argv[arg_index]);
945  
946 if(COM_is_Open())
947 COM_Close();
948  
949 printf("\n Press any key to continue ... 14");
950 getchar();
951 return 1;
952 }
953  
954 FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)MapName, &PacketSize, pmMapping, &PagePerSector);
955 // Allocate the mapping structure memory
956 pmMapping = (PMAPPING)malloc(sizeof(MAPPING));
957 pmMapping->NbSectors = 0;
958 pmMapping->pSectors = (PMAPPINGSECTOR) malloc((Size) * sizeof(MAPPINGSECTOR));
959  
960 // Get the mapping info
961 FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)(LPCTSTR)MapName, &PacketSize, pmMapping, &PagePerSector);
962  
963 SetPaketSize(PacketSize);
964  
965  
966  
967 //sending BL config byte (0x7F) & identifing target
968  
969  
970  
971 Res = STBL_Init_BL();
972  
973 if (Res == UNREOGNIZED_DEVICE)
974 {
975 write_debug_info("Activating device", 0 ,0, 0, KO);
976  
977 if(COM_is_Open())
978 COM_Close();
979  
980 printf("Unrecognized device... Please, reset your device then try again \n");
981  
982 if(COM_is_Open())
983 COM_Close();
984  
985 printf("Please, reset your device then press any key to continue \n");
986 printf("\n Press any key to continue ... 15");
987 getchar();
988 goto START;
989 }
990 else if (Res != SUCCESS)
991 {
992 write_debug_info("Activating device", 0 ,0, 0, KO);
993 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");
994  
995 if(COM_is_Open())
996 COM_Close();
997  
998 printf("Please, reset your device then then press any key to continue \n");
999 printf("\n Press any key to continue ... 16");
1000 getchar();
1001 goto START;
1002 }
1003  
1004 _sleep(TimeBO);
1005  
1006 write_debug_info("Activating device", 0 ,0, 0, OK);
1007 //Getting Target informations (version, available commands)
1008 BYTE Version ;
1009 Commands pCmds;
1010  
1011 Res = STBL_GET(&Version, &pCmds);
1012 if (Res != SUCCESS)
1013 {
1014 if(COM_is_Open())
1015 COM_Close();
1016  
1017 printf("\n Press any key to continue ... 17");
1018 getchar();
1019 return 1;
1020 }
1021  
1022 SetTimeOut(timeout);
1023  
1024 if (arg_index< argc)
1025 arg_index++;
1026 else
1027 break;
1028 }
1029 //============================ DOWNLOAD ==============================================
1030 else if (strcmp(argv[arg_index],"-d")==0)
1031 {
1032 while(arg_index < argc)
1033 {
1034 if (arg_index< argc-1)
1035 arg_index++;
1036 else
1037 break;
1038  
1039 if(Is_Option(argv[arg_index]))
1040 break;
1041  
1042 else if(Is_SubOption(argv[arg_index]))
1043 {
1044 if (arg_index< argc)
1045 arg_index++;
1046 else
1047 break;
1048  
1049 if (strcmp(argv[arg_index-1],"--a")==0)
1050 {
1051 address = _tcstoul(argv[arg_index], 0, 16) ;
1052 }
1053 else if (strcmp(argv[arg_index-1],"--v")==0)
1054 {
1055 Verify = true;
1056 arg_index--;
1057 }
1058 else if (strcmp(argv[arg_index-1],"--o")==0)
1059 {
1060 optimize = TRUE;
1061 arg_index--;
1062 }
1063 else if (strcmp(argv[arg_index-1],"--fn")==0)
1064 {
1065 filename = argv[arg_index];
1066 _splitpath(filename,Drive,Dir,Fname,Ext);
1067 ptr=strupr(Ext);
1068 strcpy(Ext, ptr);
1069 }
1070 }
1071 else
1072 {
1073 if (arg_index < argc - 1)
1074 printf("bad parameter [%s] \n", argv[arg_index]);
1075  
1076 if(COM_is_Open())
1077 COM_Close();
1078  
1079 printf("\n Press any key to continue ... 18");
1080 getchar();
1081 return 1;
1082 }
1083 }
1084  
1085 PMAPPINGSECTOR pSector = pmMapping->pSectors;
1086 for(int i = 1; i<= (int)pmMapping->NbSectors; i++)
1087 {
1088 if ((strcmp(Ext, ".BIN")!=0) && (i==0))
1089 address = pSector->dwStartAddress;
1090  
1091 pSector->UseForOperation = TRUE;
1092 pSector++;
1093 }
1094  
1095 if(!FileExist((LPCTSTR)filename))
1096 {
1097 printf( "file does not exist %s \n", filename);
1098  
1099 if(COM_is_Open())
1100 COM_Close();
1101  
1102 printf("\n Press any key to continue ... 19");
1103 getchar();
1104 return 1;
1105 }
1106  
1107 //****************** This section is only for STM8 boot loader *******************
1108 BYTE Version;
1109 Commands pCmds;
1110 CString m_Version;
1111 if (STBL_GET(&Version, &pCmds) == SUCCESS)
1112 {
1113 m_Version.Format("%x.%x",Version/16, Version & 0x0F) ;
1114 }
1115 CIni Ini((LPCSTR)MapFile);
1116  
1117 if(Ini.IsKeyExist((LPCTSTR)"Product",(LPCTSTR)m_Version))
1118 {
1119 CString E_W_ROUTINEs = Ini.GetString((LPCTSTR)"Product",(LPCTSTR)m_Version, "");
1120 CString Path(*__argv);
1121 int j=Path.ReverseFind('\\')+1;
1122 if(j) Path=Path.Left(j);
1123  
1124 CString ToFind;
1125  
1126 ToFind.Format("%s%s%s", Path, "STM8_Routines\\", E_W_ROUTINEs);
1127  
1128 if(!E_W_ROUTINEs.IsEmpty())
1129 {
1130 if(!FileExist((LPCTSTR)ToFind))
1131 {
1132 printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing [%s]\n", ToFind);
1133 }
1134 else
1135 {
1136 HANDLE Image;
1137 if (FILES_ImageFromFile((LPSTR)(LPCSTR)ToFind,&Image, 1)== FILES_NOERROR)
1138 {
1139 FILES_SetImageName(Image,(LPSTR)(LPCSTR)ToFind);
1140  
1141 DWORD NbElements;
1142 if (FILES_GetImageNbElement(Image, &NbElements) == FILES_NOERROR)
1143 {
1144 for (int el=0; el< (int)NbElements;el++)
1145 {
1146 IMAGEELEMENT Element={0};
1147 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
1148 {
1149 Element.Data=new BYTE[Element.dwDataLength];
1150 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
1151 {
1152 if (STBL_DNLOAD(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS)
1153 {
1154  
1155 }
1156 }
1157 }
1158 }
1159  
1160 // Verify writen data
1161 BOOL VerifySuccess = TRUE;
1162 _sleep(100);
1163  
2062 kakl 1164 //#ifndef _VS6_USED
2061 mija 1165 int el;
2062 kakl 1166 //#endif
2061 mija 1167  
1168 for (el=0; el< (int)NbElements;el++)
1169 {
1170 IMAGEELEMENT Element={0};
1171 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
1172 {
1173 Element.Data=new BYTE[Element.dwDataLength];
1174 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
1175 {
1176 if (STBL_VERIFY(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS)
1177 {
1178 VerifySuccess = FALSE;
1179 char str[255];
1180 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.");
1181 AfxMessageBox(str, MB_OK|MB_ICONEXCLAMATION);
1182 return 1;
1183 }
1184 }
1185 }
1186 }
1187 }
1188 }
1189 else
1190 {
1191 AfxMessageBox("Unable to load data from this file " + ToFind + " ...");
1192 return -1;
1193 }
1194 }
1195 }
1196 }
1197 else
1198 {
1199 int family = Ini.GetInt((LPCTSTR)"Product",(LPCTSTR)"family", 0);
1200 if(family == 3)
1201 {
1202 printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing\n");
1203 }
1204 }
1205 //End****************** This section is only for STM8 boot loader *******************
1206  
1207 printf("\n DOWNLOADING ... \n\n");
1208  
1209 HANDLE Handle;
1210 if (FILES_ImageFromFile((LPSTR)(LPCSTR)filename,&Handle, 1) == FILES_NOERROR)
1211 {
1212 FILES_SetImageName(Handle,(LPSTR)(LPCSTR)filename);
1213  
1214 DWORD NbElements = 0;
1215 if (FILES_GetImageNbElement(Handle, &NbElements) == FILES_NOERROR)
1216 {
1217 if ( NbElements > 0 )
1218 { // if binary file -> change the elemnts address
1219 if (strcmp(Ext, ".BIN")==0)
1220 {
1221 for (int i=0;i< (int)NbElements;i++)
1222 {
1223 IMAGEELEMENT Element={0};
1224 if (FILES_GetImageElement(Handle, i, &Element) == FILES_NOERROR)
1225 {
1226 Element.Data= (LPBYTE)malloc(Element.dwDataLength);
1227 if (FILES_GetImageElement(Handle, i, &Element) == FILES_NOERROR)
1228 {
1229 Element.dwAddress = Element.dwAddress + address;
1230 FILES_SetImageElement(Handle, i, FALSE, Element);
1231 }
1232 }
1233 }
1234 }
1235 }
1236 }
1237  
1238 FILES_FilterImageForOperation(Handle, pmMapping, OPERATION_UPLOAD, optimize);
1239 }
1240 else
1241 {
1242 printf("cannot open file %s \n", filename);
1243  
1244 if(COM_is_Open())
1245 COM_Close();
1246  
1247 printf("\n Press any key to continue ... 20");
1248 getchar();
1249 return 1;
1250 }
1251  
1252 DWORD NbElements = 0;
1253 if (FILES_GetImageNbElement(Handle, &NbElements) == FILES_NOERROR)
1254 {
1255 for (int el=0; el< (int)NbElements;el++)
1256 {
1257 IMAGEELEMENT Element={0};
1258 if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR)
1259 {
1260 Element.Data= (LPBYTE)malloc(Element.dwDataLength);
1261 if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR)
1262 {
1263 if ((strcmp(Ext, ".BIN")==0) && (el==0))
1264 Element.dwAddress = address;
1265  
1266 if (STBL_DNLOAD(Element.dwAddress, Element.Data, Element.dwDataLength, optimize) != SUCCESS)
1267 {
1268 write_debug_info( "downloading", el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, KO);
1269 write_debug_info("The flash may be read protected; use -p --drp to disable write protection." , 0, 0, 0, KO);
1270  
1271 if(COM_is_Open())
1272 COM_Close();
1273  
1274 printf("\n Press any key to continue ... 21");
1275 getchar();
1276 return 1;
1277 }
1278  
1279 write_debug_info( "downloading", el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, OK);
1280 }
1281 }
1282 }
1283 }
1284  
1285 bool VerifySuccess = true;
1286 if (Verify)
1287 {
1288 printf("\n VERIFYING ... \n\n");
1289  
1290  
1291 for (int el=0; el< (int)NbElements;el++)
1292 {
1293 IMAGEELEMENT Element={0};
1294 if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR)
1295 {
1296 Element.Data=(LPBYTE)malloc(Element.dwDataLength);
1297 if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR)
1298 {
1299 if ((strcmp(Ext, ".BIN")==0) && (el==0))
1300 Element.dwAddress = address;
1301  
1302 if (STBL_VERIFY(Element.dwAddress, Element.Data, Element.dwDataLength, optimize) != SUCCESS)
1303 {
1304 VerifySuccess = false;
1305 write_debug_info("verifying" ,el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, KO);
1306 write_debug_info("some pages may be write protected; use -p --dwp to disable write protection." , 0, 0, 0, KO);
1307  
1308 if(COM_is_Open())
1309 COM_Close();
1310  
1311 printf("\n Press any key to continue ... 22");
1312 getchar();
1313 return 1;
1314 }
1315  
1316 write_debug_info("verifying" ,el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, OK);
1317 }
1318 }
1319 }
1320 }
1321  
1322 }
1323 //============================ VERIFY ================================================
1324 else if (strcmp(argv[arg_index],"-v")==0)
1325 {
1326 if (arg_index< argc)
1327 arg_index++;
1328 else
1329 break;
1330 }
1331 //============================ Program option bytes ==================================
1332 else if (strcmp(argv[arg_index],"-o")==0)
1333 {
1334 while(arg_index < argc)
1335 {
1336 if (arg_index< argc-1)
1337 arg_index++;
1338 else
1339 break;
1340  
1341 if(Is_Option(argv[arg_index]))
1342 break;
1343  
1344 else if(Is_SubOption(argv[arg_index]))
1345 {
1346 if (arg_index< argc)
1347 arg_index++;
1348 else
1349 break;
1350  
1351 if (strcmp(argv[arg_index-1],"--get")==0)
1352 {
1353 if (arg_index< argc)
1354 arg_index++;
1355 else
1356 break;
1357  
1358 if (strcmp(argv[arg_index-1],"--fn")==0)
1359 filename = argv[arg_index];
1360  
1361 if(TARGET_GetSIFData(&User, &RDP, &Data0, &Data1, &WRP0, &WRP1, &WRP2, &WRP3) == SUCCESS)
1362 {
1363 write_debug_info("Getting Option bytes data" ,0 ,0, 0, OK);
1364  
1365 HANDLE Image;
1366 if (FILES_CreateImage(&Image, 1) == FILES_NOERROR)
1367 {
1368 IMAGEELEMENT Element={0};
1369 Element.dwAddress = 0x1FFFF800;
1370 Element.dwDataLength = 16;
1371 Element.Data = (LPBYTE)malloc(Element.dwDataLength);
1372  
1373 {
1374 Element.Data[0] = RDP;
1375 Element.Data[1] = ~RDP;
1376 Element.Data[2] = User;
1377 Element.Data[3] = ~User;
1378 Element.Data[4] = Data0;
1379 Element.Data[5] = ~Data0;
1380 Element.Data[6] = Data1;
1381 Element.Data[7] = ~Data1;
1382 Element.Data[8] = WRP0;
1383 Element.Data[9] = ~WRP0;
1384 Element.Data[10] = WRP1;
1385 Element.Data[11] = ~WRP1;
1386 Element.Data[12] = WRP2;
1387 Element.Data[13] = ~WRP2;
1388 Element.Data[14] = WRP3;
1389 Element.Data[15] = ~WRP3;
1390 }
1391  
1392 FILES_SetImageElement(Image,0,TRUE,Element);
1393 if (FILES_ImageToFile((LPSTR)(LPCSTR)filename,Image) != FILES_NOERROR)
1394 {
1395 write_debug_info("Saving Option bytes data",0 ,0, 0, KO);
1396 }
1397 else write_debug_info("Saving Option bytes data",0 ,0, 0, OK);
1398 }
1399 }
1400 else write_debug_info("Getting Option bytes data" ,0 ,0, 0, KO);
1401 }
1402 else if (strcmp(argv[arg_index-1],"--set")==0)
1403 {
1404 if (arg_index< argc) arg_index++;
1405 else break;
1406  
1407 if (strcmp(argv[arg_index-1],"--fn")==0)
1408 {
1409 filename = argv[arg_index];
1410  
1411 HANDLE OPBImage;
1412  
1413 if(!FileExist((LPCTSTR)filename))
1414 {
1415 printf( "file does not exist %s \n", filename);
1416  
1417 if(COM_is_Open())
1418 COM_Close();
1419  
1420 printf("\n Press any key to continue ... 23");
1421 getchar();
1422 return 1;
1423 }
1424  
1425 if (FILES_ImageFromFile((LPSTR)(LPCSTR)filename, &OPBImage, 0) == FILES_NOERROR)
1426 {
1427 DWORD NbElements = 0;
1428 if (FILES_GetImageNbElement(OPBImage, &NbElements) == FILES_NOERROR)
1429 {
1430 if ( NbElements == 1 )
1431 {
1432 IMAGEELEMENT Element={0};
1433 if (FILES_GetImageElement(OPBImage, 0, &Element) == FILES_NOERROR)
1434 {
1435 Element.Data= (LPBYTE)malloc(Element.dwDataLength);
1436 if (FILES_GetImageElement(OPBImage, 0, &Element) == FILES_NOERROR)
1437 {
1438 RDP = Element.Data[0] ;
1439 User = Element.Data[2] ;
1440 Data0 = Element.Data[4] ;
1441 Data1 = Element.Data[6] ;
1442 WRP0 = Element.Data[8] ;
1443 WRP1 = Element.Data[10];
1444 WRP2 = Element.Data[12];
1445 WRP3 = Element.Data[14];
1446  
1447  
1448 if (TARGET_SetSIFData(User, RDP, Data0, Data1, WRP0, WRP1, WRP2, WRP3) == SUCCESS)
1449 {
1450 write_debug_info("Setting Option bytes data" ,0 ,0, 0, OK);
1451  
1452 if(COM_is_Open())
1453 COM_Close();
1454  
1455 COM_Open();
1456  
1457 if(STBL_Init_BL() != SUCCESS)
1458 write_debug_info("Resetting device" ,0 ,0, 0, KO);
1459 else
1460 write_debug_info("Resetting device" ,0 ,0, 0, OK);
1461 }
1462 else
1463 write_debug_info("Setting Option bytes data" ,0 ,0, 0, KO);
1464 }
1465 }
1466 }
1467 }
1468 }
1469 }
1470 else if (strcmp(argv[arg_index-1],"--vals")==0)
1471 {
1472 TARGET_GetSIFData(&User, &RDP, &Data0, &Data1, &WRP0, &WRP1, &WRP2, &WRP3);
1473  
1474 while(arg_index< argc)
1475 {
1476 if(Is_Option(argv[arg_index]))
1477 break;
1478 else if(Is_SubOption(argv[arg_index]))
1479 {
1480 arg_index++;
1481 if(strcmp(argv[arg_index-1],"--RDP")==0) { RDP = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1482 else if(strcmp(argv[arg_index-1],"--User")==0) { User = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1483 else if(strcmp(argv[arg_index-1],"--data0")==0){ Data0 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1484 else if(strcmp(argv[arg_index-1],"--data1")==0){ Data1 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1485 else if(strcmp(argv[arg_index-1],"--WRP0")==0) { WRP0 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1486 else if(strcmp(argv[arg_index-1],"--WRP1")==0) { WRP1 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1487 else if(strcmp(argv[arg_index-1],"--WRP2")==0) { WRP2 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1488 else if(strcmp(argv[arg_index-1],"--WRP3")==0) { WRP3 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1489 }
1490 }
1491 if (TARGET_SetSIFData(User, RDP, Data0, Data1, WRP0, WRP1, WRP2, WRP3) != SUCCESS)
1492 write_debug_info("Setting Option bytes data" ,0 ,0, 0, KO);
1493 else
1494 {
1495 write_debug_info("Setting Option bytes data" ,0 ,0, 0, OK);
1496  
1497 if(COM_is_Open())
1498 COM_Close();
1499  
1500 COM_Open();
1501  
1502 if(STBL_Init_BL() != SUCCESS)
1503 write_debug_info("Resetting device" ,0 ,0, 0, KO);
1504 else
1505 write_debug_info("Resetting device" ,0 ,0, 0, OK);
1506 }
1507 arg_index--;
1508 }
1509 }
1510 }
1511 else
1512 {
1513 if (arg_index < argc - 1)
1514 printf("bad parameter [%s] \n", argv[arg_index]);
1515  
1516 if(COM_is_Open())
1517 COM_Close();
1518  
1519 printf("\n Press any key to continue ... 24");
1520 getchar();
1521 return 1;
1522 }
1523 }
1524 }
1525 //============================ Set/Unset R/W protection ==========================
1526 else if (strcmp(argv[arg_index],"-p")==0)
1527 {
1528 while(arg_index < argc)
1529 {
1530 if (arg_index< argc-1)
1531 arg_index++;
1532 else
1533 break;
1534  
1535 if(Is_Option(argv[arg_index]))
1536 break;
1537  
1538 else if(Is_SubOption(argv[arg_index]))
1539 {
1540 if (arg_index< argc)
1541 arg_index++;
1542 else
1543 break;
1544  
1545 if (strcmp(argv[arg_index-1],"--erp")==0)
1546 {
1547 if(STBL_READOUT_PROTECT() != SUCCESS)
1548 write_debug_info( "enabling read protection", 0 , 0, 0, KO);
1549 else
1550 write_debug_info( "enabling read protection", 0 , 0, 0, OK);
1551  
1552 _sleep(TimeBO);
1553  
1554 if(STBL_Init_BL() != SUCCESS)
1555 write_debug_info( "reseting device", 0 , 0, 0, KO);
1556 else
1557 write_debug_info( "reseting device", 0 , 0, 0, OK);
1558  
1559 arg_index--;
1560 }
1561 else if (strcmp(argv[arg_index-1],"--drp")==0)
1562 {
1563 if(STBL_READOUT_PERM_UNPROTECT() == SUCCESS)
1564 {
1565 write_debug_info( "disabling read protection", 0 , 0, 0, OK);
1566  
1567 _sleep(TimeBO);
1568  
1569 if(STBL_Init_BL() != SUCCESS)
1570 write_debug_info( "reseting device", 0 , 0, 0, KO);
1571 else
1572 write_debug_info( "reseting device", 0 , 0, 0, OK);
1573 }
1574 else
1575 write_debug_info( "disabling read protection", 0 , 0, 0, KO);
1576  
1577 arg_index--;
1578 }
1579 else if (strcmp(argv[arg_index-1],"--ewp")==0)
1580 {
1581 LPBYTE sectors;
1582 if(Is_Option(argv[arg_index])) break;
1583  
1584 nsec = atoi(argv[arg_index]);
1585 sectors = (LPBYTE)malloc(nsec + 1);
1586  
1587  
1588 sectors[0] = 0;
1589 for(int i = 1; i<= nsec; i++)
1590 {
1591 sectors[0]++;
1592 arg_index++;
1593 sectors[sectors[0]] = atoi(argv[arg_index]);
1594 }
1595  
1596  
1597 printf("\nenabling write protection %i sectors : ", sectors[0]);
1598  
2062 kakl 1599 //#ifndef _VS6_USED
2061 mija 1600 int i;
2062 kakl 1601 //#endif
2061 mija 1602  
1603 for(i = 1; i<= nsec; i++)
1604 {
1605 printf("<%i>", sectors[i]);
1606 }
1607 printf("\n");
1608  
1609 if(STBL_WRITE_PROTECT(((LPBYTE)sectors)[0],&((LPBYTE)sectors)[1]) != SUCCESS)
1610 write_debug_info( "enabling write protection", 0 , 0, 0, KO);
1611 else
1612 write_debug_info( "enabling write protection", 0 , 0, 0, OK);
1613  
1614 _sleep(TimeBO);
1615  
1616 if(STBL_Init_BL() != SUCCESS)
1617 write_debug_info( "reseting device", 0 , 0, 0, KO);
1618 else
1619 write_debug_info( "reseting device", 0 , 0, 0, OK);
1620 }
1621 else if (strcmp(argv[arg_index-1],"--dwp")==0)
1622 {
1623 if(STBL_WRITE_PERM_UNPROTECT() != SUCCESS)
1624 write_debug_info( "disabling write protection", 0 , 0, 0, KO);
1625 else
1626 write_debug_info( "disabling write protection", 0 , 0, 0, OK);
1627  
1628 _sleep(TimeBO);
1629  
1630 if(STBL_Init_BL() != SUCCESS)
1631 write_debug_info( "reseting device", 0 , 0, 0, KO);
1632 else
1633 write_debug_info( "reseting device", 0 , 0, 0, OK);
1634 arg_index--;
1635 }
1636 }
1637 else
1638 {
1639 if (arg_index < argc - 1)
1640 printf("bad parameter [%s] \n", argv[arg_index]);
1641  
1642 if(COM_is_Open())
1643 COM_Close();
1644  
1645 printf("\n Press any key to continue ... 25");
1646 getchar();
1647 return 1;
1648 }
1649 }
1650 }
1651 //============================ Run at address ========================================
1652 else if (strcmp(argv[arg_index],"-r")==0)
1653 {
1654 while(arg_index < argc)
1655 {
1656 if (arg_index< argc-1)
1657 arg_index++;
1658 else
1659 break;
1660  
1661 if(Is_Option(argv[arg_index]))
1662 break;
1663  
1664 else if(Is_SubOption(argv[arg_index]))
1665 {
1666 if (arg_index< argc)
1667 arg_index++;
1668 else
1669 break;
1670  
1671 PMAPPINGSECTOR pSector = pmMapping->pSectors;
1672 address = pSector->dwStartAddress;
1673  
1674 if (strcmp(argv[arg_index-1],"--a")==0)
1675 {
1676 address = _tcstoul(argv[arg_index], 0, 16) ;
1677 }
1678 }
1679 else
1680 {
1681 if (arg_index < argc - 1)
1682 printf("bad parameter [%s] \n", argv[arg_index]);
1683  
1684 if(COM_is_Open())
1685 COM_Close();
1686  
1687 printf("\n Press any key to continue ... 26");
1688 getchar();
1689 return 1;
1690 }
1691  
1692 if (STBL_GO(address) == SUCCESS)
1693 {
1694 printf("Your code is running...\n");
1695 }
1696 else
1697 {
1698 printf( "run fails \n");
1699 }
1700 }
1701 }
1702 else
1703 {
1704 if (arg_index < argc - 1)
1705 printf("bad parameter [%s] \n", argv[arg_index]);
1706  
1707 if(COM_is_Open())
1708 COM_Close();
1709  
1710 printf("\n Press any key to continue ... 27");
1711 getchar();
1712 return 1;
1713 }
1714 }
1715 }
1716  
1717 Done_Success:
1718 if (bAuto)
1719 {
1720 // commented
1721 STBL_SetDtr(FALSE);
1722 Sleep(50);
1723  
1724 if(COM_is_Open())
1725 COM_Close();
1726 COM_Open();
1727  
1728 // Reset = Low
1729 STBL_SetRts(TRUE);
1730 Sleep(50);
1731 write_debug_info("Unset BOOT0 & RESET ", 0 ,0, 0, OK);
1732 // Reset = High
1733 STBL_SetRts(FALSE);
1734  
1735 // printf("command executed succesfully, press any key to close the COM port and exit");
1736 // printf("\n RTS set high. Press any key to continue ... 28");
1737 // getchar();
1738 }
1739  
1740 printf("\nFlashing done. Enjoy ... \n");
1741  
1742 if(COM_is_Open())
1743 COM_Close();
1744  
1745 return 0;
1746 }
1747  
1748  
1749 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE******/