This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
burnutil.c
141 lines (110 loc) · 3.02 KB
/
burnutil.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
#include <stdlib.h>
#include <argp.h>
#include <assert.h>
#include "../libcryptoauth.h"
const char *argp_program_version =
"burnutil 0.1";
const char *argp_program_bug_address =
"<[email protected]>";
/* Program documentation. */
static char doc[] =
"Utility for burning the config and otp zones";
/* A description of the arguments we accept. */
static char args_doc[] = "BUS";
/* Number of required args */
#define NUM_ARGS 1
/* The options we understand. */
static struct argp_option options[] = {
{"verbose", 'v', 0, 0, "Produce verbose output" },
{"quiet", 'q', 0, 0, "Don't produce any output" },
{"silent", 's', 0, OPTION_ALIAS },
{"lock", 'l', 0, 0, "Locks zones"},
{"personalize", 'p', 0, 0, "Fully personalizes device"},
{"file", 'f', "XMLFILE", 0,
"XML Memory configuration file" },
{ 0 }
};
/* Used by main to communicate with parse_opt. */
struct arguments
{
char *args[NUM_ARGS]; /* arg1 & arg2 */
int silent, verbose, lock, personalize;
char *display, *input_file;
};
/* Parse a single option. */
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct arguments *arguments = state->input;
switch (key)
{
case 'q': case 's':
arguments->silent = 1;
break;
case 'v':
arguments->verbose = 1;
break;
case 'l':
arguments->lock = 1;
break;
case 'p':
arguments->personalize = 1;
break;
case 'f':
arguments->input_file = arg;
break;
case ARGP_KEY_ARG:
if (state->arg_num >= NUM_ARGS)
/* Too many arguments. */
argp_usage (state);
arguments->args[state->arg_num] = arg;
break;
case ARGP_KEY_END:
if (state->arg_num < NUM_ARGS)
/* Not enough arguments. */
argp_usage (state);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
/* Our argp parser. */
static struct argp argp = { options, parse_opt, args_doc, doc };
int
main (int argc, char **argv)
{
struct arguments arguments;
int rc = -1;
/* Default values. */
arguments.silent = 0;
arguments.verbose = 0;
arguments.lock = 0;
arguments.personalize = 0;
arguments.input_file = NULL;
/* Parse our arguments; every option seen by parse_opt will
be reflected in arguments. */
argp_parse (&argp, argc, argv, 0, 0, &arguments);
if (NULL == arguments.input_file)
{
printf("Need xml file\n");
exit (1);
}
lca_init ();
int fd = lca_atmel_setup (arguments.args[0], 0x60);
if (arguments.personalize)
rc = personalize (fd, arguments.input_file);
else
{
struct lca_octet_buffer result;
assert (0 == lca_config2bin(arguments.input_file, &result));
assert (0 == lca_burn_config_zone (fd, result));
if (arguments.lock)
assert (0 == lca_lock_config_zone (fd, result));
rc = 0;
}
close (fd);
exit (rc);
}