comparison loadtools/flprogbin.c @ 672:57cb825b505a

fc-loadtool code: flprogbin.c refactoring in prep for e-program-bin
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 08 Mar 2020 04:09:08 +0000
parents 51bcfb251b23
children f67e5ad30324
comparison
equal deleted inserted replaced
671:e66fafeeb377 672:57cb825b505a
15 extern int target_fd; 15 extern int target_fd;
16 16
17 extern struct flash_bank_info flash_bank_info[2]; 17 extern struct flash_bank_info flash_bank_info[2];
18 extern uint32_t crc32_table[]; 18 extern uint32_t crc32_table[];
19 19
20 flashcmd_progbin(argc, argv, bank) 20 flashcmd_progbin_int(bank, with_erase, flashoff, imgfile, fileoff, len,
21 char **argv; 21 len_given)
22 u_long flashoff, fileoff, len;
23 char *imgfile;
22 { 24 {
23 struct flash_bank_info *bi; 25 struct flash_bank_info *bi;
24 u_long flashoff, fileoff, len, origlen, bytesdone; 26 u_long origlen, bytesdone;
25 u_long crc_base_addr, crc_from_target; 27 u_long crc_base_addr, crc_from_target;
26 uint32_t crcaccum; 28 uint32_t crcaccum;
27 char *strtoul_endp;
28 FILE *binf; 29 FILE *binf;
29 struct stat filestat; 30 struct stat filestat;
30 char *targv[3], shortarg[10]; 31 char *targv[3], shortarg[10];
31 u_char databuf[2048 + 7], ackbyte; 32 u_char databuf[2048 + 7], ackbyte;
32 int reclen, cc, i; 33 int reclen, cc, i;
33 time_t initial_time, curtime, last_time; 34 time_t initial_time, curtime, last_time;
34 unsigned duration, mm, ss; 35 unsigned duration, mm, ss;
35 36
36 if (argc < 4 || argc > 6) {
37 inv: fprintf(stderr,
38 "usage: %s %s flash-offset binfile [file-offset [length]]\n",
39 argv[0], argv[1]);
40 return(-1);
41 }
42 flashoff = strtoul(argv[2], &strtoul_endp, 16);
43 if (*strtoul_endp)
44 goto inv;
45 if (flash_detect(bank, 0) < 0) 37 if (flash_detect(bank, 0) < 0)
46 return(-1); 38 return(-1);
47 bi = flash_bank_info + bank; 39 bi = flash_bank_info + bank;
48 if (flashoff >= bi->geom->total_size) { 40 if (flashoff >= bi->geom->total_size) {
49 fprintf(stderr, 41 fprintf(stderr,
53 } 45 }
54 if (flashoff & 1) { 46 if (flashoff & 1) {
55 fprintf(stderr, "error: flash offset must be even\n"); 47 fprintf(stderr, "error: flash offset must be even\n");
56 return(-1); 48 return(-1);
57 } 49 }
58 binf = fopen(argv[3], "r"); 50 binf = fopen(imgfile, "r");
59 if (!binf) { 51 if (!binf) {
60 perror(argv[3]); 52 perror(imgfile);
61 return(-1); 53 return(-1);
62 } 54 }
63 fstat(fileno(binf), &filestat); 55 fstat(fileno(binf), &filestat);
64 if (!S_ISREG(filestat.st_mode)) { 56 if (!S_ISREG(filestat.st_mode)) {
65 fprintf(stderr, "%s is not a regular file\n", argv[3]); 57 fprintf(stderr, "error: %s is not a regular file\n", imgfile);
66 fclose(binf); 58 fclose(binf);
67 return(-1); 59 return(-1);
68 } 60 }
69 if (argc > 4) { 61 if (fileoff > filestat.st_size) {
70 fileoff = strtoul(argv[4], &strtoul_endp, 16); 62 fprintf(stderr,
71 if (*strtoul_endp) {
72 fclose(binf);
73 goto inv;
74 }
75 if (fileoff > filestat.st_size) {
76 fprintf(stderr,
77 "error: specified file offset exceeds file length\n"); 63 "error: specified file offset exceeds file length\n");
78 fclose(binf); 64 fclose(binf);
79 return(-1); 65 return(-1);
80 } 66 }
81 } else 67 if (len_given) {
82 fileoff = 0;
83 if (argc > 5) {
84 len = strtoul(argv[5], &strtoul_endp, 16);
85 if (*strtoul_endp) {
86 fclose(binf);
87 goto inv;
88 }
89 if (len > filestat.st_size - fileoff) { 68 if (len > filestat.st_size - fileoff) {
90 fprintf(stderr, 69 fprintf(stderr,
91 "error: specified file offset+length exceed file length\n"); 70 "error: specified file offset+length exceed file length\n");
92 fclose(binf); 71 fclose(binf);
93 return(-1); 72 return(-1);
154 else 133 else
155 reclen = len; 134 reclen = len;
156 cc = fread(databuf + 7, 1, reclen, binf); 135 cc = fread(databuf + 7, 1, reclen, binf);
157 if (cc != reclen) { 136 if (cc != reclen) {
158 fclose(binf); 137 fclose(binf);
159 fprintf(stderr, "error reading from %s\n", argv[3]); 138 fprintf(stderr, "error reading from %s\n", imgfile);
160 /* don't leave loadagent in binary flash mode */ 139 /* don't leave loadagent in binary flash mode */
161 databuf[0] = 0x04; 140 databuf[0] = 0x04;
162 write(target_fd, databuf, 1); 141 write(target_fd, databuf, 1);
163 tpinterf_pass_output(1); 142 tpinterf_pass_output(1);
164 return(-1); 143 return(-1);
237 } else { 216 } else {
238 fprintf(stderr, "error: CRC mismatch!\n"); 217 fprintf(stderr, "error: CRC mismatch!\n");
239 return(-1); 218 return(-1);
240 } 219 }
241 } 220 }
221
222 flashcmd_progbin_wrap(argc, argv, bank)
223 char **argv;
224 {
225 u_long flashoff, fileoff, len;
226 int len_given;
227 char *strtoul_endp;
228
229 if (argc < 4 || argc > 6) {
230 inv: fprintf(stderr,
231 "usage: %s %s flash-offset binfile [file-offset [length]]\n",
232 argv[0], argv[1]);
233 return(-1);
234 }
235 flashoff = strtoul(argv[2], &strtoul_endp, 16);
236 if (*strtoul_endp)
237 goto inv;
238 if (argc > 4) {
239 fileoff = strtoul(argv[4], &strtoul_endp, 16);
240 if (*strtoul_endp)
241 goto inv;
242 } else
243 fileoff = 0;
244 if (argc > 5) {
245 len = strtoul(argv[5], &strtoul_endp, 16);
246 if (*strtoul_endp)
247 goto inv;
248 len_given = 1;
249 } else {
250 len = 0; /* dummy */
251 len_given = 0;
252 }
253 return flashcmd_progbin_int(bank, argv[1][0] == 'e', flashoff, argv[3],
254 fileoff, len, len_given);
255 }