comparison loadtools/flprogsrec.c @ 666:51bcfb251b23

fc-loadtool flash program-m0 changed to use binary protocol
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 07 Mar 2020 19:28:09 +0000
parents a880f48d6ac0
children 815c3f8bcff1
comparison
equal deleted inserted replaced
665:b43d8c2725b9 666:51bcfb251b23
6 #include <sys/types.h> 6 #include <sys/types.h>
7 #include <stdio.h> 7 #include <stdio.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <stdlib.h> 9 #include <stdlib.h>
10 #include <time.h> 10 #include <time.h>
11 #include <unistd.h>
11 #include "flash.h" 12 #include "flash.h"
12 #include "discontig.h" 13 #include "discontig.h"
13 #include "srecreader.h" 14 #include "srecreader.h"
15
16 extern int target_fd;
14 17
15 extern struct flash_bank_info flash_bank_info[2]; 18 extern struct flash_bank_info flash_bank_info[2];
16 extern uint32_t crc32_table[]; 19 extern uint32_t crc32_table[];
17 20
18 read_srec_img_for_flash(imgfile, is_m0, bank_size, reglistp, regcountp, 21 read_srec_img_for_flash(imgfile, is_m0, bank_size, reglistp, regcountp,
189 struct flash_bank_info *bi; 192 struct flash_bank_info *bi;
190 struct discontig_prog regions[MAX_SREC_REGIONS], *regp; 193 struct discontig_prog regions[MAX_SREC_REGIONS], *regp;
191 unsigned nregions, reg; 194 unsigned nregions, reg;
192 uint32_t total_len, bytesdone, addr, len; 195 uint32_t total_len, bytesdone, addr, len;
193 FILE *tmpfile; 196 FILE *tmpfile;
194 char *targv[4], shortarg[10], longarg[513]; 197 char *targv[3], shortarg[10];
195 u_char databuf[256]; 198 u_char databuf[2048 + 7], ackbyte;
196 int reclen, cc, rc; 199 int reclen, cc, rc;
197 time_t initial_time, curtime, last_time; 200 time_t initial_time, curtime, last_time;
198 unsigned duration, mm, ss; 201 unsigned duration, mm, ss;
199 u_long crc_from_target; 202 u_long crc_from_target;
200 203
223 if (bi->ops->prep_for_program(bi) < 0) { 226 if (bi->ops->prep_for_program(bi) < 0) {
224 fclose(tmpfile); 227 fclose(tmpfile);
225 return(-1); 228 return(-1);
226 } 229 }
227 rewind(tmpfile); 230 rewind(tmpfile);
228 targv[0] = bi->ops->loadagent_program_cmd; 231 targv[0] = bi->ops->loadagent_binmode_cmd;
229 targv[1] = shortarg; 232 targv[1] = 0;
230 targv[2] = longarg; 233 tpinterf_make_cmd(targv);
231 targv[3] = 0; 234 if (tpinterf_send_cmd() < 0) {
235 fclose(tmpfile);
236 return(-1);
237 }
232 printf("Programming flash\n"); 238 printf("Programming flash\n");
239 databuf[0] = 0x01;
233 bytesdone = 0; 240 bytesdone = 0;
234 last_time = 0; 241 last_time = 0;
235 time(&initial_time); 242 time(&initial_time);
236 for (reg = 0, regp = regions; reg < nregions; reg++, regp++) { 243 for (reg = 0, regp = regions; reg < nregions; reg++, regp++) {
237 addr = regp->start; 244 addr = regp->start;
238 len = regp->end - addr; 245 len = regp->end - addr;
239 while (len) { 246 while (len) {
240 if (len >= 256) 247 if (len >= 2048)
241 reclen = 256; 248 reclen = 2048;
242 else 249 else
243 reclen = len; 250 reclen = len;
244 cc = fread(databuf, 1, reclen, tmpfile); 251 cc = fread(databuf + 7, 1, reclen, tmpfile);
245 if (cc != reclen) { 252 if (cc != reclen) {
246 fclose(tmpfile); 253 fclose(tmpfile);
247 fprintf(stderr, 254 fprintf(stderr,
248 "error reading from temp file!\n"); 255 "error reading from temp file!\n");
249 return(-1); 256 /* don't leave loadagent in binary flash mode */
250 } 257 databuf[0] = 0x04;
251 sprintf(shortarg, "%lx", addr); 258 write(target_fd, databuf, 1);
252 build_flashw_hex_string(databuf, longarg, reclen >> 1, 259 tpinterf_pass_output(1);
253 0); 260 return(-1);
254 tpinterf_make_cmd(targv); 261 }
255 if (tpinterf_send_cmd() < 0) { 262 /* binary flash write command to loadagent */
256 fclose(tmpfile); 263 databuf[1] = addr >> 24;
257 return(-1); 264 databuf[2] = addr >> 16;
258 } 265 databuf[3] = addr >> 8;
259 rc = tpinterf_pass_output(8); /* 8 s timeout */ 266 databuf[4] = addr;
267 databuf[5] = reclen >> 8;
268 databuf[6] = reclen;
269 cc = write(target_fd, databuf, reclen + 7);
270 if (cc != reclen + 7) {
271 fclose(tmpfile);
272 perror("binary write to target");
273 return(-1);
274 }
275 rc = collect_binblock_from_target(&ackbyte, 1, 8);
260 if (rc) { 276 if (rc) {
261 fclose(tmpfile); 277 fclose(tmpfile);
262 return(rc); 278 return(rc);
279 }
280 if (ackbyte == 0x15) { /* NAK */
281 fclose(tmpfile);
282 tpinterf_pass_output(1);
283 return(-1);
284 }
285 if (ackbyte != 0x06) { /* ACK */
286 fclose(tmpfile);
287 fprintf(stderr,
288 "binary protocol error: bad ack 0x%02X\n",
289 ackbyte);
290 return(-1);
263 } 291 }
264 addr += reclen; 292 addr += reclen;
265 len -= reclen; 293 len -= reclen;
266 bytesdone += reclen; 294 bytesdone += reclen;
267 cc = bytesdone * 100 / total_len; 295 cc = bytesdone * 100 / total_len;
274 last_time = curtime; 302 last_time = curtime;
275 } 303 }
276 } 304 }
277 putchar('\n'); 305 putchar('\n');
278 fclose(tmpfile); 306 fclose(tmpfile);
307 databuf[0] = 0x04; /* EOT */
308 write(target_fd, databuf, 1);
309 rc = collect_binblock_from_target(&ackbyte, 1, 1);
310 if (rc)
311 return(rc);
312 time(&last_time);
313 if (ackbyte != '=') {
314 fprintf(stderr, "error: \'=\' not received as expected\n");
315 return(-1);
316 }
279 duration = last_time - initial_time; 317 duration = last_time - initial_time;
280 mm = duration / 60; 318 mm = duration / 60;
281 ss = duration - mm * 60; 319 ss = duration - mm * 60;
282 printf("Operation completed in %um%us\n", mm, ss); 320 printf("Operation completed in %um%us\n", mm, ss);
283 321