comparison loadtools/flprogsrec.c @ 99:b78db17bfc85

fc-loadtool: flash program-{m0,srec} implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 01 Sep 2013 18:59:48 +0000
parents
children ae4921a3aa33
comparison
equal deleted inserted replaced
98:b3ed63722eb5 99:b78db17bfc85
1 /*
2 * In this module we are going to implement the flash program-srec and
3 * flash program-m0 commands: programming flash using S-record files
4 * as the data source.
5 */
6
7 #include <sys/types.h>
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include "flash.h"
12 #include "srecreader.h"
13
14 extern struct flash_bank_info flash_bank_info[2];
15
16 flashcmd_progsrec_gen(bank, imgfile, is_m0)
17 char *imgfile;
18 {
19 struct flash_bank_info *bi;
20 struct srecreader srr;
21 char *targv[4], shortarg[10], longarg[513];
22 int resp;
23 unsigned long rec_count;
24
25 srr.filename = imgfile;
26 resp = open_srec_file(&srr);
27 if (resp < 0)
28 return(resp);
29 bi = flash_bank_info + bank;
30 sprintf(shortarg, "%lx", (u_long) bi->base_addr);
31 targv[0] = "AMFB";
32 targv[1] = shortarg;
33 targv[2] = 0;
34 printf("Setting flash base address: %s %s\n", targv[0], targv[1]);
35 tpinterf_make_cmd(targv);
36 if (tpinterf_send_cmd() < 0) {
37 fclose(srr.openfile);
38 return(-1);
39 }
40 resp = tpinterf_pass_output(1);
41 if (resp) {
42 fclose(srr.openfile);
43 return(resp);
44 }
45 targv[0] = "AMFW";
46 targv[1] = shortarg;
47 targv[2] = longarg;
48 targv[3] = 0;
49 for (rec_count = 0; ; ) {
50 if (read_s_record(&srr) < 0) {
51 /* error msg already printed */
52 fclose(srr.openfile);
53 return(-1);
54 }
55 if (srr.record_type == '0') {
56 if (srr.lineno == 1)
57 continue;
58 fprintf(stderr,
59 "Warning: S0 record found in line %d of %s (expected in line 1 only)\n",
60 srr.lineno, srr.filename);
61 continue;
62 } else if (srr.record_type == '7')
63 break;
64 else if (srr.record_type != '3') {
65 fprintf(stderr,
66 "Warning: unsupported S%c record type in line %d of %s\n",
67 srr.record_type, srr.lineno, srr.filename);
68 continue;
69 }
70 /* must be S3 */
71 if (s3s7_get_addr_data(&srr) < 0) {
72 /* error msg already printed */
73 fclose(srr.openfile);
74 return(-1);
75 }
76 if (srr.datalen < 1) {
77 fprintf(stderr,
78 "%s line %d: S3 record of zero data length ignored\n",
79 srr.filename, srr.lineno);
80 continue;
81 }
82 if (srr.addr & 1 || srr.datalen & 1) {
83 fprintf(stderr,
84 "%s line %d: violates word alignment requirement\n",
85 srr.filename, srr.lineno);
86 fclose(srr.openfile);
87 return(-1);
88 }
89 srr.addr &= bi->total_size - 1;
90 if (srr.addr + srr.datalen > bi->total_size) {
91 fprintf(stderr,
92 "%s line %d: goes past the end of the flash bank\n",
93 srr.filename, srr.lineno);
94 fclose(srr.openfile);
95 return(-1);
96 }
97 if (!rec_count)
98 printf("Programming flash, each \'.\' is one S-record\n");
99 sprintf(shortarg, "%lx", (u_long) srr.addr);
100 build_flashw_hex_string(srr.record + 5, longarg,
101 srr.datalen >> 1, is_m0);
102 tpinterf_make_cmd(targv);
103 if (tpinterf_send_cmd() < 0) {
104 fclose(srr.openfile);
105 return(-1);
106 }
107 resp = tpinterf_pass_output(8); /* 8 s timeout */
108 if (resp) {
109 fclose(srr.openfile);
110 return(resp);
111 }
112 putchar('.');
113 fflush(stdout);
114 rec_count++;
115 }
116 /* got S7 */
117 fclose(srr.openfile);
118 if (!rec_count) {
119 fprintf(stderr,
120 "%s line %d: S7 without any preceding S3 data records\n",
121 srr.filename, srr.lineno);
122 return(-1);
123 }
124 printf("\nProgramming complete\n");
125 return(0);
126 }
127
128 flashcmd_program_srec(argc, argv, bank)
129 char **argv;
130 {
131 if (argc != 3) {
132 fprintf(stderr, "usage: %s %s image.srec\n", argv[0], argv[1]);
133 return(-1);
134 }
135 return flashcmd_progsrec_gen(bank, argv[2], 0);
136 }
137
138 flashcmd_program_m0(argc, argv, bank)
139 char **argv;
140 {
141 if (argc != 3) {
142 fprintf(stderr, "usage: %s %s image.m0\n", argv[0], argv[1]);
143 return(-1);
144 }
145 return flashcmd_progsrec_gen(bank, argv[2], 1);
146 }