FreeCalypso > hg > freecalypso-tools
comparison loadtools/flprogbin.c @ 0:e7502631a0f9
initial import from freecalypso-sw rev 1033:5ab737ac3ad7
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 11 Jun 2016 00:13:35 +0000 |
parents | |
children | 0dd2c87c1b63 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e7502631a0f9 |
---|---|
1 /* | |
2 * This module implements the flash program-bin command: | |
3 * programming flash using a binary file as the data source. | |
4 */ | |
5 | |
6 #include <sys/types.h> | |
7 #include <sys/stat.h> | |
8 #include <stdio.h> | |
9 #include <stdint.h> | |
10 #include <stdlib.h> | |
11 #include <time.h> | |
12 #include "flash.h" | |
13 | |
14 extern struct flash_bank_info flash_bank_info[2]; | |
15 extern uint32_t crc32_table[]; | |
16 | |
17 flashcmd_progbin(argc, argv, bank) | |
18 char **argv; | |
19 { | |
20 struct flash_bank_info *bi; | |
21 u_long flashoff, fileoff, len, origlen, bytesdone; | |
22 u_long crc_base_addr, crc_from_target; | |
23 uint32_t crcaccum; | |
24 char *strtoul_endp; | |
25 FILE *binf; | |
26 struct stat filestat; | |
27 char *targv[4], shortarg[10], longarg[513]; | |
28 u_char databuf[256]; | |
29 int reclen, cc, i; | |
30 time_t curtime, last_time; | |
31 | |
32 if (argc < 4 || argc > 6) { | |
33 inv: fprintf(stderr, | |
34 "usage: %s %s flash-offset binfile [file-offset [length]]\n", | |
35 argv[0], argv[1]); | |
36 return(-1); | |
37 } | |
38 flashoff = strtoul(argv[2], &strtoul_endp, 16); | |
39 if (*strtoul_endp) | |
40 goto inv; | |
41 if (flash_get_cfi(bank) < 0) | |
42 return(-1); | |
43 bi = flash_bank_info + bank; | |
44 if (flashoff >= bi->geom->total_size) { | |
45 fprintf(stderr, | |
46 "error: specified flash offset exceeds bank size (0x%lx)\n", | |
47 (u_long) bi->geom->total_size); | |
48 return(-1); | |
49 } | |
50 if (flashoff & 1) { | |
51 fprintf(stderr, "error: flash offset must be even\n"); | |
52 return(-1); | |
53 } | |
54 binf = fopen(argv[3], "r"); | |
55 if (!binf) { | |
56 perror(argv[3]); | |
57 return(-1); | |
58 } | |
59 fstat(fileno(binf), &filestat); | |
60 if (!S_ISREG(filestat.st_mode)) { | |
61 fprintf(stderr, "%s is not a regular file\n", argv[3]); | |
62 fclose(binf); | |
63 return(-1); | |
64 } | |
65 if (argc > 4) { | |
66 fileoff = strtoul(argv[4], &strtoul_endp, 16); | |
67 if (*strtoul_endp) { | |
68 fclose(binf); | |
69 goto inv; | |
70 } | |
71 if (fileoff > filestat.st_size) { | |
72 fprintf(stderr, | |
73 "error: specified file offset exceeds file length\n"); | |
74 fclose(binf); | |
75 return(-1); | |
76 } | |
77 } else | |
78 fileoff = 0; | |
79 if (argc > 5) { | |
80 len = strtoul(argv[5], &strtoul_endp, 16); | |
81 if (*strtoul_endp) { | |
82 fclose(binf); | |
83 goto inv; | |
84 } | |
85 if (len > filestat.st_size - fileoff) { | |
86 fprintf(stderr, | |
87 "error: specified file offset+length exceed file length\n"); | |
88 fclose(binf); | |
89 return(-1); | |
90 } | |
91 } else | |
92 len = filestat.st_size - fileoff; | |
93 if (!len) { | |
94 printf("Length is zero - nothing to do!\n"); | |
95 fclose(binf); | |
96 return(0); | |
97 } | |
98 if (len > bi->geom->total_size - flashoff) { | |
99 fprintf(stderr, | |
100 "error: specified flash offset+length exceed bank size (0x%lx)\n", | |
101 (u_long) bi->geom->total_size); | |
102 fclose(binf); | |
103 return(-1); | |
104 } | |
105 if (len & 1) { | |
106 fprintf(stderr, "error: program length must be even\n"); | |
107 fclose(binf); | |
108 return(-1); | |
109 } | |
110 | |
111 /* finally done with the arg parsing etc, can get to work now */ | |
112 if (flash_id_check(bank, 0) < 0) { | |
113 fclose(binf); | |
114 return(-1); | |
115 } | |
116 crc_base_addr = bi->base_addr + flashoff; | |
117 sprintf(shortarg, "%lx", (u_long) bi->base_addr); | |
118 targv[0] = bi->ops->loadagent_setbase_cmd; | |
119 targv[1] = shortarg; | |
120 targv[2] = 0; | |
121 printf("Setting flash base address: %s %s\n", targv[0], targv[1]); | |
122 tpinterf_make_cmd(targv); | |
123 if (tpinterf_send_cmd() < 0) { | |
124 fclose(binf); | |
125 return(-1); | |
126 } | |
127 cc = tpinterf_pass_output(1); | |
128 if (cc) { | |
129 fclose(binf); | |
130 return(cc); | |
131 } | |
132 if (bi->ops->prep_for_program(bi) < 0) { | |
133 fclose(binf); | |
134 return(-1); | |
135 } | |
136 fseek(binf, fileoff, SEEK_SET); | |
137 targv[0] = bi->ops->loadagent_program_cmd; | |
138 targv[1] = shortarg; | |
139 targv[2] = longarg; | |
140 targv[3] = 0; | |
141 printf("Programming flash: %lu (0x%lx) bytes\n", len, len); | |
142 origlen = len; | |
143 bytesdone = 0; | |
144 last_time = 0; | |
145 crcaccum = 0xFFFFFFFF; | |
146 while (len) { | |
147 if (len >= 256) | |
148 reclen = 256; | |
149 else | |
150 reclen = len; | |
151 cc = fread(databuf, 1, reclen, binf); | |
152 if (cc != reclen) { | |
153 fclose(binf); | |
154 fprintf(stderr, "error reading from %s\n", argv[3]); | |
155 return(-1); | |
156 } | |
157 for (i = 0; i < reclen; i++) /* update running CRC */ | |
158 crcaccum = crc32_table[crcaccum & 0xFF ^ databuf[i]] | |
159 ^ (crcaccum >> 8); | |
160 sprintf(shortarg, "%lx", flashoff); | |
161 build_flashw_hex_string(databuf, longarg, reclen >> 1, 0); | |
162 tpinterf_make_cmd(targv); | |
163 if (tpinterf_send_cmd() < 0) { | |
164 fclose(binf); | |
165 return(-1); | |
166 } | |
167 i = tpinterf_pass_output(8); /* 8 s timeout */ | |
168 if (i) { | |
169 fclose(binf); | |
170 return(i); | |
171 } | |
172 flashoff += reclen; | |
173 len -= reclen; | |
174 bytesdone += reclen; | |
175 cc = bytesdone * 100 / origlen; | |
176 time(&curtime); | |
177 if (curtime != last_time || cc == 100) { | |
178 printf("\r0x%lx bytes programmed (%i%%)", | |
179 bytesdone, cc); | |
180 fflush(stdout); | |
181 } | |
182 last_time = curtime; | |
183 } | |
184 putchar('\n'); | |
185 fclose(binf); | |
186 | |
187 /* reset flash to read mode */ | |
188 if (bi->ops->reset_cmd(bi) < 0) | |
189 return(-1); | |
190 printf("Verifying CRC-32 of programmed flash area\n"); | |
191 if (crc32_on_target(crc_base_addr, origlen, &crc_from_target) < 0) | |
192 return(-1); | |
193 if (crc_from_target == crcaccum) { | |
194 printf("match (%08lX)\n", crc_from_target); | |
195 return(0); | |
196 } else { | |
197 fprintf(stderr, "error: CRC mismatch!\n"); | |
198 return(-1); | |
199 } | |
200 } |