-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
576 lines (489 loc) · 15.6 KB
/
main.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
* Linux PIC Programmer (lpicp)
* Command line interface
*
* Author: Eran Duchan <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
*/
#include <stdio.h>
#include <errno.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "lpicp.h"
#include "lpicp_log.h"
#include "lpicp_icsp.h"
#include "lpicp_image.h"
/* current version */
const char *version_string = "0.0.2";
/* to show progress */
static unsigned int lpicp_progress_current_bytes = 0;
static const char *lpicp_progress_current_operation = NULL;
/* whether to read or write */
enum lpicp_opmode_t
{
LPICP_OPMODE_UNDEFINED,
LPICP_OPMODE_READ,
LPICP_OPMODE_WRITE,
LPICP_OPMODE_GET_DEVID,
LPICP_OPMODE_ERASE_DEVICE
};
/* running configuration */
struct lpp_config_t
{
int verbose;
char *dev_name;
char *file_name;
enum lpicp_opmode_t opmode;
unsigned int offset;
unsigned int size;
};
/* initialize default configuration */
void lpicp_main_init_default_config(struct lpp_config_t *config)
{
config->verbose = 0;
config->dev_name = NULL;
config->file_name = NULL;
config->opmode = LPICP_OPMODE_UNDEFINED;
config->offset = 0;
config->size = 0;
}
/* before each operation */
int lpicp_progress_init(const char *current_operation)
{
/* zero out the progress */
lpicp_progress_current_bytes = 0;
/* save opname */
lpicp_progress_current_operation = current_operation;
/* success */
return 1;
}
/* print usage */
void lpicp_main_usage(void)
{
printf("Linux PIC Programmer v.%s (Compiled " __DATE__ " " __TIME__ ")\n", version_string);
printf("Usage: lpicp [options]\n");
printf(" -x, --exec r, read | w, write | e, erase | devid\n");
printf(" -d, --dev ICSP device name (e.g. /dev/icsp0)\n");
printf(" -f, --file Path to Intel HEX file\n");
printf(" -o, --offset Read from offset, Write to offset\n");
printf(" -s, --size Size for operation, in bytes\n");
printf(" -v, --verbose Verbose operation\n");
printf(" -h, --help Prints this usage\n");
printf("\n");
}
/* parse an integer */
int lpicp_parse_numeric(const char *number_str, int *value)
{
/* zero out errno */
errno = 0;
/* do the conversion */
*value = strtol(number_str, NULL, 0);
/* check success */
return (errno == 0);
}
/* parse arguments to configuration */
int lpicp_main_parse_args_to_config(int argc, char *argv[], struct lpp_config_t *config)
{
/* iterate over options */
while (1)
{
/* holds teh current option */
int current_option;
/* declare options */
static struct option long_options[] =
{
/* name has-arg flag val */
{"verbose", 0, 0, 'v'},
{"exec", 0, 0, 'x'},
{"help", 0, 0, 'h'},
{"dev", 1, 0, 'd'},
{"file", 1, 0, 'f'},
{"offset", 1, 0, 'o'},
{"size", 1, 0, 's'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
/* get the options */
current_option = getopt_long (argc, argv, "hvs:x:d:f:o:", long_options, &option_index);
/* Detect the end of the options. */
if (current_option == -1)
break;
/* get option */
switch (current_option)
{
/* value */
case 0:
break;
/* verbose */
case 'v':
{
/* save flag */
config->verbose = 1;
}
break;
/* execute */
case 'x':
{
/* read? */
if (strcmp(optarg, "read") == 0 || strcmp(optarg, "r") == 0)
{
/* set mode */
config->opmode = LPICP_OPMODE_READ;
}
/* write? */
else if (strcmp(optarg, "write") == 0 || strcmp(optarg, "w") == 0)
{
/* set mode */
config->opmode = LPICP_OPMODE_WRITE;
}
/* erase? */
else if (strcmp(optarg, "erase") == 0 || strcmp(optarg, "e") == 0)
{
/* set mode */
config->opmode = LPICP_OPMODE_ERASE_DEVICE;
}
/* get device? */
else if (strcmp(optarg, "devid") == 0)
{
/* set mode */
config->opmode = LPICP_OPMODE_GET_DEVID;
}
}
break;
/* device */
case 'd':
{
/* save file name */
config->dev_name = optarg;
}
break;
/* file */
case 'f':
{
/* save file name */
config->file_name = optarg;
}
break;
/* size */
case 's':
{
/* save size */
if (!lpicp_parse_numeric(optarg, (int *)&config->size))
{
/* handle error */
}
}
break;
/* help */
case 'h':
{
/* print usage */
lpicp_main_usage();
/* dont' execute the config */
return 0;
}
}
}
/* check that all args have been passed */
if (config->opmode == LPICP_OPMODE_UNDEFINED)
{
printf("Execution mode (-x,--exec) not passed\n");
return 0;
}
/* check that all args have been passed */
if (config->dev_name == NULL)
{
printf("Device name (-d,--dev) not passed\n");
return 0;
}
/* go ahead with the configuration */
return 1;
}
/* do read device id */
int lpicp_main_execute_read_devid(struct lpp_context_t *context,
struct lpp_config_t *config)
{
unsigned short device_id = 0;
/* try to get device id */
if (lpp_device_id_read(context, &device_id))
{
/* print device-id */
printf("Device ID: %02X.%02X\n", ((device_id >> 8) & 0xFF), (device_id & 0xFF));
/* success */
return 1;
}
else
{
/* error reading dev id */
printf("Error reading device ID\n");
/* error */
return 0;
}
}
/* do erase device */
int lpicp_main_execute_erase_device(struct lpp_context_t *context,
struct lpp_config_t *config)
{
/* do non bulk erase */
if (lpicp_progress_init("Erasing") &&
lpp_non_bulk_erase(context))
{
/* space out */
printf("\n");
/* success */
return 1;
}
else
{
/* error writing file */
printf("Error erasing device\n");
/* error */
return 0;
}
}
/* do write */
int lpicp_main_execute_image_write(struct lpp_context_t *context,
struct lpp_config_t *config)
{
struct lpp_image_t image, verify_image;
int ret;
/* return error, by default */
ret = 0;
/*
* Write image to flash
*/
/* try to allocate image */
if (lpp_image_init(context, &image, context->device.code_memory_size))
{
/* read the file and write to device */
if (!(lpp_image_read_from_file(context, &image, config->file_name) &&
lpicp_progress_init("Writing") &&
lpp_write_image_to_device_program(context, &image) &&
lpp_write_image_to_device_config(context, &image) &&
lpp_read_image_to_device_eeprom(context, &image)))
{
/* error writing file */
printf("Error writing file\n");
/* set error */
goto err_writing_file;
}
}
else
{
/* set error */
goto err_init_image;
}
/* space out */
printf("\n");
/*
* Verify image
*/
/* initialize verification image */
if (lpp_image_init(context, &verify_image, image.contents_size))
{
/* try to read */
if (lpicp_progress_init("Reading") &&
lpp_read_device_program_to_image(context, 0, image.contents_size, &verify_image) &&
lpp_read_device_eeprom_to_image(context, &verify_image))
{
/* compare them */
int cmp_result = memcmp(image.contents, verify_image.contents, image.contents_size) == 0 &&
memcmp(image.eeprom, verify_image.eeprom, context->device.eeprom_bytes) == 0;
/* print result */
printf("\nVerification %s (%d program + %d EEPROM bytes compared)\n",
cmp_result ? "success" : "failed",
image.contents_size, context->device.eeprom_bytes);
#if 0
/* print image */
printf("File (%d bytes):\n", image.contents_size);
lpp_image_print(context, &image);
/* print image */
printf("Device (%d bytes):\n", verify_image.contents_size);
lpp_image_print(context, &verify_image);
#endif
/* return compare result */
ret = (cmp_result);
}
}
else
{
/* set error */
goto err_init_verify_image;
}
/* free verification image*/
lpp_image_destroy(context, &verify_image);
err_init_verify_image:
err_writing_file:
lpp_image_destroy(context, &image);
err_init_image:
/* return result */
return ret;
}
/* do read */
int lpicp_main_execute_image_read(struct lpp_context_t *context,
struct lpp_config_t *config)
{
struct lpp_image_t image;
int ret;
/* error by default */
ret = 0;
/* size to read (either specifed by user or program memory size of device if user
* doesn't specify
*/
unsigned int size = (config->size == 0 ? context->device.code_memory_size : config->size);
/* initialize image */
if (lpp_image_init(context, &image, size))
{
/* try to read the image */
if (lpicp_progress_init("Reading") &&
lpp_read_device_program_to_image(context, 0, size, &image) &&
lpp_read_device_config_to_image(context, &image) &&
lpp_read_device_eeprom_to_image(context, &image))
{
/* print image */
lpp_image_print(context, &image);
/* success */
ret = 1;
}
else
{
/* error writing file */
printf("\nError reading file from device\n");
}
}
else
{
/* log */
printf("Error allocating image\n");
/* */
goto err_init_image;
}
/* free image */
lpp_image_destroy(context, &image);
err_init_image:
return ret;
}
/* show progress */
int lpicp_progress_show(struct lpp_context_t *context,
const unsigned int current_bytes,
const unsigned int total_bytes)
{
/* print each X bytes */
if ((current_bytes - lpicp_progress_current_bytes) > 1024 ||
current_bytes >= total_bytes)
{
/* do the print */
printf("\r%s: %d of %d (%d%%)", lpicp_progress_current_operation,
current_bytes, total_bytes, current_bytes * 100 / total_bytes);
fflush(stdout);
/* save printed bytes mark */
lpicp_progress_current_bytes = current_bytes;
}
/* success */
return 1;
}
/* parse arguments to configuration */
int lpicp_main_execute_config(struct lpp_config_t *config)
{
struct lpp_context_t context;
int ret;
/* assume no error */
ret = 1;
/* try to init context */
if (lpp_context_init(&context, LPP_DEVICE_FAMILY_18F, config->dev_name, lpicp_progress_show))
{
struct timeval start_time, end_time, diff_time;
/* print device */
printf("Found device (%s)\n", context.device.name);
/* init log if verbose */
if (config->verbose)
{
/* try to init log */
if (!lpp_log_init(&context, 4096))
{
/* set err */
ret = 0;
/* error */
goto err_log_init;
}
}
/* get start time */
gettimeofday(&start_time, NULL);
/* handle opmode */
switch (config->opmode)
{
/* read image from device and write to file */
case LPICP_OPMODE_READ:
ret = lpicp_main_execute_image_read(&context, config);
break;
/* write image from file to the device */
case LPICP_OPMODE_WRITE:
ret = lpicp_main_execute_image_write(&context, config);
break;
/* erase the device */
case LPICP_OPMODE_ERASE_DEVICE:
ret = lpicp_main_execute_erase_device(&context, config);
break;
/* read device-id */
case LPICP_OPMODE_GET_DEVID:
ret = lpicp_main_execute_read_devid(&context, config);
break;
/* unknown */
case LPICP_OPMODE_UNDEFINED:
ret = 0;
break;
}
/* get end time */
gettimeofday(&end_time, NULL);
/* diff the time */
timersub(&end_time, &start_time, &diff_time);
/* print time */
if (ret) printf("Done successfully in %d.%03ds\n", (int)diff_time.tv_sec, (int)(diff_time.tv_usec / 1000));
/* print log if verbose */
if (config->verbose)
{
/* print the log on success */
if (ret) lpp_log_print(&context);
/* and release it */
lpp_log_destroy(&context);
}
}
else
{
/* no device found */
printf("Failed to find supported device\n");
/* set err */
ret = 1;
/* error */
goto err_init_context;
}
err_log_init:
lpp_context_destroy(&context);
err_init_context:
return ret;
}
/* entry */
int main(int argc, char *argv[])
{
int ret;
struct lpp_config_t running_config;
/* by default, return with error */
ret = 1;
/* set default configuration */
lpicp_main_init_default_config(&running_config);
/* try to parse the args into config */
if (lpicp_main_parse_args_to_config(argc, argv, &running_config))
{
/* execute the configuration */
ret = lpicp_main_execute_config(&running_config);
}
/* done */
return ret;
}