forked from LinuxCNC/mesaflash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesaflash.c
415 lines (379 loc) · 15.2 KB
/
mesaflash.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//
// Copyright (C) 2013-2014 Michael Geszkiewicz
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
#include <string.h>
#include <stdio.h>
#include <getopt.h>
#include <ctype.h>
#include <stdlib.h>
#include <errno.h>
#include "anyio.h"
#ifdef __linux__
#include <pci/pci.h>
#elif _WIN32
#include "libpci/pci.h"
#endif
static int device_flag;
static int addr_flag;
static int addr_hi_flag;
static int write_flag;
static int fix_boot_flag;
static int verify_flag;
static int auto_verify_flag = 1;
static int fallback_flag;
static int recover_flag;
static int program_flag;
static int readhmid_flag;
static int reload_flag;
static int reset_flag;
static int sserial_flag;
static int epp_flag;
static int usb_flag;
static int spi_flag;
static int serial_flag;
static int rpo_flag;
static int wpo_flag;
static u16 rpo_addr;
static u16 wpo_addr;
static u32 wpo_data;
static int set_flag;
static int xml_flag;
static char *lbp16_set_ip_addr;
static int info_flag;
static int verbose_flag;
static char bitfile_name[255];
static board_access_t access;
static struct option long_options[] = {
{"device", required_argument, 0, 'd'},
{"addr", required_argument, 0, 'a'},
{"addr_hi", required_argument, 0, 'b'},
{"write", required_argument, 0, 'w'},
{"no-auto-verify", no_argument, &auto_verify_flag, 0},
{"fix-boot-block", no_argument, &fix_boot_flag, 1},
{"verify", required_argument, 0, 'v'},
{"fallback", no_argument, &fallback_flag, 1},
{"recover", no_argument, &recover_flag, 1},
{"program", required_argument, 0, 'p'},
{"readhmid", no_argument, &readhmid_flag, 1},
{"reload", no_argument, &reload_flag, 1},
{"reset", no_argument, &reset_flag, 1},
{"sserial", no_argument, &sserial_flag, 1},
{"epp", no_argument, &epp_flag, 1},
{"usb", no_argument, &usb_flag, 1},
{"spi", no_argument, &spi_flag, 1},
{"serial", no_argument, &serial_flag, 1},
{"rpo", required_argument, 0, 'r'},
{"wpo", required_argument, 0, 'o'},
{"set", required_argument, 0, 's'},
{"xml", no_argument, &xml_flag, 1},
{"info", required_argument, 0, 'i'},
{"help", no_argument, 0, 'h'},
{"verbose", no_argument, &verbose_flag, 1},
{0, 0, 0, 0}
};
void print_short_usage() {
printf("\nMesaflash version 3.3.0~pre (built on %s %s with libpci %s)\n", __DATE__, __TIME__, PCILIB_VERSION);
printf("Configuration and diagnostic tool for Mesa Electronics PCI(E)/ETH/EPP/USB boards\n");
printf("(C) 2013-2015 Michael Geszkiewicz (contact: micges@wp.pl)\n");
printf("(C) Mesa Electronics\n");
printf("Try 'mesaflash --help' for more information\n");
}
void print_usage() {
printf("Syntax:\n");
printf(" mesaflash --device device_name [options]\n");
printf(" mesaflash --device device_name [options] --write filename\n");
printf(" mesaflash --device device_name [options] --verify filename\n");
printf(" mesaflash --device device_name [options] --program filename\n");
printf(" mesaflash --device device_name [options] --readhmid\n");
printf(" mesaflash --device device_name [options] --reload | --reset\n");
printf(" mesaflash --device device_name [options] --sserial\n");
printf(" mesaflash --device device_name [options] --rpo address\n");
printf(" mesaflash --device device_name [options] --wpo address=value\n");
printf(" mesaflash --device device_name [options] --set ip=n.n.n.n\n");
printf(" mesaflash --info file_name\n");
printf(" mesaflash --help\n");
printf("\nOptions:\n");
printf(" --device select active device name. If no command is given it will detect board with given name and print info about it.\n");
printf(" --addr <device_address>\n");
printf(" select <device address> for looking for <device_name> (network C mask for ethernet boards, serial port for USB boards)\n");
printf(" --addr_hi set the high register address for the EPP interface\n");
printf(" --epp use EPP interface to connect to board, only for boards with multiple interfaces (7i43, 7i90, 7i64)\n");
printf(" --usb use USB interface to connect to board, only for boards with multiple interfaces (7i43, 7i90, 7i64)\n");
printf(" --spi use SPI interface to connect to board, only for boards with multiple interfaces (7i43, 7i90, 7i64)\n");
printf(" --serial use serial interface to connect to board, only for boards with multiple interfaces (7i43, 7i90, 7i64)\n");
printf(" --fallback use the fallback area of the EEPROM while executing commands\n");
printf(" --recover access board using PCI bridge GPIO (currently only 6I25)\n");
printf(" --xml format output from 'readhmid' command into XML\n");
printf(" --verbose print detailed information while running commands\n");
printf("\nCommands:\n");
printf(" --write writes a standard bitfile 'filename' configuration to the userarea of the EEPROM (IMPORTANT! 'filename' must be VALID FPGA configuration file)\n");
printf(" --fix-boot-block If a write operation does not detect a valid boot block, write one\n");
printf(" --no-auto-verify Don't automatically verify after writing\n");
printf(" --verify verifies the EEPROM configuration against the bitfile 'filename'\n");
printf(" --program writes a standard bitfile 'filename' configuration to the FPGA (IMPORTANT! 'filename' must be VALID FPGA configuration file)\n");
printf(" --readhmid print hostmot2 configuration in PIN file format\n");
printf(" --reload do full FPGA reload from flash (only ethernet and pci boards)\n");
printf(" --reset do full firmware reset (only ethernet boards)\n");
printf(" --sserial print full information about all sserial remote boards\n");
printf(" --rpo read hostmot2 variable directly at 'address'\n");
printf(" --wpo write hostmot2 variable directly at 'address' with 'value'\n");
printf(" --set set board IP address in eeprom to n.n.n.n (only ethernet boards)\n");
printf(" --info print info about configuration in 'file_name'\n");
printf(" --help print this help message\n");
}
int process_cmd_line(int argc, char *argv[]) {
int c;
while (1) {
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long(argc, argv, "", long_options, &option_index);
/* Detect the end of the options. */
if (c == -1) break;
switch (c) {
case 0: {
/* If this option set a flag, do nothing else now. */
if (long_options[option_index].flag != 0) break;
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
}
break;
case 'd': {
int i;
if (device_flag > 0) {
printf("Error: multiple --device options\n");
exit(-1);
}
access.device_name = optarg;
for (i = 0; optarg[i] != '\0'; i++)
access.device_name[i] = toupper(access.device_name[i]);
device_flag++;
}
break;
case 'a': {
if (addr_flag > 0) {
printf("Error: multiple --addr options\n");
exit(-1);
}
access.dev_addr = optarg;
addr_flag++;
}
break;
case 'b': {
if (addr_hi_flag > 0) {
printf("Error: multiple --addr_hi options\n");
exit(-1);
}
access.dev_hi_addr = optarg;
addr_hi_flag++;
}
break;
case 'w': {
if (write_flag > 0) {
printf("Error: multiple --write options\n");
exit(-1);
}
strncpy(bitfile_name, optarg, 255);
write_flag++;
}
break;
case 'p': {
if (program_flag > 0) {
printf("Error: multiple --program options\n");
exit(-1);
}
strncpy(bitfile_name, optarg, 255);
program_flag++;
}
break;
case 'r': {
if (rpo_flag > 0) {
printf("Error: multiple --rpo options\n");
exit(-1);
}
if (strncmp(optarg, "0x", 2) == 0) {
optarg[0] = '0';
optarg[1] = '0';
rpo_addr = strtol(optarg, NULL, 16);
} else {
rpo_addr = strtol(optarg, NULL, 10);
}
rpo_flag++;
}
break;
case 'o': {
char *pch;
if (wpo_flag > 0) {
printf("Error: multiple --wpo options\n");
exit(-1);
}
pch = strtok(optarg, "=");
if (strncmp(pch, "0x", 2) == 0) {
pch[0] = '0';
pch[1] = '0';
wpo_addr = strtol(pch, NULL, 16);
} else {
wpo_addr = strtol(pch, NULL, 10);
}
pch = strtok(NULL, "=");
if (strncmp(pch, "0x", 2) == 0) {
pch[0] = '0';
pch[1] = '0';
wpo_data = strtol(pch, NULL, 16);
} else {
wpo_data = strtol(pch, NULL, 10);
}
wpo_flag++;
}
break;
case 's': {
if (set_flag > 0) {
printf("Error: multiple --set options\n");
exit(-1);
}
if (strncmp(optarg, "ip=", 3) == 0) {
char *pch;
pch = strtok(optarg, "=");
pch = strtok(NULL, "=");
lbp16_set_ip_addr = pch;
} else {
printf("Error: Unknown set command syntax, see --help for examples\n");
exit(-1);
}
set_flag++;
}
break;
case 'v': {
if (verify_flag > 0) {
printf("Error: multiple --verify options\n");
exit(-1);
}
strncpy(bitfile_name, optarg, 255);
verify_flag++;
}
break;
case 'i': {
if (info_flag > 0) {
printf("Error: multiple --info options\n");
exit(-1);
}
strncpy(bitfile_name, optarg, 255);
info_flag++;
}
break;
case 'h': {
print_usage();
exit(0);
}
break;
case '?':
/* getopt_long already printed an error message. */
return -1;
break;
default:
abort();
}
}
access.type = BOARD_ANY;
if ((epp_flag == 1) && (usb_flag == 0) && (spi_flag == 0) && (serial_flag == 0)) {
access.type = BOARD_EPP;
} else if ((epp_flag == 0) && (usb_flag == 1) && (spi_flag == 0) && (serial_flag == 0)) {
access.type = BOARD_USB;
} else if ((epp_flag == 0) && (usb_flag == 0) && (spi_flag == 1) && (serial_flag == 0)) {
access.type = BOARD_SPI;
} else if ((epp_flag == 0) && (usb_flag == 0) && (spi_flag == 0) && (serial_flag == 1)) {
access.type = BOARD_SER;
}
return 0;
}
int main(int argc, char *argv[]) {
int ret = 0;
if (argc == 1) {
print_short_usage();
return 0;
}
if (process_cmd_line(argc, argv) == -1)
return -1;
if (info_flag == 1) {
anyio_bitfile_print_info(bitfile_name, 1);
} else if (device_flag == 1) {
board_t *board = NULL;
access.verbose = verbose_flag;
access.recover = recover_flag;
access.address = addr_flag;
if (anyio_init(&access) != 0)
exit(1);
ret = anyio_find_dev(&access);
if (ret < 0) {
return -1;
}
board = anyio_get_dev(&access, 1);
if (board == NULL) {
printf("No %s board found\n", access.device_name);
return -1;
}
board->open(board);
if (readhmid_flag == 1) {
anyio_dev_print_hm2_info(board, xml_flag);
} else if (sserial_flag == 1) {
anyio_dev_print_sserial_info(board);
} else if (rpo_flag == 1) {
u32 data;
board->llio.read(&(board->llio), rpo_addr, &data, sizeof(u32));
printf("%08X\n", data);
} else if (wpo_flag == 1) {
board->llio.write(&(board->llio), wpo_addr, &wpo_data, sizeof(u32));
} else if (set_flag == 1) {
ret = anyio_dev_set_remote_ip(board, lbp16_set_ip_addr);
} else if (write_flag == 1) {
ret = anyio_dev_write_flash(board, bitfile_name, fallback_flag, fix_boot_flag);
if (ret == 0) {
if (auto_verify_flag) {
ret = anyio_dev_verify_flash(board, bitfile_name, fallback_flag);
}
}
if (ret == 0) {
if (reload_flag == 1) {
ret = anyio_dev_reload(board, fallback_flag);
if (ret == -1) {
printf("\nYou must power cycle the hardware to load a new firmware.\n");
}
} else if (board->llio.reload) {
printf("\nYou must power cycle the hardware or use the --reload command to load a new firmware.\n");
} else {
printf("\nYou must power cycle the hardware\n");
}
}
} else if (verify_flag == 1) {
ret = anyio_dev_verify_flash(board, bitfile_name, fallback_flag);
} else if (program_flag == 1) {
ret = anyio_dev_program_fpga(board, bitfile_name);
} else if (reload_flag == 1) {
anyio_dev_reload(board, fallback_flag);
} else if (reset_flag == 1) {
anyio_dev_reset(board);
} else {
board->print_info(board);
}
board->close(board);
anyio_cleanup(&access);
} else {
printf("No action requested. Please specify at least --device or --info.\n");
}
return ret;
}