-
Notifications
You must be signed in to change notification settings - Fork 0
/
tssupervisorupdate.c
264 lines (233 loc) · 5.9 KB
/
tssupervisorupdate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <assert.h>
#include <getopt.h>
#include "micro.h"
#include "update-v0.h"
#include "update-v1.h"
board_t boards[] = {
{
.compatible = "technologic,imx6q-ts7970",
.i2c_bus = 0,
.i2c_chip = 0x10,
.modelnum = 0x7970,
.min_rev = 7,
.method = UPDATE_V0,
},
{
.compatible = "technologic,imx6dl-ts7970",
.i2c_bus = 0,
.i2c_chip = 0x10,
.modelnum = 0x7970,
.min_rev = 7,
.method = UPDATE_V0,
},
{
.compatible = "fsl,imx6q-ts7970", /* Legacy < 4.9.x kernels */
.i2c_bus = 0,
.i2c_chip = 0x10,
.modelnum = 0x7970,
.min_rev = 7,
.method = UPDATE_V0,
},
{
.compatible = "fsl,imx6dl-ts7970", /* Legacy < 4.9.x kernels */
.i2c_bus = 0,
.i2c_chip = 0x10,
.modelnum = 0x7970,
.min_rev = 7,
.method = UPDATE_V0,
},
{
.compatible = "technologic,ts7250v3",
.i2c_bus = 0,
.i2c_chip = 0x10,
.modelnum = 0x7250,
.method = UPDATE_V1,
},
{
.compatible = "technologic,ts9370",
.modelnum = 0x9370,
.i2c_bus = 0,
.i2c_chip = 0x54,
.method = UPDATE_V1,
},
};
board_t *get_board()
{
FILE *file;
char comp[256];
file = fopen("/sys/firmware/devicetree/base/compatible", "r");
if (!file) {
perror("Unable to open /sys/firmware/devicetree/base/compatible");
return NULL;
}
if (fgets(comp, sizeof(comp), file) == NULL) {
perror("Failed to read compatible string");
fclose(file);
return NULL;
}
fclose(file);
for (int i = 0; i < (int)(sizeof(boards) / sizeof(boards[0])); i++) {
if (strstr(comp, boards[i].compatible) != NULL) {
return &boards[i];
}
}
return NULL;
}
void usage(char **argv)
{
fprintf(stderr,
"Usage: %s [OPTION] ...\n"
"embeddedTS supervisory microcontroller update utility\n"
"\n"
" -i, --info Print current revision information and close\n"
" -f, --force Update even if revisions match (not recommended).\n"
" Requires -u.\n"
" -n, --dry-run Check file and current revision, prints the changes\n"
" it would make but does not update. Requires -u.\n"
" -u, --update <file> Update file.\n"
" -b, --bus Override default i2c bus\n"
" -c, --chip-addr Override default i2c chip address\n"
" -v, --version Print version\n"
" -h, --help This message\n"
"\n",
argv[0]);
}
int main(int argc, char *argv[])
{
int option_index = 0;
board_t *board;
int update_revision;
int micro_revision;
int i2cfd;
int ret;
int c;
int dry_run_flag = 0;
int force_flag = 0;
int info_flag = 0;
char *update_path = 0;
int opt_bus = -1;
int opt_chip_addr = -1;
if (argc < 2) {
usage(argv);
return 1;
}
static struct option long_options[] = { { "info", no_argument, NULL, 'i' },
{ "force", no_argument, NULL, 'f' },
{ "update", required_argument, NULL, 'u' },
{ "dry-run", no_argument, NULL, 'n' },
{ "chip-addr", required_argument, NULL, 'c' },
{ "bus", required_argument, NULL, 'b' },
{ "version", no_argument, NULL, 'v' },
{ "help", no_argument, NULL, 'h' },
{ 0, 0, 0, 0 } };
while ((c = getopt_long(argc, argv, "u:nihfc:b:v", long_options, &option_index)) != -1) {
switch (c) {
case 'f':
force_flag = 1;
break;
case 'h':
usage(argv);
return 0;
case 'i':
info_flag = 1;
break;
case 'n':
dry_run_flag = 1;
break;
case 'c':
opt_chip_addr = strtoul(optarg, NULL, 0);
break;
case 'b':
opt_bus = strtoul(optarg, NULL, 0);
break;
case 'u':
update_path = optarg;
break;
case 'v':
printf("tssupervisorupdate %s\n", TAG);
return 0;
case '?':
default:
printf("Unexpected argument \"%s\"\n", optarg);
return 1;
}
}
if ((dry_run_flag || force_flag) && update_path == NULL) {
printf("Must specify the update file\n");
return 1;
}
board = get_board();
if (!board) {
printf("Unsupported board\n");
return 1;
}
if (opt_chip_addr != -1)
board->i2c_chip = opt_chip_addr;
if (opt_bus != -1)
board->i2c_bus = opt_bus;
int (*update_func)(board_t * board, int i2cfd, char *update_path) = NULL;
int (*get_rev_func)(board_t * board, int i2cfd, int *revision) = NULL;
int (*get_update_rev_func)(board_t * board, int *revision, char *update_path) = NULL;
int (*print_micro_info_func)(board_t * board, int i2cfd) = NULL;
switch (board->method) {
case UPDATE_V0:
update_func = do_v0_micro_update;
get_rev_func = do_v0_micro_get_rev;
get_update_rev_func = do_v0_micro_get_file_rev;
print_micro_info_func = do_v0_micro_print_info;
break;
case UPDATE_V1:
update_func = do_v1_micro_update;
get_rev_func = do_v1_micro_get_rev;
get_update_rev_func = do_v1_micro_get_file_rev;
print_micro_info_func = do_v1_micro_print_info;
break;
default:
fprintf(stderr, "Unsupported update method\n");
return 1;
}
i2cfd = micro_init(board->i2c_bus, board->i2c_chip);
if (i2cfd < 0) {
perror("Unable to open i2c bus");
return 1;
}
if (info_flag) {
if (print_micro_info_func(board, i2cfd) < 0)
return 1;
}
if (update_path) {
if (get_rev_func(board, i2cfd, µ_revision) < 0)
return 1;
if (get_update_rev_func(board, &update_revision, update_path) < 0)
return 1;
if (micro_revision < board->min_rev) {
fprintf(stderr, "Microcontroller must be at least rev %d to support in-field updates.\n",
board->min_rev);
return 0;
}
if ((update_revision <= micro_revision) && !force_flag) {
printf("Already at revision %d, update file is revision %d\n", micro_revision, update_revision);
return 0;
}
printf("Updating from revision %d to %d\n", micro_revision, update_revision);
if (dry_run_flag) {
printf("Dry run specified, not updating\n");
return 0;
}
ret = update_func(board, i2cfd, update_path);
if (ret != 0)
return ret;
}
close(i2cfd);
return 0;
}