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